Skip to content

Commit

Permalink
refine PDB and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens committed Jun 27, 2024
1 parent 8ec83d7 commit 3c07f1c
Show file tree
Hide file tree
Showing 19 changed files with 496 additions and 115 deletions.
2 changes: 0 additions & 2 deletions .gitignore

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

4 changes: 4 additions & 0 deletions .projen/deps.json

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

4 changes: 2 additions & 2 deletions .projen/tasks.json

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

1 change: 1 addition & 0 deletions .projenrc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const project = new cdk.JsiiProject({
description: 'Provides a CMS Plone Backend and Frontend for Kubernetes with cdk8s',
deps: [
'cdk8s',
'cdk8s-plus-24',
'constructs',
],
peerDeps: [
Expand Down
1 change: 1 addition & 0 deletions package.json

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

6 changes: 3 additions & 3 deletions src/backend-deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface PloneBackendDeploymentOptions {
* If given
* @default - none
*/
readonly pdbOptions?: PlonePDBOptions;
readonly pdb?: PlonePDBOptions;
}

export class PloneBackendDeployment extends Construct {
Expand Down Expand Up @@ -73,8 +73,8 @@ export class PloneBackendDeployment extends Construct {

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

if (options.pdbOptions ?? false) {
const pdbOptions = options.pdbOptions ?? {};
if (options.pdb ?? false) {
const pdbOptions = options.pdb ?? {};
new PlonePDB(this, 'pdb', label, pdbOptions);
}
}
Expand Down
81 changes: 0 additions & 81 deletions src/backend-frontend.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/frontend-deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface PloneFrontendDeploymentOptions {
* If given
* @default - none
*/
readonly pdbOptions?: PlonePDBOptions;
readonly pdb?: PlonePDBOptions;
}

export class PloneFrontendDeployment extends Construct {
Expand Down Expand Up @@ -74,9 +74,9 @@ export class PloneFrontendDeployment extends Construct {
new k8s.KubeDeployment(this, 'deployment', deploymentOptions);


if (options.pdbOptions ?? false) {
const pdbOptions = options.pdbOptions ?? {};
new PlonePDB(this, id + '-pdb', label, pdbOptions);
if (options.pdb ?? false) {
const pdbOptions = options.pdb ?? {};
new PlonePDB(this, 'pdb', label, pdbOptions);
}
}
}
48 changes: 32 additions & 16 deletions src/pdb.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { Construct } from 'constructs';
import { IntOrString, KubePodDisruptionBudget } from './imports/k8s';
import { IntOrString, KubePodDisruptionBudget, PodDisruptionBudgetSpec } from './imports/k8s';

export interface PlonePDBOptions {
/**
Expand All @@ -11,6 +11,7 @@ export interface PlonePDBOptions {

/**
* minAvailable specification.
* default only set if maxUnavailable is not set.
* @default 1
*/
readonly minAvailable?: number | string;
Expand All @@ -27,28 +28,43 @@ export class PlonePDB extends Construct {
constructor(scope: Construct, id: string, selectorLabel: { [name: string]: string }, options: PlonePDBOptions) {
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 spec: PodDisruptionBudgetSpec = {};
if (typeof options.maxUnavailable === 'number') {
spec = {
maxUnavailable: IntOrString.fromNumber(options.maxUnavailable as number),
};
} else if (typeof options.maxUnavailable === 'string') {
spec = {
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);
if (typeof options.minAvailable === 'number') {
spec = {
...spec,
minAvailable: IntOrString.fromNumber(options.minAvailable as number),
};
} else if (typeof options.minAvailable === 'string') {
spec = {
...spec,
minAvailable: IntOrString.fromString(options.minAvailable as string),
};
}
if (options.maxUnavailable === undefined && options.minAvailable === undefined) {
spec = {
minAvailable: IntOrString.fromNumber(1),
};
}

spec = {
...spec,
selector: { matchLabels: selectorLabel },
};

new KubePodDisruptionBudget(this, 'PDB', {
metadata: {
labels: options.labels ?? {},
},
spec: {
selector: { matchLabels: selectorLabel },
maxUnavailable: maxUnavailable,
minAvailable: minAvailable,
},
spec: spec,
});
}
}
20 changes: 20 additions & 0 deletions src/plone.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { Construct } from 'constructs';
import { PloneBackend, PloneBackendOptions } from './backend';
import { PloneFrontend, PloneFrontendOptions } from './frontend';

export interface PloneOptions {
readonly frontend?: PloneFrontendOptions;
readonly backend?: PloneBackendOptions;
}

export class Plone extends Construct {

constructor(scope: Construct, id: string, options: PloneOptions = {}) {
super(scope, id);
const frontendOptions = options.frontend ?? {};
const backendOptions = options.backend ?? {};
new PloneBackend(this, 'backend', backendOptions);
new PloneFrontend(this, 'frontend', frontendOptions);
}
}
3 changes: 1 addition & 2 deletions test/__snapshots__/backend.test.ts.snap

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

5 changes: 2 additions & 3 deletions test/__snapshots__/frontend.test.ts.snap

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

82 changes: 82 additions & 0 deletions test/__snapshots__/pdb.test.ts.snap

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

Loading

0 comments on commit 3c07f1c

Please sign in to comment.