Skip to content

Commit

Permalink
add more readable units
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmainguy committed Mar 7, 2020
1 parent 305b7b9 commit 41535c2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 50 deletions.
39 changes: 24 additions & 15 deletions cmd/k8sCapcity/humanMode.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,30 @@ import (
resource "k8s.io/apimachinery/pkg/api/resource"
)

func toGibFromMib(mib int64) (result float64) {
func toGib(rq resource.Quantity) (result float64) {
mib := toMib(rq)
result = float64(mib) / 1024
return result
}

func toGib(rq resource.Quantity) (result int64) {
result = int64(float64(rq.ScaledValue(resource.Giga)) / 1.073741824)
return result
}

func toMib(rq resource.Quantity) (result int64) {
result = int64(float64(rq.ScaledValue(resource.Mega)) / 1.048576)
return result
}

func toMibFromByte(bytes int64) (mib int64) {
kib := int64(float64(bytes) / 1024)
mib = int64(float64(kib) / 1024)
return mib
}

func toGibFromByte(bytes int64) (gib float64) {
kib := int64(float64(bytes) / 1024)
mib := int64(float64(kib) / 1024)
gib = float64(mib) / 1024
return gib
}

func humanMode(clusterInfo ClusterInfo) {

fmt.Printf("There are %d nodes in this cluster\n", len(clusterInfo.NodeInfo))
Expand All @@ -30,14 +39,14 @@ func humanMode(clusterInfo ClusterInfo) {
fmt.Println("================")
fmt.Printf("NodeName: %s\n", name)
fmt.Printf("Allocatable CPU: %s\n", &node.AllocatableCPU)
fmt.Printf("Allocatable Memory: %dGiB\n", toGib(node.AllocatableMemory))
fmt.Printf("Allocatable Memory: %.1fGiB\n", toGib(node.AllocatableMemory))
fmt.Printf("Allocatable Pods: %s\n", &node.AllocatablePods)
fmt.Println("----------------")
fmt.Printf("Used CPU: %s\n", &node.UsedCPU)
fmt.Printf("Used Memory: %dGiB\n", toGib(node.UsedMemory))
fmt.Printf("Used Memory: %.1fGiB\n", toGib(node.UsedMemory))
fmt.Printf("Used Pods: %d\n", node.UsedPods)
fmt.Printf("Used CPU Requests: %s\n", &node.UsedCPURequests)
fmt.Printf("Used Memory Requests: %dGiB\n", toGib(node.UsedMemoryRequests))
fmt.Printf("Used Memory Requests: %.1fGiB\n", toGib(node.UsedMemoryRequests))
fmt.Println("----------------")

AvailbleCPURequests := resource.Quantity{}
Expand All @@ -49,7 +58,7 @@ func humanMode(clusterInfo ClusterInfo) {

AvailableMemoryRequests = node.AllocatableMemory
AvailableMemoryRequests.Sub(node.UsedMemoryRequests)
fmt.Printf("Available Memory Requests: %dGiB\n", toGib(AvailableMemoryRequests))
fmt.Printf("Available Memory Requests: %.1fGiB\n", toGib(AvailableMemoryRequests))

AvailablePods, _ := node.AllocatablePods.AsInt64()
AvailablePods = AvailablePods - node.UsedPods
Expand All @@ -64,21 +73,21 @@ func humanMode(clusterInfo ClusterInfo) {
}
}
fmt.Println("================")
fmt.Printf("ClusterWide Allocatable Memory: %dGiB\n", toGib(clusterInfo.ClusterAllocatableMemory))
fmt.Printf("ClusterWide Allocatable Memory: %.1fGiB\n", toGib(clusterInfo.ClusterAllocatableMemory))
fmt.Printf("ClusterWide Allocatable CPU: %s\n", &clusterInfo.ClusterAllocatableCPU)
fmt.Printf("ClusterWide Allocatable Pods: %d\n", clusterInfo.ClusterAllocatablePods.Value())
fmt.Println("================")
fmt.Printf("ResourceQuota ClusterWide Allocated Limits.Memory: %dGiB\n", toGib(clusterInfo.RqclusterAllocatedLimitsMemory))
fmt.Printf("ResourceQuota ClusterWide Allocated Limits.Memory: %.1fGiB\n", toGib(clusterInfo.RqclusterAllocatedLimitsMemory))
fmt.Printf("ResourceQuota ClusterWide Allocated Limits.CPU: %d\n", clusterInfo.RqclusterAllocatedLimitsCPU.AsDec())
fmt.Printf("ResourceQuota ClusterWide Allocated Pods: %d\n", clusterInfo.RqclusterAllocatedPods.Value())
fmt.Println("================")
fmt.Printf("ResourceQuota ClusterWide Allocated Requests.Memory: %dGiB\n", toGib(clusterInfo.RqclusterAllocatedRequestsMemory))
fmt.Printf("ResourceQuota ClusterWide Allocated Requests.Memory: %.1fGiB\n", toGib(clusterInfo.RqclusterAllocatedRequestsMemory))
fmt.Printf("ResourceQuota ClusterWide Allocated Requests.CPU: %d\n", clusterInfo.RqclusterAllocatedRequestsCPU.AsDec())
fmt.Println("----------------")
fmt.Printf("ClusterWide Used CPU: %d\n", clusterInfo.ClusterUsedCPU.Value())
fmt.Printf("ClusterWide Used Memory: %dGiB\n", toGib(clusterInfo.ClusterUsedMemory))
fmt.Printf("ClusterWide Used Memory: %.1fGiB\n", toGib(clusterInfo.ClusterUsedMemory))
fmt.Printf("ClusterWide Used Pods: %d\n", clusterInfo.ClusterUsedPods)
fmt.Printf("ClusterWide Used CPU Requests: %d\n", clusterInfo.ClusterUsedCPURequests.Value())
fmt.Printf("ClusterWide Used Memory Requests: %dGiB\n", toGib(clusterInfo.ClusterUsedMemoryRequests))
fmt.Printf("ClusterWide Used Memory Requests: %.1fGiB\n", toGib(clusterInfo.ClusterUsedMemoryRequests))

}
2 changes: 1 addition & 1 deletion cmd/k8sCapcity/humanMode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestToGib(t *testing.T) {
memory := resource.MustParse("1024Mi")
memoryGib := toGib(memory)
if memoryGib != 1 {
t.Errorf("Expected 1, got %d", memoryGib)
t.Errorf("Expected 1, got %f", memoryGib)
}
}

Expand Down
31 changes: 19 additions & 12 deletions cmd/k8sCapcity/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ func gatherPodSpecInfo(pod corev1.Pod, nsInfo NamespaceInfo) NamespaceInfo {
for _, container := range pod.Spec.Containers {
uniqueContainerName := fmt.Sprintf("%s-%s", pod.Name, container.Name)
containerStats := nsInfo.NamespacePods[pod.Name].Containers[uniqueContainerName]
containerStats.MemoryRequests = *container.Resources.Requests.Memory()
containerStats.MemoryLimits = *container.Resources.Limits.Memory()
containerStats.MemoryRequests = container.Resources.Requests.Memory().Value()
containerStats.MemoryRequestsMiB = toMib(*container.Resources.Requests.Memory())
containerStats.MemoryLimits = container.Resources.Limits.Memory().Value()
containerStats.MemoryLimitsMiB = toMib(*container.Resources.Limits.Memory())
containerStats.CPURequestsMilliCores = container.Resources.Requests.Cpu().ScaledValue(resource.Milli)
containerStats.CPURequestsCores = float64(container.Resources.Requests.Cpu().ScaledValue(resource.Milli)) / 1000
containerStats.CPULimitsMilliCores = container.Resources.Limits.Cpu().ScaledValue(resource.Milli)
Expand All @@ -23,8 +25,8 @@ func gatherPodSpecInfo(pod corev1.Pod, nsInfo NamespaceInfo) NamespaceInfo {
containerStats.Pod = pod.Name
nsInfo.NamespacePods[pod.Name].Containers[uniqueContainerName] = containerStats
// Add up for the namespace
nsInfo.NamespaceMemoryLimits.Add(containerStats.MemoryLimits)
nsInfo.NamespaceMemoryRequests.Add(containerStats.MemoryRequests)
nsInfo.NamespaceMemoryLimits = nsInfo.NamespaceMemoryLimits + containerStats.MemoryLimits
nsInfo.NamespaceMemoryRequests = nsInfo.NamespaceMemoryRequests + containerStats.MemoryRequests
nsInfo.NamespaceCPULimitsMilliCores = nsInfo.NamespaceCPULimitsMilliCores + containerStats.CPULimitsMilliCores
nsInfo.NamespaceCPURequestsMilliCores = nsInfo.NamespaceCPURequestsMilliCores + containerStats.CPURequestsMilliCores
}
Expand All @@ -43,13 +45,14 @@ func gatherNamespaceInfo(clientset *kubernetes.Clientset, nameSpace *string) Nam
for _, container := range metricPod.Containers {
uniqueContainerName := fmt.Sprintf("%s-%s", metricPod.Name, container.Name)
c := &ContainerInfo{
MemoryUsed: *container.Usage.Memory(),
MemoryUsed: container.Usage.Memory().Value(),
MemoryUsedMiB: toMib(*container.Usage.Memory()),
CPUUsedMilliCores: container.Usage.Cpu().MilliValue(),
CPUUsedCores: float64(container.Usage.Cpu().MilliValue()) / 1000,
}
containerArray[uniqueContainerName] = *c
// Add up for the namespace
nsInfo.NamespaceMemoryUsed.Add(*container.Usage.Memory())
nsInfo.NamespaceMemoryUsed = nsInfo.NamespaceMemoryUsed + container.Usage.Memory().Value()
nsInfo.NamespaceCPUUsedMilliCores = nsInfo.NamespaceCPUUsedMilliCores + container.Usage.Cpu().MilliValue()
}
nsInfo.NamespacePods[metricPod.Name] = &Pod{Containers: containerArray}
Expand All @@ -67,6 +70,9 @@ func gatherNamespaceInfo(clientset *kubernetes.Clientset, nameSpace *string) Nam
nsInfo.NamespaceCPUUsedCores = float64(nsInfo.NamespaceCPUUsedMilliCores) / 1000
nsInfo.NamespaceCPULimitsCores = float64(nsInfo.NamespaceCPULimitsMilliCores) / 1000
nsInfo.NamespaceCPURequestsCores = float64(nsInfo.NamespaceCPURequestsMilliCores) / 1000
nsInfo.NamespaceMemoryLimitsGiB = toGibFromByte(nsInfo.NamespaceMemoryLimits)
nsInfo.NamespaceMemoryRequestsGiB = toGibFromByte(nsInfo.NamespaceMemoryRequests)
nsInfo.NamespaceMemoryUsedGiB = toGibFromByte(nsInfo.NamespaceMemoryUsed)
nsInfo.Name = *nameSpace
return nsInfo
}
Expand All @@ -88,23 +94,24 @@ func namespaceHumanMode(nsInfo NamespaceInfo) {
fmt.Printf("Container Name: %s\n", container.Name)
fmt.Println("----------------")
fmt.Printf("CPURequests: %v\n", container.CPURequestsCores)
fmt.Printf("MemoryRequests: %vMiB\n", toMib(container.MemoryRequests))
fmt.Printf("MemoryRequests: %dMiB\n", toMibFromByte(container.MemoryRequests))
fmt.Printf("CPULimits: %v\n", container.CPULimitsCores)
fmt.Printf("MemoryLimits: %vMiB\n", toMib(container.MemoryLimits))
fmt.Printf("MemoryLimits: %dMiB\n", toMibFromByte(container.MemoryLimits))
fmt.Println("----------------")
fmt.Printf("CPU Used: %dm\n", container.CPUUsedMilliCores)
fmt.Printf("Memory Used: %vMiB\n", toMib(container.MemoryUsed))
fmt.Printf("Memory Used: %dMiB\n", toMibFromByte(container.MemoryUsed))
fmt.Println("================")
}
}
fmt.Printf("<><><><><>Sum Total for Namespace: %s<><><><><>\n", nsInfo.Name)
fmt.Println("----------------")
fmt.Printf("Namespace Total CPURequests: %v\n", nsInfo.NamespaceCPURequestsCores)
fmt.Printf("Namespace Total MemoryRequests: %vMiB (%.1fGiB)\n", toMib(nsInfo.NamespaceMemoryRequests), toGibFromMib(toMib(nsInfo.NamespaceMemoryRequests)))
fmt.Printf("Namespace Total MemoryRequests: %vMiB (%.1fGiB)\n", toMibFromByte(nsInfo.NamespaceMemoryRequests), nsInfo.NamespaceMemoryRequestsGiB)
fmt.Printf("Namespace Total CPULimits: %v\n", nsInfo.NamespaceCPULimitsCores)
fmt.Printf("Namespace Total MemoryLimits: %vMiB (%.1fGiB)\n", toMib(nsInfo.NamespaceMemoryLimits), toGibFromMib(toMib(nsInfo.NamespaceMemoryLimits)))
fmt.Printf("Namespace Total MemoryLimits: %vMiB (%.1fGiB)\n", toMibFromByte(nsInfo.NamespaceMemoryLimits), nsInfo.NamespaceMemoryLimitsGiB)
fmt.Println("----------------")
fmt.Printf("Namespace Total CPU Used: %v\n", nsInfo.NamespaceCPUUsedCores)
fmt.Printf("Namespace Total Memory Used: %vMiB (%.1fGiB)\n", toMib(nsInfo.NamespaceMemoryUsed), toGibFromMib(toMib(nsInfo.NamespaceMemoryUsed)))
fmt.Printf("Namespace Total Memory Used: %dMiB (%.1fGiB)\n", toMibFromByte(nsInfo.NamespaceMemoryUsed), nsInfo.NamespaceMemoryUsedGiB)

os.Exit(0)
}
50 changes: 28 additions & 22 deletions cmd/k8sCapcity/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,20 @@ type NodeInfo struct {

// ContainerInfo : Information about the container
type ContainerInfo struct {
Name string `json:"name"`
Pod string `json:"pod"`
CPURequestsMilliCores int64 `json:"cpu_requests.millicores"`
CPURequestsCores float64 `json:"cpu_requests.cores"`
CPULimitsMilliCores int64 `json:"cpu_limits.millicores"`
CPULimitsCores float64 `json:"cpu_limits.cores"`
MemoryRequests resource.Quantity `json:"memory_requests"`
MemoryLimits resource.Quantity `json:"memory_limits"`
CPUUsedMilliCores int64 `json:"cpu_used.millicores"`
CPUUsedCores float64 `json:"cpu_used.cores"`
MemoryUsed resource.Quantity `json:"memory_used"`
Name string `json:"name"`
Pod string `json:"pod"`
CPURequestsMilliCores int64 `json:"cpu_requests.millicores"`
CPULimitsMilliCores int64 `json:"cpu_limits.millicores"`
MemoryRequests int64 `json:"memory_requests.bytes"`
MemoryLimits int64 `json:"memory_limits.bytes"`
CPUUsedMilliCores int64 `json:"cpu_used.millicores"`
MemoryUsed int64 `json:"memory_used.bytes"`
MemoryRequestsMiB int64 `json:"memory_requests.mebibytes"`
MemoryLimitsMiB int64 `json:"memory_limits.mebibytes"`
MemoryUsedMiB int64 `json:"memory_used.mebibytes"`
CPURequestsCores float64 `json:"cpu_requests.cores"`
CPULimitsCores float64 `json:"cpu_limits.cores"`
CPUUsedCores float64 `json:"cpu_used.cores"`
}

// DaemonLog : Json to print out about metrics we gathered
Expand Down Expand Up @@ -105,17 +108,20 @@ type DaemonLog struct {

// NamespaceInfo : Information about the namespace
type NamespaceInfo struct {
Name string `json:"k8s_quota.namespace.name"`
NamespacePods map[string]*Pod `json:"k8s_quota.namespace.pods"`
NamespaceMemoryLimits resource.Quantity `json:"k8s_quota.namespace.memory_limits"`
NamespaceMemoryRequests resource.Quantity `json:"k8s_quota.namespace.memory_requests"`
NamespaceMemoryUsed resource.Quantity `json:"k8s_quota.namespace.memory_used"`
NamespaceCPULimitsCores float64 `json:"k8s_quota.namespace.cpu_limits.cores"`
NamespaceCPULimitsMilliCores int64 `json:"k8s_quota.namespace.cpu_limits.millicores"`
NamespaceCPURequestsCores float64 `json:"k8s_quota.namespace.cpu_requests.cores"`
NamespaceCPURequestsMilliCores int64 `json:"k8s_quota.namespace.cpu_requests.millicores"`
NamespaceCPUUsedCores float64 `json:"k8s_quota.namespace.cpu_used.cores"`
NamespaceCPUUsedMilliCores int64 `json:"k8s_quota.namespace.cpu_used.millicores"`
Name string `json:"k8s_quota.namespace.name"`
NamespacePods map[string]*Pod `json:"k8s_quota.namespace.pods"`
NamespaceMemoryLimits int64 `json:"k8s_quota.namespace.memory_limits.bytes"`
NamespaceMemoryRequests int64 `json:"k8s_quota.namespace.memory_requests.bytes"`
NamespaceMemoryUsed int64 `json:"k8s_quota.namespace.memory_used.bytes"`
NamespaceCPULimitsMilliCores int64 `json:"k8s_quota.namespace.cpu_limits.millicores"`
NamespaceCPURequestsMilliCores int64 `json:"k8s_quota.namespace.cpu_requests.millicores"`
NamespaceCPUUsedMilliCores int64 `json:"k8s_quota.namespace.cpu_used.millicores"`
NamespaceMemoryLimitsGiB float64 `json:"k8s_quota.namespace.memory_limits.gibibytes"`
NamespaceMemoryRequestsGiB float64 `json:"k8s_quota.namespace.memory_requests.gibibytes"`
NamespaceMemoryUsedGiB float64 `json:"k8s_quota.namespace.memory_used.gibibytes"`
NamespaceCPULimitsCores float64 `json:"k8s_quota.namespace.cpu_limits.cores"`
NamespaceCPURequestsCores float64 `json:"k8s_quota.namespace.cpu_requests.cores"`
NamespaceCPUUsedCores float64 `json:"k8s_quota.namespace.cpu_used.cores"`
}

// Pod : A pod full of containers
Expand Down

0 comments on commit 41535c2

Please sign in to comment.