Repository: cdk8s-team/cdk8s-plus
Pull request #1665 (2023-01-30 08:29:51)

fix(secret): metadata not fully passed to super in child classes
Fixes https://github.com/cdk8s-team/cdk8s-plus/issues/1664


Patch:
diff --git a/src/secret.ts b/src/secret.ts
index eb98247d..07c2da00 100644
--- a/src/secret.ts
+++ b/src/secret.ts
@@ -199,6 +199,7 @@ export class BasicAuthSecret extends Secret {
         password: props.password,
       },
       immutable: props.immutable,
+      metadata: props.metadata,
     });
   }
 }
@@ -226,6 +227,7 @@ export class SshAuthSecret extends Secret {
         'ssh-privatekey': props.sshPrivateKey,
       },
       immutable: props.immutable,
+      metadata: props.metadata,
     });
   }
 }
@@ -249,13 +251,11 @@ export class ServiceAccountTokenSecret extends Secret {
   public constructor(scope: Construct, id: string, props: ServiceAccountTokenSecretProps) {
     super(scope, id, {
       type: 'kubernetes.io/service-account-token',
-      metadata: {
-        annotations: {
-          'kubernetes.io/service-account.name': props.serviceAccount.name,
-        },
-      },
+      metadata: props.metadata,
       immutable: props.immutable,
     });
+
+    this.metadata.addAnnotation('kubernetes.io/service-account.name', props.serviceAccount.name);
   }
 }
 
@@ -288,6 +288,7 @@ export class TlsSecret extends Secret {
         'tls.key': props.tlsKey,
       },
       immutable: props.immutable,
+      metadata: props.metadata,
     });
   }
 }
@@ -319,6 +320,7 @@ export class DockerConfigSecret extends Secret {
         '.dockerconfigjson': JSON.stringify(props.data),
       },
       immutable: props.immutable,
+      metadata: props.metadata,
     });
   }
 }


Test patch:
diff --git a/test/secret.test.ts b/test/secret.test.ts
index 80cc5d1b..f8724d70 100644
--- a/test/secret.test.ts
+++ b/test/secret.test.ts
@@ -108,6 +108,25 @@ Array [
 `);
 });
 
+test('can override the name of a basic auth secret', () => {
+
+  const chart = Testing.chart();
+
+  const secret = new kplus.BasicAuthSecret(chart, 'BasicAuthSecret', {
+    metadata: {
+      name: 'override-name',
+    },
+    username: 'admin',
+    password: 't0p-Secret',
+  });
+
+  const manifest = Testing.synth(chart);
+
+  expect(secret.name).toEqual('override-name');
+  expect(manifest[0].metadata.name).toEqual('override-name');
+
+});
+
 test('Can create an ssh auth secret', () => {
   const chart = Testing.chart();
 
@@ -133,6 +152,24 @@ Array [
 `);
 });
 
+test('can override the name of an ssh auth secret', () => {
+
+  const chart = Testing.chart();
+
+  const secret = new kplus.SshAuthSecret(chart, 'SshAuthSecret', {
+    metadata: {
+      name: 'override-name',
+    },
+    sshPrivateKey: 'fake-private-key',
+  });
+
+  const manifest = Testing.synth(chart);
+
+  expect(secret.name).toEqual('override-name');
+  expect(manifest[0].metadata.name).toEqual('override-name');
+
+});
+
 test('Can create a service account token secret', () => {
   const chart = Testing.chart();
 
@@ -173,6 +210,50 @@ Array [
 `);
 });
 
+test('can override the name of a service account token secret', () => {
+
+  const chart = Testing.chart();
+
+  const sa = new kplus.ServiceAccount(chart, 'ServiceAccount');
+  const secret = new kplus.ServiceAccountTokenSecret(chart, 'ServiceAccountToken', {
+    metadata: {
+      name: 'override-name',
+    },
+    serviceAccount: sa,
+  });
+  secret.addStringData('extra', 'foo');
+
+  const manifest = Testing.synth(chart);
+
+  expect(secret.name).toEqual('override-name');
+  expect(manifest[1].metadata.name).toEqual('override-name');
+
+});
+
+test('can add annotations to a service account token secret', () => {
+
+  const chart = Testing.chart();
+
+  const sa = new kplus.ServiceAccount(chart, 'ServiceAccount');
+  const secret = new kplus.ServiceAccountTokenSecret(chart, 'ServiceAccountToken', {
+    metadata: {
+      annotations: {
+        'cdk8s.io/hello': 'world',
+      },
+    },
+    serviceAccount: sa,
+  });
+  secret.addStringData('extra', 'foo');
+
+  const manifest = Testing.synth(chart);
+
+  expect(manifest[1].metadata.annotations).toStrictEqual({
+    'kubernetes.io/service-account.name': 'test-serviceaccount-c8f15383',
+    'cdk8s.io/hello': 'world',
+  });
+
+});
+
 test('Can create a TLS secret', () => {
   const chart = Testing.chart();
 
@@ -200,6 +281,25 @@ Array [
 `);
 });
 
+test('can override the name of a tls secret', () => {
+
+  const chart = Testing.chart();
+
+  const secret = new kplus.TlsSecret(chart, 'TlsSecret', {
+    metadata: {
+      name: 'override-name',
+    },
+    tlsCert: 'tls-cert-value',
+    tlsKey: 'tls-key-value',
+  });
+
+  const manifest = Testing.synth(chart);
+
+  expect(secret.name).toEqual('override-name');
+  expect(manifest[0].metadata.name).toEqual('override-name');
+
+});
+
 test('Can create a Docker config secret', () => {
   const chart = Testing.chart();
 
@@ -234,6 +334,33 @@ Array [
 `);
 });
 
+test('can override the name of a DockerConfig secret', () => {
+
+  const chart = Testing.chart();
+
+  const secret = new kplus.DockerConfigSecret(chart, 'DockerConfigSecret', {
+    metadata: {
+      name: 'override-name',
+    },
+    data: {
+      auths: {
+        'hub.xxx.com': {
+          username: 'xxx',
+          password: 'xxx',
+          email: 'xxx',
+          auth: 'xxx',
+        },
+      },
+    },
+  });
+
+  const manifest = Testing.synth(chart);
+
+  expect(secret.name).toEqual('override-name');
+  expect(manifest[0].metadata.name).toEqual('override-name');
+
+});
+
 test('default immutability', () => {
 
   const chart = Testing.chart();