Warn if the terminationGracePeriodSeconds is negative.

Co-authored-by: Jordan Liggitt <jordan@liggitt.net>
This commit is contained in:
Shiming Zhang 2023-02-22 13:35:55 +08:00
parent 5e5c543b13
commit 4bdc1364ea
2 changed files with 17 additions and 0 deletions

View File

@ -250,5 +250,9 @@ func warningsForPodSpecAndMeta(fieldPath *field.Path, podSpec *api.PodSpec, meta
return true
})
// warn if the terminationGracePeriodSeconds is negative.
if podSpec.TerminationGracePeriodSeconds != nil && *podSpec.TerminationGracePeriodSeconds < 0 {
warnings = append(warnings, fmt.Sprintf("%s: must be >= 0; negative values are invalid and will be treated as 1", fieldPath.Child("spec", "terminationGracePeriodSeconds")))
}
return warnings
}

View File

@ -24,6 +24,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/sets"
api "k8s.io/kubernetes/pkg/apis/core"
utilpointer "k8s.io/utils/pointer"
)
func BenchmarkNoWarnings(b *testing.B) {
@ -489,6 +490,18 @@ func TestWarnings(t *testing.T) {
`spec.volumes[0].ephemeral.volumeClaimTemplate.spec.resources.requests[storage]: fractional byte value "200m" is invalid, must be an integer`,
},
},
{
name: "terminationGracePeriodSeconds is negative",
template: &api.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{},
Spec: api.PodSpec{
TerminationGracePeriodSeconds: utilpointer.Int64Ptr(-1),
},
},
expected: []string{
`spec.terminationGracePeriodSeconds: must be >= 0; negative values are invalid and will be treated as 1`,
},
},
}
for _, tc := range testcases {