Skip to content

Commit

Permalink
Remove populate function as non needed and look it up in map directly
Browse files Browse the repository at this point in the history
Signed-off-by: cmoulliard <[email protected]>
  • Loading branch information
cmoulliard committed Nov 22, 2024
1 parent 031e81b commit 68bc257
Showing 1 changed file with 11 additions and 26 deletions.
37 changes: 11 additions & 26 deletions pkg/cmd/get/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,17 @@ func populateClusterList() ([]Cluster, error) {
}

// Get Node capacity
aNode.Capacity = populateNodeCapacity(node.Status.Capacity)
resources := node.Status.Capacity

memory := resources[corev1.ResourceMemory]
cpu := resources[corev1.ResourceCPU]
pods := resources[corev1.ResourcePods]

aNode.Capacity = Capacity{
Memory: float64(memory.Value()) / (1024 * 1024 * 1024),
Cpu: cpu.Value(),
Pods: pods.Value(),
}

// Get Node Allocated resources
allocated, err := printAllocatedResources(context.Background(), cli, node.Name)
Expand Down Expand Up @@ -232,31 +242,6 @@ func printTable(opts printers.PrintOptions, table metav1.Table) {
fmt.Println(out.String())
}

func populateNodeCapacity(resources corev1.ResourceList) Capacity {
capacity := Capacity{}
for name, quantity := range resources {
if strings.HasPrefix(string(name), "hugepages-") {
continue
}

if name == corev1.ResourceMemory {
memoryInBytes := quantity.Value() // .Value() gives the value in bytes
memoryInGB := float64(memoryInBytes) / (1024 * 1024 * 1024) // Convert to GB
capacity.Memory = memoryInGB
}

if name == corev1.ResourceCPU {
capacity.Cpu = quantity.Value()
}

if name == corev1.ResourcePods {
capacity.Pods = quantity.Value()
}

}
return capacity
}

func printAllocatedResources(ctx context.Context, k8sClient client.Client, nodeName string) (Allocated, error) {
// List all pods on the specified node
var podList corev1.PodList
Expand Down

0 comments on commit 68bc257

Please sign in to comment.