mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-01 15:58:37 +00:00
Merge pull request #103245 from wzshiming/fix/prober-termination
Add validation for Prober TerminationGracePeriodSeconds
This commit is contained in:
commit
e375563732
2
api/openapi-spec/swagger.json
generated
2
api/openapi-spec/swagger.json
generated
@ -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"
|
||||
},
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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"}}},
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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"`
|
||||
}
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user