mirror of
				https://github.com/optim-enterprises-bv/kubernetes.git
				synced 2025-11-04 04:08:16 +00:00 
			
		
		
		
	Added sidecar support in ValidatePodResize and dropNonResizeUpdates
This commit is contained in:
		@@ -5654,8 +5654,9 @@ func ValidatePodResize(newPod, oldPod *core.Pod, opts PodValidationOptions) fiel
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	// Ensure that only CPU and memory resources are mutable for restartable init containers.
 | 
						// Ensure that only CPU and memory resources are mutable for restartable init containers.
 | 
				
			||||||
	var newInitContainers []core.Container
 | 
						var newInitContainers []core.Container
 | 
				
			||||||
 | 
						if utilfeature.DefaultFeatureGate.Enabled(features.SidecarContainers) {
 | 
				
			||||||
		for ix, container := range originalCPUMemPodSpec.InitContainers {
 | 
							for ix, container := range originalCPUMemPodSpec.InitContainers {
 | 
				
			||||||
		if container.RestartPolicy != nil && *container.RestartPolicy == core.ContainerRestartPolicyAlways {
 | 
								if container.RestartPolicy != nil && *container.RestartPolicy == core.ContainerRestartPolicyAlways { // restartable init container
 | 
				
			||||||
				lim := dropCPUMemoryUpdates(container.Resources.Limits, oldPod.Spec.InitContainers[ix].Resources.Limits)
 | 
									lim := dropCPUMemoryUpdates(container.Resources.Limits, oldPod.Spec.InitContainers[ix].Resources.Limits)
 | 
				
			||||||
				req := dropCPUMemoryUpdates(container.Resources.Requests, oldPod.Spec.InitContainers[ix].Resources.Requests)
 | 
									req := dropCPUMemoryUpdates(container.Resources.Requests, oldPod.Spec.InitContainers[ix].Resources.Requests)
 | 
				
			||||||
				container.Resources = core.ResourceRequirements{Limits: lim, Requests: req}
 | 
									container.Resources = core.ResourceRequirements{Limits: lim, Requests: req}
 | 
				
			||||||
@@ -5663,6 +5664,7 @@ func ValidatePodResize(newPod, oldPod *core.Pod, opts PodValidationOptions) fiel
 | 
				
			|||||||
				newInitContainers = append(newInitContainers, container)
 | 
									newInitContainers = append(newInitContainers, container)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	if len(newInitContainers) > 0 {
 | 
						if len(newInitContainers) > 0 {
 | 
				
			||||||
		originalCPUMemPodSpec.InitContainers = newInitContainers
 | 
							originalCPUMemPodSpec.InitContainers = newInitContainers
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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 {
 | 
					func dropNonResizeUpdates(newPod, oldPod *api.Pod) *api.Pod {
 | 
				
			||||||
	pod := dropPodUpdates(newPod, oldPod)
 | 
						pod := dropPodUpdates(newPod, oldPod)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Containers are not allowed to be re-ordered, but in case they were,
 | 
						// 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.
 | 
						// we don't want to corrupt them here. It will get caught in validation.
 | 
				
			||||||
	oldCtrToIndex := make(map[string]int)
 | 
						oldCtrToIndex := make(map[string]int)
 | 
				
			||||||
 | 
						oldInitCtrToIndex := make(map[string]int)
 | 
				
			||||||
	for idx, ctr := range pod.Spec.Containers {
 | 
						for idx, ctr := range pod.Spec.Containers {
 | 
				
			||||||
		oldCtrToIndex[ctr.Name] = idx
 | 
							oldCtrToIndex[ctr.Name] = idx
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	// TODO: Once we add in-place pod resize support for sidecars, we need to allow
 | 
						for idx, ctr := range pod.Spec.InitContainers {
 | 
				
			||||||
	// modifying sidecar resources via resize subresource too.
 | 
							oldInitCtrToIndex[ctr.Name] = idx
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, ctr := range newPod.Spec.Containers {
 | 
						for _, ctr := range newPod.Spec.Containers {
 | 
				
			||||||
		idx, ok := oldCtrToIndex[ctr.Name]
 | 
							idx, ok := oldCtrToIndex[ctr.Name]
 | 
				
			||||||
		if !ok {
 | 
							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].Resources = ctr.Resources
 | 
				
			||||||
		pod.Spec.Containers[idx].ResizePolicy = ctr.ResizePolicy
 | 
							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
 | 
						return pod
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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",
 | 
								description: "resized with no sidecar containers, no resize status",
 | 
				
			||||||
			expectedRequests: v1.ResourceList{
 | 
								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",
 | 
								description: "resized, infeasible, but don't use status",
 | 
				
			||||||
			expectedRequests: v1.ResourceList{
 | 
								expectedRequests: v1.ResourceList{
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -162,16 +162,11 @@ func makeResizableContainer(tcInfo ResizableContainerInfo) v1.Container {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func MakePodWithResizableContainers(ns, name, timeStamp string, tcInfo []ResizableContainerInfo) *v1.Pod {
 | 
					func MakePodWithResizableContainers(ns, name, timeStamp string, tcInfo []ResizableContainerInfo) *v1.Pod {
 | 
				
			||||||
	var testContainers []v1.Container
 | 
						var testContainers []v1.Container
 | 
				
			||||||
	var testInitContainers []v1.Container
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, ci := range tcInfo {
 | 
						for _, ci := range tcInfo {
 | 
				
			||||||
		tc := makeResizableContainer(ci)
 | 
							tc := makeResizableContainer(ci)
 | 
				
			||||||
		if ci.IsRestartableInitCtr {
 | 
					 | 
				
			||||||
			testInitContainers = append(testInitContainers, tc)
 | 
					 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
		testContainers = append(testContainers, tc)
 | 
							testContainers = append(testContainers, tc)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	pod := &v1.Pod{
 | 
						pod := &v1.Pod{
 | 
				
			||||||
		ObjectMeta: metav1.ObjectMeta{
 | 
							ObjectMeta: metav1.ObjectMeta{
 | 
				
			||||||
			Name:      name,
 | 
								Name:      name,
 | 
				
			||||||
@@ -186,12 +181,6 @@ func MakePodWithResizableContainers(ns, name, timeStamp string, tcInfo []Resizab
 | 
				
			|||||||
			RestartPolicy: v1.RestartPolicyOnFailure,
 | 
								RestartPolicy: v1.RestartPolicyOnFailure,
 | 
				
			||||||
		},
 | 
							},
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					 | 
				
			||||||
	if len(testInitContainers) > 0 {
 | 
					 | 
				
			||||||
		pod.Spec.RestartPolicy = v1.RestartPolicyAlways
 | 
					 | 
				
			||||||
		pod.Spec.InitContainers = testInitContainers
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return pod
 | 
						return pod
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user