diff --git a/pkg/helmvm/helmvm_node.go b/pkg/helmvm/helmvm_node.go index 7054e0cf26..cf576b8aa2 100644 --- a/pkg/helmvm/helmvm_node.go +++ b/pkg/helmvm/helmvm_node.go @@ -60,9 +60,9 @@ func nodeMetrics(ctx context.Context, client kubernetes.Interface, metricsClient return nil, fmt.Errorf("pods per node: %w", err) } - cpuCapacity := types.CapacityAvailable{} - memoryCapacity := types.CapacityAvailable{} - podCapacity := types.CapacityAvailable{} + cpuCapacity := types.CapacityUsed{} + memoryCapacity := types.CapacityUsed{} + podCapacity := types.CapacityUsed{} memoryCapacity.Capacity = float64(node.Status.Capacity.Memory().Value()) / math.Pow(2, 30) // capacity in GB @@ -76,36 +76,67 @@ func nodeMetrics(ctx context.Context, client kubernetes.Interface, metricsClient nodeUsageMetrics, err := metricsClient.MetricsV1beta1().NodeMetricses().Get(ctx, node.Name, metav1.GetOptions{}) if err == nil { if nodeUsageMetrics.Usage.Memory() != nil { - memoryCapacity.Available = memoryCapacity.Capacity - float64(nodeUsageMetrics.Usage.Memory().Value())/math.Pow(2, 30) + memoryCapacity.Used = float64(nodeUsageMetrics.Usage.Memory().Value()) / math.Pow(2, 30) } if nodeUsageMetrics.Usage.Cpu() != nil { - cpuCapacity.Available = cpuCapacity.Capacity - nodeUsageMetrics.Usage.Cpu().AsApproximateFloat64() + cpuCapacity.Used = nodeUsageMetrics.Usage.Cpu().AsApproximateFloat64() } } else { // if we can't get metrics, we'll do nothing for now // in the future we may decide to retry or log a warning } - podCapacity.Available = podCapacity.Capacity - float64(len(nodePods)) + podCapacity.Used = float64(len(nodePods)) nodeLabelArray := []string{} for k, v := range node.Labels { nodeLabelArray = append(nodeLabelArray, fmt.Sprintf("%s:%s", k, v)) } + podInfo := []types.PodInfo{} + + for _, pod := range nodePods { + newInfo := types.PodInfo{ + Name: pod.Name, + Namespace: pod.Namespace, + Status: string(pod.Status.Phase), + } + + podMetrics, err := metricsClient.MetricsV1beta1().PodMetricses(pod.Namespace).Get(ctx, pod.Name, metav1.GetOptions{}) + if err == nil { + podTotalMemory := 0.0 + podTotalCPU := 0.0 + for _, container := range podMetrics.Containers { + if container.Usage.Memory() != nil { + podTotalMemory += float64(container.Usage.Memory().Value()) / math.Pow(2, 30) + } + if container.Usage.Cpu() != nil { + podTotalCPU += container.Usage.Cpu().AsApproximateFloat64() + } + } + newInfo.Memory = podTotalMemory + newInfo.CPU = podTotalCPU + } + + podInfo = append(podInfo, newInfo) + } + return &types.Node{ - Name: node.Name, - IsConnected: isConnected(node), - IsReady: isReady(node), - IsPrimaryNode: isPrimary(node), - CanDelete: node.Spec.Unschedulable && !isConnected(node), - KubeletVersion: node.Status.NodeInfo.KubeletVersion, - CPU: cpuCapacity, - Memory: memoryCapacity, - Pods: podCapacity, - Labels: nodeLabelArray, - Conditions: findNodeConditions(node.Status.Conditions), - PodList: nodePods, + Name: node.Name, + IsConnected: isConnected(node), + IsReady: isReady(node), + IsPrimaryNode: isPrimary(node), + CanDelete: node.Spec.Unschedulable && !isConnected(node), + KubeletVersion: node.Status.NodeInfo.KubeletVersion, + KubeProxyVersion: node.Status.NodeInfo.KubeProxyVersion, + OperatingSystem: node.Status.NodeInfo.OperatingSystem, + KernelVersion: node.Status.NodeInfo.KernelVersion, + CPU: cpuCapacity, + Memory: memoryCapacity, + Pods: podCapacity, + Labels: nodeLabelArray, + Conditions: findNodeConditions(node.Status.Conditions), + PodList: podInfo, }, nil } diff --git a/pkg/helmvm/types/types.go b/pkg/helmvm/types/types.go index 78ea23f248..b94f254dc0 100644 --- a/pkg/helmvm/types/types.go +++ b/pkg/helmvm/types/types.go @@ -1,7 +1,5 @@ package types -import corev1 "k8s.io/api/core/v1" - type HelmVMNodes struct { Nodes []Node `json:"nodes"` HA bool `json:"ha"` @@ -9,23 +7,26 @@ type HelmVMNodes struct { } type Node struct { - Name string `json:"name"` - IsConnected bool `json:"isConnected"` - IsReady bool `json:"isReady"` - IsPrimaryNode bool `json:"isPrimaryNode"` - CanDelete bool `json:"canDelete"` - KubeletVersion string `json:"kubeletVersion"` - CPU CapacityAvailable `json:"cpu"` - Memory CapacityAvailable `json:"memory"` - Pods CapacityAvailable `json:"pods"` - Labels []string `json:"labels"` - Conditions NodeConditions `json:"conditions"` - PodList []corev1.Pod `json:"podList"` + Name string `json:"name"` + IsConnected bool `json:"isConnected"` + IsReady bool `json:"isReady"` + IsPrimaryNode bool `json:"isPrimaryNode"` + CanDelete bool `json:"canDelete"` + KubeletVersion string `json:"kubeletVersion"` + KubeProxyVersion string `json:"kubeProxyVersion"` + OperatingSystem string `json:"operatingSystem"` + KernelVersion string `json:"kernelVersion"` + CPU CapacityUsed `json:"cpu"` + Memory CapacityUsed `json:"memory"` + Pods CapacityUsed `json:"pods"` + Labels []string `json:"labels"` + Conditions NodeConditions `json:"conditions"` + PodList []PodInfo `json:"podList"` } -type CapacityAvailable struct { - Capacity float64 `json:"capacity"` - Available float64 `json:"available"` +type CapacityUsed struct { + Capacity float64 `json:"capacity"` + Used float64 `json:"used"` } type NodeConditions struct { @@ -34,3 +35,11 @@ type NodeConditions struct { PidPressure bool `json:"pidPressure"` Ready bool `json:"ready"` } + +type PodInfo struct { + Name string `json:"name"` + Status string `json:"status"` + Namespace string `json:"namespace"` + CPU float64 `json:"cpu"` + Memory float64 `json:"memory"` +}