From 65764c8ed2c7954d860924cd48b333e68857c820 Mon Sep 17 00:00:00 2001 From: derekwaynecarr Date: Tue, 14 Jun 2016 11:16:16 -0400 Subject: [PATCH] kubectl describe node is allocatable aware --- pkg/kubectl/describe.go | 42 ++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/pkg/kubectl/describe.go b/pkg/kubectl/describe.go index 83a2a106ea..b7bfb73dc2 100644 --- a/pkg/kubectl/describe.go +++ b/pkg/kubectl/describe.go @@ -1660,12 +1660,27 @@ func describeNode(node *api.Node, nodeNonTerminatedPodsList *api.PodList, events for _, address := range node.Status.Addresses { addresses = append(addresses, address.Address) } + + printResourceList := func(resourceList api.ResourceList) { + resources := make([]api.ResourceName, 0, len(resourceList)) + for resource := range resourceList { + resources = append(resources, resource) + } + sort.Sort(SortableResourceNames(resources)) + for _, resource := range resources { + value := resourceList[resource] + fmt.Fprintf(out, " %s:\t%s\n", resource, value.String()) + } + } + fmt.Fprintf(out, "Addresses:\t%s\n", strings.Join(addresses, ",")) if len(node.Status.Capacity) > 0 { fmt.Fprintf(out, "Capacity:\n") - for resource, value := range node.Status.Capacity { - fmt.Fprintf(out, " %s:\t%s\n", resource, value.String()) - } + printResourceList(node.Status.Capacity) + } + if len(node.Status.Allocatable) > 0 { + fmt.Fprintf(out, "Allocatable:\n") + printResourceList(node.Status.Allocatable) } fmt.Fprintf(out, "System Info:\n") @@ -1802,16 +1817,21 @@ func describeNodeResource(nodeNonTerminatedPodsList *api.PodList, node *api.Node fmt.Fprintf(out, "Non-terminated Pods:\t(%d in total)\n", len(nodeNonTerminatedPodsList.Items)) fmt.Fprint(out, " Namespace\tName\t\tCPU Requests\tCPU Limits\tMemory Requests\tMemory Limits\n") fmt.Fprint(out, " ---------\t----\t\t------------\t----------\t---------------\t-------------\n") + allocatable := node.Status.Capacity + if len(node.Status.Allocatable) > 0 { + allocatable = node.Status.Allocatable + } + for _, pod := range nodeNonTerminatedPodsList.Items { req, limit, err := api.PodRequestsAndLimits(&pod) if err != nil { return err } cpuReq, cpuLimit, memoryReq, memoryLimit := req[api.ResourceCPU], limit[api.ResourceCPU], req[api.ResourceMemory], limit[api.ResourceMemory] - fractionCpuReq := float64(cpuReq.MilliValue()) / float64(node.Status.Capacity.Cpu().MilliValue()) * 100 - fractionCpuLimit := float64(cpuLimit.MilliValue()) / float64(node.Status.Capacity.Cpu().MilliValue()) * 100 - fractionMemoryReq := float64(memoryReq.MilliValue()) / float64(node.Status.Capacity.Memory().MilliValue()) * 100 - fractionMemoryLimit := float64(memoryLimit.MilliValue()) / float64(node.Status.Capacity.Memory().MilliValue()) * 100 + fractionCpuReq := float64(cpuReq.MilliValue()) / float64(allocatable.Cpu().MilliValue()) * 100 + fractionCpuLimit := float64(cpuLimit.MilliValue()) / float64(allocatable.Cpu().MilliValue()) * 100 + fractionMemoryReq := float64(memoryReq.Value()) / float64(allocatable.Memory().Value()) * 100 + fractionMemoryLimit := float64(memoryLimit.Value()) / float64(allocatable.Memory().Value()) * 100 fmt.Fprintf(out, " %s\t%s\t\t%s (%d%%)\t%s (%d%%)\t%s (%d%%)\t%s (%d%%)\n", pod.Namespace, pod.Name, cpuReq.String(), int64(fractionCpuReq), cpuLimit.String(), int64(fractionCpuLimit), memoryReq.String(), int64(fractionMemoryReq), memoryLimit.String(), int64(fractionMemoryLimit)) @@ -1824,10 +1844,10 @@ func describeNodeResource(nodeNonTerminatedPodsList *api.PodList, node *api.Node return err } cpuReqs, cpuLimits, memoryReqs, memoryLimits := reqs[api.ResourceCPU], limits[api.ResourceCPU], reqs[api.ResourceMemory], limits[api.ResourceMemory] - fractionCpuReqs := float64(cpuReqs.MilliValue()) / float64(node.Status.Capacity.Cpu().MilliValue()) * 100 - fractionCpuLimits := float64(cpuLimits.MilliValue()) / float64(node.Status.Capacity.Cpu().MilliValue()) * 100 - fractionMemoryReqs := float64(memoryReqs.MilliValue()) / float64(node.Status.Capacity.Memory().MilliValue()) * 100 - fractionMemoryLimits := float64(memoryLimits.MilliValue()) / float64(node.Status.Capacity.Memory().MilliValue()) * 100 + fractionCpuReqs := float64(cpuReqs.MilliValue()) / float64(allocatable.Cpu().MilliValue()) * 100 + fractionCpuLimits := float64(cpuLimits.MilliValue()) / float64(allocatable.Cpu().MilliValue()) * 100 + fractionMemoryReqs := float64(memoryReqs.Value()) / float64(allocatable.Memory().Value()) * 100 + fractionMemoryLimits := float64(memoryLimits.Value()) / float64(allocatable.Memory().Value()) * 100 fmt.Fprintf(out, " %s (%d%%)\t%s (%d%%)\t%s (%d%%)\t%s (%d%%)\n", cpuReqs.String(), int64(fractionCpuReqs), cpuLimits.String(), int64(fractionCpuLimits), memoryReqs.String(), int64(fractionMemoryReqs), memoryLimits.String(), int64(fractionMemoryLimits))