Skip to content

Commit

Permalink
add frontend and refactor pdb to genric use
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens committed Jun 26, 2024
1 parent ef341ab commit 8ec83d7
Show file tree
Hide file tree
Showing 12 changed files with 501 additions and 25 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Each step need to be implemented with tests!

- [ ] Start Backend
- [ ] deployment
- [ ] depend on "some" postgres db - which can be provided in different ways
- [ ] service
- [ ] pdb
- [ ] init container running plone-site-create
Expand All @@ -41,6 +42,7 @@ Each step need to be implemented with tests!
- [ ] (optional) direct way to specify logging sidecar (fluentd/loki?)
- [ ] Start Frontend
- [ ] deployment
- [ ] depend on ready/live backend
- [ ] service
- [ ] pdb
- [ ] lifecycle checks (readiness, liveness)
Expand All @@ -51,13 +53,15 @@ Each step need to be implemented with tests!

- [ ] Start Varnish
- [ ] deployment
- [ ] do not depend on backend/front end to be up, but configure to deliver from cache if possible.
- [ ] service
- [ ] pdb
- [ ] lifecycle checks (readiness, liveness)
- [ ] sidecars
- [ ] generic way to inject sidecars
- [ ] (optional) direct way to specify metrics sidecar (prometheus exporter)
- [ ] (optional) direct way to specify logging sidecar (fluentd/loki?)
- find a way to purge caches. based on kitconcept varnish purger? needs

- [ ] Other Languages
- [ ] Check Python distribution
Expand Down
16 changes: 6 additions & 10 deletions src/backend-deployment.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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';
import { PlonePDB, PlonePDBOptions } from './pdb';

export interface PloneBackendDeploymentOptions {
/**
Expand Down Expand Up @@ -34,12 +34,12 @@ export interface PloneBackendDeploymentOptions {
* If given
* @default - none
*/
readonly pdbOptions?: PloneBackendPDBOptions;
readonly pdbOptions?: PlonePDBOptions;
}

