-
Notifications
You must be signed in to change notification settings - Fork 0
/
rancher-project-id.js
65 lines (59 loc) · 1.75 KB
/
rancher-project-id.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
module.exports = async (manifests, options, { config, logger, kubectl }) => {
const { ciNamespace, kubeconfig, kubeconfigContext } = config
const rancherNsMissingProjectId = []
for (const manifest of manifests) {
if (
!(
manifest.kind === "Namespace" &&
manifest.metadata?.annotations?.["field.cattle.io/projectId"] === ""
)
) {
continue
}
rancherNsMissingProjectId.push(manifest)
}
if (rancherNsMissingProjectId.length === 0) {
return
}
if (!ciNamespace) {
logger.warn(
`♉ missing rancher projectId not provided, unable to retrieve it as ci-namespace is not defined`
)
return
}
const { surviveOnBrokenCluster = false } = options
let rancherProjectId = ""
if (options.rancherProjectId) {
rancherProjectId = options.rancherProjectId
} else if (process.env.RANCHER_PROJECT_ID) {
rancherProjectId = process.env.RANCHER_PROJECT_ID
} else {
logger.info(
`♉ missing rancher projectId, getting from cluster using ci-namespace "${ciNamespace}"`
)
try {
const json = await kubectl(
`${
kubeconfigContext ? `--context ${kubeconfigContext}` : ""
} get ns ${ciNamespace} -o json`,
{
kubeconfig,
logInfo: false,
logError: false,
surviveOnBrokenCluster,
}
)
const data = JSON.parse(json)
rancherProjectId = data.metadata.annotations["field.cattle.io/projectId"]
} catch (error) {
logger.warn(
{ error },
"♉ unable to retrieve optional missing rancher projectId from cluster"
)
}
}
for (const manifest of rancherNsMissingProjectId) {
manifest.metadata.annotations["field.cattle.io/projectId"] =
rancherProjectId
}
}