|
1 | 1 | import { exec } from 'child-process-promise'; |
2 | | -import { accessSync, chmodSync, constants, writeFileSync } from 'fs'; |
| 2 | +import { chmodSync, writeFileSync, existsSync, unlinkSync } from 'fs'; |
3 | 3 | import { platform } from 'os'; |
4 | 4 | import { resolve } from 'path'; |
5 | 5 | import * as needle from 'needle'; |
6 | 6 | import * as sleep from 'sleep-promise'; |
7 | 7 |
|
8 | | -export async function downloadKubectl(): Promise<void> { |
9 | | - try { |
10 | | - accessSync(resolve(process.cwd(), 'kubectl'), constants.R_OK); |
11 | | - } catch (error) { |
12 | | - console.log('Downloading kubectl...'); |
13 | | - |
14 | | - // eslint-disable-next-line @typescript-eslint/camelcase |
15 | | - const requestOptions = { follow_max: 2 }; |
16 | | - const k8sRelease = await getLatestStableK8sRelease(); |
17 | | - const osDistro = platform(); |
18 | | - const bodyData = null; |
19 | | - await needle('get', 'https://storage.googleapis.com/kubernetes-release/release/' + |
20 | | - `${k8sRelease}/bin/${osDistro}/amd64/kubectl`, |
21 | | - bodyData, |
22 | | - requestOptions, |
23 | | - ).then((response) => { |
24 | | - writeFileSync('kubectl', response.body); |
25 | | - chmodSync('kubectl', 0o755); // rwxr-xr-x |
26 | | - }); |
27 | | - |
28 | | - console.log('kubectl downloaded'); |
| 8 | +/** |
| 9 | + * @param version For example: "v1.18.0" |
| 10 | + */ |
| 11 | +export async function downloadKubectl(version: string): Promise<void> { |
| 12 | + const kubectlPath = resolve(process.cwd(), 'kubectl'); |
| 13 | + if (existsSync(kubectlPath)) { |
| 14 | + if (version === 'latest') { |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + // Always start clean when requesting a specific version. |
| 19 | + unlinkSync(kubectlPath); |
29 | 20 | } |
| 21 | + |
| 22 | + console.log(`Downloading kubectl ${version}...`); |
| 23 | + |
| 24 | + // eslint-disable-next-line @typescript-eslint/camelcase |
| 25 | + const requestOptions = { follow_max: 2 }; |
| 26 | + const k8sRelease = version === 'latest' ? await getLatestStableK8sRelease() : version; |
| 27 | + const osDistro = platform(); |
| 28 | + const bodyData = null; |
| 29 | + await needle('get', 'https://storage.googleapis.com/kubernetes-release/release/' + |
| 30 | + `${k8sRelease}/bin/${osDistro}/amd64/kubectl`, |
| 31 | + bodyData, |
| 32 | + requestOptions, |
| 33 | + ).then((response) => { |
| 34 | + writeFileSync('kubectl', response.body); |
| 35 | + chmodSync('kubectl', 0o755); // rwxr-xr-x |
| 36 | + }); |
| 37 | + |
| 38 | + console.log('kubectl downloaded'); |
30 | 39 | } |
31 | 40 |
|
32 | 41 | export async function createNamespace(namespace: string): Promise<void> { |
@@ -63,7 +72,7 @@ export async function applyK8sYaml(pathToYamlDeployment: string): Promise<void> |
63 | 72 |
|
64 | 73 | export async function createPodFromImage(name: string, image: string, namespace: string) { |
65 | 74 | console.log(`Letting Kubernetes decide how to manage image ${image} with name ${name}`); |
66 | | - await exec(`./kubectl run ${name} --image=${image} -n ${namespace} -- sleep 999999999`); |
| 75 | + await exec(`./kubectl run ${name} --generator=run-pod/v1 --image=${image} -n ${namespace} -- sleep 999999999`); |
67 | 76 | console.log(`Done Letting Kubernetes decide how to manage image ${image} with name ${name}`); |
68 | 77 | } |
69 | 78 |
|
|
0 commit comments