-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
104 lines (91 loc) · 4.56 KB
/
index.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
const core = require('@actions/core');
const tc = require('@actions/tool-cache');
const os = require('os');
const child_process = require('child_process');
const supportedCombination = ["darwin-amd64", "linux-amd64", "linux-arm64", "linux-ppc64le"];
const installedBinary = ["kubectl", "kube-apiserver", "kubebuilder", "etcd"];
function execSync(command) {
child_process.execSync(command, {shell: '/bin/bash'});
}
async function run() {
try {
core.debug(`NodeJS version: ${process.version}`);
const version = core.getInput('version');
const kubebuilderOnly = core.getInput('kubebuilderOnly') === 'true';
let etcdVersion = core.getInput('etcdVersion');
let kubernetesVersion = core.getInput('kubernetesVersion');
let sudo = core.getInput('useSudo') ? 'sudo' : '';
const majorVersion = version.split(".")[0];
if (kubebuilderOnly && etcdVersion) {
core.warning("kubebuilderOnly is activated. etcdVersion will not be respected.");
} else if (!kubebuilderOnly && etcdVersion && ! majorVersion > 2) {
core.warning("Requested kubebuilder major version is less than 3. etcdVersion will not be respected.");
} else if (!kubebuilderOnly && !!!etcdVersion) {
core.info("No etcdVersion specified. Going to use the default one.");
etcdVersion = 'v3.2.32';
}
if (kubebuilderOnly && kubernetesVersion) {
core.warning("kubebuilderOnly is activated. kubernetesVersion will not be respected.");
} else if (!kubebuilderOnly && kubernetesVersion && ! majorVersion > 2) {
core.warning("Requested kubebuilder major version is less than 3. kubernetesVersion will not be respected.");
} else if (!kubebuilderOnly && !!!kubernetesVersion) {
core.info("No kubernetesVersion specified. Going to use the latest one.");
}
const osPlat = os.platform();
var osArch = os.arch();
if (osArch === "x64") {
osArch = "amd64";
}
if (!supportedCombination.includes(`${osPlat}-${osArch}`)) {
throw `${osPlat}-${osArch} is not supported by this action now`;
}
core.info(`Going to install kubebuilder ${version} for ${osPlat}-${osArch}`);
if (majorVersion > 2) {
core.debug(`MajorVersion is greater than 2`);
const downloadUrl = `https://github.com/kubernetes-sigs/kubebuilder/releases/download/v${version}/kubebuilder_${osPlat}_${osArch}`;
execSync(`${sudo} mkdir -p /usr/local/kubebuilder/bin`);
execSync(`${sudo} curl -L ${downloadUrl} -o /usr/local/kubebuilder/bin/kubebuilder`);
execSync(`${sudo} chmod +x /usr/local/kubebuilder/bin/kubebuilder`);
if (!kubebuilderOnly) {
// Install kubectl and kube-apiserver
["kubectl", "kube-apiserver"].map((binary) => {
if (kubernetesVersion) {
core.info(`Going to install ${binary} ${kubernetesVersion}`);
execSync(`curl -LO "https://dl.k8s.io//release/${kubernetesVersion}/bin/${osPlat}/${osArch}/${binary}"`);
} else {
core.info(`Going to install ${binary} in latest version`);
execSync(`curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/${osPlat}/${osArch}/${binary}"`);
}
execSync(`chmod +x ${binary}`);
execSync(`${sudo} mv ${binary} /usr/local/kubebuilder/bin`);
})
// Install etcd
core.info(`Going to install etcd ${etcdVersion}`);
execSync(`curl -L https://github.com/etcd-io/etcd/releases/download/${etcdVersion}/etcd-${etcdVersion}-${osPlat}-${osArch}.tar.gz | tar -xz --strip-components=1 -C /tmp/`);
execSync(`${sudo} mv /tmp/etcd /usr/local/kubebuilder/bin`);
} else {
core.debug(`No extra binary will be installed.`);
}
} else {
const downloadUrl = `https://github.com/kubernetes-sigs/kubebuilder/releases/download/v${version}/kubebuilder_${version}_${osPlat}_${osArch}.tar.gz`;
execSync(`curl -L ${downloadUrl} | tar -xz -C /tmp/`);
execSync(`${sudo} mv /tmp/kubebuilder_${version}_${osPlat}_${osArch}/ /usr/local/kubebuilder/`);
execSync(`ls -la /usr/local/kubebuilder/bin`);
if (kubebuilderOnly) {
installedBinary
.filter(x => x !== "kubebuilder")
.map(x => {
core.info(`Going to remove ${x}`);
execSync(`${sudo} rm /usr/local/kubebuilder/bin/${x}`);
});
} else {
core.debug(`No extra binary will be deleted.`);
}
}
const cachedPath = await tc.cacheDir('/usr/local/kubebuilder', 'kubebuilder', version);
core.addPath(cachedPath);
} catch (error) {
core.setFailed(error.message);
}
}
run();