Skip to content

Commit

Permalink
fix labels, names and selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
jensens committed Jun 17, 2024
1 parent ac2ba60 commit 9f682fb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 23 deletions.
10 changes: 4 additions & 6 deletions src/backend-deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,26 @@ export class PloneBackendDeployment extends Construct {
const image = options.image ?? 'plone/plone-backend:latest';
const replicas = options.replicas ?? 2;
const label = { app: Names.toLabelValue(this) };
const labels = {
const template_labels = {
...options.labels ?? {},
...label,
};
const deployment_name = id + '-deployment';
const pod_name = id + '-pod';

const deploymentOpts: k8s.KubeDeploymentProps = {
metadata: {
name: deployment_name,
labels: options.labels ?? {},
},
spec: {
replicas,
selector: {
matchLabels: label,
},
template: {
metadata: { labels: labels },
metadata: { labels: template_labels },
spec: {
containers: [
{
name: pod_name,
name: id + '-container', // here the namespaced name shold be used, but how?
image: image,
},
],
Expand Down
14 changes: 9 additions & 5 deletions src/backend-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ export interface PloneBackendServiceOptions {
*/
readonly targetPort?: number;

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

/**
* Extra labels to associate with resources.
* @default - none
Expand All @@ -24,23 +29,22 @@ export interface PloneBackendServiceOptions {

export class PloneBackendService extends Construct {

constructor(scope: Construct, id: string, options: PloneBackendServiceOptions = {}) {
constructor(scope: Construct, id: string, options: PloneBackendServiceOptions) {
super(scope, id);

const port = options.port ?? 8080;
const targetPort = IntOrString.fromNumber(options.targetPort ?? 8080);
const labels = options.labels ?? {};
const name = id + '-service';
const selector_label = options.selector_label;

const serviceOpts: KubeServiceProps = {
metadata: {
name: name,
labels: options.labels ?? {},
},
spec: {
type: 'ClusterIP',
clusterIp: 'None',
ports: [{ port: port, targetPort: targetPort }],
selector: labels,
selector: selector_label,
},
};
new KubeService(this, 'service', serviceOpts);
Expand Down
12 changes: 8 additions & 4 deletions src/backend.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Names } from 'cdk8s';
// eslint-disable-next-line import/no-extraneous-dependencies
import { Construct } from 'constructs';
import { PloneBackendDeploymentOptions, PloneBackendDeployment } from './backend-deployment';
Expand All @@ -12,13 +13,16 @@ export class PloneBackend extends Construct {

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

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

// Create a service
new PloneBackendService(this, 'service', service);
const serviceOptions = {
...options.service ?? {},
selector_label: { app: Names.toLabelValue(deployment) },
};
new PloneBackendService(this, 'service', serviceOptions);
}
}
14 changes: 8 additions & 6 deletions test/__snapshots__/backend.test.ts.snap

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

4 changes: 2 additions & 2 deletions test/backend.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { PloneBackend } from '../src/backend';
test('defaults', () => {
// GIVEN
const app = Testing.app();
const chart = new Chart(app, 'test');
const chart = new Chart(app, 'plone');

// WHEN
new PloneBackend(chart, 'plone');
new PloneBackend(chart, 'backend');

// THEN
expect(Testing.synth(chart)).toMatchSnapshot();
Expand Down

0 comments on commit 9f682fb

Please sign in to comment.