From 1a43dcf720dd983652afeddbfa6d1b82a1f215d1 Mon Sep 17 00:00:00 2001 From: Sergiusz Urbaniak Date: Fri, 13 Nov 2015 15:21:53 +0100 Subject: [PATCH] api, kubectl: move getSinglePodTotalRequestsAndLimits to api.PodRequestsAndLimits --- pkg/api/resource_helpers.go | 23 +++++++++++++++++++++++ pkg/kubectl/describe.go | 25 ++----------------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/pkg/api/resource_helpers.go b/pkg/api/resource_helpers.go index 257cb36429b..3715a90c248 100644 --- a/pkg/api/resource_helpers.go +++ b/pkg/api/resource_helpers.go @@ -97,3 +97,26 @@ func IsNodeReady(node *Node) bool { } return false } + +// PodRequestsAndLimits returns a dictionary of all defined resources summed up for all +// containers of the pod. +func PodRequestsAndLimits(pod *Pod) (reqs map[ResourceName]resource.Quantity, limits map[ResourceName]resource.Quantity, err error) { + reqs, limits = map[ResourceName]resource.Quantity{}, map[ResourceName]resource.Quantity{} + for _, container := range pod.Spec.Containers { + for name, quantity := range container.Resources.Requests { + if value, ok := reqs[name]; !ok { + reqs[name] = *quantity.Copy() + } else if err = value.Add(quantity); err != nil { + 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 +} \ No newline at end of file diff --git a/pkg/kubectl/describe.go b/pkg/kubectl/describe.go index c5679cb66d3..bcb44f05531 100644 --- a/pkg/kubectl/describe.go +++ b/pkg/kubectl/describe.go @@ -1415,7 +1415,7 @@ func describeNodeResource(pods []*api.Pod, node *api.Node, out io.Writer) error 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") for _, pod := range nonTerminatedPods { - req, limit, err := getSinglePodTotalRequestsAndLimits(pod) + req, limit, err := api.PodRequestsAndLimits(pod) if err != nil { return err } @@ -1463,7 +1463,7 @@ func filterTerminatedPods(pods []*api.Pod) []*api.Pod { func getPodsTotalRequestsAndLimits(pods []*api.Pod) (reqs map[api.ResourceName]resource.Quantity, limits map[api.ResourceName]resource.Quantity, err error) { reqs, limits = map[api.ResourceName]resource.Quantity{}, map[api.ResourceName]resource.Quantity{} for _, pod := range pods { - podReqs, podLimits, err := getSinglePodTotalRequestsAndLimits(pod) + podReqs, podLimits, err := api.PodRequestsAndLimits(pod) if err != nil { return nil, nil, err } @@ -1485,27 +1485,6 @@ func getPodsTotalRequestsAndLimits(pods []*api.Pod) (reqs map[api.ResourceName]r return } -func getSinglePodTotalRequestsAndLimits(pod *api.Pod) (reqs map[api.ResourceName]resource.Quantity, limits map[api.ResourceName]resource.Quantity, err error) { - reqs, limits = map[api.ResourceName]resource.Quantity{}, map[api.ResourceName]resource.Quantity{} - for _, container := range pod.Spec.Containers { - for name, quantity := range container.Resources.Requests { - if value, ok := reqs[name]; !ok { - reqs[name] = *quantity.Copy() - } else if err = value.Add(quantity); err != nil { - 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 -} - func DescribeEvents(el *api.EventList, w io.Writer) { if len(el.Items) == 0 { fmt.Fprint(w, "No events.")