UNPKG

@topgroup/diginext

Version:

A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.

73 lines (72 loc) 3.56 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DeploymentCleaner = void 0; const log_1 = require("diginext-utils/dist/xconsole/log"); const k8s_1 = __importDefault(require("../../../modules/k8s")); class DeploymentCleaner { constructor(context, namespace, onUpdate) { this.context = context; this.namespace = namespace; this.onUpdate = onUpdate; } async cleanupOldDeployments(appName, currentVersion) { const cleanupTasks = [ this.cleanupIngress(appName, currentVersion).catch((e) => { var _a; (0, log_1.logWarn)(`Cleanup task failed for "ingress" of "${appName}": ${e}`); (_a = this.onUpdate) === null || _a === void 0 ? void 0 : _a.call(this, `Cleanup task failed for "ingress" of "${appName}": ${e}`); }), this.cleanupServices(appName, currentVersion).catch((e) => { var _a; (0, log_1.logWarn)(`Cleanup task failed for "service" of "${appName}": ${e}`); (_a = this.onUpdate) === null || _a === void 0 ? void 0 : _a.call(this, `Cleanup task failed for "service" of "${appName}": ${e}`); }), this.cleanupDeployments(appName, currentVersion).catch((e) => { var _a; (0, log_1.logWarn)(`Cleanup task failed for "deployment" of "${appName}": ${e}`); (_a = this.onUpdate) === null || _a === void 0 ? void 0 : _a.call(this, `Cleanup task failed for "deployment" of "${appName}": ${e}`); }), ]; const cleanupTaskNames = ["ingress", "service", "deployment"]; await Promise.allSettled(cleanupTasks) .then((results) => { results.forEach((result, index) => { var _a; if (result.status === "rejected") { (0, log_1.logWarn)(`Cleanup task failed for "${cleanupTaskNames[index]}" of "${appName}": ${result.reason}`); (_a = this.onUpdate) === null || _a === void 0 ? void 0 : _a.call(this, `Cleanup task failed for "${cleanupTaskNames[index]}" of "${appName}": ${result.reason}`); } }); }) .catch((e) => { var _a; (0, log_1.logWarn)(`Cleanup tasks failed for "${appName}": ${e}`); (_a = this.onUpdate) === null || _a === void 0 ? void 0 : _a.call(this, `Cleanup tasks failed for "${appName}": ${e}`); }); } async cleanupIngress(appName, currentVersion) { return k8s_1.default.deleteIngressByFilter(this.namespace, { context: this.context, skipOnError: true, filterLabel: `main-app=${appName},app-version!=${currentVersion}`, }); } async cleanupServices(appName, currentVersion) { return k8s_1.default.deleteServiceByFilter(this.namespace, { context: this.context, skipOnError: true, filterLabel: `main-app=${appName},app-version!=${currentVersion}`, }); } async cleanupDeployments(appName, currentVersion) { return k8s_1.default.deleteDeploymentsByFilter(this.namespace, { context: this.context, skipOnError: true, filterLabel: `main-app=${appName},app-version!=${currentVersion}`, }); } } exports.DeploymentCleaner = DeploymentCleaner;