Skip to content

Commit

Permalink
Merge pull request #103245 from wzshiming/fix/prober-termination
Browse files Browse the repository at this point in the history
Add validation for Prober TerminationGracePeriodSeconds
  • Loading branch information
k8s-ci-robot committed Jul 13, 2021
2 parents 234d731 + 513bd93 commit e375563
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 2 deletions.
2 changes: 1 addition & 1 deletion api/openapi-spec/swagger.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/apis/core/validation/validation.go
Expand Up @@ -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
}

Expand Down
127 changes: 127 additions & 0 deletions pkg/apis/core/validation/validation_test.go
Expand Up @@ -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"}}},
Expand Down
1 change: 1 addition & 0 deletions staging/src/k8s.io/api/core/v1/generated.proto

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions staging/src/k8s.io/api/core/v1/types.go
Expand Up @@ -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"`
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e375563

Please sign in to comment.