Skip to content

Commit 944ddf5

Browse files
fix(pod): allow sidecar containers to have probes (#4483)
https://kubernetes.io/docs/concepts/workloads/pods/init-containers/#differences-from-regular-containers if we check docs, sidecar containers support readiness, liveness and startup probes. I added an extra check to make sure the init container is not sidecar for probe checks. this way it will be possible to add probes for initcontainers when they are sidecar. edit: this should be backported to k8s-29/main as well Fixes #4338
1 parent 3dc6bfa commit 944ddf5

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

src/pod.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ export abstract class AbstractPod extends base.Resource implements IPodSelector,
122122
public addInitContainer(cont: container.ContainerProps): container.Container {
123123

124124
// https://kubernetes.io/docs/concepts/workloads/pods/init-containers/#differences-from-regular-containers
125-
if (cont.readiness) {
125+
if (!this.isSidecarContainer(cont) && cont.readiness) {
126126
throw new Error('Init containers must not have a readiness probe');
127127
}
128128

129-
if (cont.liveness) {
129+
if (!this.isSidecarContainer(cont) && cont.liveness) {
130130
throw new Error('Init containers must not have a liveness probe');
131131
}
132132

133-
if (cont.startup) {
133+
if (!this.isSidecarContainer(cont) && cont.startup) {
134134
throw new Error('Init containers must not have a startup probe');
135135
}
136136

@@ -143,6 +143,13 @@ export abstract class AbstractPod extends base.Resource implements IPodSelector,
143143
return impl;
144144
}
145145

146+
// Any initContainer that has `restartPolicy=Always` is a sidecar container. Please refer to
147+
// documentation for more details:
148+
// https://kubernetes.io/docs/concepts/workloads/pods/sidecar-containers/#differences-from-init-containers
149+
private isSidecarContainer(cont: container.ContainerProps) {
150+
return cont.restartPolicy === container.ContainerRestartPolicy.ALWAYS;
151+
}
152+
146153
public addHostAlias(hostAlias: HostAlias): void {
147154
this._hostAliases.push(hostAlias);
148155
}

test/pod.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,45 @@ test('init containers cannot have startup probe', () => {
254254

255255
});
256256

257+
test('sidecar containers can have liveness probe', () => {
258+
259+
const chart = Testing.chart();
260+
const pod = new kplus.Pod(chart, 'Pod', { containers: [{ image: 'image' }] });
261+
262+
pod.addInitContainer({ image: 'image', liveness: Probe.fromTcpSocket(), restartPolicy: ContainerRestartPolicy.ALWAYS });
263+
264+
const spec = Testing.synth(chart)[0].spec;
265+
266+
expect(spec.initContainers[0].livenessProbe).toBeTruthy();
267+
268+
});
269+
270+
test('sidecar containers can have readiness probe', () => {
271+
272+
const chart = Testing.chart();
273+
const pod = new kplus.Pod(chart, 'Pod', { containers: [{ image: 'image' }] });
274+
275+
pod.addInitContainer({ image: 'image', readiness: Probe.fromTcpSocket(), restartPolicy: ContainerRestartPolicy.ALWAYS });
276+
277+
const spec = Testing.synth(chart)[0].spec;
278+
279+
expect(spec.initContainers[0].readinessProbe).toBeTruthy();
280+
281+
});
282+
283+
test('sidecar containers can have startup probe', () => {
284+
285+
const chart = Testing.chart();
286+
const pod = new kplus.Pod(chart, 'Pod', { containers: [{ image: 'image' }] });
287+
288+
pod.addInitContainer({ image: 'image', startup: Probe.fromTcpSocket(), restartPolicy: ContainerRestartPolicy.ALWAYS });
289+
290+
const spec = Testing.synth(chart)[0].spec;
291+
292+
expect(spec.initContainers[0].startupProbe).toBeTruthy();
293+
294+
});
295+
257296
test('can specify init containers at instantiation', () => {
258297

259298
const chart = Testing.chart();

0 commit comments

Comments
 (0)