Skip to content

Commit 5889da1

Browse files
committed
Resolved latest review comments
1 parent 242dec3 commit 5889da1

File tree

4 files changed

+86
-53
lines changed

4 files changed

+86
-53
lines changed

pkg/apis/core/validation/validation.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5649,22 +5649,29 @@ func ValidatePodResize(newPod, oldPod *core.Pod, opts PodValidationOptions) fiel
56495649
originalCPUMemPodSpec.Containers = newContainers
56505650

56515651
// Ensure that only CPU and memory resources are mutable for restartable init containers.
5652+
// Also ensure that resources are immutable for non-restartable init containers.
56525653
var newInitContainers []core.Container
56535654
if utilfeature.DefaultFeatureGate.Enabled(features.SidecarContainers) {
56545655
for ix, container := range originalCPUMemPodSpec.InitContainers {
56555656
if isRestartableInitContainer(&container) { // restartable init container
56565657
dropCPUMemoryResourcesFromContainer(&container, &oldPod.Spec.InitContainers[ix])
5658+
if !apiequality.Semantic.DeepEqual(container, oldPod.Spec.InitContainers[ix]) {
5659+
// This likely means that the user has made changes to resources other than CPU and memory for sidecar container.
5660+
specDiff := cmp.Diff(oldPod.Spec.InitContainers[ix], container)
5661+
errs := field.Forbidden(specPath, fmt.Sprintf("only cpu and memory resources for sidecar containers are mutable\n%v", specDiff))
5662+
allErrs = append(allErrs, errs)
5663+
return allErrs
5664+
}
5665+
} else if !apiequality.Semantic.DeepEqual(container, oldPod.Spec.InitContainers[ix]) { // non-restartable init container
5666+
// This likely means that the user has modified resources of non-sidecar init container.
5667+
specDiff := cmp.Diff(oldPod.Spec.InitContainers[ix], container)
5668+
errs := field.Forbidden(specPath, fmt.Sprintf("resources for non-sidecar init containers are immutable\n%v", specDiff))
5669+
allErrs = append(allErrs, errs)
5670+
return allErrs
56575671
}
56585672
newInitContainers = append(newInitContainers, container)
56595673
}
56605674
originalCPUMemPodSpec.InitContainers = newInitContainers
5661-
if !apiequality.Semantic.DeepEqual(originalCPUMemPodSpec.InitContainers, oldPod.Spec.InitContainers) {
5662-
// This likely means that the user has modified non-sidecar container resources.
5663-
specDiff := cmp.Diff(oldPod.Spec.InitContainers, originalCPUMemPodSpec.InitContainers)
5664-
errs := field.Forbidden(specPath, fmt.Sprintf("cpu and memory resources for only sidecar containers are mutable\n%v", specDiff))
5665-
allErrs = append(allErrs, errs)
5666-
return allErrs
5667-
}
56685675
}
56695676

