diff --git a/cmd/crane-agent/app/options/option.go b/cmd/crane-agent/app/options/option.go index 2001ced74..a222fd456 100644 --- a/cmd/crane-agent/app/options/option.go +++ b/cmd/crane-agent/app/options/option.go @@ -22,7 +22,7 @@ type Options struct { // MaxInactivity is the maximum time from last recorded activity before automatic restart MaxInactivity time.Duration // Ifaces is the network devices to collect metric - Ifaces []string + Ifaces []string NodeResourceReserved map[string]string } diff --git a/pkg/agent/agent.go b/pkg/agent/agent.go index 89779d194..98d4409ba 100644 --- a/pkg/agent/agent.go +++ b/pkg/agent/agent.go @@ -11,6 +11,7 @@ import ( "time" v1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/apimachinery/pkg/util/uuid" @@ -143,8 +144,13 @@ func getAgentName(nodeName string) string { } func (a *Agent) CreateNodeResourceTsp() (string, error) { - tsp, _ := a.craneClient.PredictionV1alpha1().TimeSeriesPredictions(resource.TspNamespace).Get(context.TODO(), a.GenerateNodeResourceTspName(), metav1.GetOptions{}) - + tsp, err := a.craneClient.PredictionV1alpha1().TimeSeriesPredictions(resource.TspNamespace).Get(context.TODO(), a.GenerateNodeResourceTspName(), metav1.GetOptions{}) + if err != nil { + if !errors.IsNotFound(err) { + klog.Errorf("Failed to get noderesource tsp : %v", err) + return "", err + } + } config, err := a.kubeClient.CoreV1().ConfigMaps(resource.TspNamespace).Get(context.TODO(), "noderesource-tsp-template", metav1.GetOptions{}) if err != nil { @@ -190,16 +196,20 @@ func (a *Agent) CreateNodeResourceTsp() (string, error) { } if tsp != nil { - if reflect.DeepEqual(tsp.Spec, spec){ + klog.V(4).Infof("Discover the presence of old noderesource tsp and try to contrast the changes: %s", a.GenerateNodeResourceTspName()) + if reflect.DeepEqual(tsp.Spec, spec) { return a.GenerateNodeResourceTspName(), nil } + klog.V(4).Infof("Discover the presence of old noderesource tsp and the Tsp rules have been changed: %s", a.GenerateNodeResourceTspName()) tsp.Spec = spec _, err := a.craneClient.PredictionV1alpha1().TimeSeriesPredictions(tsp.Namespace).Update(context.TODO(), tsp, metav1.UpdateOptions{}) if err != nil { klog.Errorf("Failed to update noderesource tsp %s : %v", a.GenerateNodeResourceTspName(), err) return "", err } - }else{ + klog.V(4).Infof("The noderesource tsp is updated successfully: %s", a.GenerateNodeResourceTspName()) + } else { + klog.V(4).Infof("The noderesource tsp does not exist, try to create a new one: %s", a.GenerateNodeResourceTspName()) tsp = &v1alpha12.TimeSeriesPrediction{} tsp.Name = a.GenerateNodeResourceTspName() tsp.Namespace = resource.TspNamespace @@ -210,6 +220,7 @@ func (a *Agent) CreateNodeResourceTsp() (string, error) { klog.Errorf("Failed to create noderesource tsp %s : %v", a.GenerateNodeResourceTspName(), err) return "", err } + klog.V(4).Infof("The noderesource tsp is created successfully: %s", a.GenerateNodeResourceTspName()) } return a.GenerateNodeResourceTspName(), nil @@ -232,4 +243,4 @@ func appendManagerIfNotNil(managers []manager.Manager, m manager.Manager) []mana return append(managers, m) } return managers -} \ No newline at end of file +} diff --git a/pkg/ensurance/collector/cadvisor/cadvisor_unsupported.go b/pkg/ensurance/collector/cadvisor/cadvisor_unsupported.go index b53cfce86..9b20d808e 100644 --- a/pkg/ensurance/collector/cadvisor/cadvisor_unsupported.go +++ b/pkg/ensurance/collector/cadvisor/cadvisor_unsupported.go @@ -4,6 +4,8 @@ package cadvisor import ( + "errors" + info "github.com/google/cadvisor/info/v1" cadvisorapiv2 "github.com/google/cadvisor/info/v2" corelisters "k8s.io/client-go/listers/core/v1" @@ -11,13 +13,14 @@ import ( "github.com/gocrane/crane/pkg/common" "github.com/gocrane/crane/pkg/ensurance/collector/types" ) + var errUnsupported = errors.New("cAdvisor is unsupported in this build") type CadvisorCollectorUnsupport struct { Manager Manager } -type CadvisorManagerUnsupport struct {} +type CadvisorManagerUnsupport struct{} func NewCadvisorManager() Manager { return &CadvisorManagerUnsupport{} diff --git a/pkg/ensurance/collector/nodelocal/cpu.go b/pkg/ensurance/collector/nodelocal/cpu.go index 6e140240c..8fa7446c1 100644 --- a/pkg/ensurance/collector/nodelocal/cpu.go +++ b/pkg/ensurance/collector/nodelocal/cpu.go @@ -3,7 +3,6 @@ package nodelocal import ( "errors" "fmt" - "k8s.io/kubernetes/pkg/kubelet/cm/cpuset" "math" "strconv" "strings" @@ -12,6 +11,7 @@ import ( "github.com/shirou/gopsutil/cpu" "github.com/shirou/gopsutil/load" "k8s.io/klog/v2" + "k8s.io/kubernetes/pkg/kubelet/cm/cpuset" "github.com/gocrane/crane/pkg/common" "github.com/gocrane/crane/pkg/ensurance/collector/types" diff --git a/pkg/resource/node_resource_manager.go b/pkg/resource/node_resource_manager.go index 35759c4b8..e6e7d31d7 100644 --- a/pkg/resource/node_resource_manager.go +++ b/pkg/resource/node_resource_manager.go @@ -60,7 +60,7 @@ type NodeResourceManager struct { tspLister predictionlisters.TimeSeriesPredictionLister tspSynced cache.InformerSynced - recorder record.EventRecorder + recorder record.EventRecorder stateChann chan map[string][]common.TimeSeries @@ -193,7 +193,7 @@ func (o *NodeResourceManager) FindTargetNode(tsp *predictionapi.TimeSeriesPredic return false, nil } -func (o *NodeResourceManager) BuildNodeStatus(node *v1.Node) map[v1.ResourceName]string{ +func (o *NodeResourceManager) BuildNodeStatus(node *v1.Node) map[v1.ResourceName]string { tspCanNotBeReclaimedResource := o.GetCanNotBeReclaimedResourceFromTsp(node) localCanNotBeReclaimedResource := o.GetCanNotBeReclaimedResourceFromLocal() reserveCpuPercent := o.reserveResource.CpuPercent @@ -215,9 +215,9 @@ func (o *NodeResourceManager) BuildNodeStatus(node *v1.Node) map[v1.ResourceName switch resourceName { case v1.ResourceCPU: if reserveCpuPercent != nil { - nextRecommendation = float64(node.Status.Allocatable.Cpu().Value()) - float64(node.Status.Allocatable.Cpu().Value()) **reserveCpuPercent - maxUsage / 1000 + nextRecommendation = float64(node.Status.Allocatable.Cpu().Value()) - float64(node.Status.Allocatable.Cpu().Value())*(*reserveCpuPercent) - maxUsage/1000 } else { - nextRecommendation = float64(node.Status.Allocatable.Cpu().Value()) - maxUsage / 1000 + nextRecommendation = float64(node.Status.Allocatable.Cpu().Value()) - maxUsage/1000 } case v1.ResourceMemory: // unit of memory in prometheus is in Ki, need to be converted to byte diff --git a/pkg/utils/pod.go b/pkg/utils/pod.go index c08758e36..836f7a8b4 100644 --- a/pkg/utils/pod.go +++ b/pkg/utils/pod.go @@ -159,7 +159,7 @@ func GetContainerNameFromPod(pod *v1.Pod, containerId string) string { } func GetContainerFromPod(pod *v1.Pod, containerName string) *v1.Container { - if containerName == ""{ + if containerName == "" { return nil } for _, v := range pod.Spec.Containers { diff --git a/pkg/utils/string.go b/pkg/utils/string.go index 5ec868f56..9010814be 100644 --- a/pkg/utils/string.go +++ b/pkg/utils/string.go @@ -31,4 +31,4 @@ func ParsePercentage(input string) (float64, error) { return 0, err } return value / 100, nil -} \ No newline at end of file +}