|
| 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 | +} |
0 commit comments