Skip to content

Commit f63359e

Browse files
fix(pod/util): typos in getting pod validation options
Before, containers with the PostStart sleep lifecycle hook would cause null pointer panics due to a typo in the field name being checked. This commit fixes that. The check also needs to be done on the oldPodSpec, rather than the podSpec, so that existing workloads which use the zero value continue functioning in the same way.
1 parent 4032177 commit f63359e

File tree

2 files changed

+91
-2
lines changed

2 files changed

+91
-2
lines changed

pkg/api/pod/util.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ func GetValidationOptionsFromPodSpecAndMeta(podSpec, oldPodSpec *api.PodSpec, po
418418
}
419419
}
420420

421-
opts.AllowPodLifecycleSleepActionZeroValue = opts.AllowPodLifecycleSleepActionZeroValue || podLifecycleSleepActionZeroValueInUse(podSpec)
421+
opts.AllowPodLifecycleSleepActionZeroValue = opts.AllowPodLifecycleSleepActionZeroValue || podLifecycleSleepActionZeroValueInUse(oldPodSpec)
422422
// If oldPod has resize policy set on the restartable init container, we must allow it
423423
opts.AllowSidecarResizePolicy = opts.AllowSidecarResizePolicy || hasRestartableInitContainerResizePolicy(oldPodSpec)
424424
}
@@ -775,7 +775,7 @@ func podLifecycleSleepActionZeroValueInUse(podSpec *api.PodSpec) bool {
775775
inUse = true
776776
return false
777777
}
778-
if c.Lifecycle.PostStart != nil && c.Lifecycle.PostStart.Sleep != nil && c.Lifecycle.PreStop.Sleep.Seconds == 0 {
778+
if c.Lifecycle.PostStart != nil && c.Lifecycle.PostStart.Sleep != nil && c.Lifecycle.PostStart.Sleep.Seconds == 0 {
779779
inUse = true
780780
return false
781781
}

pkg/api/pod/util_test.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4657,3 +4657,92 @@ func TestValidateInvalidLabelValueInNodeSelectorOption(t *testing.T) {
46574657
})
46584658
}
46594659
}
4660+
4661+
func TestValidateAllowPodLifecycleSleepActionZeroValue(t *testing.T) {
4662+
testCases := []struct {
4663+
name string
4664+
podSpec *api.PodSpec
4665+
expectAllowPodLifecycleSleepActionZeroValue bool
4666+
}{
4667+
{
4668+
name: "no lifecycle hooks",
4669+
podSpec: &api.PodSpec{},
4670+
expectAllowPodLifecycleSleepActionZeroValue: false,
4671+
},
4672+
{
4673+
name: "Prestop with non-zero second duration",
4674+
podSpec: &api.PodSpec{
4675+
Containers: []api.Container{
4676+
{
4677+
Lifecycle: &api.Lifecycle{
4678+
PreStop: &api.LifecycleHandler{
4679+
Sleep: &api.SleepAction{
4680+
Seconds: 1,
4681+
},
4682+
},
4683+
},
4684+
},
4685+
},
4686+
},
4687+
expectAllowPodLifecycleSleepActionZeroValue: false,
4688+
},
4689+
{
4690+
name: "PostStart with non-zero second duration",
4691+
podSpec: &api.PodSpec{
4692+
Containers: []api.Container{
4693+
{
4694+
Lifecycle: &api.Lifecycle{
4695+
PostStart: &api.LifecycleHandler{
4696+
Sleep: &api.SleepAction{
4697+
Seconds: 1,
4698+
},
4699+
},
4700+
},
4701+
},
4702+
},
4703+
},
4704+
expectAllowPodLifecycleSleepActionZeroValue: false,
4705+
},
4706+
{
4707+
name: "PreStop with zero seconds",
4708+
podSpec: &api.PodSpec{
4709+
Containers: []api.Container{
4710+
{
4711+
Lifecycle: &api.Lifecycle{
4712+
PreStop: &api.LifecycleHandler{
4713+
Sleep: &api.SleepAction{
4714+
Seconds: 0,
4715+
},
4716+
},
4717+
},
4718+
},
4719+
},
4720+
},
4721+
expectAllowPodLifecycleSleepActionZeroValue: true,
4722+
},
4723+
{
4724+
name: "PostStart with zero seconds",
4725+
podSpec: &api.PodSpec{
4726+
Containers: []api.Container{
4727+
{
4728+
Lifecycle: &api.Lifecycle{
4729+
PostStart: &api.LifecycleHandler{
4730+
Sleep: &api.SleepAction{
4731+
Seconds: 0,
4732+
},
4733+
},
4734+
},
4735+
},
4736+
},
4737+
},
4738+
expectAllowPodLifecycleSleepActionZeroValue: true,
4739+
},
4740+
}
4741+
4742+
for _, tc := range testCases {
4743+
t.Run(tc.name, func(t *testing.T) {
4744+
gotOptions := GetValidationOptionsFromPodSpecAndMeta(&api.PodSpec{}, tc.podSpec, nil, nil)
4745+
assert.Equal(t, tc.expectAllowPodLifecycleSleepActionZeroValue, gotOptions.AllowPodLifecycleSleepActionZeroValue, "AllowPodLifecycleSleepActionZeroValue")
4746+
})
4747+
}
4748+
}

0 commit comments

Comments
 (0)