Skip to content

Commit 9b95eaf

Browse files
committed
Fix pod resize validation when adding non-resizable resources
1 parent 3ec72e2 commit 9b95eaf

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

pkg/apis/core/validation/validation.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6222,11 +6222,11 @@ func validatePodResizeContainerOrdering(newPod, oldPod *core.Pod, specPath *fiel
62226222
// dropCPUMemoryResourcesFromContainer deletes the cpu and memory resources from the container, and copies them from the old pod container resources if present.
62236223
func dropCPUMemoryResourcesFromContainer(container *core.Container, oldPodSpecContainer *core.Container) {
62246224
dropCPUMemoryUpdates := func(resourceList, oldResourceList core.ResourceList) core.ResourceList {
6225-
if oldResourceList == nil {
6226-
return nil
6227-
}
62286225
var mungedResourceList core.ResourceList
62296226
if resourceList == nil {
6227+
if oldResourceList == nil {
6228+
return nil
6229+
}
62306230
mungedResourceList = make(core.ResourceList)
62316231
} else {
62326232
mungedResourceList = resourceList.DeepCopy()

pkg/apis/core/validation/validation_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28309,6 +28309,21 @@ func TestValidatePodResize(t *testing.T) {
2830928309
old: mkPodWithInitContainers(getResources("100m", "0", "1Gi", ""), core.ResourceList{}, core.ContainerRestartPolicyAlways, resizePolicy(core.ResourceMemory, core.RestartContainer)),
2831028310
new: mkPodWithInitContainers(getResources("100m", "0", "2Gi", ""), core.ResourceList{}, core.ContainerRestartPolicyAlways, resizePolicy(core.ResourceMemory, core.NotRequired)),
2831128311
err: "spec: Forbidden: only cpu and memory resources are mutable",
28312+
}, {
28313+
test: "invalid: adding non-resizable resources to a container without resources",
28314+
old: podtest.MakePod("pod", podtest.SetContainers(
28315+
podtest.MakeContainer("c1"),
28316+
)),
28317+
new: podtest.MakePod("pod", podtest.SetContainers(
28318+
podtest.MakeContainer("c1",
28319+
podtest.SetContainerResources(core.ResourceRequirements{
28320+
Requests: core.ResourceList{
28321+
core.ResourceEphemeralStorage: resource.MustParse("10Gi"),
28322+
},
28323+
}),
28324+
),
28325+
)),
28326+
err: "spec: Forbidden: only cpu and memory resources are mutable",
2831228327
},
2831328328
}
2831428329

test/integration/pods/pods_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,11 @@ func TestPodResizeRBAC(t *testing.T) {
869869
{
870870
Name: "fake-name",
871871
Image: "fakeimage",
872+
Resources: v1.ResourceRequirements{
873+
Requests: v1.ResourceList{
874+
v1.ResourceCPU: resource.MustParse("100m"),
875+
},
876+
},
872877
},
873878
},
874879
},
@@ -929,7 +934,7 @@ func TestPodResizeRBAC(t *testing.T) {
929934
}
930935
resp.Spec.Containers[0].Resources = v1.ResourceRequirements{
931936
Requests: v1.ResourceList{
932-
v1.ResourceEphemeralStorage: resource.MustParse("2Gi"),
937+
v1.ResourceCPU: resource.MustParse("200m"),
933938
},
934939
}
935940
_, err = saClient.CoreV1().Pods(ns.Name).UpdateResize(context.TODO(), resp.Name, resp, metav1.UpdateOptions{})

0 commit comments

Comments
 (0)