From 4bdc1364eaa1b79c53717076f2cc5f42253ccd78 Mon Sep 17 00:00:00 2001 From: Shiming Zhang Date: Wed, 22 Feb 2023 13:35:55 +0800 Subject: [PATCH] Warn if the terminationGracePeriodSeconds is negative. Co-authored-by: Jordan Liggitt --- pkg/api/pod/warnings.go | 4 ++++ pkg/api/pod/warnings_test.go | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/pkg/api/pod/warnings.go b/pkg/api/pod/warnings.go index 0289cedeb3e..f40d5320520 100644 --- a/pkg/api/pod/warnings.go +++ b/pkg/api/pod/warnings.go @@ -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 } diff --git a/pkg/api/pod/warnings_test.go b/pkg/api/pod/warnings_test.go index 6b28eac14dd..b1b814a4f12 100644 --- a/pkg/api/pod/warnings_test.go +++ b/pkg/api/pod/warnings_test.go @@ -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 {