diff --git a/api/openapi-spec/swagger.json b/api/openapi-spec/swagger.json index ef2099ac2b64..447124493014 100644 --- a/api/openapi-spec/swagger.json +++ b/api/openapi-spec/swagger.json @@ -8219,7 +8219,7 @@ "description": "TCPSocket specifies an action involving a TCP port. TCP hooks not yet supported" }, "terminationGracePeriodSeconds": { - "description": "Optional duration in seconds the pod needs to terminate gracefully upon probe failure. The grace period is the duration in seconds after the processes running in the pod are sent a termination signal and the time when the processes are forcibly halted with a kill signal. Set this value longer than the expected cleanup time for your process. If this value is nil, the pod's terminationGracePeriodSeconds will be used. Otherwise, this value overrides the value provided by the pod spec. Value must be non-negative integer. The value zero indicates stop immediately via the kill signal (no opportunity to shut down). This is an alpha field and requires enabling ProbeTerminationGracePeriod feature gate.", + "description": "Optional duration in seconds the pod needs to terminate gracefully upon probe failure. The grace period is the duration in seconds after the processes running in the pod are sent a termination signal and the time when the processes are forcibly halted with a kill signal. Set this value longer than the expected cleanup time for your process. If this value is nil, the pod's terminationGracePeriodSeconds will be used. Otherwise, this value overrides the value provided by the pod spec. Value must be non-negative integer. The value zero indicates stop immediately via the kill signal (no opportunity to shut down). This is an alpha field and requires enabling ProbeTerminationGracePeriod feature gate. Minimum value is 1. spec.terminationGracePeriodSeconds is used if unset.", "format": "int64", "type": "integer" }, diff --git a/pkg/apis/core/validation/validation.go b/pkg/apis/core/validation/validation.go index 9b3be268f9ef..4ea4f0c7318c 100644 --- a/pkg/apis/core/validation/validation.go +++ b/pkg/apis/core/validation/validation.go @@ -2655,6 +2655,9 @@ func validateProbe(probe *core.Probe, fldPath *field.Path) field.ErrorList { allErrs = append(allErrs, ValidateNonnegativeField(int64(probe.PeriodSeconds), fldPath.Child("periodSeconds"))...) allErrs = append(allErrs, ValidateNonnegativeField(int64(probe.SuccessThreshold), fldPath.Child("successThreshold"))...) allErrs = append(allErrs, ValidateNonnegativeField(int64(probe.FailureThreshold), fldPath.Child("failureThreshold"))...) + if probe.TerminationGracePeriodSeconds != nil && *probe.TerminationGracePeriodSeconds <= 0 { + allErrs = append(allErrs, field.Invalid(fldPath.Child("terminationGracePeriodSeconds"), *probe.TerminationGracePeriodSeconds, "must be greater than 0")) + } return allErrs } diff --git a/pkg/apis/core/validation/validation_test.go b/pkg/apis/core/validation/validation_test.go index 0523358cc869..2ec590aef68c 100644 --- a/pkg/apis/core/validation/validation_test.go +++ b/pkg/apis/core/validation/validation_test.go @@ -6032,6 +6032,133 @@ func TestValidateProbe(t *testing.T) { } } +func Test_validateProbe(t *testing.T) { + defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ProbeTerminationGracePeriod, true)() + + fldPath := field.NewPath("test") + type args struct { + probe *core.Probe + fldPath *field.Path + } + tests := []struct { + name string + args args + want field.ErrorList + }{ + { + args: args{ + probe: &core.Probe{}, + fldPath: fldPath, + }, + want: field.ErrorList{field.Required(fldPath, "must specify a handler type")}, + }, + { + args: args{ + probe: &core.Probe{ + Handler: core.Handler{Exec: &core.ExecAction{Command: []string{"echo"}}}, + }, + fldPath: fldPath, + }, + want: field.ErrorList{}, + }, + { + args: args{ + probe: &core.Probe{ + Handler: core.Handler{Exec: &core.ExecAction{Command: []string{"echo"}}}, + InitialDelaySeconds: -1, + }, + fldPath: fldPath, + }, + want: field.ErrorList{field.Invalid(fldPath.Child("initialDelaySeconds"), -1, "must be greater than or equal to 0")}, + }, + { + args: args{ + probe: &core.Probe{ + Handler: core.Handler{Exec: &core.ExecAction{Command: []string{"echo"}}}, + TimeoutSeconds: -1, + }, + fldPath: fldPath, + }, + want: field.ErrorList{field.Invalid(fldPath.Child("timeoutSeconds"), -1, "must be greater than or equal to 0")}, + }, + { + args: args{ + probe: &core.Probe{ + Handler: core.Handler{Exec: &core.ExecAction{Command: []string{"echo"}}}, + PeriodSeconds: -1, + }, + fldPath: fldPath, + }, + want: field.ErrorList{field.Invalid(fldPath.Child("periodSeconds"), -1, "must be greater than or equal to 0")}, + }, + { + args: args{ + probe: &core.Probe{ + Handler: core.Handler{Exec: &core.ExecAction{Command: []string{"echo"}}}, + SuccessThreshold: -1, + }, + fldPath: fldPath, + }, + want: field.ErrorList{field.Invalid(fldPath.Child("successThreshold"), -1, "must be greater than or equal to 0")}, + }, + { + args: args{ + probe: &core.Probe{ + Handler: core.Handler{Exec: &core.ExecAction{Command: []string{"echo"}}}, + FailureThreshold: -1, + }, + fldPath: fldPath, + }, + want: field.ErrorList{field.Invalid(fldPath.Child("failureThreshold"), -1, "must be greater than or equal to 0")}, + }, + { + args: args{ + probe: &core.Probe{ + Handler: core.Handler{Exec: &core.ExecAction{Command: []string{"echo"}}}, + TerminationGracePeriodSeconds: utilpointer.Int64(-1), + }, + fldPath: fldPath, + }, + want: field.ErrorList{field.Invalid(fldPath.Child("terminationGracePeriodSeconds"), -1, "must be greater than 0")}, + }, + { + args: args{ + probe: &core.Probe{ + Handler: core.Handler{Exec: &core.ExecAction{Command: []string{"echo"}}}, + TerminationGracePeriodSeconds: utilpointer.Int64(0), + }, + fldPath: fldPath, + }, + want: field.ErrorList{field.Invalid(fldPath.Child("terminationGracePeriodSeconds"), 0, "must be greater than 0")}, + }, + { + args: args{ + probe: &core.Probe{ + Handler: core.Handler{Exec: &core.ExecAction{Command: []string{"echo"}}}, + TerminationGracePeriodSeconds: utilpointer.Int64(1), + }, + fldPath: fldPath, + }, + want: field.ErrorList{}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := validateProbe(tt.args.probe, tt.args.fldPath) + if len(got) != len(tt.want) { + t.Errorf("validateProbe() = %v, want %v", got, tt.want) + return + } + for i := range got { + if got[i].Type != tt.want[i].Type || + got[i].Field != tt.want[i].Field { + t.Errorf("validateProbe()[%d] = %v, want %v", i, got[i], tt.want[i]) + } + } + }) + } +} + func TestValidateHandler(t *testing.T) { successCases := []core.Handler{ {Exec: &core.ExecAction{Command: []string{"echo"}}}, diff --git a/staging/src/k8s.io/api/core/v1/generated.proto b/staging/src/k8s.io/api/core/v1/generated.proto index a4546cdbd31d..40875bfac68c 100644 --- a/staging/src/k8s.io/api/core/v1/generated.proto +++ b/staging/src/k8s.io/api/core/v1/generated.proto @@ -3913,6 +3913,7 @@ message Probe { // Value must be non-negative integer. The value zero indicates stop immediately via // the kill signal (no opportunity to shut down). // This is an alpha field and requires enabling ProbeTerminationGracePeriod feature gate. + // Minimum value is 1. spec.terminationGracePeriodSeconds is used if unset. // +optional optional int64 terminationGracePeriodSeconds = 7; } diff --git a/staging/src/k8s.io/api/core/v1/types.go b/staging/src/k8s.io/api/core/v1/types.go index 6d0bd518499d..4d7fd6be7c60 100644 --- a/staging/src/k8s.io/api/core/v1/types.go +++ b/staging/src/k8s.io/api/core/v1/types.go @@ -2151,6 +2151,7 @@ type Probe struct { // Value must be non-negative integer. The value zero indicates stop immediately via // the kill signal (no opportunity to shut down). // This is an alpha field and requires enabling ProbeTerminationGracePeriod feature gate. + // Minimum value is 1. spec.terminationGracePeriodSeconds is used if unset. // +optional TerminationGracePeriodSeconds *int64 `json:"terminationGracePeriodSeconds,omitempty" protobuf:"varint,7,opt,name=terminationGracePeriodSeconds"` } diff --git a/staging/src/k8s.io/api/core/v1/types_swagger_doc_generated.go b/staging/src/k8s.io/api/core/v1/types_swagger_doc_generated.go index 399147a670af..c760eddd289c 100644 --- a/staging/src/k8s.io/api/core/v1/types_swagger_doc_generated.go +++ b/staging/src/k8s.io/api/core/v1/types_swagger_doc_generated.go @@ -1775,7 +1775,7 @@ var map_Probe = map[string]string{ "periodSeconds": "How often (in seconds) to perform the probe. Default to 10 seconds. Minimum value is 1.", "successThreshold": "Minimum consecutive successes for the probe to be considered successful after having failed. Defaults to 1. Must be 1 for liveness and startup. Minimum value is 1.", "failureThreshold": "Minimum consecutive failures for the probe to be considered failed after having succeeded. Defaults to 3. Minimum value is 1.", - "terminationGracePeriodSeconds": "Optional duration in seconds the pod needs to terminate gracefully upon probe failure. The grace period is the duration in seconds after the processes running in the pod are sent a termination signal and the time when the processes are forcibly halted with a kill signal. Set this value longer than the expected cleanup time for your process. If this value is nil, the pod's terminationGracePeriodSeconds will be used. Otherwise, this value overrides the value provided by the pod spec. Value must be non-negative integer. The value zero indicates stop immediately via the kill signal (no opportunity to shut down). This is an alpha field and requires enabling ProbeTerminationGracePeriod feature gate.", + "terminationGracePeriodSeconds": "Optional duration in seconds the pod needs to terminate gracefully upon probe failure. The grace period is the duration in seconds after the processes running in the pod are sent a termination signal and the time when the processes are forcibly halted with a kill signal. Set this value longer than the expected cleanup time for your process. If this value is nil, the pod's terminationGracePeriodSeconds will be used. Otherwise, this value overrides the value provided by the pod spec. Value must be non-negative integer. The value zero indicates stop immediately via the kill signal (no opportunity to shut down). This is an alpha field and requires enabling ProbeTerminationGracePeriod feature gate. Minimum value is 1. spec.terminationGracePeriodSeconds is used if unset.", } func (Probe) SwaggerDoc() map[string]string {