forked from kyma-project/busola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
busola-e2e-cleaner.yaml
172 lines (157 loc) · 4.4 KB
/
busola-e2e-cleaner.yaml
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: busola-e2e-cleaner-rb
subjects:
- kind: ServiceAccount
name: default
namespace: kube-public
roleRef:
kind: ClusterRole
name: cluster-admin
apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: ConfigMap
metadata:
name: busola-e2e-cleaner-cm
namespace: kube-public
data:
index.mjs: |-
import fs from "fs";
import https from "https";
//https://ozzyczech.cz/js/fetch-in-pure-node-js/
const fetch = (params, postData) =>
new Promise((resolve, reject) => {
const req = https.request(params, (res) => {
// reject on bad status
if (res.statusCode < 200 || res.statusCode >= 300) {
return reject(new Error("statusCode=" + res.statusCode));
}
// read data
let body = [];
res.on("data", (chunk) => {
body.push(chunk);
});
res.on("end", () => {
try {
body = Buffer.concat(body).toString();
} catch (e) {
reject(e);
}
resolve(body); // submit data
});
});
req.on("error", (err) => {
reject(err);
});
if (postData) {
req.write(postData);
}
req.end(); // close request
});
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
const { KUBERNETES_SERVICE_HOST: host, KUBERNETES_SERVICE_PORT: port } =
process.env;
const tokenPath = "/var/run/secrets/kubernetes.io/serviceaccount/token";
const token = fs.readFileSync(tokenPath);
const resources = [
{
path: "/api/v1/namespaces",
namePrefix: "a-busola-test-",
},
{
path: "/apis/applicationconnector.kyma-project.io/v1alpha1/applications",
namePrefix: "test-mock-app-a-busola-test-",
},
{
path: "/apis/storage.k8s.io/v1/storageclasses",
namePrefix: "a-busola-test-",
},
{
path: "/api/v1/persistentvolumes",
namePrefix: "test-pv-",
},
{
path: "/apis/rbac.authorization.k8s.io/v1/clusterroles",
namePrefix: "test-cr-",
},
{
path: "/apis/rbac.authorization.k8s.io/v1/clusterrolebindings",
namePrefix: "test-crb-",
},
{
path: "/apis/apiextensions.k8s.io/v1/customresourcedefinitions",
namePrefix: "test-",
},
];
async function doDelete({ path, namePrefix }) {
return fetch({
headers: { Authorization: `Bearer ${token}` },
host,
port,
path: path,
})
.then((e) => Promise.resolve(JSON.parse(e)))
.then((data) => {
const resources = data.items
.filter((res) => res.metadata.name.startsWith(namePrefix))
.map(({ metadata }) => ({
name: metadata.name,
ageInMinutes:
(Date.now() - Date.parse(metadata.creationTimestamp)) / 1000 / 60,
}));
const resourcesToDelete = resources.filter(
({ ageInMinutes }) => ageInMinutes > 60
);
if (!resourcesToDelete.length) {
console.log("nothing to delete");
return Promise.resolve();
}
const promises = resourcesToDelete.map((r) =>
fetch({
headers: { Authorization: `Bearer ${token}` },
host,
port,
path: path + "/" + r.name,
method: "DELETE",
})
);
return Promise.allSettled(promises);
})
.catch(console.log);
}
(async () => {
for (const resource of resources) {
console.log("deleting " + resource.path);
await doDelete(resource);
}
})();
---
apiVersion: batch/v1
kind: CronJob
metadata:
name: busola-e2e-cleaner-cj
namespace: kube-public
spec:
schedule: '0 */3 * * *'
jobTemplate:
spec:
template:
spec:
containers:
- name: arnold
image: node:12
imagePullPolicy: Always
command:
- node
- /src/index.mjs
volumeMounts:
- name: code
mountPath: /src
volumes:
- name: code
configMap:
name: busola-e2e-cleaner-cm
restartPolicy: OnFailure
concurrencyPolicy: Allow