diff --git a/fullstack-network-manager/src/commands/chart.mjs b/fullstack-network-manager/src/commands/chart.mjs index 7864eef24..66a3ce110 100644 --- a/fullstack-network-manager/src/commands/chart.mjs +++ b/fullstack-network-manager/src/commands/chart.mjs @@ -176,8 +176,10 @@ export class ChartCommand extends BaseCommand { task: async (ctx, task) => { self.configManager.load(argv) const namespace = self.configManager.getFlag(flags.namespace) + const deletePvcs = self.configManager.getFlag(flags.deletePvcs) ctx.config = { - namespace: await prompts.promptNamespaceArg(task, namespace) + namespace: await prompts.promptNamespaceArg(task, namespace), + deletePvcs: await prompts.promptDeletePvcs(task, deletePvcs) } } }, @@ -186,6 +188,24 @@ export class ChartCommand extends BaseCommand { task: async (ctx, _) => { await self.chartManager.uninstall(ctx.config.namespace, constants.CHART_FST_DEPLOYMENT_NAME) } + }, + { + title: 'Get PVCs for namespace', + task: async (ctx, _) => { + if(ctx.config.deletePvcs === true) { + ctx.config.pvcs = await self.k8.listPvcsByNamespace(ctx.config.namespace) + } + } + }, + { + title: 'Delete PVCs for namespace', + task: async (ctx, _) => { + if (ctx.config.pvcs) { + for (const pvc of ctx.config.pvcs) { + await self.k8.deletePvc(pvc, ctx.config.namespace) + } + } + } } ], { concurrent: false, @@ -286,7 +306,10 @@ export class ChartCommand extends BaseCommand { .command({ command: 'uninstall', desc: 'Uninstall network deployment chart', - builder: y => flags.setCommandFlags(y, flags.namespace), + builder: y => flags.setCommandFlags(y, + flags.namespace, + flags.deletePvcs + ), handler: argv => { chartCmd.logger.debug("==== Running 'chart uninstall' ===") chartCmd.logger.debug(argv) diff --git a/fullstack-network-manager/src/commands/flags.mjs b/fullstack-network-manager/src/commands/flags.mjs index 93974ad91..b5c5209db 100644 --- a/fullstack-network-manager/src/commands/flags.mjs +++ b/fullstack-network-manager/src/commands/flags.mjs @@ -299,6 +299,15 @@ export const enableHederaExplorerTls = { } } +export const deletePvcs = { + name: 'delete-pvcs', + definition: { + describe: 'Delete the persistent volume claims, defaults to false', + default: false, + type: 'boolean' + } +} + export const allFlags = [ devMode, clusterName, @@ -328,5 +337,6 @@ export const allFlags = [ tlsClusterIssuerName, tlsClusterIssuerNamespace, enableHederaExplorerTls, - selfSignedClusterIssuer + selfSignedClusterIssuer, + deletePvcs ] diff --git a/fullstack-network-manager/src/commands/prompts.mjs b/fullstack-network-manager/src/commands/prompts.mjs index ddf81f3bc..ad60e2032 100644 --- a/fullstack-network-manager/src/commands/prompts.mjs +++ b/fullstack-network-manager/src/commands/prompts.mjs @@ -494,3 +494,19 @@ export async function promptReplicaCount (task, input) { throw new FullstackTestingError(`input failed: ${flags.replicaCount.name}`, e) } } + +export async function promptDeletePvcs (task, input) { + try { + if (input === undefined) { + input = await task.prompt(ListrEnquirerPromptAdapter).run({ + type: 'toggle', + default: flags.deletePvcs.definition.default, + message: 'Would you like to delete persistent volume claims upon uninstall?' + }) + } + + return input + } catch (e) { + throw new FullstackTestingError(`input failed: ${flags.deletePvcs.name}`, e) + } +} diff --git a/fullstack-network-manager/src/core/k8.mjs b/fullstack-network-manager/src/core/k8.mjs index 6b352e97e..0df440216 100644 --- a/fullstack-network-manager/src/core/k8.mjs +++ b/fullstack-network-manager/src/core/k8.mjs @@ -637,6 +637,39 @@ export class K8 { }) } + /** + * Get a list of persistent volume claim names for the given namespace + * @param namespace the namespace of the persistent volume claims to return + * @returns {Promise<*[]>} list of persistent volume claims + */ + async listPvcsByNamespace (namespace) { + const pvcs = [] + const resp = await this.kubeClient.listNamespacedPersistentVolumeClaim ( + namespace + ) + + for (const item of resp.body.items) { + pvcs.push(item.metadata.name) + } + + return pvcs + } + + /** + * Delete a persistent volume claim + * @param name the name of the persistent volume claim to delete + * @param namespace the namespace of the persistent volume claim to delete + * @returns {Promise} true if the persistent volume claim was deleted + */ + async deletePvc (name, namespace) { + const resp = await this.kubeClient.deleteNamespacedPersistentVolumeClaim ( + name, + namespace + ) + + return resp.response.statusCode === 200.0 + } + _getNamespace () { const ns = this.configManager.getFlag(flags.namespace) if (!ns) throw new MissingArgumentError('namespace is not set') diff --git a/fullstack-network-manager/test/e2e/core/k8_e2e.test.mjs b/fullstack-network-manager/test/e2e/core/k8_e2e.test.mjs index c4e63667a..f66211687 100644 --- a/fullstack-network-manager/test/e2e/core/k8_e2e.test.mjs +++ b/fullstack-network-manager/test/e2e/core/k8_e2e.test.mjs @@ -131,4 +131,9 @@ describe('K8', () => { fs.rmdirSync(tmpDir, { recursive: true }) }) + + it('should be able to get two persistent volume claims', async () => { + const pvcs = await k8.listPvcsByNamespace(k8._getNamespace()) + expect(pvcs).toHaveLength(2) + }) })