Skip to content

Commit

Permalink
Merge pull request #121 from reactiveops/rs/init-exception
Browse files Browse the repository at this point in the history
Ensuring that readiness probes in init containers are not validated
  • Loading branch information
robscott authored May 20, 2019
2 parents 7b29a87 + f5c7087 commit df93a14
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
10 changes: 7 additions & 3 deletions pkg/validator/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ import (
// ContainerValidation tracks validation failures associated with a Container.
type ContainerValidation struct {
*ResourceValidation
Container *corev1.Container
Container *corev1.Container
IsInitContainer bool
}

// ValidateContainer validates that each pod conforms to the Polaris config, returns a ResourceResult.
func ValidateContainer(cnConf *conf.Configuration, container *corev1.Container) ContainerResult {
func ValidateContainer(cnConf *conf.Configuration, container *corev1.Container, isInit bool) ContainerResult {
cv := ContainerValidation{
Container: container,
ResourceValidation: &ResourceValidation{},
IsInitContainer: isInit,
}

cv.validateResources(&cnConf.Resources)
Expand Down Expand Up @@ -107,7 +109,9 @@ func (cv *ContainerValidation) validateResourceRange(resourceName string, rangeC

func (cv *ContainerValidation) validateHealthChecks(conf *conf.HealthChecks) {
category := messages.CategoryHealthChecks
if conf.ReadinessProbeMissing.IsActionable() {

// Don't validate readiness probes on init containers
if !cv.IsInitContainer && conf.ReadinessProbeMissing.IsActionable() {
if cv.Container.ReadinessProbe == nil {
cv.addFailure(messages.ReadinessProbeFailure, conf.ReadinessProbeMissing, category)
} else {
Expand Down
20 changes: 13 additions & 7 deletions pkg/validator/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,11 +222,16 @@ func TestValidateHealthChecks(t *testing.T) {
}

probe := corev1.Probe{}
cv1 := ContainerValidation{
emptyCV := ContainerValidation{
Container: &corev1.Container{Name: ""},
ResourceValidation: &ResourceValidation{},
}
cv2 := ContainerValidation{
emptyCVInit := ContainerValidation{
Container: &corev1.Container{Name: ""},
ResourceValidation: &ResourceValidation{},
IsInitContainer: true,
}
goodCV := ContainerValidation{
Container: &corev1.Container{
Name: "",
LivenessProbe: &probe,
Expand All @@ -248,11 +253,12 @@ func TestValidateHealthChecks(t *testing.T) {
errors *[]*ResultMessage
warnings *[]*ResultMessage
}{
{name: "probes not configured", probes: p1, cv: cv1, errors: &f1},
{name: "probes not required", probes: p2, cv: cv1, errors: &f1},
{name: "probes required & configured", probes: p3, cv: cv2, errors: &f1},
{name: "probes required & not configured", probes: p3, cv: cv1, errors: &f2, warnings: &w1},
{name: "probes configured, but not required", probes: p2, cv: cv2, errors: &f1},
{name: "probes not configured", probes: p1, cv: emptyCV, errors: &f1},
{name: "probes not required", probes: p2, cv: emptyCV, errors: &f1},
{name: "probes required & configured", probes: p3, cv: goodCV, errors: &f1},
{name: "probes required, not configured, but init", probes: p3, cv: emptyCVInit, errors: &f1},
{name: "probes required & not configured", probes: p3, cv: emptyCV, errors: &f2, warnings: &w1},
{name: "probes configured, but not required", probes: p2, cv: goodCV, errors: &f1},
}

for _, tt := range testCases {
Expand Down
8 changes: 4 additions & 4 deletions pkg/validator/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func ValidatePod(podConf conf.Configuration, pod *corev1.PodSpec) PodResult {
Summary: pv.summary(),
}

pv.validateContainers(pod.InitContainers, &pRes, &podConf)
pv.validateContainers(pod.Containers, &pRes, &podConf)
pv.validateContainers(pod.InitContainers, &pRes, &podConf, true)
pv.validateContainers(pod.Containers, &pRes, &podConf, false)

for _, cRes := range pRes.ContainerResults {
pRes.Summary.appendResults(*cRes.Summary)
Expand All @@ -52,9 +52,9 @@ func ValidatePod(podConf conf.Configuration, pod *corev1.PodSpec) PodResult {
return pRes
}

func (pv *PodValidation) validateContainers(containers []corev1.Container, pRes *PodResult, podConf *conf.Configuration) {
func (pv *PodValidation) validateContainers(containers []corev1.Container, pRes *PodResult, podConf *conf.Configuration, isInit bool) {
for _, container := range containers {
cRes := ValidateContainer(podConf, &container)
cRes := ValidateContainer(podConf, &container, isInit)
pRes.ContainerResults = append(pRes.ContainerResults, cRes)
}
}
Expand Down

0 comments on commit df93a14

Please sign in to comment.