Added sidecar support in ValidatePodResize and dropNonResizeUpdates

This commit is contained in:
vivzbansal
2024-11-06 22:31:19 +00:00
parent cdddaed841
commit 3885d2f8ab
4 changed files with 31 additions and 115 deletions

View File

@@ -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
}