Skip to content

Commit

Permalink
fix(pod): allow sidecar containers to have probes (#4483) (#4575)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `k8s-30/main` to `k8s-29/main`:
 - [fix(pod): allow sidecar containers to have probes (#4483)](#4483)



### Questions ?
Please refer to the [Backport tool documentation](https://github.com/sqren/backport)
  • Loading branch information
cdk8s-automation authored Aug 27, 2024
1 parent bcd98f9 commit 36edb94
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/pod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,15 @@ export abstract class AbstractPod extends base.Resource implements IPodSelector,
public addInitContainer(cont: container.ContainerProps): container.Container {

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

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

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

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

// Any initContainer that has `restartPolicy=Always` is a sidecar container. Please refer to
// documentation for more details:
// https://kubernetes.io/docs/concepts/workloads/pods/sidecar-containers/#differences-from-init-containers
private isSidecarContainer(cont: container.ContainerProps) {
return cont.restartPolicy === container.ContainerRestartPolicy.ALWAYS;
}

public addHostAlias(hostAlias: HostAlias): void {
this._hostAliases.push(hostAlias);
}
Expand Down
39 changes: 39 additions & 0 deletions test/pod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,45 @@ test('init containers cannot have startup probe', () => {

});

test('sidecar containers can have liveness probe', () => {

const chart = Testing.chart();
const pod = new kplus.Pod(chart, 'Pod', { containers: [{ image: 'image' }] });

pod.addInitContainer({ image: 'image', liveness: Probe.fromTcpSocket(), restartPolicy: ContainerRestartPolicy.ALWAYS });

const spec = Testing.synth(chart)[0].spec;

expect(spec.initContainers[0].livenessProbe).toBeTruthy();

});

test('sidecar containers can have readiness probe', () => {

const chart = Testing.chart();
const pod = new kplus.Pod(chart, 'Pod', { containers: [{ image: 'image' }] });

pod.addInitContainer({ image: 'image', readiness: Probe.fromTcpSocket(), restartPolicy: ContainerRestartPolicy.ALWAYS });

const spec = Testing.synth(chart)[0].spec;

expect(spec.initContainers[0].readinessProbe).toBeTruthy();

});

test('sidecar containers can have startup probe', () => {

const chart = Testing.chart();
const pod = new kplus.Pod(chart, 'Pod', { containers: [{ image: 'image' }] });

pod.addInitContainer({ image: 'image', startup: Probe.fromTcpSocket(), restartPolicy: ContainerRestartPolicy.ALWAYS });

const spec = Testing.synth(chart)[0].spec;

expect(spec.initContainers[0].startupProbe).toBeTruthy();

});

test('can specify init containers at instantiation', () => {

const chart = Testing.chart();
Expand Down

0 comments on commit 36edb94

Please sign in to comment.