@@ -5289,7 +5289,6 @@ func ValidatePodUpdate(newPod, oldPod *core.Pod, opts PodValidationOptions) fiel
5289
5289
for ix , container := range mungedPodSpec .Containers {
5290
5290
container .Image = oldPod .Spec .Containers [ix ].Image // +k8s:verify-mutation:reason=clone
5291
5291
newContainers = append (newContainers , container )
5292
-
5293
5292
}
5294
5293
mungedPodSpec .Containers = newContainers
5295
5294
// munge spec.initContainers[*].image
@@ -5644,10 +5643,7 @@ func ValidatePodResize(newPod, oldPod *core.Pod, opts PodValidationOptions) fiel
5644
5643
originalCPUMemPodSpec := * newPod .Spec .DeepCopy ()
5645
5644
var newContainers []core.Container
5646
5645
for ix , container := range originalCPUMemPodSpec .Containers {
5647
- lim := dropCPUMemoryUpdates (container .Resources .Limits , oldPod .Spec .Containers [ix ].Resources .Limits )
5648
- req := dropCPUMemoryUpdates (container .Resources .Requests , oldPod .Spec .Containers [ix ].Resources .Requests )
5649
- container .Resources = core.ResourceRequirements {Limits : lim , Requests : req }
5650
- container .ResizePolicy = oldPod .Spec .Containers [ix ].ResizePolicy // +k8s:verify-mutation:reason=clone
5646
+ dropCPUMemoryResourcesFromContainer (& container , & oldPod .Spec .Containers [ix ])
5651
5647
newContainers = append (newContainers , container )
5652
5648
}
5653
5649
originalCPUMemPodSpec .Containers = newContainers
@@ -5657,10 +5653,7 @@ func ValidatePodResize(newPod, oldPod *core.Pod, opts PodValidationOptions) fiel
5657
5653
if utilfeature .DefaultFeatureGate .Enabled (features .SidecarContainers ) {
5658
5654
for ix , container := range originalCPUMemPodSpec .InitContainers {
5659
5655
if container .RestartPolicy != nil && * container .RestartPolicy == core .ContainerRestartPolicyAlways { // restartable init container
5660
- lim := dropCPUMemoryUpdates (container .Resources .Limits , oldPod .Spec .InitContainers [ix ].Resources .Limits )
5661
- req := dropCPUMemoryUpdates (container .Resources .Requests , oldPod .Spec .InitContainers [ix ].Resources .Requests )
5662
- container .Resources = core.ResourceRequirements {Limits : lim , Requests : req }
5663
- container .ResizePolicy = oldPod .Spec .InitContainers [ix ].ResizePolicy // +k8s:verify-mutation:reason=clone
5656
+ dropCPUMemoryResourcesFromContainer (& container , & oldPod .Spec .InitContainers [ix ])
5664
5657
}
5665
5658
newInitContainers = append (newInitContainers , container )
5666
5659
}
@@ -5683,25 +5676,32 @@ func ValidatePodResize(newPod, oldPod *core.Pod, opts PodValidationOptions) fiel
5683
5676
return allErrs
5684
5677
}
5685
5678
5686
- func dropCPUMemoryUpdates (resourceList , oldResourceList core.ResourceList ) core.ResourceList {
5687
- if oldResourceList == nil {
5688
- return nil
5689
- }
5690
- var mungedResourceList core.ResourceList
5691
- if resourceList == nil {
5692
- mungedResourceList = make (core.ResourceList )
5693
- } else {
5694
- mungedResourceList = resourceList .DeepCopy ()
5695
- }
5696
- delete (mungedResourceList , core .ResourceCPU )
5697
- delete (mungedResourceList , core .ResourceMemory )
5698
- if cpu , found := oldResourceList [core .ResourceCPU ]; found {
5699
- mungedResourceList [core .ResourceCPU ] = cpu
5700
- }
5701
- if mem , found := oldResourceList [core .ResourceMemory ]; found {
5702
- mungedResourceList [core .ResourceMemory ] = mem
5679
+ // dropCPUMemoryResourcesFromContainer deletes the cpu and memory resources from the container, and copies them from the old pod container resources if present.
5680
+ func dropCPUMemoryResourcesFromContainer (container * core.Container , oldPodSpecContainer * core.Container ) {
5681
+ dropCPUMemoryUpdates := func (resourceList , oldResourceList core.ResourceList ) core.ResourceList {
5682
+ if oldResourceList == nil {
5683
+ return nil
5684
+ }
5685
+ var mungedResourceList core.ResourceList
5686
+ if resourceList == nil {
5687
+ mungedResourceList = make (core.ResourceList )
5688
+ } else {
5689
+ mungedResourceList = resourceList .DeepCopy ()
5690
+ }
5691
+ delete (mungedResourceList , core .ResourceCPU )
5692
+ delete (mungedResourceList , core .ResourceMemory )
5693
+ if cpu , found := oldResourceList [core .ResourceCPU ]; found {
5694
+ mungedResourceList [core .ResourceCPU ] = cpu
5695
+ }
5696
+ if mem , found := oldResourceList [core .ResourceMemory ]; found {
5697
+ mungedResourceList [core .ResourceMemory ] = mem
5698
+ }
5699
+ return mungedResourceList
5703
5700
}
5704
- return mungedResourceList
5701
+ lim := dropCPUMemoryUpdates (container .Resources .Limits , oldPodSpecContainer .Resources .Limits )
5702
+ req := dropCPUMemoryUpdates (container .Resources .Requests , oldPodSpecContainer .Resources .Requests )
5703
+ container .Resources = core.ResourceRequirements {Limits : lim , Requests : req }
5704
+ container .ResizePolicy = oldPodSpecContainer .ResizePolicy // +k8s:verify-mutation:reason=clone
5705
5705
}
5706
5706
5707
5707
// isPodResizeRequestSupported checks whether the pod is running on a node with InPlacePodVerticalScaling enabled.
@@ -8630,3 +8630,11 @@ func validateImageVolumeSource(imageVolume *core.ImageVolumeSource, fldPath *fie
8630
8630
allErrs = append (allErrs , validatePullPolicy (imageVolume .PullPolicy , fldPath .Child ("pullPolicy" ))... )
8631
8631
return allErrs
8632
8632
}
8633
+
8634
+ // isRestartableInitContainer returns true if the container has ContainerRestartPolicyAlways.
8635
+ func isRestartableInitContainer (initContainer * core.Container ) bool {
8636
+ if initContainer == nil || initContainer .RestartPolicy == nil {
8637
+ return false
8638
+ }
8639
+ return * initContainer .RestartPolicy == core .ContainerRestartPolicyAlways
8640
+ }
0 commit comments