Skip to content

Commit

Permalink
feat(secret): convert a secret to an EnvValue (#1643) (#1647)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `k8s-25/main` to `k8s-23/main`:
 - [feat(secret): convert a secret to an `EnvValue` (#1643)](#1643)



### Questions ?
Please refer to the [Backport tool documentation](https://github.com/sqren/backport)
  • Loading branch information
cdk8s-automation committed Jan 25, 2023
1 parent 3e45b3c commit 6667f94
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/secret.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}

}

/**
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down
30 changes: 30 additions & 0 deletions test/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 6667f94

Please sign in to comment.