-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_kubectl.mjs
94 lines (83 loc) · 2.41 KB
/
update_kubectl.mjs
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
// While waiting for renovate to have custom datasource management
import fs from "fs";
const platforms = [
"darwin/amd64",
"linux/amd64",
"darwin/arm64",
"linux/arm64",
"windows/amd64",
];
async function getVersionMap(version) {
let map = {};
for (let platform of platforms) {
const bin = `kubectl${platform.startsWith("windows") ? ".exe" : ""}`;
let digest = (
await (
await fetch(
`https://dl.k8s.io/release/v${version}/bin/${platform}/${bin}.sha256`
)
).text()
).trim();
map[platform.replace("/", "_")] = [
`https://dl.k8s.io/release/v${version}/bin/${platform}/${bin}`,
digest,
];
}
return map;
}
const FILE = "lib/private/kubectl_toolchain.bzl";
let fileContent = fs.readFileSync(FILE, "utf8").toString();
// Get latest stable version
let stable = (
await (await fetch("https://dl.k8s.io/release/stable.txt")).text()
)
.trim()
.substring(1);
let content = /_binaries\s*=(?<versions>(\s*{[^{]+{[^}]+})+[^}]+})/gm.exec(
fileContent
);
if (!content) {
throw new Error("Could not find _binaries in kubectl.bzl");
}
const json = content.groups.versions
.replace(/\(/g, "[")
.replace(/\)/g, "]")
.replace(/},\s*}/g, "}}")
.replace(/],\s*}/g, "]}");
console.log(json);
let info = JSON.parse(json);
// Read current versions from https://kubernetes.io/releases/
const html = await (await fetch("https://kubernetes.io/releases/")).text();
html.match(/<a href="\/docs\/reference\/kubectl\/versions.html">/g);
const re = /CHANGELOG\/CHANGELOG[^>]+>(?<version>\d+\.\d+\.\d+)/g;
let updated = false;
// New stable
if (!info[stable]) {
console.log("New latest version found:", stable);
info[stable] = await getVersionMap(stable);
fileContent = fileContent.replace(
/DEFAULT_KUBECTL_VERSION = "\d+\.\d+\.\d+"/,
`DEFAULT_KUBECTL_VERSION = "${stable}"`
);
updated = true;
}
for (let match of html.matchAll(re)) {
let version = match.groups.version;
if (!info[version]) {
console.log("New version found:", version);
info[version] = await getVersionMap(version);
updated = true;
}
}
if (updated) {
const newContent = JSON.stringify(info, null, 2)
.replace(/\[/g, "(")
.replace(/\]/g, ")")
.replace(/\)(\s*)}/, "),$1}")
.replace(/\}(\s*)}/, "},$1}");
fileContent = fileContent.replace(
/_binaries\s*=(\s*{[^{]+{[^}]+})+[^}]+}/gm,
`_binaries = ${newContent}`
);
fs.writeFileSync(FILE, fileContent);
}