Skip to content

Commit fbed15d

Browse files
authored
Do not alert on missing readiness / liveness probe for init containers (#302)
1 parent ab8a80b commit fbed15d

File tree

5 files changed

+60
-4
lines changed

5 files changed

+60
-4
lines changed

pkg/templates/livenessprobe/template.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func init() {
2424
Parameters: params.ParamDescs,
2525
ParseAndValidateParams: params.ParseAndValidate,
2626
Instantiate: params.WrapInstantiateFunc(func(_ params.Params) (check.Func, error) {
27-
return util.PerContainerCheck(func(container *v1.Container) []diagnostic.Diagnostic {
27+
return util.PerNonInitContainerCheck(func(container *v1.Container) []diagnostic.Diagnostic {
2828
if container.LivenessProbe == nil {
2929
return []diagnostic.Diagnostic{{Message: fmt.Sprintf("container %q does not specify a liveness probe", container.Name)}}
3030
}

pkg/templates/readinessprobe/template.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func init() {
2424
Parameters: params.ParamDescs,
2525
ParseAndValidateParams: params.ParseAndValidate,
2626
Instantiate: params.WrapInstantiateFunc(func(_ params.Params) (check.Func, error) {
27-
return util.PerContainerCheck(func(container *v1.Container) []diagnostic.Diagnostic {
27+
return util.PerNonInitContainerCheck(func(container *v1.Container) []diagnostic.Diagnostic {
2828
if container.ReadinessProbe == nil {
2929
return []diagnostic.Diagnostic{{Message: fmt.Sprintf("container %q does not specify a readiness probe", container.Name)}}
3030
}

pkg/templates/util/per_container_check.go

+18
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,21 @@ func PerContainerCheck(matchFunc func(container *v1.Container) []diagnostic.Diag
2525
return results
2626
}
2727
}
28+
29+
// PerNonInitContainerCheck returns a check that abstracts away some of the boilerplate of writing a check
30+
// that applies to all non-init containers. The given function is passed each non-init container,
31+
// and is allowed to return diagnostics if an error is found.
32+
func PerNonInitContainerCheck(matchFunc func(container *v1.Container) []diagnostic.Diagnostic) check.Func {
33+
return func(_ lintcontext.LintContext, object lintcontext.Object) []diagnostic.Diagnostic {
34+
podSpec, found := extract.PodSpec(object.K8sObject)
35+
if !found {
36+
return nil
37+
}
38+
var results []diagnostic.Diagnostic
39+
containers := podSpec.NonInitContainers()
40+
for i := range containers {
41+
results = append(results, matchFunc(&containers[i])...)
42+
}
43+
return results
44+
}
45+
}

tests/checks/no-liveness-probe.yml

+20-1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,23 @@ spec:
5151
template:
5252
spec:
5353
containers:
54-
- name: app2
54+
- name: app2
55+
---
56+
apiVersion: apps/v1
57+
kind: Deployment
58+
metadata:
59+
name: dont-fire
60+
spec:
61+
template:
62+
spec:
63+
initContainers:
64+
- name: some-container
65+
containers:
66+
- name: app
67+
livenessProbe:
68+
exec:
69+
command:
70+
- cat
71+
- /tmp/healthy
72+
initialDelaySeconds: 5
73+
periodSeconds: 5

tests/checks/no-readiness-probe.yml

+20-1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,23 @@ spec:
5151
template:
5252
spec:
5353
containers:
54-
- name: app
54+
- name: app
55+
---
56+
apiVersion: apps/v1
57+
kind: Deployment
58+
metadata:
59+
name: dont-fire
60+
spec:
61+
template:
62+
spec:
63+
initContainers:
64+
- name: some-container
65+
containers:
66+
- name: app
67+
readinessProbe:
68+
exec:
69+
command:
70+
- cat
71+
- /tmp/healthy
72+
initialDelaySeconds: 5
73+
periodSeconds: 5

0 commit comments

Comments
 (0)