Skip to content

Commit

Permalink
include pod usage metrics
Browse files Browse the repository at this point in the history
add kube-proxy/os/kernel to node metrics

return 'used' not 'available'
  • Loading branch information
laverya committed Oct 19, 2023
1 parent ccd51bd commit 4e8b32d
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 35 deletions.
67 changes: 49 additions & 18 deletions pkg/helmvm/helmvm_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
}
43 changes: 26 additions & 17 deletions pkg/helmvm/types/types.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
package types

import corev1 "k8s.io/api/core/v1"

type HelmVMNodes struct {
Nodes []Node `json:"nodes"`
HA bool `json:"ha"`
IsHelmVMEnabled bool `json:"isHelmVMEnabled"`
}

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 {
Expand All @@ -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"`
}

0 comments on commit 4e8b32d

Please sign in to comment.