Skip to content

feat(synth): option to specify chart name for helm format #3458

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/cli/cmds/synth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class Command implements yargs.CommandModule {
.option('validation-reports-output-file', { required: false, desc: 'File to write a JSON representation of the validation reports to' })
.option('format', { required: false, desc: 'Synthesis format for Kubernetes manifests. The default synthesis format is plain kubernetes manifests.', type: 'string' })
.option('chart-api-version', { required: false, desc: 'Chart API version of helm chart. The default value would be \'v2\' api version when synthesis format is helm. There is no default set when synthesis format is plain.', type: 'string' })
.option('chart-version', { required: false, desc: 'Chart version of helm chart. This is required if synthesis format is helm.' });
.option('chart-version', { required: false, desc: 'Chart version of helm chart. This is required if synthesis format is helm.' })
.option('chart-name', { required: false, desc: 'Chart name of helm chart. Use when synthesis format is helm. The default is the applications base directory name.' });

public async handler(argv: any) {

Expand All @@ -45,6 +46,7 @@ class Command implements yargs.CommandModule {
const format = argv.format ?? config?.synthConfig?.format ?? SynthesisFormat.PLAIN;
const chartVersion = argv.chartVersion ?? config?.synthConfig?.chartVersion;
const chartApiVersion = argv.chartApiVersion ?? config?.synthConfig?.chartApiVersion ?? getDefaultChartApiVersion(format);
const chartName = argv.chartName ?? config?.synthConfig?.chartName ?? path.basename(path.resolve());

if (outdir && outdir !== config?.output && stdout) {
throw new Error('\'--output\' and \'--stdout\' are mutually exclusive. Please only use one.');
Expand Down Expand Up @@ -104,7 +106,7 @@ class Command implements yargs.CommandModule {
let manifests: SynthesizedApp;

if (format === SynthesisFormat.HELM) {
await createHelmScaffolding(chartApiVersion, chartVersion, outdir);
await createHelmScaffolding(chartApiVersion, chartVersion, chartName, outdir);
const templateDir = path.join(outdir, 'templates');

manifests = await synthApp(command, templateDir, stdout, recordConstructMetadata);
Expand All @@ -130,13 +132,13 @@ async function fetchValidations(): Promise<ValidationConfig[] | undefined> {
}
}

async function createHelmScaffolding(apiVersion: string, chartVersion: string, outdir: string) {
async function createHelmScaffolding(apiVersion: string, chartVersion: string, chartName: string, outdir: string) {
const tempHelmStructure = createFolderStructure();

const substituteValues = {
apiVersion: apiVersion,
version: chartVersion,
app: path.basename(path.resolve()),
app: chartName,
};

try {
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface SynthConfig {
readonly format?: SynthesisFormat;
readonly chartApiVersion?: HelmChartApiVersion;
readonly chartVersion?: string;
readonly chartName?: string;
}

export interface Config {
Expand Down
136 changes: 136 additions & 0 deletions test/synth/__snapshots__/synth-stdout.test.ts.snap

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

114 changes: 114 additions & 0 deletions test/synth/synth-stdout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,118 @@ describe('Helm synthesis', () => {
// This would be run 4 times with test.each
await synth(synthOptions);
});

test.each([
[
withOnlyCliInputs,
{
format: SynthesisFormat.HELM,
chartVersion: '1.1.1',
chartName: 'custom-chart-name',
postSynth: matchSynthSnapshot,
},
],
[
withOnlyConfigInputs,
{
config: {
synthConfig: {
format: SynthesisFormat.HELM,
chartVersion: '1.1.1',
chartName: 'custom-chart-name',
},
},
postSynth: matchSynthSnapshot,
},
],
[
withSameInputsInBoth,
{
format: SynthesisFormat.HELM,
chartVersion: '1.1.1',
chartName: 'custom-chart-name',
config: {
synthConfig: {
format: SynthesisFormat.HELM,
chartVersion: '1.1.1',
chartName: 'custom-chart-name',
},
},
postSynth: matchSynthSnapshot,
},
],
[
withDifferentInputsInBoth,
{
format: SynthesisFormat.HELM,
chartVersion: '1.1.1',
chartName: 'cli-chart-name',
config: {
synthConfig: {
format: SynthesisFormat.HELM,
chartVersion: '1.1.1',
chartName: 'config-chart-name',
},
},
postSynth: matchSynthSnapshot,
},
],
])('--chart-name is used when specified %s', async (_testName, synthOptions) => {
await synth(synthOptions);
});

test.each([
[
withOnlyCliInputs,
{
format: SynthesisFormat.HELM,
chartVersion: '1.1.1',
postSynth: matchSynthSnapshot,
},
],
[
withOnlyConfigInputs,
{
config: {
synthConfig: {
format: SynthesisFormat.HELM,
chartVersion: '1.1.1',
},
},
postSynth: matchSynthSnapshot,
},
],
[
withSameInputsInBoth,
{
format: SynthesisFormat.HELM,
chartVersion: '1.1.1',
config: {
synthConfig: {
format: SynthesisFormat.HELM,
chartVersion: '1.1.1',
},
},
postSynth: matchSynthSnapshot,
},
],
[
withDifferentInputsInBoth,
{
format: SynthesisFormat.HELM,
chartVersion: '1.1.1',
config: {
synthConfig: {
format: SynthesisFormat.PLAIN,
chartVersion: '1.1.1',
},
},
postSynth: matchSynthSnapshot,
},
],
])('default chart name is used when --chart-name is not specified %s', async (_testName, synthOptions) => {
await synth(synthOptions);
});
});

interface SynthCliOptions {
Expand All @@ -1021,6 +1133,7 @@ interface SynthCliOptions {
readonly format?: string;
readonly chartApiVersion?: string;
readonly chartVersion?: string;
readonly chartName?: string;
}

interface SynthOptions extends SynthCliOptions {
Expand Down Expand Up @@ -1086,6 +1199,7 @@ app.synth();
format: options.format,
chartApiVersion: options.chartApiVersion,
chartVersion: options.chartVersion,
chartName: options.chartName,
});

if (options.postSynth) {
Expand Down