-
Notifications
You must be signed in to change notification settings - Fork 0
/
80-update-manifests-with-preprod-resources.js
69 lines (63 loc) · 1.9 KB
/
80-update-manifests-with-preprod-resources.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
module.exports = async function updateManifestsWithPreprodResources(
manifests,
_options,
context
) {
const { config, logger, kubectl } = context
const { repositoryName, environment } = config
if (environment !== "dev") {
return
}
const preprodNamespace = `${repositoryName}-preprod`
const { surviveOnBrokenCluster, kubeconfig, kubeconfigContext } = config
async function getPreprodResources(name, kind) {
try {
const result = await kubectl(
`get -n ${preprodNamespace} ${kind} ${name} -o json`,
{
kubeconfig,
kubeconfigContext,
logInfo: false,
logError: false,
surviveOnBrokenCluster,
}
)
const resourceData = JSON.parse(result)
return resourceData.spec.template.spec.containers.reduce(
(acc, container) => {
acc[container.name] = container.resources
return acc
},
{}
)
} catch (err) {
logger.warn({ err }, `Failed to get resources for ${kind} ${name}`)
return {}
}
}
function updateResources(container, preprodResources) {
if (preprodResources[container.name]) {
container.resources = preprodResources[container.name]
}
}
for (const manifest of manifests) {
if (["Deployment", "StatefulSet"].includes(manifest.kind)) {
const preprodResources = await getPreprodResources(
manifest.metadata.name,
manifest.kind.toLowerCase()
)
for (const container of manifest.spec.template.spec.containers) {
updateResources(container, preprodResources)
}
} else if (manifest.kind === "CronJob") {
const preprodResources = await getPreprodResources(
manifest.metadata.name,
"cronjob"
)
for (const container of manifest.spec.jobTemplate.spec.template.spec
.containers) {
updateResources(container, preprodResources)
}
}
}
}