|
1 |
| -"use strict"; |
2 |
| -var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
3 |
| - function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
4 |
| - return new (P || (P = Promise))(function (resolve, reject) { |
5 |
| - function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
6 |
| - function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
7 |
| - function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
8 |
| - step((generator = generator.apply(thisArg, _arguments || [])).next()); |
9 |
| - }); |
10 |
| -}; |
11 |
| -Object.defineProperty(exports, "__esModule", { value: true }); |
12 |
| -const core = require("@actions/core"); |
13 |
| -const command_1 = require("@actions/core/lib/command"); |
14 |
| -const path = require("path"); |
15 |
| -const fs = require("fs"); |
16 |
| -const io = require("@actions/io"); |
17 |
| -const toolCache = require("@actions/tool-cache"); |
18 |
| -const os = require("os"); |
19 |
| -const toolrunner_1 = require("@actions/exec/lib/toolrunner"); |
20 |
| -const jsyaml = require("js-yaml"); |
21 |
| -const util = require("util"); |
22 |
| -function getKubeconfig() { |
23 |
| - const method = core.getInput('method', { required: true }); |
24 |
| - if (method == 'kubeconfig') { |
25 |
| - const kubeconfig = core.getInput('kubeconfig', { required: true }); |
26 |
| - core.debug("Setting context using kubeconfig"); |
27 |
| - return kubeconfig; |
28 |
| - } |
29 |
| - else if (method == 'service-account') { |
30 |
| - const clusterUrl = core.getInput('k8s-url', { required: true }); |
31 |
| - core.debug("Found clusterUrl, creating kubeconfig using certificate and token"); |
32 |
| - let k8sSecret = core.getInput('k8s-secret', { required: true }); |
33 |
| - var parsedk8sSecret = jsyaml.safeLoad(k8sSecret); |
34 |
| - let kubernetesServiceAccountSecretFieldNotPresent = 'The service acount secret yaml does not contain %s; field. Make sure that its present and try again.'; |
35 |
| - if (!parsedk8sSecret) { |
36 |
| - throw Error("The service account secret yaml specified is invalid. Make sure that its a valid yaml and try again."); |
37 |
| - } |
38 |
| - if (!parsedk8sSecret.data) { |
39 |
| - throw Error(util.format(kubernetesServiceAccountSecretFieldNotPresent, "data")); |
40 |
| - } |
41 |
| - if (!parsedk8sSecret.data.token) { |
42 |
| - throw Error(util.format(kubernetesServiceAccountSecretFieldNotPresent, "data.token")); |
43 |
| - } |
44 |
| - if (!parsedk8sSecret.data["ca.crt"]) { |
45 |
| - throw Error(util.format(kubernetesServiceAccountSecretFieldNotPresent, "data[ca.crt]")); |
46 |
| - } |
47 |
| - const certAuth = parsedk8sSecret.data["ca.crt"]; |
48 |
| - const token = Buffer.from(parsedk8sSecret.data.token, 'base64').toString(); |
49 |
| - const kubeconfigObject = { |
50 |
| - "apiVersion": "v1", |
51 |
| - "kind": "Config", |
52 |
| - "clusters": [ |
53 |
| - { |
54 |
| - "cluster": { |
55 |
| - "certificate-authority-data": certAuth, |
56 |
| - "server": clusterUrl |
57 |
| - } |
58 |
| - } |
59 |
| - ], |
60 |
| - "users": [ |
61 |
| - { |
62 |
| - "user": { |
63 |
| - "token": token |
64 |
| - } |
65 |
| - } |
66 |
| - ] |
67 |
| - }; |
68 |
| - return JSON.stringify(kubeconfigObject); |
69 |
| - } |
70 |
| - else { |
71 |
| - throw Error("Invalid method specified. Acceptable values are kubeconfig and service-account."); |
72 |
| - } |
73 |
| -} |
74 |
| -function getExecutableExtension() { |
75 |
| - if (os.type().match(/^Win/)) { |
76 |
| - return '.exe'; |
77 |
| - } |
78 |
| - return ''; |
79 |
| -} |
80 |
| -function getKubectlPath() { |
81 |
| - return __awaiter(this, void 0, void 0, function* () { |
82 |
| - let kubectlPath = yield io.which('kubectl', false); |
83 |
| - if (!kubectlPath) { |
84 |
| - const allVersions = toolCache.findAllVersions('kubectl'); |
85 |
| - kubectlPath = allVersions.length > 0 ? toolCache.find('kubectl', allVersions[0]) : ''; |
86 |
| - if (!kubectlPath) { |
87 |
| - throw new Error('Kubectl is not installed'); |
88 |
| - } |
89 |
| - kubectlPath = path.join(kubectlPath, `kubectl${getExecutableExtension()}`); |
90 |
| - } |
91 |
| - return kubectlPath; |
92 |
| - }); |
93 |
| -} |
94 |
| -function setContext(kubeconfigPath) { |
95 |
| - return __awaiter(this, void 0, void 0, function* () { |
96 |
| - let context = core.getInput('context'); |
97 |
| - if (context) { |
98 |
| - //To use kubectl commands, the environment variable KUBECONFIG needs to be set for this step |
99 |
| - process.env['KUBECONFIG'] = kubeconfigPath; |
100 |
| - const kubectlPath = yield getKubectlPath(); |
101 |
| - let toolRunner = new toolrunner_1.ToolRunner(kubectlPath, ['config', 'use-context', context]); |
102 |
| - yield toolRunner.exec(); |
103 |
| - toolRunner = new toolrunner_1.ToolRunner(kubectlPath, ['config', 'current-context']); |
104 |
| - yield toolRunner.exec(); |
105 |
| - } |
106 |
| - }); |
107 |
| -} |
108 |
| -function run() { |
109 |
| - return __awaiter(this, void 0, void 0, function* () { |
110 |
| - let kubeconfig = getKubeconfig(); |
111 |
| - const runnerTempDirectory = process.env['RUNNER_TEMP']; // Using process.env until the core libs are updated |
112 |
| - const kubeconfigPath = path.join(runnerTempDirectory, `kubeconfig_${Date.now()}`); |
113 |
| - core.debug(`Writing kubeconfig contents to ${kubeconfigPath}`); |
114 |
| - fs.writeFileSync(kubeconfigPath, kubeconfig); |
115 |
| - fs.chmodSync(kubeconfigPath, '600'); |
116 |
| - command_1.issueCommand('set-env', { name: 'KUBECONFIG' }, kubeconfigPath); |
117 |
| - console.log('KUBECONFIG environment variable is set'); |
118 |
| - yield setContext(kubeconfigPath); |
119 |
| - }); |
120 |
| -} |
121 |
| -run().catch(core.setFailed); |
| 1 | +"use strict"; |
| 2 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 3 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 4 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 5 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 6 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 7 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 8 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 9 | + }); |
| 10 | +}; |
| 11 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 12 | +exports.run = void 0; |
| 13 | +const core = require("@actions/core"); |
| 14 | +const path = require("path"); |
| 15 | +const fs = require("fs"); |
| 16 | +const io = require("@actions/io"); |
| 17 | +const toolCache = require("@actions/tool-cache"); |
| 18 | +const os = require("os"); |
| 19 | +const toolrunner_1 = require("@actions/exec/lib/toolrunner"); |
| 20 | +const jsyaml = require("js-yaml"); |
| 21 | +const util = require("util"); |
| 22 | +function getKubeconfig() { |
| 23 | + const method = core.getInput('method', { required: true }); |
| 24 | + if (method == 'kubeconfig') { |
| 25 | + const kubeconfig = core.getInput('kubeconfig', { required: true }); |
| 26 | + core.debug("Setting context using kubeconfig"); |
| 27 | + return kubeconfig; |
| 28 | + } |
| 29 | + else if (method == 'service-account') { |
| 30 | + const clusterUrl = core.getInput('k8s-url', { required: true }); |
| 31 | + core.debug("Found clusterUrl, creating kubeconfig using certificate and token"); |
| 32 | + let k8sSecret = core.getInput('k8s-secret', { required: true }); |
| 33 | + var parsedk8sSecret = jsyaml.safeLoad(k8sSecret); |
| 34 | + let kubernetesServiceAccountSecretFieldNotPresent = 'The service acount secret yaml does not contain %s; field. Make sure that its present and try again.'; |
| 35 | + if (!parsedk8sSecret) { |
| 36 | + throw Error("The service account secret yaml specified is invalid. Make sure that its a valid yaml and try again."); |
| 37 | + } |
| 38 | + if (!parsedk8sSecret.data) { |
| 39 | + throw Error(util.format(kubernetesServiceAccountSecretFieldNotPresent, "data")); |
| 40 | + } |
| 41 | + if (!parsedk8sSecret.data.token) { |
| 42 | + throw Error(util.format(kubernetesServiceAccountSecretFieldNotPresent, "data.token")); |
| 43 | + } |
| 44 | + if (!parsedk8sSecret.data["ca.crt"]) { |
| 45 | + throw Error(util.format(kubernetesServiceAccountSecretFieldNotPresent, "data[ca.crt]")); |
| 46 | + } |
| 47 | + const certAuth = parsedk8sSecret.data["ca.crt"]; |
| 48 | + const token = Buffer.from(parsedk8sSecret.data.token, 'base64').toString(); |
| 49 | + const kubeconfigObject = { |
| 50 | + "apiVersion": "v1", |
| 51 | + "kind": "Config", |
| 52 | + "clusters": [ |
| 53 | + { |
| 54 | + "cluster": { |
| 55 | + "certificate-authority-data": certAuth, |
| 56 | + "server": clusterUrl |
| 57 | + } |
| 58 | + } |
| 59 | + ], |
| 60 | + "users": [ |
| 61 | + { |
| 62 | + "user": { |
| 63 | + "token": token |
| 64 | + } |
| 65 | + } |
| 66 | + ] |
| 67 | + }; |
| 68 | + return JSON.stringify(kubeconfigObject); |
| 69 | + } |
| 70 | + else { |
| 71 | + throw Error("Invalid method specified. Acceptable values are kubeconfig and service-account."); |
| 72 | + } |
| 73 | +} |
| 74 | +function getExecutableExtension() { |
| 75 | + if (os.type().match(/^Win/)) { |
| 76 | + return '.exe'; |
| 77 | + } |
| 78 | + return ''; |
| 79 | +} |
| 80 | +function getKubectlPath() { |
| 81 | + return __awaiter(this, void 0, void 0, function* () { |
| 82 | + let kubectlPath = yield io.which('kubectl', false); |
| 83 | + if (!kubectlPath) { |
| 84 | + const allVersions = toolCache.findAllVersions('kubectl'); |
| 85 | + kubectlPath = allVersions.length > 0 ? toolCache.find('kubectl', allVersions[0]) : ''; |
| 86 | + if (!kubectlPath) { |
| 87 | + throw new Error('Kubectl is not installed'); |
| 88 | + } |
| 89 | + kubectlPath = path.join(kubectlPath, `kubectl${getExecutableExtension()}`); |
| 90 | + } |
| 91 | + return kubectlPath; |
| 92 | + }); |
| 93 | +} |
| 94 | +function setContext(kubeconfigPath) { |
| 95 | + return __awaiter(this, void 0, void 0, function* () { |
| 96 | + let context = core.getInput('context'); |
| 97 | + if (context) { |
| 98 | + //To use kubectl commands, the environment variable KUBECONFIG needs to be set for this step |
| 99 | + process.env['KUBECONFIG'] = kubeconfigPath; |
| 100 | + const kubectlPath = yield getKubectlPath(); |
| 101 | + let toolRunner = new toolrunner_1.ToolRunner(kubectlPath, ['config', 'use-context', context]); |
| 102 | + yield toolRunner.exec(); |
| 103 | + toolRunner = new toolrunner_1.ToolRunner(kubectlPath, ['config', 'current-context']); |
| 104 | + yield toolRunner.exec(); |
| 105 | + } |
| 106 | + }); |
| 107 | +} |
| 108 | +function run() { |
| 109 | + return __awaiter(this, void 0, void 0, function* () { |
| 110 | + let kubeconfig = getKubeconfig(); |
| 111 | + const runnerTempDirectory = process.env['RUNNER_TEMP']; // Using process.env until the core libs are updated |
| 112 | + const kubeconfigPath = path.join(runnerTempDirectory, `kubeconfig_${Date.now()}`); |
| 113 | + core.debug(`Writing kubeconfig contents to ${kubeconfigPath}`); |
| 114 | + fs.writeFileSync(kubeconfigPath, kubeconfig); |
| 115 | + fs.chmodSync(kubeconfigPath, '600'); |
| 116 | + core.exportVariable('KUBECONFIG', kubeconfigPath); |
| 117 | + console.log('KUBECONFIG environment variable is set'); |
| 118 | + yield setContext(kubeconfigPath); |
| 119 | + }); |
| 120 | +} |
| 121 | +exports.run = run; |
| 122 | +run().catch(core.setFailed); |
0 commit comments