Skip to content

Commit 6cc8d8d

Browse files
Feature/andrii/saas 6026 set helm version (#417)
* set helm version * bump version * set helm config, tests
1 parent 70df5d8 commit 6cc8d8d

File tree

4 files changed

+262
-72
lines changed

4 files changed

+262
-72
lines changed
Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,32 @@
1-
let setHelmVersionCmd = require('./set-helm-version.cmd');
1+
let setHelmConfigCmd = require('./set-helm-config.cmd');
22

3-
setHelmVersionCmd.requiresAuthentication = false;
3+
setHelmConfigCmd.requiresAuthentication = false;
44

5-
setHelmVersionCmd = setHelmVersionCmd.toCommand();
5+
setHelmConfigCmd = setHelmConfigCmd.toCommand();
66

77
jest.mock('../../helpers/helm');
88
jest.mock('../../helpers/logs');
99
jest.spyOn(process, 'exit').mockImplementation();
1010

1111
const request = require('requestretry');
1212

13+
jest.mock('../../../../logic', () => {
14+
const mockPipelinesList = jest.fn();
15+
mockPipelinesList.mockReturnValue(Promise.resolve([{
16+
metadata: {
17+
name: 'pip1',
18+
id: 'pip1',
19+
},
20+
}]));
21+
22+
// eslint-disable-next-line global-require
23+
const sdk = require('../../../../logic/sdk');
24+
sdk.pipelines = {
25+
getNames: mockPipelinesList,
26+
};
27+
return { sdk };
28+
});
29+
1330
const DEFAULT_RESPONSE = request.__defaultResponse();
1431

1532
describe('helm commands', () => {
@@ -19,27 +36,47 @@ describe('helm commands', () => {
1936
await configureSdk(); // eslint-disable-line
2037
});
2138

22-
describe('set-helm-version', () => {
39+
describe('set-helm-config', () => {
2340

24-
it('should set version', async () => {
41+
it('should set config', async () => {
2542
const argv = {
2643
cluster: 'selector',
27-
version: '3',
44+
version: 'helm3',
45+
testReleasePipeline: 'pip1',
46+
rollbackPipeline: 'pip1',
47+
deleteReleasePipeline: 'pip1',
48+
tillerNamespace: 'kube-system',
49+
releaseNamespaces: ['ns1', 'ns2'],
2850
};
29-
await setHelmVersionCmd.handler(argv);
51+
await setHelmConfigCmd.handler(argv);
3052
await verifyResponsesReturned([DEFAULT_RESPONSE]); // eslint-disable-line
3153
});
3254

3355
it('should throw error for wrong version', async () => {
3456
const argv = {
3557
cluster: 'selector',
36-
version: '1',
58+
version: 'helm1',
3759
};
3860
try {
39-
await setHelmVersionCmd.handler(argv);
61+
await setHelmConfigCmd.handler(argv);
4062
} catch (error) {
4163
expect(error.message).toBe('Wrong version value');
4264
}
4365
});
66+
67+
it('should throw error for wrong pipeline name', async () => {
68+
const argv = {
69+
cluster: 'selector',
70+
version: 'helm3',
71+
testReleasePipeline: 'pip2',
72+
};
73+
try {
74+
await setHelmConfigCmd.handler(argv);
75+
} catch (error) {
76+
expect(error.message).toBe('Pipeline pip2 not found');
77+
return;
78+
}
79+
throw new Error('should throw error for wrong pipeline name');
80+
});
4481
});
4582
});
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
const Command = require('../../Command');
2+
const { sdk } = require('../../../../logic');
3+
const Output = require('../../../../output/Output');
4+
const _ = require('lodash');
5+
6+
const install = new Command({
7+
root: true,
8+
command: 'set-helm-config <cluster>',
9+
description: 'Set Helm config',
10+
webDocs: {
11+
category: 'Predefined Pipelines',
12+
title: 'Set Helm config',
13+
weight: 20,
14+
},
15+
builder: (yargs) => {
16+
return yargs
17+
.positional('cluster', {
18+
describe: 'Cluster name on integrations page',
19+
required: true,
20+
})
21+
.option('version', {
22+
describe: 'Major part of helm version (helm2 | helm3)',
23+
alias: 'v',
24+
})
25+
.option('test-release-pipeline', {
26+
describe: 'Pipeline for testing release',
27+
alias: 't',
28+
})
29+
.option('rollback-pipeline', {
30+
describe: 'Pipeline for rollback',
31+
alias: 'r',
32+
})
33+
.option('delete-release-pipeline', {
34+
describe: 'Pipeline for deleting release',
35+
alias: 'd',
36+
})
37+
.option('tiller-namespace', {
38+
describe: 'Tiller namespace for helm2',
39+
})
40+
.option('release-namespaces', {
41+
describe: 'List of namespaces for using',
42+
})
43+
.example('codefresh set-helm-config cluster -v 2 -t pip1 -r pip2 -d pip3 --tiller-namespace kube-system', 'Helm2 config')
44+
.example('codefresh set-helm-config cluster -v 3 -t pip1 -r pip2 -d pip3 --release-namespaces ns1 --release-namespaces ns2', 'Helm3 config');
45+
},
46+
handler: async (argv) => {
47+
try {
48+
if (!['helm2', 'helm3'].includes(argv.version)) {
49+
throw new Error('Wrong version value');
50+
}
51+
let options = {};
52+
options.selector = argv.cluster;
53+
options.helmVersion = argv.version;
54+
55+
const pipelines = await sdk.pipelines.getNames({});
56+
57+
if (argv.deleteReleasePipeline) {
58+
const pipeline = _.find(pipelines, p => p.metadata.name === argv.deleteReleasePipeline);
59+
options.deleteReleasePipeline = _.get(pipeline, 'metadata.id');
60+
if (!options.deleteReleasePipeline) {
61+
throw new Error(`Pipeline ${argv.deleteReleasePipeline} not found`);
62+
}
63+
}
64+
65+
if (argv.testReleasePipeline) {
66+
const pipeline = _.find(pipelines, p => p.metadata.name === argv.testReleasePipeline);
67+
options.testReleasePipeline = _.get(pipeline, 'metadata.id');
68+
if (!options.testReleasePipeline) {
69+
throw new Error(`Pipeline ${argv.testReleasePipeline} not found`);
70+
}
71+
}
72+
73+
if (argv.rollbackPipeline) {
74+
const pipeline = _.find(pipelines, p => p.metadata.name === argv.rollbackPipeline);
75+
options.rollbackPipeline = _.get(pipeline, 'metadata.id');
76+
if (!options.rollbackPipeline) {
77+
throw new Error(`Pipeline ${argv.rollbackPipeline} not found`);
78+
}
79+
}
80+
81+
options = _.merge(options, _.pick(argv, ['releaseNamespaces', 'tillerNamespace']));
82+
83+
await sdk.helm['cluster-config'].store(options);
84+
console.log('Helm config was set successfully');
85+
process.exit();
86+
} catch (err) {
87+
Output.printError(err);
88+
process.exit(1);
89+
}
90+
},
91+
});
92+
93+
module.exports = install;

lib/interface/cli/commands/helm/set-helm-version.cmd.js

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)