diff --git a/pkg/apis/core/validation/validation.go b/pkg/apis/core/validation/validation.go index 2b18e8023a4..5330c217837 100644 --- a/pkg/apis/core/validation/validation.go +++ b/pkg/apis/core/validation/validation.go @@ -5654,13 +5654,15 @@ func ValidatePodResize(newPod, oldPod *core.Pod, opts PodValidationOptions) fiel // Ensure that only CPU and memory resources are mutable for restartable init containers. var newInitContainers []core.Container - for ix, container := range originalCPUMemPodSpec.InitContainers { - if container.RestartPolicy != nil && *container.RestartPolicy == core.ContainerRestartPolicyAlways { - lim := dropCPUMemoryUpdates(container.Resources.Limits, oldPod.Spec.InitContainers[ix].Resources.Limits) - req := dropCPUMemoryUpdates(container.Resources.Requests, oldPod.Spec.InitContainers[ix].Resources.Requests) - container.Resources = core.ResourceRequirements{Limits: lim, Requests: req} - container.ResizePolicy = oldPod.Spec.InitContainers[ix].ResizePolicy // +k8s:verify-mutation:reason=clone - newInitContainers = append(newInitContainers, container) + if utilfeature.DefaultFeatureGate.Enabled(features.SidecarContainers) { + for ix, container := range originalCPUMemPodSpec.InitContainers { + if container.RestartPolicy != nil && *container.RestartPolicy == core.ContainerRestartPolicyAlways { // restartable init container + lim := dropCPUMemoryUpdates(container.Resources.Limits, oldPod.Spec.InitContainers[ix].Resources.Limits) + req := dropCPUMemoryUpdates(container.Resources.Requests, oldPod.Spec.InitContainers[ix].Resources.Requests) + container.Resources = core.ResourceRequirements{Limits: lim, Requests: req} + container.ResizePolicy = oldPod.Spec.InitContainers[ix].ResizePolicy // +k8s:verify-mutation:reason=clone + newInitContainers = append(newInitContainers, container) + } } } if len(newInitContainers) > 0 { diff --git a/pkg/registry/core/pod/strategy.go b/pkg/registry/core/pod/strategy.go index fd239f6970e..914cbfbec0a 100644 --- a/pkg/registry/core/pod/strategy.go +++ b/pkg/registry/core/pod/strategy.go @@ -289,18 +289,21 @@ var ResizeStrategy = podResizeStrategy{ ), } -// dropNonResizeUpdates discards all changes except for pod.Spec.Containers[*].Resources,ResizePolicy and certain metadata +// dropNonResizeUpdates discards all changes except for pod.Spec.Containers[*].Resources, pod.Spec.InitContainers[*].Resources, ResizePolicy, and certain metadata func dropNonResizeUpdates(newPod, oldPod *api.Pod) *api.Pod { pod := dropPodUpdates(newPod, oldPod) // Containers are not allowed to be re-ordered, but in case they were, // we don't want to corrupt them here. It will get caught in validation. oldCtrToIndex := make(map[string]int) + oldInitCtrToIndex := make(map[string]int) for idx, ctr := range pod.Spec.Containers { oldCtrToIndex[ctr.Name] = idx } - // TODO: Once we add in-place pod resize support for sidecars, we need to allow - // modifying sidecar resources via resize subresource too. + for idx, ctr := range pod.Spec.InitContainers { + oldInitCtrToIndex[ctr.Name] = idx + } + for _, ctr := range newPod.Spec.Containers { idx, ok := oldCtrToIndex[ctr.Name] if !ok { @@ -309,6 +312,21 @@ func dropNonResizeUpdates(newPod, oldPod *api.Pod) *api.Pod { pod.Spec.Containers[idx].Resources = ctr.Resources pod.Spec.Containers[idx].ResizePolicy = ctr.ResizePolicy } + + if utilfeature.DefaultFeatureGate.Enabled(features.SidecarContainers) { + for _, ctr := range newPod.Spec.InitContainers { + idx, ok := oldInitCtrToIndex[ctr.Name] + if !ok { + continue + } + // Changes are only allowed for restartable init containers. + if podutil.IsRestartableInitContainer(&ctr) { + pod.Spec.InitContainers[idx].Resources = ctr.Resources + pod.Spec.InitContainers[idx].ResizePolicy = ctr.ResizePolicy + } + } + } + return pod } diff --git a/staging/src/k8s.io/component-helpers/resource/helpers_test.go b/staging/src/k8s.io/component-helpers/resource/helpers_test.go index 5381322558b..60d697c22f1 100644 --- a/staging/src/k8s.io/component-helpers/resource/helpers_test.go +++ b/staging/src/k8s.io/component-helpers/resource/helpers_test.go @@ -456,52 +456,6 @@ func TestPodResourceRequests(t *testing.T) { }, }, }, - { - description: "resized with sidecar containers, infeasible", - hasSidecarContainer: true, - expectedRequests: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("5"), - }, - podResizeStatus: v1.PodResizeStatusInfeasible, - options: PodResourcesOptions{UseStatusResources: true}, - containers: []v1.Container{ - { - Name: "container-1", - Resources: v1.ResourceRequirements{ - Requests: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("4"), - }, - }, - }, - }, - initContainers: []v1.Container{ - { - Name: "restartable-init-1", - RestartPolicy: &restartAlways, - Resources: v1.ResourceRequirements{ - Requests: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("10"), - }, - }, - }, - }, - containerStatus: []v1.ContainerStatus{ - { - Name: "container-1", - AllocatedResources: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("2"), - }, - }, - }, - initContainerStatus: []v1.ContainerStatus{ - { - Name: "restartable-init-1", - AllocatedResources: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("5"), - }, - }, - }, - }, { description: "resized with no sidecar containers, no resize status", expectedRequests: v1.ResourceList{ @@ -537,53 +491,6 @@ func TestPodResourceRequests(t *testing.T) { }, }, }, - { - description: "resized with sidecar containers, no resize status", - hasSidecarContainer: true, - expectedRequests: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("3"), - }, - options: PodResourcesOptions{UseStatusResources: true}, - /* containers: []v1.Container{ - { - Name: "container-1", - Resources: v1.ResourceRequirements{ - Requests: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("4"), - }, - }, - }, - }, */ - initContainers: []v1.Container{ - { - Name: "restartable-init-1", - RestartPolicy: &restartAlways, - Resources: v1.ResourceRequirements{ - Requests: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("3"), - }, - }, - }, - }, - /* containerStatus: []v1.ContainerStatus{ - { - Name: "container-1", - Resources: &v1.ResourceRequirements{ - Requests: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("2"), - }, - }, - }, - }, */ - initContainerStatus: []v1.ContainerStatus{ - { - Name: "restartable-init-1", - AllocatedResources: v1.ResourceList{ - v1.ResourceCPU: resource.MustParse("2"), - }, - }, - }, - }, { description: "resized, infeasible, but don't use status", expectedRequests: v1.ResourceList{ diff --git a/test/e2e/framework/pod/resize.go b/test/e2e/framework/pod/resize.go index efda69887f2..9496f18ca88 100644 --- a/test/e2e/framework/pod/resize.go +++ b/test/e2e/framework/pod/resize.go @@ -162,15 +162,10 @@ func makeResizableContainer(tcInfo ResizableContainerInfo) v1.Container { func MakePodWithResizableContainers(ns, name, timeStamp string, tcInfo []ResizableContainerInfo) *v1.Pod { var testContainers []v1.Container - var testInitContainers []v1.Container for _, ci := range tcInfo { tc := makeResizableContainer(ci) - if ci.IsRestartableInitCtr { - testInitContainers = append(testInitContainers, tc) - } else { - testContainers = append(testContainers, tc) - } + testContainers = append(testContainers, tc) } pod := &v1.Pod{ ObjectMeta: metav1.ObjectMeta{ @@ -186,12 +181,6 @@ func MakePodWithResizableContainers(ns, name, timeStamp string, tcInfo []Resizab RestartPolicy: v1.RestartPolicyOnFailure, }, } - - if len(testInitContainers) > 0 { - pod.Spec.RestartPolicy = v1.RestartPolicyAlways - pod.Spec.InitContainers = testInitContainers - } - return pod }