56705677
if !apiequality.Semantic.DeepEqual(originalCPUMemPodSpec, oldPod.Spec) {

pkg/apis/core/validation/validation_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25911,7 +25911,7 @@ func TestValidatePodResize(t *testing.T) {
2591125911
test: "Pod QoS unchanged, burstable -> burstable, remove cpu and memory requests",
2591225912
old: mkPodWithInitContainers(getResources("100m", "100Mi", "", ""), getResources("100m", "", "", ""), ""),
2591325913
new: mkPodWithInitContainers(core.ResourceList{}, getResources("100m", "", "", ""), ""),
25914-
err: "spec: Forbidden: cpu and memory resources for only sidecar containers are mutable",
25914+
err: "spec: Forbidden: resources for non-sidecar init containers are immutable",
2591525915
}, {
2591625916
test: "Pod with nil Resource field in Status",
2591725917
old: mkPod(core.ResourceList{}, getResources("100m", "0", "1Gi", ""), podtest.SetStatus(core.PodStatus{
@@ -25993,7 +25993,7 @@ func TestValidatePodResize(t *testing.T) {
2599325993
test: "storage limit change for sidecar containers",
2599425994
old: mkPodWithInitContainers(core.ResourceList{}, getResources("100m", "100Mi", "2Gi", ""), core.ContainerRestartPolicyAlways),
2599525995
new: mkPodWithInitContainers(core.ResourceList{}, getResources("100m", "100Mi", "1Gi", ""), core.ContainerRestartPolicyAlways),
25996-
err: "spec: Forbidden: cpu and memory resources for only sidecar containers are mutable",
25996+
err: "spec: Forbidden: only cpu and memory resources for sidecar containers are mutable",
2599725997
}, {
2599825998
test: "cpu request change for sidecar containers",
2599925999
old: mkPodWithInitContainers(getResources("200m", "0", "", ""), core.ResourceList{}, core.ContainerRestartPolicyAlways),
@@ -26008,7 +26008,7 @@ func TestValidatePodResize(t *testing.T) {
2600826008
test: "storage request change for sidecar containers",
2600926009
old: mkPodWithInitContainers(getResources("100m", "0", "1Gi", ""), core.ResourceList{}, core.ContainerRestartPolicyAlways),
2601026010
new: mkPodWithInitContainers(getResources("100m", "0", "2Gi", ""), core.ResourceList{}, core.ContainerRestartPolicyAlways),
26011-
err: "spec: Forbidden: cpu and memory resources for only sidecar containers are mutable",
26011+
err: "spec: Forbidden: only cpu and memory resources for sidecar containers are mutable",
2601226012
},
2601326013
}
2601426014

pkg/kubelet/kubelet_pods_test.go

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6880,44 +6880,59 @@ func TestAllocatedResourcesMatchStatus(t *testing.T) {
68806880
}}
68816881

68826882
for _, test := range tests {
6883-
t.Run(test.name, func(t *testing.T) {
6884-
allocatedPod := v1.Pod{
6885-
ObjectMeta: metav1.ObjectMeta{
6886-
Name: "test",
6887-
},
6888-
Spec: v1.PodSpec{
6889-
Containers: []v1.Container{{
6890-
Name: "c",
6891-
Resources: test.allocatedResources,
6892-
}},
6893-
InitContainers: []v1.Container{{
6894-
Name: "c1-init",
6895-
Resources: test.allocatedResources,
6896-
RestartPolicy: &containerRestartPolicyAlways,
6897-
}},
6898-
},
6899-
}
6900-
state := kubecontainer.ContainerStateRunning
6901-
if test.statusTerminated {
6902-
state = kubecontainer.ContainerStateExited
6903-
}
6904-
podStatus := &kubecontainer.PodStatus{
6905-
Name: "test",
6906-
ContainerStatuses: []*kubecontainer.Status{
6907-
{
6908-
Name: "c",
6909-
State: state,
6910-
Resources: test.statusResources,
6911-
},
6912-
{
6913-
Name: "c1-init",
6914-
State: state,
6915-
Resources: test.statusResources,
6883+
for _, isSidecarContainer := range []bool{false, true} {
6884+
t.Run(test.name, func(t *testing.T) {
6885+
var podStatus *kubecontainer.PodStatus
6886+
state := kubecontainer.ContainerStateRunning
6887+
if test.statusTerminated {
6888+
state = kubecontainer.ContainerStateExited
6889+
}
6890+
6891+
allocatedPod := v1.Pod{
6892+
ObjectMeta: metav1.ObjectMeta{
6893+
Name: "test",
69166894
},
6917-
},
6918-
}
6919-
match := allocatedResourcesMatchStatus(&allocatedPod, podStatus)
6920-
assert.Equal(t, test.expectMatch, match)
6921-
})
6895+
}
6896+
6897+
if isSidecarContainer {
6898+
allocatedPod.Spec = v1.PodSpec{
6899+
InitContainers: []v1.Container{{
6900+
Name: "c1-init",
6901+
Resources: test.allocatedResources,
6902+
RestartPolicy: &containerRestartPolicyAlways,
6903+
}},
6904+
}
6905+
podStatus = &kubecontainer.PodStatus{
6906+
Name: "test",
6907+
ContainerStatuses: []*kubecontainer.Status{
6908+
{
6909+
Name: "c1-init",
6910+
State: state,
6911+
Resources: test.statusResources,
6912+
},
6913+
},
6914+
}
6915+
} else {
6916+
allocatedPod.Spec = v1.PodSpec{
6917+
Containers: []v1.Container{{
6918+
Name: "c",
6919+
Resources: test.allocatedResources,
6920+
}},
6921+
}
6922+
podStatus = &kubecontainer.PodStatus{
6923+
Name: "test",
6924+
ContainerStatuses: []*kubecontainer.Status{
6925+
{
6926+
Name: "c",
6927+
State: state,
6928+
Resources: test.statusResources,
6929+
},
6930+
},
6931+
}
6932+
}
6933+
match := allocatedResourcesMatchStatus(&allocatedPod, podStatus)
6934+
assert.Equal(t, test.expectMatch, match)
6935+
})
6936+
}
69226937
}
69236938
}

staging/src/k8s.io/kubectl/pkg/util/resource/resource.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,15 @@ func podRequests(pod *corev1.Pod) corev1.ResourceList {
4545
for i := range pod.Status.ContainerStatuses {
4646
containerStatuses[pod.Status.ContainerStatuses[i].Name] = &pod.Status.ContainerStatuses[i]
4747
}
48+
for i := range pod.Status.InitContainerStatuses {
49+
containerStatuses[pod.Status.InitContainerStatuses[i].Name] = &pod.Status.InitContainerStatuses[i]
50+
}
4851

4952
for _, container := range pod.Spec.Containers {
5053
containerReqs := container.Resources.Requests
5154
cs, found := containerStatuses[container.Name]
5255
if found && cs.Resources != nil {
53-
if pod.Status.Resize == corev1.PodResizeStatusInfeasible {
54-
containerReqs = cs.Resources.Requests.DeepCopy()
55-
} else {
56-
containerReqs = max(container.Resources.Requests, cs.Resources.Requests)
57-
}
56+
containerReqs = determineContainerReqs(pod, &container, cs)
5857
}
5958
addResourceList(reqs, containerReqs)
6059
}
@@ -66,6 +65,10 @@ func podRequests(pod *corev1.Pod) corev1.ResourceList {
6665
containerReqs := container.Resources.Requests
6766

6867
if container.RestartPolicy != nil && *container.RestartPolicy == corev1.ContainerRestartPolicyAlways {
68+
cs, found := containerStatuses[container.Name]
69+
if found && cs.Resources != nil {
70+
containerReqs = determineContainerReqs(pod, &container, cs)
71+
}
6972
// and add them to the resulting cumulative container requests
7073
addResourceList(reqs, containerReqs)
7174

@@ -137,6 +140,14 @@ func podLimits(pod *corev1.Pod) corev1.ResourceList {
137140
return limits
138141
}
139142

143+
// determineContainerReqs will return a copy of the container requests based on if resizing is feasible or not.
144+
func determineContainerReqs(pod *corev1.Pod, container *corev1.Container, cs *corev1.ContainerStatus) corev1.ResourceList {
145+
if pod.Status.Resize == corev1.PodResizeStatusInfeasible {
146+
return cs.Resources.Requests.DeepCopy()
147+
}
148+
return max(container.Resources.Requests, cs.Resources.Requests)
149+
}
150+
140151
// max returns the result of max(a, b) for each named resource and is only used if we can't
141152
// accumulate into an existing resource list
142153
func max(a corev1.ResourceList, b corev1.ResourceList) corev1.ResourceList {

0 commit comments

Comments
 (0)