-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm-cli.js
111 lines (94 loc) · 3.12 KB
/
helm-cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const {
docker,
helpers,
} = require("@kaholo/plugin-library");
const { promisify } = require("util");
const tmp = require("tmp");
const fs = require("fs");
const exec = promisify(require("child_process").exec);
const helmhelp = require("./helpers-helm");
const { HELM_DOCKER_IMAGE } = require("./consts");
async function runCommand(helmConfig) {
const {
kubeToken,
kubeApiServer,
kubeCertificate,
command,
workingDirectory,
} = helmConfig;
const tmpfile = tmp.fileSync({ prefix: "kubeCAcert-", postfix: ".tmp" });
fs.writeFileSync(tmpfile.name, atob(kubeCertificate));
const certificateFilePath = tmpfile.name;
// this probably because Helm caches things in user's home folder
const additionalArguments = [
"-v /tmp/root:/root",
];
const [certificatePath, certificateFileName] = helmhelp.splitDirectory(certificateFilePath);
const certificateVolumeDefinition = docker.createVolumeDefinition(certificatePath);
const volumeDefinitions = [
certificateVolumeDefinition,
];
const workDir = workingDirectory || await helpers.analyzePath("./");
const workingDirectoryVolumeDefinition = docker.createVolumeDefinition(
workDir.absolutePath,
);
volumeDefinitions.push(workingDirectoryVolumeDefinition);
additionalArguments.push("-w", `$${workingDirectoryVolumeDefinition.mountPoint.name}`);
const authenticationParametersMap = new Map([
["--kube-ca-file", `${certificateVolumeDefinition.mountPoint.value}/${certificateFileName}`],
["--kube-token", kubeToken],
["--kube-apiserver", kubeApiServer],
]);
const sanitizedParametersMap = helmhelp.sanitizeParameters(
command,
authenticationParametersMap,
);
// eslint-disable-next-line max-len
const parametersWithEnvironmentalVariablesArray = helmhelp.paramsMapToParamsWithEnvironmentalVariablesArray(
sanitizedParametersMap,
);
// eslint-disable-next-line max-len
const environmentalVariablesContainingParametersObject = helmhelp.paramsMapToEnvironmentalVariablesObject(
sanitizedParametersMap,
);
const dockerEnvironmentalVariables = volumeDefinitions.reduce(
(acc, cur) => ({
...acc,
[cur.mountPoint.name]: cur.mountPoint.value,
}),
{},
);
const shellEnvironmentalVariables = volumeDefinitions.reduce(
(acc, cur) => ({
...acc,
[cur.path.name]: cur.path.value,
}),
{
...dockerEnvironmentalVariables,
...environmentalVariablesContainingParametersObject,
},
);
const helmCommand = `\
${helmhelp.sanitizeCommand(command)} \
${parametersWithEnvironmentalVariablesArray.join(" ")}`;
const completeCommand = docker.buildDockerCommand({
command: helmCommand,
image: HELM_DOCKER_IMAGE,
environmentVariables: dockerEnvironmentalVariables,
volumeDefinitionsArray: volumeDefinitions,
additionalArguments,
});
const result = await exec(completeCommand, {
env: shellEnvironmentalVariables,
});
if (result.stdout && !result.stderr) {
return helmhelp.redactTokenValue(result.stdout);
}
return {
stderr: result.stderr,
stdout: helmhelp.redactTokenValue(result.stdout),
};
}
module.exports = {
runCommand,
};