Skip to content

Commit

Permalink
feat: introduce --delete-all flag to delete both Eclipse Che and Dev … (
Browse files Browse the repository at this point in the history
#2196)

* feat: introduce --delete-all flag to delete both Eclipse Che and Dev Workspace related resources

Signed-off-by: Anatolii Bazko <[email protected]>
  • Loading branch information
tolusha authored Aug 1, 2022
1 parent 72bcbab commit 24265f6
Show file tree
Hide file tree
Showing 9 changed files with 442 additions and 512 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ _See code: [src/commands/server/debug.ts](https://github.com/che-incubator/chect

## `chectl server:delete`

delete any Eclipse Che related resource: Kubernetes/OpenShift
delete any Eclipse Che related resource

```
USAGE
Expand All @@ -228,6 +228,8 @@ OPTIONS
--batch Batch mode. Running a command without end user interaction.
--delete-all Indicates to delete Eclipse Che and Dev Workspace related resources
--delete-namespace Indicates that a Eclipse Che namespace will be deleted as well
--skip-kubernetes-health-check Skip Kubernetes health check
Expand Down
121 changes: 67 additions & 54 deletions src/commands/server/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,24 @@ import { Command, flags } from '@oclif/command'
import { boolean } from '@oclif/command/lib/flags'
import { cli } from 'cli-ux'
import * as Listrq from 'listr'
import { OLMDevWorkspaceTasks } from '../../tasks/installers/olm-dev-workspace-operator'
import { OLMDevWorkspaceTasks } from '../../tasks/component-installers/devworkspace/olm-installer'
import { ChectlContext } from '../../api/context'
import { KubeHelper } from '../../api/kube'
import { assumeYes, batch, cheNamespace, CHE_TELEMETRY, listrRenderer, skipKubeHealthzCheck } from '../../common-flags'
import {
DEFAULT_ANALYTIC_HOOK_NAME,
DEFAULT_CHE_NAMESPACE,
WORKSPACE_CONTROLLER_NAMESPACE,
OPENSHIFT_OPERATORS_NAMESPACE,
DEFAULT_CHE_NAMESPACE, OPENSHIFT_OPERATORS_NAMESPACE, WORKSPACE_CONTROLLER_NAMESPACE,
} from '../../constants'
import { CheTasks } from '../../tasks/che'
import { DevWorkspaceTasks } from '../../tasks/component-installers/devfile-workspace-operator-installer'
import { DevWorkspaceTasks } from '../../tasks/component-installers/devworkspace/operator-installer'
import { OLMTasks } from '../../tasks/installers/olm'
import { OperatorTasks } from '../../tasks/installers/operator'
import { ApiTasks } from '../../tasks/platforms/api'
import { findWorkingNamespace, getCommandSuccessMessage, notifyCommandCompletedSuccessfully, wrapCommandError } from '../../util'
import Listr = require('listr')

export default class Delete extends Command {
static description = 'delete any Eclipse Che related resource: Kubernetes/OpenShift'
static description = 'delete any Eclipse Che related resource'

static flags: flags.Input<any> = {
help: flags.help({ char: 'h' }),
Expand All @@ -43,6 +41,10 @@ export default class Delete extends Command {
description: 'Indicates that a Eclipse Che namespace will be deleted as well',
default: false,
}),
'delete-all': boolean({
description: 'Indicates to delete Eclipse Che and Dev Workspace related resources',
default: false,
}),
'listr-renderer': listrRenderer,
'skip-deletion-check': boolean({
description: 'Skip user confirmation on deletion check',
Expand All @@ -56,74 +58,85 @@ export default class Delete extends Command {

async run() {
const { flags } = this.parse(Delete)
const ctx = await ChectlContext.initAndGet(flags, this)

flags.chenamespace = flags.chenamespace || await findWorkingNamespace(flags) || DEFAULT_CHE_NAMESPACE

const ctx = await ChectlContext.initAndGet(flags, this)
await this.config.runHook(DEFAULT_ANALYTIC_HOOK_NAME, { command: Delete.id, flags })

if (flags['skip-deletion-check']) {
this.warn('\'--skip-deletion-check\' flag is deprecated, use \'--yes\' instead.')
flags.yes = flags['skip-deletion-check']
}

const apiTasks = new ApiTasks()
const kube = new KubeHelper(flags)
const operatorTasks = new OperatorTasks(flags)
const olmTasks = new OLMTasks(flags)
const cheTasks = new CheTasks(flags)
const olmDevWorkspaceTasks = new OLMDevWorkspaceTasks(flags)
const devWorkspaceTasks = new DevWorkspaceTasks(flags)

const tasks = new Listrq([], ctx.listrOptions)

const apiTasks = new ApiTasks()
tasks.add(apiTasks.testApiTasks(flags))
tasks.add({
title: 'Delete operator resources',
task: () => new Listr(operatorTasks.getDeleteTasks(flags)),
})
tasks.add({
title: 'Delete OLM resources',
task: () => new Listr(olmTasks.getDeleteTasks(flags)),
})
tasks.add({
title: 'Wait until all pods are deleted',
task: () => new Listr(cheTasks.getWaitPodsDeletedTasks()),
})

// Remove devworkspace controller only if there are no more cheClusters after olm/operator tasks
tasks.add({
title: 'Uninstall Dev Workspace Operator',
task: async (_ctx: any, task: any) => {
try {
const checlusters = await kube.getAllCheClusters()
if (checlusters.length === 0) {
const tasks = new Listr()

if (await olmDevWorkspaceTasks.isCustomDevWorkspaceCatalogExists()) {
tasks.add(devWorkspaceTasks.deleteDevOperatorCRsAndCRDsTasks())
tasks.add(olmDevWorkspaceTasks.deleteResourcesTasks())
tasks.add(devWorkspaceTasks.deleteDevWorkspaceWebhooksTasks(OPENSHIFT_OPERATORS_NAMESPACE))
}

if (!await olmDevWorkspaceTasks.isDevWorkspaceOperatorInstalledViaOLM()) {
tasks.add(devWorkspaceTasks.deleteDevOperatorCRsAndCRDsTasks())
tasks.add(devWorkspaceTasks.deleteResourcesTasks())
tasks.add(devWorkspaceTasks.deleteDevWorkspaceWebhooksTasks(WORKSPACE_CONTROLLER_NAMESPACE))
}

return tasks
task: async (_ctx: any, _task: any) => {
if (flags['delete-all']) {
const olmDevWorkspaceTasks = new OLMDevWorkspaceTasks(flags)
const devWorkspaceTasks = new DevWorkspaceTasks(flags)

const tasks = new Listrq([], ctx.listrOptions)
tasks.add({
title: 'Delete Custom Resources',
task: () => new Listr(devWorkspaceTasks.getDeleteCRsTasks()),
})

let devWorkspaceNamespace = WORKSPACE_CONTROLLER_NAMESPACE
if (await olmDevWorkspaceTasks.isDevWorkspaceOperatorInstalledViaOLM()) {
devWorkspaceNamespace = OPENSHIFT_OPERATORS_NAMESPACE

tasks.add({
title: 'Delete OLM resources',
task: () => new Listr(olmDevWorkspaceTasks.getDeleteTasks()),
})
}

task.title = `${task.title}...[Skipped: another Eclipse Che instance found]`
} catch (e: any) {
task.title = `${task.title}...[Failed: ${e.message}]`
tasks.add({
title: 'Delete operator resources',
task: () => new Listr(devWorkspaceTasks.getDeleteTasks(devWorkspaceNamespace)),
})

return tasks
}
},
})

if (flags['delete-namespace']) {
tasks.add(cheTasks.getDeleteNamespaceTasks(flags))
}
tasks.add({
title: 'Uninstall Eclipse Che Operator',
task: async (_ctx: any, _task: any) => {
const operatorTasks = new OperatorTasks(flags)
const olmTasks = new OLMTasks(flags)
const cheTasks = new CheTasks(flags)

const tasks = new Listrq([], ctx.listrOptions)
tasks.add({
title: 'Delete Custom Resources',
task: () => new Listr(operatorTasks.getDeleteCRsTasks(flags)),
})
tasks.add({
title: 'Delete OLM resources',
task: () => new Listr(olmTasks.getDeleteTasks(flags)),
})
tasks.add({
title: 'Delete operator resources',
task: () => new Listr(operatorTasks.getDeleteTasks(flags)),
})
tasks.add({
title: 'Wait until all pods are deleted',
task: () => new Listr(cheTasks.getWaitPodsDeletedTasks()),
})
if (flags['delete-namespace'] || flags['delete-all']) {
tasks.add(cheTasks.getDeleteNamespaceTasks(flags))
}

return tasks
},
})

if (flags.batch || await this.isDeletionConfirmed(flags)) {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const CHE_CLUSTER_API_VERSION_V2 = 'v2'
export const CHE_CLUSTER_KIND_PLURAL = 'checlusters'

export const DEVFILE_WORKSPACE_API_GROUP = 'workspace.devfile.io'
export const DEVFILE_WORKSPACE_API_VERSION = 'v1alpha2'
export const DEVFILE_WORKSPACE_API_VERSION = 'v1alpha1'
export const DEVFILE_WORKSPACE_KIND_PLURAL = 'devworkspaces'

export const DEVFILE_WORKSPACE_ROUTINGS_API_GROUP = 'controller.devfile.io'
Expand Down
Loading

0 comments on commit 24265f6

Please sign in to comment.