From efca28f8a5f261850bb356d902d5753af2e35d1f Mon Sep 17 00:00:00 2001 From: yanxuean Date: Fri, 17 Aug 2018 17:03:30 +0800 Subject: [PATCH] remove deplicate code for PodRequestsAndLimits Signed-off-by: yanxuean --- pkg/api/pod/util_test.go | 3 +- pkg/api/resource/helpers.go | 71 ++++++++++++++++------------------ pkg/api/v1/resource/helpers.go | 71 ++++++++++++++++------------------ 3 files changed, 67 insertions(+), 78 deletions(-) diff --git a/pkg/api/pod/util_test.go b/pkg/api/pod/util_test.go index b29f822013..e27ac9c4e7 100644 --- a/pkg/api/pod/util_test.go +++ b/pkg/api/pod/util_test.go @@ -18,9 +18,8 @@ package pod import ( "reflect" - "testing" - "strings" + "testing" "k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/validation/field" diff --git a/pkg/api/resource/helpers.go b/pkg/api/resource/helpers.go index 6947799e87..4daf3e9254 100644 --- a/pkg/api/resource/helpers.go +++ b/pkg/api/resource/helpers.go @@ -25,50 +25,45 @@ import ( api "k8s.io/kubernetes/pkg/apis/core" ) -// PodRequestsAndLimits returns a dictionary of all defined resources summed up for all -// containers of the pod. -func PodRequestsAndLimits(pod *api.Pod) (reqs map[api.ResourceName]resource.Quantity, limits map[api.ResourceName]resource.Quantity) { - 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 { - value.Add(quantity) - reqs[name] = value - } +// addResourceList adds the resources in newList to list +func addResourceList(list, new api.ResourceList) { + for name, quantity := range new { + if value, ok := list[name]; !ok { + list[name] = *quantity.Copy() + } else { + value.Add(quantity) + list[name] = value } - for name, quantity := range container.Resources.Limits { - if value, ok := limits[name]; !ok { - limits[name] = *quantity.Copy() - } else { - value.Add(quantity) - limits[name] = value + } +} + +// maxResourceList sets list to the greater of list/newList for every resource +// either list +func maxResourceList(list, new api.ResourceList) { + for name, quantity := range new { + if value, ok := list[name]; !ok { + list[name] = *quantity.Copy() + continue + } else { + if quantity.Cmp(value) > 0 { + list[name] = *quantity.Copy() } } } +} + +// PodRequestsAndLimits returns a dictionary of all defined resources summed up for all +// containers of the pod. +func PodRequestsAndLimits(pod *api.Pod) (reqs api.ResourceList, limits api.ResourceList) { + reqs, limits = api.ResourceList{}, api.ResourceList{} + for _, container := range pod.Spec.Containers { + addResourceList(reqs, container.Resources.Requests) + addResourceList(limits, container.Resources.Limits) + } // init containers define the minimum of any resource for _, container := range pod.Spec.InitContainers { - for name, quantity := range container.Resources.Requests { - value, ok := reqs[name] - if !ok { - reqs[name] = *quantity.Copy() - continue - } - if quantity.Cmp(value) > 0 { - reqs[name] = *quantity.Copy() - } - } - for name, quantity := range container.Resources.Limits { - value, ok := limits[name] - if !ok { - limits[name] = *quantity.Copy() - continue - } - if quantity.Cmp(value) > 0 { - limits[name] = *quantity.Copy() - } - } + maxResourceList(reqs, container.Resources.Requests) + maxResourceList(limits, container.Resources.Limits) } return } diff --git a/pkg/api/v1/resource/helpers.go b/pkg/api/v1/resource/helpers.go index 09d9ea0418..6f7c59f587 100644 --- a/pkg/api/v1/resource/helpers.go +++ b/pkg/api/v1/resource/helpers.go @@ -25,50 +25,45 @@ import ( "k8s.io/apimachinery/pkg/api/resource" ) -// PodRequestsAndLimits returns a dictionary of all defined resources summed up for all -// containers of the pod. -func PodRequestsAndLimits(pod *v1.Pod) (reqs map[v1.ResourceName]resource.Quantity, limits map[v1.ResourceName]resource.Quantity) { - reqs, limits = map[v1.ResourceName]resource.Quantity{}, map[v1.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 { - value.Add(quantity) - reqs[name] = value - } +// addResourceList adds the resources in newList to list +func addResourceList(list, new v1.ResourceList) { + for name, quantity := range new { + if value, ok := list[name]; !ok { + list[name] = *quantity.Copy() + } else { + value.Add(quantity) + list[name] = value } - for name, quantity := range container.Resources.Limits { - if value, ok := limits[name]; !ok { - limits[name] = *quantity.Copy() - } else { - value.Add(quantity) - limits[name] = value + } +} + +// maxResourceList sets list to the greater of list/newList for every resource +// either list +func maxResourceList(list, new v1.ResourceList) { + for name, quantity := range new { + if value, ok := list[name]; !ok { + list[name] = *quantity.Copy() + continue + } else { + if quantity.Cmp(value) > 0 { + list[name] = *quantity.Copy() } } } +} + +// PodRequestsAndLimits returns a dictionary of all defined resources summed up for all +// containers of the pod. +func PodRequestsAndLimits(pod *v1.Pod) (reqs, limits v1.ResourceList) { + reqs, limits = v1.ResourceList{}, v1.ResourceList{} + for _, container := range pod.Spec.Containers { + addResourceList(reqs, container.Resources.Requests) + addResourceList(limits, container.Resources.Limits) + } // init containers define the minimum of any resource for _, container := range pod.Spec.InitContainers { - for name, quantity := range container.Resources.Requests { - value, ok := reqs[name] - if !ok { - reqs[name] = *quantity.Copy() - continue - } - if quantity.Cmp(value) > 0 { - reqs[name] = *quantity.Copy() - } - } - for name, quantity := range container.Resources.Limits { - value, ok := limits[name] - if !ok { - limits[name] = *quantity.Copy() - continue - } - if quantity.Cmp(value) > 0 { - limits[name] = *quantity.Copy() - } - } + maxResourceList(reqs, container.Resources.Requests) + maxResourceList(limits, container.Resources.Limits) } return }