Repository: cdk8s-team/cdk8s-plus
Pull request #1643 (2023-01-24 12:54:15)

feat(secret): convert a secret to an `EnvValue`
Signed-off-by: Yair Fried <5310525+yfried@users.noreply.github.com>

Add a shorter and more OO way to extract secret value as env

```typescript
env={"VARNAME": kplus.EnvValue.fromSecretValue({secret: mysecret, key: "mykey"}), ...}
```
```python
env={"VARNAME": kplus.EnvValue.from_secret_value(
                                       secret_value=kplus.SecretValue(secret=mysecret,
                                                                      key="mykey")), ...}
```

do 

```typescript
env={"VARNAME": mysecret.envValue("mykey"), ...}
```

```python
env={"VARNAME": mysecret.env_value(key="mykey"), ...}
```


iliapolo: @Mergifyio refresh
mergify[bot]: > refresh

#### ✅ Pull request refreshed



<!---
DO NOT EDIT
-*- Mergify Payload -*-
{"command": "refresh", "conclusion": "success"}
-*- Mergify Payload End -*-
-->

Patch:
diff --git a/src/secret.ts b/src/secret.ts
index 872cdd4f..eb98247d 100644
--- a/src/secret.ts
+++ b/src/secret.ts
@@ -1,6 +1,7 @@
 import { ApiObject } from 'cdk8s';
 import { Construct } from 'constructs';
 import * as base from './base';
+import { EnvValue, EnvValueFromSecretOptions } from './container';
 import * as k8s from './imports/k8s';
 import * as serviceaccount from './service-account';
 
@@ -42,7 +43,12 @@ export interface SecretProps extends CommonSecretProps {
 }
 
 export interface ISecret extends base.IResource {
-
+  /**
+   * Returns EnvValue object from a secret's key.
+   * @param key Secret's key
+   * @param options Additional EnvValue options
+   */
+  envValue(key: string, options?: EnvValueFromSecretOptions): EnvValue;
 }
 
 /**
@@ -91,6 +97,10 @@ class ImportedSecret extends Construct implements ISecret {
     return this.name;
   }
 
+  public envValue(key: string, options?: EnvValueFromSecretOptions): EnvValue {
+    return EnvValue.fromSecretValue({ secret: this, key }, options);
+  }
+
 }
 
 /**
@@ -154,6 +164,10 @@ export class Secret extends base.Resource implements ISecret {
   public getStringData(key: string): string | undefined {
     return this.stringData[key];
   }
+
+  public envValue(key: string, options?: EnvValueFromSecretOptions): EnvValue {
+    return EnvValue.fromSecretValue({ secret: this, key }, options);
+  }
 }
 
 /**


Test patch:
diff --git a/test/container.test.ts b/test/container.test.ts
index 97429f1d..331b6fd5 100644
--- a/test/container.test.ts
+++ b/test/container.test.ts
@@ -47,6 +47,36 @@ describe('EnvValue', () => {
     });
   });
 
+  test('Can be created from ISecret.envValue', () => {
+    const chart = Testing.chart();
+
+    const actual = kplus.Secret.fromSecretName(chart, 'Secret', 'Secret').envValue('my-key', { optional: false });
+
+    expect(actual.value).toBeUndefined();
+    expect(actual.valueFrom).toEqual({
+      secretKeyRef: {
+        key: 'my-key',
+        name: 'Secret',
+        optional: false,
+      },
+    });
+  });
+
+  test('Can be created from new secret.envValue', () => {
+    const chart = Testing.chart();
+
+    const actual = new kplus.Secret(chart, 'Secret', { stringData: { 'my-key': 'my-value' } }).envValue('my-key', { optional: true });
+
+    expect(actual.value).toBeUndefined();
+    expect(actual.valueFrom).toEqual({
+      secretKeyRef: {
+        key: 'my-key',
+        name: 'test-secret-c837fa76',
+        optional: true,
+      },
+    });
+  });
+
   test('Cannot be created from missing required process env', () => {
 
     const key = 'cdk8s-plus.tests.container.env.fromProcess';