mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-31 15:25:57 +00:00
Use resource.Quantity string representation to print requests and limits
This commit is contained in:
parent
22a794cc22
commit
5ffe46d376
@ -1128,7 +1128,9 @@ func describeNode(node *api.Node, pods []*api.Pod, events *api.EventList) (strin
|
|||||||
if len(node.Spec.ExternalID) > 0 {
|
if len(node.Spec.ExternalID) > 0 {
|
||||||
fmt.Fprintf(out, "ExternalID:\t%s\n", node.Spec.ExternalID)
|
fmt.Fprintf(out, "ExternalID:\t%s\n", node.Spec.ExternalID)
|
||||||
}
|
}
|
||||||
describeNodeResource(pods, node, out)
|
if err := describeNodeResource(pods, node, out); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if events != nil {
|
if events != nil {
|
||||||
DescribeEvents(events, out)
|
DescribeEvents(events, out)
|
||||||
@ -1186,52 +1188,38 @@ func (d *HorizontalPodAutoscalerDescriber) Describe(namespace, name string) (str
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func describeNodeResource(pods []*api.Pod, node *api.Node, out io.Writer) {
|
func describeNodeResource(pods []*api.Pod, node *api.Node, out io.Writer) error {
|
||||||
nonTerminatedPods := filterTerminatedPods(pods)
|
nonTerminatedPods := filterTerminatedPods(pods)
|
||||||
fmt.Fprintf(out, "Non-terminated Pods:\t(%d in total)\n", len(nonTerminatedPods))
|
fmt.Fprintf(out, "Non-terminated Pods:\t(%d in total)\n", len(nonTerminatedPods))
|
||||||
fmt.Fprint(out, " Namespace\tName\t\tCPU Requests\tCPU Limits\tMemory Requests\tMemory Limits\n")
|
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")
|
fmt.Fprint(out, " ─────────\t────\t\t────────────\t──────────\t───────────────\t─────────────\n")
|
||||||
totalMilliCPUReq := int64(0)
|
for _, pod := range nonTerminatedPods {
|
||||||
totalMemoryReq := int64(0)
|
req, limit, err := getSinglePodTotalRequestsAndLimits(pod)
|
||||||
fractionPodCPUReq := float64(0)
|
if err != nil {
|
||||||
fractionPodMemoryReq := float64(0)
|
return err
|
||||||
fractionTotalCPUReq := float64(0)
|
|
||||||
fractionTotalMemoryReq := float64(0)
|
|
||||||
totalMilliCPULimit := int64(0)
|
|
||||||
totalMemoryLimit := int64(0)
|
|
||||||
fractionPodCPULimit := float64(0)
|
|
||||||
fractionPodMemoryLimit := float64(0)
|
|
||||||
for _, pod := range pods {
|
|
||||||
podTotalMilliCPUReq := int64(0)
|
|
||||||
podTotalMemoryReq := int64(0)
|
|
||||||
podTotalMilliCPULimit := int64(0)
|
|
||||||
podTotalMemoryLimit := int64(0)
|
|
||||||
|
|
||||||
for ix := range pod.Spec.Containers {
|
|
||||||
requests := pod.Spec.Containers[ix].Resources.Requests
|
|
||||||
podTotalMilliCPUReq += requests.Cpu().MilliValue()
|
|
||||||
podTotalMemoryReq += requests.Memory().Value()
|
|
||||||
|
|
||||||
limits := pod.Spec.Containers[ix].Resources.Limits
|
|
||||||
podTotalMilliCPULimit += limits.Cpu().MilliValue()
|
|
||||||
podTotalMemoryLimit += limits.Memory().Value()
|
|
||||||
}
|
}
|
||||||
totalMilliCPUReq += podTotalMilliCPUReq
|
cpuReq, cpuLimit, memoryReq, memoryLimit := req[api.ResourceCPU], limit[api.ResourceCPU], req[api.ResourceMemory], limit[api.ResourceMemory]
|
||||||
totalMemoryReq += podTotalMemoryReq
|
fractionCpuReq := float64(cpuReq.MilliValue()) / float64(node.Status.Capacity.Cpu().MilliValue()) * 100
|
||||||
totalMilliCPULimit += podTotalMilliCPULimit
|
fractionCpuLimit := float64(cpuLimit.MilliValue()) / float64(node.Status.Capacity.Cpu().MilliValue()) * 100
|
||||||
totalMemoryLimit += podTotalMemoryLimit
|
fractionMemoryReq := float64(memoryReq.MilliValue()) / float64(node.Status.Capacity.Memory().MilliValue()) * 100
|
||||||
fractionPodCPUReq = float64(podTotalMilliCPUReq) / float64(node.Status.Capacity.Cpu().MilliValue()) * 100
|
fractionMemoryLimit := float64(memoryLimit.MilliValue()) / float64(node.Status.Capacity.Memory().MilliValue()) * 100
|
||||||
fractionPodMemoryReq = float64(podTotalMemoryReq) / float64(node.Status.Capacity.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,
|
||||||
fractionPodCPULimit = float64(podTotalMilliCPULimit) / float64(node.Status.Capacity.Cpu().MilliValue()) * 100
|
cpuReq.String(), int64(fractionCpuReq), cpuLimit.String(), int64(fractionCpuLimit),
|
||||||
fractionPodMemoryLimit = float64(podTotalMemoryLimit) / float64(node.Status.Capacity.Memory().Value()) * 100
|
memoryReq.String(), int64(fractionMemoryReq), memoryLimit.String(), int64(fractionMemoryLimit))
|
||||||
fmt.Fprintf(out, " %s\t%s\t\t%dm (%d%%)\t%dKi (%d%%)\t%dm (%d%%)\t%dKi (%d%%)\n", pod.Namespace, pod.Name,
|
|
||||||
podTotalMilliCPUReq, int64(fractionPodCPUReq), podTotalMilliCPULimit, int64(fractionPodCPULimit), podTotalMemoryReq/1000, int64(fractionPodMemoryReq), podTotalMemoryLimit/1000, int64(fractionPodMemoryLimit))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprint(out, "Allocated resources:\n CPU Requests\tCPU Limits\tMemory Requests\tMemory Limits\n")
|
fmt.Fprint(out, "Allocated resources:\n CPU Requests\tCPU Limits\tMemory Requests\tMemory Limits\n")
|
||||||
fmt.Fprint(out, " ────────────\t──────────\t───────────────\t─────────────\n")
|
fmt.Fprint(out, " ────────────\t──────────\t───────────────\t─────────────\n")
|
||||||
fractionTotalCPUReq = float64(totalMilliCPUReq) / float64(node.Status.Capacity.Cpu().MilliValue()) * 100
|
reqs, limits, err := getPodsTotalRequestsAndLimits(nonTerminatedPods)
|
||||||
fractionTotalMemoryReq = float64(totalMemoryReq) / float64(node.Status.Capacity.Memory().Value()) * 100
|
if err != nil {
|
||||||
fmt.Fprintf(out, " %dm (%d%%)\t%dm\t%dKi (%d%%)\t%dKi\n", totalMilliCPUReq, int64(fractionTotalCPUReq), totalMilliCPULimit, totalMemoryReq/1000, int64(fractionTotalMemoryReq), totalMemoryLimit/1000)
|
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
|
||||||
|
fractionMemoryReqs := float64(memoryReqs.MilliValue()) / float64(node.Status.Capacity.Memory().MilliValue()) * 100
|
||||||
|
fmt.Fprintf(out, " %s (%d%%)\t%s\t%s (%d%%)\t%s\n",
|
||||||
|
cpuReqs.String(), int64(fractionCpuReqs), cpuLimits.String(), memoryReqs.String(), int64(fractionMemoryReqs), memoryLimits.String())
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func filterTerminatedPods(pods []*api.Pod) []*api.Pod {
|
func filterTerminatedPods(pods []*api.Pod) []*api.Pod {
|
||||||
@ -1248,36 +1236,50 @@ func filterTerminatedPods(pods []*api.Pod) []*api.Pod {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func getPodsTotalRequests(pods []*api.Pod) (map[api.ResourceName]resource.Quantity, error) {
|
func getPodsTotalRequestsAndLimits(pods []*api.Pod) (reqs map[api.ResourceName]resource.Quantity, limits map[api.ResourceName]resource.Quantity, err error) {
|
||||||
reqs := map[api.ResourceName]resource.Quantity{}
|
reqs, limits = map[api.ResourceName]resource.Quantity{}, map[api.ResourceName]resource.Quantity{}
|
||||||
for _, pod := range pods {
|
for _, pod := range pods {
|
||||||
podReqs, err := getSinglePodTotalRequests(pod)
|
podReqs, podLimits, err := getSinglePodTotalRequestsAndLimits(pod)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
for podReqName, podReqValue := range podReqs {
|
for podReqName, podReqValue := range podReqs {
|
||||||
if value, ok := reqs[podReqName]; !ok {
|
if value, ok := reqs[podReqName]; !ok {
|
||||||
reqs[podReqName] = podReqValue
|
reqs[podReqName] = *podReqValue.Copy()
|
||||||
} else if err = value.Add(podReqValue); err != nil {
|
} else if err = value.Add(podReqValue); err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for podLimitName, podLimitValue := range podLimits {
|
||||||
|
if value, ok := limits[podLimitName]; !ok {
|
||||||
|
limits[podLimitName] = *podLimitValue.Copy()
|
||||||
|
} else if err = value.Add(podLimitValue); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return reqs, nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSinglePodTotalRequests(pod *api.Pod) (map[api.ResourceName]resource.Quantity, error) {
|
func getSinglePodTotalRequestsAndLimits(pod *api.Pod) (reqs map[api.ResourceName]resource.Quantity, limits map[api.ResourceName]resource.Quantity, err error) {
|
||||||
reqs := map[api.ResourceName]resource.Quantity{}
|
reqs, limits = map[api.ResourceName]resource.Quantity{}, map[api.ResourceName]resource.Quantity{}
|
||||||
for _, container := range pod.Spec.Containers {
|
for _, container := range pod.Spec.Containers {
|
||||||
for name, quantity := range container.Resources.Requests {
|
for name, quantity := range container.Resources.Requests {
|
||||||
if value, ok := reqs[name]; !ok {
|
if value, ok := reqs[name]; !ok {
|
||||||
reqs[name] = quantity
|
reqs[name] = *quantity.Copy()
|
||||||
} else if err := value.Add(quantity); err != nil {
|
} else if err = value.Add(quantity); err != nil {
|
||||||
return nil, err
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for name, quantity := range container.Resources.Limits {
|
||||||
|
if value, ok := limits[name]; !ok {
|
||||||
|
limits[name] = *quantity.Copy()
|
||||||
|
} else if err = value.Add(quantity); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return reqs, nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func DescribeEvents(el *api.EventList, w io.Writer) {
|
func DescribeEvents(el *api.EventList, w io.Writer) {
|
||||||
|
@ -339,8 +339,8 @@ func TestDefaultDescribers(t *testing.T) {
|
|||||||
|
|
||||||
func TestGetPodsTotalRequests(t *testing.T) {
|
func TestGetPodsTotalRequests(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
pods []*api.Pod
|
pods []*api.Pod
|
||||||
expectedReqs map[api.ResourceName]resource.Quantity
|
expectedReqs, expectedLimits map[api.ResourceName]resource.Quantity
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
pods: []*api.Pod{
|
pods: []*api.Pod{
|
||||||
@ -402,7 +402,7 @@ func TestGetPodsTotalRequests(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, testCase := range testCases {
|
for _, testCase := range testCases {
|
||||||
reqs, err := getPodsTotalRequests(testCase.pods)
|
reqs, _, err := getPodsTotalRequestsAndLimits(testCase.pods)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error %v", err)
|
t.Errorf("Unexpected error %v", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user