export class PloneBackendDeployment extends Construct {

constructor(scope: Construct, id: string, options: PloneBackendDeploymentOptions = { }) {
constructor(scope: Construct, id: string, options: PloneBackendDeploymentOptions = {}) {
super(scope, id);
const image = options.image ?? 'plone/plone-backend:latest';
const replicas = options.replicas ?? 2;
Expand All @@ -48,7 +48,6 @@ export class PloneBackendDeployment extends Construct {
...options.labels ?? {},
...label,
};
const pdb = options.pdbOptions ?? true;
const deploymentOptions: k8s.KubeDeploymentProps = {
metadata: {
labels: options.labels ?? {},
Expand All @@ -74,12 +73,9 @@ export class PloneBackendDeployment extends Construct {

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

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

export interface PloneBackendDeploymentOptions {
/**
* Specify a custom image for Plone Backend.
* @default "plone/plone-backend:latest"
*/
readonly image?: string;

/**
* Number of replicas.
* @default 2
*/
readonly replicas?: number;

/**
* Port number.
* @default 8080
*/
readonly port?: number;

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

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

export class PloneBackendDeployment extends Construct {

constructor(scope: Construct, id: string, options: PloneBackendDeploymentOptions = {}) {
super(scope, id);
const image = options.image ?? 'plone/plone-backend:latest';
const replicas = options.replicas ?? 2;
const label = { app: Names.toLabelValue(this) };
const template_labels = {
...options.labels ?? {},
...label,
};
const deploymentOptions: k8s.KubeDeploymentProps = {
metadata: {
labels: options.labels ?? {},
},
spec: {
replicas,
selector: {
matchLabels: label,
},
template: {
metadata: { labels: template_labels },
spec: {
containers: [
{
name: id + '-container', // here the namespaced name shold be used, but how?
image: image,
},
],
},
},
},
};

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

if (options.pdbOptions ?? false) {
const pdbOptions = options.pdbOptions ?? {};
new PlonePDB(this, 'pdb', label, pdbOptions);
}
}
}
82 changes: 82 additions & 0 deletions src/frontend-deployment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Names } from 'cdk8s';
// eslint-disable-next-line import/no-extraneous-dependencies
import { Construct } from 'constructs';
import * as k8s from './imports/k8s';
import { PlonePDB, PlonePDBOptions } from './pdb';

export interface PloneFrontendDeploymentOptions {
/**
* Specify a custom image for Plone Frontend.
* @default "plone/plone-frontend:latest"
*/
readonly image?: string;

/**
* Number of replicas.
* @default 2
*/
readonly replicas?: number;

/**
* Port number.
* @default 8080
*/
readonly port?: number;

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

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

export class PloneFrontendDeployment extends Construct {

constructor(scope: Construct, id: string, options: PloneFrontendDeploymentOptions = {}) {
super(scope, id);
const image = options.image ?? 'plone/plone-backend:latest';
const replicas = options.replicas ?? 2;
const label = { app: Names.toLabelValue(this) };
const template_labels = {
...options.labels ?? {},
...label,
};
const deploymentOptions: k8s.KubeDeploymentProps = {
metadata: {
labels: options.labels ?? {},
},
spec: {
replicas,
selector: {
matchLabels: label,
},
template: {
metadata: { labels: template_labels },
spec: {
containers: [
{
name: id + '-container', // here the namespaced name shold be used, but how?
image: image,
},
],
},
},
},
};

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


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

export interface PloneFrontendServiceOptions {
/**
* Port number.
* @default 3000
*/
readonly port?: number;

/**
* Port number.
* @default 3000;
*/
readonly targetPort?: number;

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

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

export class PloneFrontendService extends Construct {

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

const port = options.port ?? 3000;
const targetPort = IntOrString.fromNumber(options.targetPort ?? 3000);
const selectorLabel = options.selectorLabel;

const serviceOpts: KubeServiceProps = {
metadata: {
labels: options.labels ?? {},
},
spec: {
type: 'ClusterIP',
clusterIp: 'None',
ports: [{ port: port, targetPort: targetPort }],
selector: selectorLabel,
},
};
new KubeService(this, 'service', serviceOpts);
}
}
28 changes: 28 additions & 0 deletions src/frontend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Names } from 'cdk8s';
// eslint-disable-next-line import/no-extraneous-dependencies
import { Construct } from 'constructs';
import { PloneFrontendDeploymentOptions, PloneFrontendDeployment } from './frontend-deployment';
import { PloneFrontendServiceOptions, PloneFrontendService } from './frontend-service';

export interface PloneFrontendOptions {
readonly deployment?: PloneFrontendDeploymentOptions;
readonly service?: PloneFrontendServiceOptions;
}

export class PloneFrontend extends Construct {

constructor(scope: Construct, id: string, options: PloneFrontendOptions = {}) {
super(scope, id);
const deploymentOptions = options.deployment ?? {};

// Create a deployment
const deployment = new PloneFrontendDeployment(this, 'deployment', deploymentOptions);

// Create a service
const serviceOptions = {
...options.service ?? {},
selectorLabel: { app: Names.toLabelValue(deployment) },
};
new PloneFrontendService(this, 'service', serviceOptions);
}
}
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export { PlonePDBOptions, PlonePDB } from './pdb';
export { PloneBackendDeploymentOptions, PloneBackendDeployment } from './backend-deployment';
export { PloneBackendPDBOptions, PloneBackendPDB } from './backend-pdb';
export { PloneBackendServiceOptions, PloneBackendService } from './backend-service';
export { PloneBackendOptions, PloneBackend } from './backend';
export { PloneFrontendDeploymentOptions, PloneFrontendDeployment } from './frontend-deployment';
export { PloneFrontendServiceOptions, PloneFrontendService } from './frontend-service';
export { PloneFrontendOptions, PloneFrontend } from './frontend';
13 changes: 4 additions & 9 deletions src/backend-pdb.ts → src/pdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Construct } from 'constructs';
import { IntOrString, KubePodDisruptionBudget } from './imports/k8s';

export interface PloneBackendPDBOptions {
export interface PlonePDBOptions {
/**
* maxUnavailable specification
* @default - none
Expand All @@ -15,21 +15,16 @@ export interface PloneBackendPDBOptions {
*/
readonly minAvailable?: number | string;

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

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

export class PloneBackendPDB extends Construct {
export class PlonePDB extends Construct {

constructor(scope: Construct, id: string, options: PloneBackendPDBOptions) {
constructor(scope: Construct, id: string, selectorLabel: { [name: string]: string }, options: PlonePDBOptions) {
super(scope, id);

var maxUnavailable: IntOrString = IntOrString.fromString(''); // default value
Expand All @@ -50,7 +45,7 @@ export class PloneBackendPDB extends Construct {
labels: options.labels ?? {},
},
spec: {
selector: { matchLabels: options.selectorLabel },
selector: { matchLabels: selectorLabel },
maxUnavailable: maxUnavailable,
minAvailable: minAvailable,
},
Expand Down
Loading

0 comments on commit 8ec83d7

Please sign in to comment.