From ce4b721ee793844e89c49a8c89c3185fe32e9ee9 Mon Sep 17 00:00:00 2001 From: Andru Cherny Date: Mon, 8 Jan 2024 17:45:18 +0200 Subject: [PATCH] Implement KUBE_CLEANER_FORCE_CLEAN_ALL to remove all releases in ns --- .env | 1 + src/Config/app-config.ts | 2 ++ src/Modules/RemoverModule.ts | 19 ++++++++++++++++++- src/Modules/VersionModule.ts | 2 +- 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/.env b/.env index e39f485..36486cf 100644 --- a/.env +++ b/.env @@ -12,3 +12,4 @@ HELM_DEBUG=false HELM_DRY_RUN=false KUBE_NAMESPACE=default KUBE_CLEANER_DEBUG=false +KUBE_CLEANER_FORCE_CLEAN_ALL=false diff --git a/src/Config/app-config.ts b/src/Config/app-config.ts index 229548d..f35ecaa 100644 --- a/src/Config/app-config.ts +++ b/src/Config/app-config.ts @@ -18,6 +18,7 @@ export class ConfigFactory { KUBECTL_CMD_ARGS: env('KUBECTL_CMD_ARGS', ''), KUBE_NAMESPACE: env('KUBE_NAMESPACE', 'default'), KUBE_CLEANER_DEBUG: envBoolean('KUBE_CLEANER_DEBUG', false), + KUBE_CLEANER_FORCE_CLEAN_ALL: envBoolean('KUBE_CLEANER_FORCE_CLEAN_ALL', false), }; } } @@ -35,4 +36,5 @@ interface CoreConfigInterface { KUBECTL_CMD_ARGS: string; KUBE_NAMESPACE: string; KUBE_CLEANER_DEBUG: boolean; + KUBE_CLEANER_FORCE_CLEAN_ALL: boolean; } diff --git a/src/Modules/RemoverModule.ts b/src/Modules/RemoverModule.ts index d07cf31..d93b362 100644 --- a/src/Modules/RemoverModule.ts +++ b/src/Modules/RemoverModule.ts @@ -42,9 +42,26 @@ export class RemoverModule { ); }); const realisesList = await Promise.all(promiseList); - + if (ConfigFactory.getCore().KUBE_CLEANER_FORCE_CLEAN_ALL === true) { + console.log('[kube-resource-cleanup] KUBE_CLEANER_FORCE_CLEAN_ALL - true'); + console.log('[kube-resource-cleanup] Remove all helm releases'); + } const deletedRealisesPromiseList = realisesList.map((item) => { return new Promise((resolve, reject) => { + if (ConfigFactory.getCore().KUBE_CLEANER_FORCE_CLEAN_ALL === true) { + KubeProcessHelper.helm( + [ + 'uninstall', + '--namespace', ConfigFactory.getCore().KUBE_NAMESPACE, + item.helmReleaseName + ], + { + grabStdOut: true, + } + ).then((data) => { + resolve({helmReleaseName: item.helmReleaseName, status: true, reason: data}); + }); + } if (typeof item === 'object') { const releaseGitlabProjectName = item?.metadata?.gitlabProjectName; const releaseGitlabProjectBranch = item?.metadata?.gitlabProjectBranch; diff --git a/src/Modules/VersionModule.ts b/src/Modules/VersionModule.ts index 12fa787..911e9a8 100644 --- a/src/Modules/VersionModule.ts +++ b/src/Modules/VersionModule.ts @@ -2,7 +2,7 @@ import {ConfigFactory} from '../Config/app-config'; export class VersionModule { public async run(cliArgs: string[]) { - console.log('[kube-resource-cleanup]' + ConfigFactory.getBase().id + ': ' + ConfigFactory.getBase().version); + console.log('[kube-resource-cleanup] ' + ConfigFactory.getBase().id + ': ' + ConfigFactory.getBase().version); return Promise.resolve(); }