8000 [PodLevelResources] Update Downward API defaulting for resource limits by toVersus · Pull Request #132605 · kubernetes/kubernetes · GitHub
[go: up one dir, main page]

Skip to content

[PodLevelResources] Update Downward API defaulting for resource limits #132605

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions pkg/kubelet/kubelet_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ import (

"k8s.io/klog/v2"

"k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
utilfeature "k8s.io/apiserver/pkg/util/feature"
resourcehelper "k8s.io/component-helpers/resource"
"k8s.io/kubernetes/pkg/api/v1/resource"
kubefeatures "k8s.io/kubernetes/pkg/features"
)

// defaultPodLimitsForDownwardAPI copies the input pod, and optional container,
// and applies default resource limits. it returns a copy of the input pod,
// and a copy of the input container (if specified) with default limits
// applied. if a container has no limit specified, it will default the limit to
// the node allocatable.
// TODO: if/when we have pod level resources, we need to update this function
// to use those limits instead of node allocatable.
func (kl *Kubelet) defaultPodLimitsForDownwardAPI(pod *v1.Pod, container *v1.Container) (*v1.Pod, *v1.Container, error) {
// applied.
// If a container has no limits specified, it defaults to the pod-level resources.
// If neither container-level nor pod-level resources are specified, it defaults
// to the node's allocatable resources.
func (kl *Kubelet) defaultPodLimitsForDownwardAPI(pod *corev1.Pod, container *corev1.Container) (*corev1.Pod, *corev1.Container, error) {
if pod == nil {
return nil, nil, fmt.Errorf("invalid input, pod cannot be nil")
}
Expand All @@ -42,13 +45,29 @@ func (kl *Kubelet) defaultPodLimitsForDownwardAPI(pod *v1.Pod, container *v1.Con
return nil, nil, fmt.Errorf("failed to find node object, expected a node")
}
allocatable := node.Status.Allocatable
if utilfeature.DefaultFeatureGate.Enabled(kubefeatures.PodLevelResources) && resourcehelper.IsPodLevelLimitsSet(pod) {
allocatable = allocatable.DeepCopy()
// Resources supported by the Downward API
for _, resource := range []corev1.ResourceName{corev1.ResourceCPU, corev1.ResourceMemory, corev1.ResourceEphemeralStorage} {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - only cpu, memory and huge pages are supported at pod-level.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This slice contains resources supported by the Downward API, so I added a check to verify whether each resource is also supported by Pod Level Resources.

// Skip resources not supported by Pod Level Resources
if !resourcehelper.IsSupportedPodLevelResource(resource) {
continue
}
if val, exists := pod.Spec.Resources.Limits[resource]; exists && !val.IsZero() {
if _, exists := allocatable[resource]; exists {
allocatable[resource] = val.DeepCopy()
}
}
}
}

klog.InfoS("Allocatable", "allocatable", allocatable)
outputPod := pod.DeepCopy()
for idx := range outputPod.Spec.Containers {
resource.MergeContainerResourceLimits(&outputPod.Spec.Containers[idx], allocatable)
}

var outputContainer *v1.Container
var outputContainer *corev1.Container
if container != nil {
outputContainer = container.DeepCopy()
resource.MergeContainerResourceLimits(outputContainer, allocatable)
Expand Down
50 changes: 48 additions & 2 deletions
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import (
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilfeature "k8s.io/apiserver/pkg/util/feature"
featuregatetesting "k8s.io/component-base/featuregate/testing"
kubefeatures "k8s.io/kubernetes/pkg/features"
)

func TestPodResourceLimitsDefaulting(t *testing.T) {
Expand All @@ -46,8 +49,9 @@ func TestPodResourceLimitsDefaulting(t *testing.T) {
},
}
cases := []struct {
pod *v1.Pod
expected *v1.Pod
pod *v1.Pod
expected *v1.Pod
podLevelResourcesEnabled bool
}{
{
pod: getPod("0", "0"),
Expand All @@ -65,9 +69,35 @@ func TestPodResourceLimitsDefaulting(t *testing.T) {
pod: getPod("0", "1Mi"),
expected: getPod("6", "1Mi"),
},
{
pod: getPodWithPodLevelResources("0", "1Mi", "0", "0"),
expected: getPodWithPodLevelResources("0", "1Mi", "6", "1Mi"),
podLevelResourcesEnabled: true,
},
{
pod: getPodWithPodLevelResources("1", "0", "0", "0"),
expected: getPodWithPodLevelResources("1", "0", "1", "4Gi"),
podLevelResourcesEnabled: true,
},
{
pod: getPodWithPodLevelResources("1", "1Mi", "", ""),
expected: getPodWithPodLevelResources("1", "1Mi", "1", "1Mi"),
podLevelResourcesEnabled: true,
},
{
pod: getPodWithPodLevelResources("1", "5Mi", "0", "1Mi"),
expected: getPodWithPodLevelResources("1", "5Mi", "1", "1Mi"),
podLevelResourcesEnabled: true,
},
{
pod: getPodWithPodLevelResources("1", "5Mi", "1", "1Mi"),
expected: getPodWithPodLevelResources("1", "5Mi", "1", "1Mi"),
podLevelResourcesEnabled: true,
},
}
as := assert.New(t)
for idx, tc := range cases {
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, kubefeatures.PodLevelResources, tc.podLevelResourcesEnabled)
actual, _, err := tk.kubelet.defaultPodLimitsForDownwardAPI(tc.pod, nil)
as.NoError(err, "failed to default pod limits: %v", err)
if !apiequality.Semantic.DeepEqual(tc.expected, actual) {
Expand Down Expand Up @@ -98,3 +128,19 @@ func getPod(cpuLimit, memoryLimit string) *v1.Pod {
},
}
}

func getPodWithPodLevelResources(plCPULimit, plMemoryLimit, clCPULimit, clMemoryLimit string) *v1.Pod {
pod := getPod(clCPULimit, clMemoryLimit)
resources := v1.ResourceRequirements{}
if plCPULimit != "" || plMemoryLimit != "" {
resources.Limits = make(v1.ResourceList)
}
if plCPULimit != "" {
resources.Limits[v1.ResourceCPU] = resource.MustParse(plCPULimit)
}
if plMemoryLimit != "" {
resources.Limits[v1.ResourceMemory] = resource.MustParse(plMemoryLimit)
}
pod.Spec.Resources = &resources
return pod
}
20 changes: 20 additions & 0 deletions staging/src/k8s.io/component-helpers/resource/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@ func IsPodLevelRequestsSet(pod *v1.Pod) bool {
return false
}

// IsPodLevelLimitsSet checks if pod-level limits are set. It returns true if
// Limits map is non-empty and contains at least one supported pod-level resource.
func IsPodLevelLimitsSet(pod *v1.Pod) bool {
if pod.Spec.Resources == nil {
return false
}

if len(pod.Spec.Resources.Limits) == 0 {
return false
}

for resourceName := range pod.Spec.Resources.Limits {
if IsSupportedPodLevelResource(resourceName) {
return true
}
}

return false
}

// PodRequests computes the total pod requests per the PodResourcesOptions supplied.
// If PodResourcesOptions is nil, then the requests are returned including pod overhead.
// If the PodLevelResources feature is enabled AND the pod-level resources are set,
Expand Down
52 changes: 52 additions & 0 deletions staging/src/k8s.io/component-helpers/resource/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,58 @@ func TestIsPodLevelResourcesSet(t *testing.T) {

}

func TestIsPodLevelLimitsSet(t *testing.T) {
testCases := []struct {
name string
podResources *v1.ResourceRequirements
expected bool
}{
{
name: "nil resources struct",
expected: false,
},
{
name: "empty resources struct",
podResources: &v1.ResourceRequirements{},
expected: false,
},
{
name: "only resource requests set",
podResources: &v1.ResourceRequirements{
Requests: v1.ResourceList{v1.ResourceMemory: resource.MustParse("100Mi")},
},
expected: false,
},
{
name: "only unsupported resource limits set",
podResources: &v1.ResourceRequirements{
Limits: v1.ResourceList{v1.ResourceEphemeralStorage: resource.MustParse("1Mi")},
},
expected: false,
},
{
name: "unsupported and suported resources limits set",
podResources: &v1.ResourceRequirements{
Limits: v1.ResourceList{
v1.ResourceEphemeralStorage: resource.MustParse("1Mi"),
v1.ResourceCPU: resource.MustParse("1m"),
},
},
expected: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
testPod := &v1.Pod{Spec: v1.PodSpec{Resources: tc.podResources}}
if got := IsPodLevelLimitsSet(testPod); got != tc.expected {
t.Errorf("got=%t, want=%t", got, tc.expected)
}
})
}

}

func TestPodLevelResourceRequests(t *testing.T) {
restartAlways := v1.ContainerRestartPolicyAlways
testCases := []struct {
Expand Down
Loading
0