-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathclean.js
84 lines (77 loc) · 2.63 KB
/
clean.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
"use strict";
const { OpenShiftClientX } = require("@bcgov/pipeline-cli");
const getTargetPhases = (env, phases) => {
let target_phase = [];
for (const phase in phases) {
if (env.match(/^(all|transient)$/) && phases[phase].transient) {
target_phase.push(phase);
} else if (env === phase) {
target_phase.push(phase);
break;
}
}
return target_phase;
};
module.exports = (settings) => {
const phases = settings.phases;
const options = settings.options;
const oc = new OpenShiftClientX(
Object.assign({ namespace: phases.build.namespace }, options)
);
const target_phases = getTargetPhases(options.env, phases);
target_phases.forEach((k) => {
if (phases.hasOwnProperty(k)) {
const phase = phases[k];
let buildConfigs = oc.get("bc", {
selector: `app=${phase.instance},env-id=${phase.changeId},!shared,github-repo=${oc.git.repository},github-owner=${oc.git.owner}`,
namespace: phase.namespace,
});
buildConfigs.forEach((bc) => {
if (bc.spec.output.to.kind == "ImageStreamTag") {
oc.delete([`ImageStreamTag/${bc.spec.output.to.name}`], {
"ignore-not-found": "true",
wait: "true",
namespace: phase.namespace,
});
}
});
let deploymentConfigs = oc.get("dc", {
selector: `app=${phase.instance},env-id=${phase.changeId},env-name=${k},!shared,github-repo=${oc.git.repository},github-owner=${oc.git.owner}`,
namespace: phase.namespace,
});
deploymentConfigs.forEach((dc) => {
dc.spec.triggers.forEach((trigger) => {
if (
trigger.type == "ImageChange" &&
trigger.imageChangeParams.from.kind == "ImageStreamTag"
) {
oc.delete(
[`ImageStreamTag/${trigger.imageChangeParams.from.name}`],
{
"ignore-not-found": "true",
wait: "true",
namespace: phase.namespace,
}
);
}
});
});
oc.raw("delete", ["all"], {
selector: `app=${phase.instance},env-id=${phase.changeId},!shared,github-repo=${oc.git.repository},github-owner=${oc.git.owner}`,
wait: "true",
namespace: phase.namespace,
});
oc.raw(
"delete",
[
"pvc,Secret,configmap,endpoints,RoleBinding,role,ServiceAccount,Endpoints",
],
{
selector: `app=${phase.instance},env-id=${phase.changeId},!shared,github-repo=${oc.git.repository},github-owner=${oc.git.owner}`,
wait: "true",
namespace: phase.namespace,
}
);
}
});
};