Skip to content

Commit

Permalink
add PDB
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens committed Jun 18, 2024
1 parent b83bcd1 commit 1640afc
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/backend-deployment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Names } from 'cdk8s';
// eslint-disable-next-line import/no-extraneous-dependencies
import { Construct } from 'constructs';
import { PloneBackendPDB, PloneBackendPDBOptions } from './backend-pdb';
import * as k8s from './imports/k8s';

export interface PloneBackendDeploymentOptions {
Expand All @@ -27,6 +28,13 @@ export interface PloneBackendDeploymentOptions {
* @default - none
*/
readonly labels?: { [name: string]: string };

/**
* Create a PodDisruptionBugdet for the deployment?
* If given
* @default - none
*/
readonly pdbOptions?: PloneBackendPDBOptions;
}

export class PloneBackendDeployment extends Construct {
Expand All @@ -40,8 +48,8 @@ export class PloneBackendDeployment extends Construct {
...options.labels ?? {},
...label,
};

const deploymentOpts: k8s.KubeDeploymentProps = {
const pdb = options.pdbOptions ?? true;
const deploymentOptions: k8s.KubeDeploymentProps = {
metadata: {
labels: options.labels ?? {},
},
Expand All @@ -64,6 +72,14 @@ export class PloneBackendDeployment extends Construct {
},
};

new k8s.KubeDeployment(this, 'deployment', deploymentOpts);
new k8s.KubeDeployment(this, 'deployment', deploymentOptions);

if (pdb ?? false) {
const pdbOptions = {
...options.pdbOptions ?? {},
selector_label: { app: Names.toLabelValue(this) },
};
new PloneBackendPDB(this, 'pdb', pdbOptions);
}
}
}
59 changes: 59 additions & 0 deletions src/backend-pdb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { Construct } from 'constructs';
import { IntOrString, KubePodDisruptionBudget } from './imports/k8s';

export interface PloneBackendPDBOptions {
/**
* maxUnavailable specification
* @default - none
*/
readonly maxUnavailable?: number | string;

/**
* minAvailable specification.
* @default 1
*/
readonly minAvailable?: number | string;

/**
* Selector label.
*/
readonly selector_label: { [name: string]: string };

/**
* Extra labels to associate with resources.
* @default - none
*/
readonly labels?: { [name: string]: string };
}

export class PloneBackendPDB extends Construct {

constructor(scope: Construct, id: string, options: PloneBackendPDBOptions) {
super(scope, id);

var maxUnavailable: IntOrString = IntOrString.fromString(''); // default value
if (typeof maxUnavailable === 'number') {
maxUnavailable = IntOrString.fromNumber(options.maxUnavailable as number);
} else if (typeof maxUnavailable === 'string') {
maxUnavailable = IntOrString.fromString(options.maxUnavailable as string);
}
var minAvailable: IntOrString = IntOrString.fromString(''); // default value
if (typeof minAvailable === 'number') {
minAvailable = IntOrString.fromNumber(options.minAvailable as number);
} else if (typeof minAvailable === 'string') {
minAvailable = IntOrString.fromString(options.minAvailable as string);
}

new KubePodDisruptionBudget(this, 'PDB', {
metadata: {
labels: options.labels ?? {},
},
spec: {
selector: { matchLabels: options.selector_label },
maxUnavailable: maxUnavailable,
minAvailable: minAvailable,
},
});
}
}
16 changes: 16 additions & 0 deletions test/__snapshots__/backend.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1640afc

Please sign in to comment.