Skip to content

Commit 8cf05f5

Browse files
authored
Merge pull request kubernetes#79247 from egernst/kubelet-PodOverhead
Kubelet enabling to support pod-overhead
2 parents f17b608 + 80ee072 commit 8cf05f5

File tree

4 files changed

+239
-105
lines changed

4 files changed

+239
-105
lines changed

pkg/api/v1/resource/BUILD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ go_test(
1111
srcs = ["helpers_test.go"],
1212
embed = [":go_default_library"],
1313
deps = [
14+
"//pkg/features:go_default_library",
1415
"//staging/src/k8s.io/api/core/v1:go_default_library",
16+
"//staging/src/k8s.io/apimachinery/pkg/api/equality:go_default_library",
1517
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
18+
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
19+
"//staging/src/k8s.io/component-base/featuregate/testing:go_default_library",
1620
"//vendor/github.com/stretchr/testify/assert:go_default_library",
1721
],
1822
)
@@ -22,8 +26,10 @@ go_library(
2226
srcs = ["helpers.go"],
2327
importpath = "k8s.io/kubernetes/pkg/api/v1/resource",
2428
deps = [
29+
"//pkg/features:go_default_library",
2530
"//staging/src/k8s.io/api/core/v1:go_default_library",
2631
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
32+
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
2733
],
2834
)
2935

pkg/api/v1/resource/helpers.go

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,13 @@ import (
2323

2424
"k8s.io/api/core/v1"
2525
"k8s.io/apimachinery/pkg/api/resource"
26+
utilfeature "k8s.io/apiserver/pkg/util/feature"
27+
"k8s.io/kubernetes/pkg/features"
2628
)
2729

2830
// addResourceList adds the resources in newList to list
29-
func addResourceList(list, new v1.ResourceList) {
30-
for name, quantity := range new {
31+
func addResourceList(list, newList v1.ResourceList) {
32+
for name, quantity := range newList {
3133
if value, ok := list[name]; !ok {
3234
list[name] = *quantity.Copy()
3335
} else {
@@ -53,7 +55,9 @@ func maxResourceList(list, new v1.ResourceList) {
5355
}
5456

5557
// PodRequestsAndLimits returns a dictionary of all defined resources summed up for all
56-
// containers of the pod.
58+
// containers of the pod. If PodOverhead feature is enabled, pod overhead is added to the
59+
// total container resource requests and to the total container limits which have a
60+
// non-zero quantity.
5761
func PodRequestsAndLimits(pod *v1.Pod) (reqs, limits v1.ResourceList) {
5862
reqs, limits = v1.ResourceList{}, v1.ResourceList{}
5963
for _, container := range pod.Spec.Containers {
@@ -65,37 +69,79 @@ func PodRequestsAndLimits(pod *v1.Pod) (reqs, limits v1.ResourceList) {
6569
maxResourceList(reqs, container.Resources.Requests)
6670
maxResourceList(limits, container.Resources.Limits)
6771
}
72+
73+
// if PodOverhead feature is supported, add overhead for running a pod
74+
// to the sum of reqeuests and to non-zero limits:
75+
if pod.Spec.Overhead != nil && utilfeature.DefaultFeatureGate.Enabled(features.PodOverhead) {
76+
addResourceList(reqs, pod.Spec.Overhead)
77+
78+
for name, quantity := range pod.Spec.Overhead {
79+
if value, ok := limits[name]; ok && !value.IsZero() {
80+
value.Add(quantity)
81+
limits[name] = value
82+
}
83+
}
84+
}
85+
6886
return
6987
}
7088

71-
// GetResourceRequest finds and returns the request for a specific resource.
72-
func GetResourceRequest(pod *v1.Pod, resource v1.ResourceName) int64 {
73-
if resource == v1.ResourcePods {
74-
return 1
89+
// GetResourceRequestQuantity finds and returns the request quantity for a specific resource.
90+
func GetResourceRequestQuantity(pod *v1.Pod, resourceName v1.ResourceName) resource.Quantity {
91+
requestQuantity := resource.Quantity{}
92+
93+
switch resourceName {
94+
case v1.ResourceCPU:
95+
requestQuantity = resource.Quantity{Format: resource.DecimalSI}
96+
case v1.ResourceMemory, v1.ResourceStorage, v1.ResourceEphemeralStorage:
97+
requestQuantity = resource.Quantity{Format: resource.BinarySI}
98+
default:
99+
requestQuantity = resource.Quantity{Format: resource.DecimalSI}
100+
}
101+
102+
if resourceName == v1.ResourceEphemeralStorage && !utilfeature.DefaultFeatureGate.Enabled(features.LocalStorageCapacityIsolation) {
103+
// if the local storage capacity isolation feature gate is disabled, pods request 0 disk
104+
return requestQuantity
75105
}
76-
totalResources := int64(0)
106+
77107
for _, container := range pod.Spec.Containers {
78-
if rQuantity, ok := container.Resources.Requests[resource]; ok {
79-
if resource == v1.ResourceCPU {
80-
totalResources += rQuantity.MilliValue()
81-
} else {
82-
totalResources += rQuantity.Value()
83-
}
108+
if rQuantity, ok := container.Resources.Requests[resourceName]; ok {
109+
requestQuantity.Add(rQuantity)
84110
}
85111
}
86-
// take max_resource(sum_pod, any_init_container)
112+
87113
for _, container := range pod.Spec.InitContainers {
88-
if rQuantity, ok := container.Resources.Requests[resource]; ok {
89-
if resource == v1.ResourceCPU {
90-
if rQuantity.MilliValue() > totalResources {
91-
totalResources = rQuantity.MilliValue()
92-
}
93-
} else if rQuantity.Value() > totalResources {
94-
totalResources = rQuantity.Value()
114+
if rQuantity, ok := container.Resources.Requests[resourceName]; ok {
115+
if requestQuantity.Cmp(rQuantity) < 0 {
116+
requestQuantity = rQuantity.DeepCopy()
95117
}
96118
}
97119
}
98-
return totalResources
120+
121+
// if PodOverhead feature is supported, add overhead for running a pod
122+
// to the total requests if the resource total is non-zero
123+
if pod.Spec.Overhead != nil && utilfeature.DefaultFeatureGate.Enabled(features.PodOverhead) {
124+
if podOverhead, ok := pod.Spec.Overhead[resourceName]; ok && !requestQuantity.IsZero() {
125+
requestQuantity.Add(podOverhead)
126+
}
127+
}
128+
129+
return requestQuantity
130+
}
131+
132+
// GetResourceRequest finds and returns the request value for a specific resource.
133+
func GetResourceRequest(pod *v1.Pod, resource v1.ResourceName) int64 {
134+
if resource == v1.ResourcePods {
135+
return 1
136+
}
137+
138+
requestQuantity := GetResourceRequestQuantity(pod, resource)
139+
140+
if resource == v1.ResourceCPU {
141+
return requestQuantity.MilliValue()
142+
}
143+
144+
return requestQuantity.Value()
99145
}
100146

101147
// ExtractResourceValueByContainerName extracts the value of a resource

0 commit comments

Comments
 (0)