From 2c0fad1f527dc28bd2ab118499f7be5e62c3b0e2 Mon Sep 17 00:00:00 2001 From: Eric Ernst Date: Thu, 9 Sep 2021 12:14:17 -0700 Subject: [PATCH] kuberuntime: populate sandbox resources, overhead Populate Resources and Overhead fields which, are now part of LinuxPodSandboxConfig. Signed-off-by: Eric Ernst --- .../kuberuntime/kuberuntime_sandbox.go | 4 + .../kuberuntime/kuberuntime_sandbox_linux.go | 58 ++++++++ .../kuberuntime_sandbox_linux_test.go | 129 ++++++++++++++++++ .../kuberuntime_sandbox_unsupported.go | 29 ++++ .../kuberuntime_sandbox_windows.go | 29 ++++ 5 files changed, 249 insertions(+) create mode 100644 pkg/kubelet/kuberuntime/kuberuntime_sandbox_linux.go create mode 100644 pkg/kubelet/kuberuntime/kuberuntime_sandbox_linux_test.go create mode 100644 pkg/kubelet/kuberuntime/kuberuntime_sandbox_unsupported.go create mode 100644 pkg/kubelet/kuberuntime/kuberuntime_sandbox_windows.go diff --git a/pkg/kubelet/kuberuntime/kuberuntime_sandbox.go b/pkg/kubelet/kuberuntime/kuberuntime_sandbox.go index f5ada096d16..dbdeb9450dd 100644 --- a/pkg/kubelet/kuberuntime/kuberuntime_sandbox.go +++ b/pkg/kubelet/kuberuntime/kuberuntime_sandbox.go @@ -148,6 +148,10 @@ func (m *kubeGenericRuntimeManager) generatePodSandboxConfig(pod *v1.Pod, attemp podSandboxConfig.Windows = wc } + // Update config to include overhead, sandbox level resources + if err := m.applySandboxResources(pod, podSandboxConfig); err != nil { + return nil, err + } return podSandboxConfig, nil } diff --git a/pkg/kubelet/kuberuntime/kuberuntime_sandbox_linux.go b/pkg/kubelet/kuberuntime/kuberuntime_sandbox_linux.go new file mode 100644 index 00000000000..77dc010acd3 --- /dev/null +++ b/pkg/kubelet/kuberuntime/kuberuntime_sandbox_linux.go @@ -0,0 +1,58 @@ +//go:build linux +// +build linux + +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package kuberuntime + +import ( + v1 "k8s.io/api/core/v1" + utilfeature "k8s.io/apiserver/pkg/util/feature" + runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" + resourcehelper "k8s.io/kubernetes/pkg/api/v1/resource" + "k8s.io/kubernetes/pkg/features" +) + +func (m *kubeGenericRuntimeManager) convertOverheadToLinuxResources(pod *v1.Pod) *runtimeapi.LinuxContainerResources { + resources := &runtimeapi.LinuxContainerResources{} + if pod.Spec.Overhead != nil && utilfeature.DefaultFeatureGate.Enabled(features.PodOverhead) { + cpu := pod.Spec.Overhead.Cpu() + memory := pod.Spec.Overhead.Memory() + + // For overhead, we do not differentiate between requests and limits. Treat this overhead + // as "guaranteed", with requests == limits + resources = m.calculateLinuxResources(cpu, cpu, memory) + } + + return resources +} + +func (m *kubeGenericRuntimeManager) calculateSandboxResources(pod *v1.Pod) *runtimeapi.LinuxContainerResources { + req, lim := resourcehelper.PodRequestsAndLimitsWithoutOverhead(pod) + return m.calculateLinuxResources(req.Cpu(), lim.Cpu(), lim.Memory()) +} + +func (m *kubeGenericRuntimeManager) applySandboxResources(pod *v1.Pod, config *runtimeapi.PodSandboxConfig) error { + + if config.Linux == nil { + return nil + } + config.Linux.Resources = m.calculateSandboxResources(pod) + config.Linux.Overhead = m.convertOverheadToLinuxResources(pod) + + return nil +} diff --git a/pkg/kubelet/kuberuntime/kuberuntime_sandbox_linux_test.go b/pkg/kubelet/kuberuntime/kuberuntime_sandbox_linux_test.go new file mode 100644 index 00000000000..19e4b0efd56 --- /dev/null +++ b/pkg/kubelet/kuberuntime/kuberuntime_sandbox_linux_test.go @@ -0,0 +1,129 @@ +//go:build linux +// +build linux + +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package kuberuntime + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + v1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/resource" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" +) + +func TestApplySandboxResources(t *testing.T) { + _, _, m, err := createTestRuntimeManager() + m.cpuCFSQuota = true + + config := &runtimeapi.PodSandboxConfig{ + Linux: &runtimeapi.LinuxPodSandboxConfig{}, + } + + require.NoError(t, err) + + tests := []struct { + description string + pod *v1.Pod + expectedResource *runtimeapi.LinuxContainerResources + expectedOverhead *runtimeapi.LinuxContainerResources + }{ + { + description: "pod with overhead defined", + pod: &v1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + UID: "12345678", + Name: "bar", + Namespace: "new", + }, + Spec: v1.PodSpec{ + Containers: []v1.Container{ + { + Resources: v1.ResourceRequirements{ + Requests: v1.ResourceList{ + v1.ResourceMemory: resource.MustParse("128Mi"), + v1.ResourceCPU: resource.MustParse("2"), + }, + Limits: v1.ResourceList{ + v1.ResourceMemory: resource.MustParse("256Mi"), + v1.ResourceCPU: resource.MustParse("4"), + }, + }, + }, + }, + Overhead: v1.ResourceList{ + v1.ResourceMemory: resource.MustParse("128Mi"), + v1.ResourceCPU: resource.MustParse("1"), + }, + }, + }, + expectedResource: &runtimeapi.LinuxContainerResources{ + MemoryLimitInBytes: 268435456, + CpuPeriod: 100000, + CpuQuota: 400000, + CpuShares: 2048, + }, + expectedOverhead: &runtimeapi.LinuxContainerResources{ + MemoryLimitInBytes: 134217728, + CpuPeriod: 100000, + CpuQuota: 100000, + CpuShares: 1024, + }, + }, + { + description: "pod without overhead defined", + pod: &v1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + UID: "12345678", + Name: "bar", + Namespace: "new", + }, + Spec: v1.PodSpec{ + Containers: []v1.Container{ + { + Resources: v1.ResourceRequirements{ + Requests: v1.ResourceList{ + v1.ResourceMemory: resource.MustParse("128Mi"), + }, + Limits: v1.ResourceList{ + v1.ResourceMemory: resource.MustParse("256Mi"), + }, + }, + }, + }, + }, + }, + expectedResource: &runtimeapi.LinuxContainerResources{ + MemoryLimitInBytes: 268435456, + CpuPeriod: 100000, + CpuQuota: 0, + CpuShares: 2, + }, + expectedOverhead: &runtimeapi.LinuxContainerResources{}, + }, + } + + for i, test := range tests { + m.applySandboxResources(test.pod, config) + assert.Equal(t, test.expectedResource, config.Linux.Resources, "TestCase[%d]: %s", i, test.description) + assert.Equal(t, test.expectedOverhead, config.Linux.Overhead, "TestCase[%d]: %s", i, test.description) + } +} diff --git a/pkg/kubelet/kuberuntime/kuberuntime_sandbox_unsupported.go b/pkg/kubelet/kuberuntime/kuberuntime_sandbox_unsupported.go new file mode 100644 index 00000000000..005f15f976a --- /dev/null +++ b/pkg/kubelet/kuberuntime/kuberuntime_sandbox_unsupported.go @@ -0,0 +1,29 @@ +//go:build !linux && !windows +// +build !linux,!windows + +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package kuberuntime + +import ( + v1 "k8s.io/api/core/v1" + runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" +) + +func (m *kubeGenericRuntimeManager) applySandboxResources(pod *v1.Pod, config *runtimeapi.PodSandboxConfig) error { + return nil +} diff --git a/pkg/kubelet/kuberuntime/kuberuntime_sandbox_windows.go b/pkg/kubelet/kuberuntime/kuberuntime_sandbox_windows.go new file mode 100644 index 00000000000..4a9a0af9594 --- /dev/null +++ b/pkg/kubelet/kuberuntime/kuberuntime_sandbox_windows.go @@ -0,0 +1,29 @@ +//go:build windows +// +build windows + +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package kuberuntime + +import ( + v1 "k8s.io/api/core/v1" + runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" +) + +func (m *kubeGenericRuntimeManager) applySandboxResources(pod *v1.Pod, config *runtimeapi.PodSandboxConfig) error { + return nil +}