remove deplicate code for PodRequestsAndLimits

Signed-off-by: yanxuean <yan.xuean@zte.com.cn>
pull/8/head
yanxuean 2018-08-17 17:03:30 +08:00
parent 330cf37e0a
commit efca28f8a5
3 changed files with 67 additions and 78 deletions

View File

@ -18,9 +18,8 @@ package pod
import ( import (
"reflect" "reflect"
"testing"
"strings" "strings"
"testing"
"k8s.io/apimachinery/pkg/util/sets" "k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/validation/field" "k8s.io/apimachinery/pkg/util/validation/field"

View File

@ -25,50 +25,45 @@ import (
api "k8s.io/kubernetes/pkg/apis/core" api "k8s.io/kubernetes/pkg/apis/core"
) )
// PodRequestsAndLimits returns a dictionary of all defined resources summed up for all // addResourceList adds the resources in newList to list
// containers of the pod. func addResourceList(list, new api.ResourceList) {
func PodRequestsAndLimits(pod *api.Pod) (reqs map[api.ResourceName]resource.Quantity, limits map[api.ResourceName]resource.Quantity) { for name, quantity := range new {
reqs, limits = map[api.ResourceName]resource.Quantity{}, map[api.ResourceName]resource.Quantity{} if value, ok := list[name]; !ok {
for _, container := range pod.Spec.Containers { list[name] = *quantity.Copy()
for name, quantity := range container.Resources.Requests { } else {
if value, ok := reqs[name]; !ok { value.Add(quantity)
reqs[name] = *quantity.Copy() list[name] = value
} else {
value.Add(quantity)
reqs[name] = value
}
} }
for name, quantity := range container.Resources.Limits { }
if value, ok := limits[name]; !ok { }
limits[name] = *quantity.Copy()
} else { // maxResourceList sets list to the greater of list/newList for every resource
value.Add(quantity) // either list
limits[name] = value 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 // init containers define the minimum of any resource
for _, container := range pod.Spec.InitContainers { for _, container := range pod.Spec.InitContainers {
for name, quantity := range container.Resources.Requests { maxResourceList(reqs, container.Resources.Requests)
value, ok := reqs[name] maxResourceList(limits, container.Resources.Limits)
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()
}
}
} }
return return
} }

View File

@ -25,50 +25,45 @@ import (
"k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/api/resource"
) )
// PodRequestsAndLimits returns a dictionary of all defined resources summed up for all // addResourceList adds the resources in newList to list
// containers of the pod. func addResourceList(list, new v1.ResourceList) {
func PodRequestsAndLimits(pod *v1.Pod) (reqs map[v1.ResourceName]resource.Quantity, limits map[v1.ResourceName]resource.Quantity) { for name, quantity := range new {
reqs, limits = map[v1.ResourceName]resource.Quantity{}, map[v1.ResourceName]resource.Quantity{} if value, ok := list[name]; !ok {
for _, container := range pod.Spec.Containers { list[name] = *quantity.Copy()
for name, quantity := range container.Resources.Requests { } else {
if value, ok := reqs[name]; !ok { value.Add(quantity)
reqs[name] = *quantity.Copy() list[name] = value
} else {
value.Add(quantity)
reqs[name] = value
}
} }
for name, quantity := range container.Resources.Limits { }
if value, ok := limits[name]; !ok { }
limits[name] = *quantity.Copy()
} else { // maxResourceList sets list to the greater of list/newList for every resource
value.Add(quantity) // either list
limits[name] = value 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 // init containers define the minimum of any resource
for _, container := range pod.Spec.InitContainers { for _, container := range pod.Spec.InitContainers {
for name, quantity := range container.Resources.Requests { maxResourceList(reqs, container.Resources.Requests)
value, ok := reqs[name] maxResourceList(limits, container.Resources.Limits)
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()
}
}
} }
return return
} }