mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-22 11:21:47 +00:00
Merge pull request #4451 from mikedanese/validate-probe
add validation for api.Probe
This commit is contained in:
commit
76edde3c23
@ -352,6 +352,22 @@ func validateVolumeMounts(mounts []api.VolumeMount, volumes util.StringSet) errs
|
||||
return allErrs
|
||||
}
|
||||
|
||||
func validateProbe(probe *api.Probe) errs.ValidationErrorList {
|
||||
allErrs := errs.ValidationErrorList{}
|
||||
|
||||
if probe == nil {
|
||||
return allErrs
|
||||
}
|
||||
allErrs = append(allErrs, validateHandler(&probe.Handler)...)
|
||||
if probe.InitialDelaySeconds < 0 {
|
||||
allErrs = append(allErrs, errs.NewFieldInvalid("initialDelay", probe.InitialDelaySeconds, "may not be less than zero"))
|
||||
}
|
||||
if probe.TimeoutSeconds < 0 {
|
||||
allErrs = append(allErrs, errs.NewFieldInvalid("timeout", probe.TimeoutSeconds, "may not be less than zero"))
|
||||
}
|
||||
return allErrs
|
||||
}
|
||||
|
||||
// AccumulateUniquePorts runs an extraction function on each Port of each Container,
|
||||
// accumulating the results and returning an error if any ports conflict.
|
||||
func AccumulateUniquePorts(containers []api.Container, accumulator map[int]bool, extract func(*api.Port) int) errs.ValidationErrorList {
|
||||
@ -465,6 +481,8 @@ func validateContainers(containers []api.Container, volumes util.StringSet) errs
|
||||
if ctr.Lifecycle != nil {
|
||||
cErrs = append(cErrs, validateLifecycle(ctr.Lifecycle).Prefix("lifecycle")...)
|
||||
}
|
||||
cErrs = append(cErrs, validateProbe(ctr.LivenessProbe).Prefix("livenessProbe")...)
|
||||
cErrs = append(cErrs, validateProbe(ctr.ReadinessProbe).Prefix("readinessProbe")...)
|
||||
cErrs = append(cErrs, validatePorts(ctr.Ports).Prefix("ports")...)
|
||||
cErrs = append(cErrs, validateEnv(ctr.Env).Prefix("env")...)
|
||||
cErrs = append(cErrs, validateVolumeMounts(ctr.VolumeMounts, volumes).Prefix("volumeMounts")...)
|
||||
|
@ -304,6 +304,32 @@ func TestValidateVolumeMounts(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateProbe(t *testing.T) {
|
||||
handler := api.Handler{Exec: &api.ExecAction{Command: []string{"echo"}}}
|
||||
successCases := []*api.Probe{
|
||||
nil,
|
||||
{TimeoutSeconds: 10, InitialDelaySeconds: 0, Handler: handler},
|
||||
{TimeoutSeconds: 0, InitialDelaySeconds: 10, Handler: handler},
|
||||
}
|
||||
for _, p := range successCases {
|
||||
if errs := validateProbe(p); len(errs) != 0 {
|
||||
t.Errorf("expected success: %v", errs)
|
||||
}
|
||||
}
|
||||
|
||||
errorCases := []*api.Probe{
|
||||
{TimeoutSeconds: 10, InitialDelaySeconds: 10},
|
||||
{TimeoutSeconds: 10, InitialDelaySeconds: -10, Handler: handler},
|
||||
{TimeoutSeconds: -10, InitialDelaySeconds: 10, Handler: handler},
|
||||
{TimeoutSeconds: -10, InitialDelaySeconds: -10, Handler: handler},
|
||||
}
|
||||
for _, p := range errorCases {
|
||||
if errs := validateProbe(p); len(errs) == 0 {
|
||||
t.Errorf("expected failure for %v", p)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidatePullPolicy(t *testing.T) {
|
||||
type T struct {
|
||||
Container api.Container
|
||||
|
Loading…
Reference in New Issue
Block a user