Skip to content

Commit 6e3ec5e

Browse files
committed
Create job to delete individual previews
Signed-off-by: ArthurSens <[email protected]>
1 parent 9b382b6 commit 6e3ec5e

4 files changed

+412
-240
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { Werft } from "./util/werft";
2+
import * as Tracing from "./observability/tracing";
3+
import { HarvesterPreviewEnvironment, PreviewEnvironment } from "./util/preview";
4+
import { SpanStatusCode } from "@opentelemetry/api";
5+
import { exec } from "./util/shell";
6+
import { CORE_DEV_KUBECONFIG_PATH, HARVESTER_KUBECONFIG_PATH } from "./jobs/build/const";
7+
import * as fs from "fs";
8+
9+
// Will be set once tracing has been initialized
10+
let werft: Werft;
11+
12+
const context: any = JSON.parse(fs.readFileSync("context.json").toString());
13+
const annotations = context.Annotations || {};
14+
const previewName = annotations["preview"] || "";
15+
// for testing purposes
16+
// if set to 'true' it shows only previews that would be deleted
17+
const DRY_RUN = annotations["dry-run"] || false;
18+
19+
const SLICES = {
20+
VALIDATE_CONFIGURATION: "Validating configuration",
21+
CONFIGURE_ACCESS: "Configuring access to relevant resources",
22+
INSTALL_HARVESTER_KUBECONFIG: "Install Harvester kubeconfig",
23+
DELETING_PREVIEW: `Deleting preview environment: ${previewName}`,
24+
};
25+
26+
Tracing.initialize()
27+
.then(() => {
28+
werft = new Werft("delete-preview-environment-cron");
29+
})
30+
.then(() => deletePreviewEnvironment())
31+
.catch((err) => {
32+
werft.rootSpan.setStatus({
33+
code: SpanStatusCode.ERROR,
34+
message: err,
35+
});
36+
console.error("Werft job failed with an error", err);
37+
// Explicitly not using process.exit as we need to flush tracing, see tracing.js
38+
process.exitCode = 1;
39+
})
40+
.finally(() => {
41+
werft.phase("Flushing telemetry", "Flushing telemetry before stopping job");
42+
werft.endAllSpans();
43+
});
44+
45+
async function deletePreviewEnvironment() {
46+
// Fail early if no preview was passed through annotations.
47+
werft.log(SLICES.VALIDATE_CONFIGURATION, "Validating annotations");
48+
if (previewName == "") {
49+
werft.fail(
50+
SLICES.VALIDATE_CONFIGURATION,
51+
"A preview name is required. Please inform the preview name with '-a preview=<name of the preview>'.",
52+
);
53+
}
54+
werft.done(SLICES.VALIDATE_CONFIGURATION);
55+
56+
werft.phase("Configure access");
57+
try {
58+
const GCLOUD_SERVICE_ACCOUNT_PATH = "/mnt/secrets/gcp-sa/service-account.json";
59+
exec(`gcloud auth activate-service-account --key-file "${GCLOUD_SERVICE_ACCOUNT_PATH}"`, {
60+
slice: SLICES.CONFIGURE_ACCESS,
61+
});
62+
exec(
63+
`KUBECONFIG=${CORE_DEV_KUBECONFIG_PATH} gcloud container clusters get-credentials core-dev --zone europe-west1-b --project gitpod-core-dev`,
64+
{ slice: SLICES.CONFIGURE_ACCESS },
65+
);
66+
werft.done(SLICES.CONFIGURE_ACCESS);
67+
} catch (err) {
68+
werft.fail(SLICES.CONFIGURE_ACCESS, err);
69+
}
70+
71+
werft.phase("Install Harvester kubeconfig");
72+
try {
73+
exec(`cp /mnt/secrets/harvester-kubeconfig/harvester-kubeconfig.yml ${HARVESTER_KUBECONFIG_PATH}`, {
74+
slice: SLICES.INSTALL_HARVESTER_KUBECONFIG,
75+
});
76+
werft.done(SLICES.INSTALL_HARVESTER_KUBECONFIG);
77+
} catch (err) {
78+
werft.fail(SLICES.INSTALL_HARVESTER_KUBECONFIG, err);
79+
}
80+
81+
const preview = new HarvesterPreviewEnvironment(werft, previewName);
82+
if (DRY_RUN) {
83+
werft.log(SLICES.DELETING_PREVIEW, `Would have deleted preview ${preview.name}`);
84+
} else {
85+
removePreviewEnvironment(preview);
86+
}
87+
}
88+
89+
async function removePreviewEnvironment(previewEnvironment: PreviewEnvironment) {
90+
werft.log(SLICES.DELETING_PREVIEW, `Starting deletion of all resources related to ${previewEnvironment.name}`);
91+
try {
92+
// We're running these promises sequentially to make it easier to read the log output.
93+
await previewEnvironment.removeDNSRecords(SLICES.DELETING_PREVIEW);
94+
await previewEnvironment.delete();
95+
werft.done(SLICES.DELETING_PREVIEW);
96+
} catch (e) {
97+
werft.failSlice(SLICES.DELETING_PREVIEW, e);
98+
}
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
pod:
2+
serviceAccount: werft
3+
affinity:
4+
nodeAffinity:
5+
requiredDuringSchedulingIgnoredDuringExecution:
6+
nodeSelectorTerms:
7+
- matchExpressions:
8+
- key: dev/workload
9+
operator: In
10+
values:
11+
- "builds"
12+
volumes:
13+
- name: gcp-sa
14+
secret:
15+
secretName: gcp-sa-gitpod-dev-deployer
16+
- name: harvester-kubeconfig
17+
secret:
18+
secretName: harvester-kubeconfig
19+
- name: harvester-k3s-dockerhub-pull-account
20+
secret:
21+
secretName: harvester-k3s-dockerhub-pull-account
22+
- name: harvester-vm-ssh-keys
23+
secret:
24+
secretName: harvester-vm-ssh-keys
25+
containers:
26+
- name: build
27+
image: eu.gcr.io/gitpod-core-dev/dev/dev-environment:pd-dev-image-gcloud.2
28+
workingDir: /workspace
29+
imagePullPolicy: IfNotPresent
30+
volumeMounts:
31+
- name: gcp-sa
32+
mountPath: /mnt/secrets/gcp-sa
33+
readOnly: true
34+
- name: harvester-kubeconfig
35+
mountPath: /mnt/secrets/harvester-kubeconfig
36+
- name: harvester-vm-ssh-keys
37+
mountPath: /mnt/secrets/harvester-vm-ssh-keys
38+
- name: harvester-k3s-dockerhub-pull-account
39+
mountPath: /mnt/secrets/harvester-k3s-dockerhub-pull-account
40+
env:
41+
- name: HONEYCOMB_DATASET
42+
value: "werft"
43+
- name: HONEYCOMB_API_KEY
44+
valueFrom:
45+
secretKeyRef:
46+
name: honeycomb-api-key
47+
key: apikey
48+
command:
49+
- bash
50+
- -c
51+
- |
52+
sleep 1
53+
set -Eeuo pipefail
54+
55+
sudo chown -R gitpod:gitpod /workspace
56+
mkdir /workspace/.ssh
57+
cp /mnt/secrets/harvester-vm-ssh-keys/id_rsa /workspace/.ssh/id_rsa_harvester_vm
58+
cp /mnt/secrets/harvester-vm-ssh-keys/id_rsa.pub /workspace/.ssh/id_rsa_harvester_vm.pub
59+
sudo chmod 600 /workspace/.ssh/id_rsa_harvester_vm
60+
sudo chmod 644 /workspace/.ssh/id_rsa_harvester_vm.pub
61+
62+
(cd .werft && yarn install && mv node_modules ..) | werft log slice prep
63+
printf '{{ toJson . }}' > context.json
64+
65+
npx ts-node .werft/platform-delete-preview-environment.ts

0 commit comments

Comments
 (0)