Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove completelly Devworkspace Che-operator when server:delete is executed #1261

Merged
merged 7 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions src/api/kube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { merge } from 'lodash'
import * as net from 'net'
import { Writable } from 'stream'

import { CHE_CLUSTER_CRD, DEFAULT_K8S_POD_ERROR_RECHECK_TIMEOUT, DEFAULT_K8S_POD_WAIT_TIMEOUT, OLM_STABLE_CHANNEL_NAME } from '../constants'
import { CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL, DEFAULT_K8S_POD_ERROR_RECHECK_TIMEOUT, DEFAULT_K8S_POD_WAIT_TIMEOUT, OLM_STABLE_CHANNEL_NAME } from '../constants'
import { getClusterClientCommand, isKubernetesPlatformFamily, safeLoadFromYamlFile } from '../util'

import { V1Certificate } from './typings/cert-manager'
Expand Down Expand Up @@ -790,7 +790,7 @@ export class KubeHelper {
}
}

async patchCheClusterCustomResource(name: string, namespace: string, patch: any): Promise<any | undefined> {
async patchCustomResource(name: string, namespace: string, patch: any, resourceAPIGroup: string, resourceAPIVersion: string, resourcePlural: string): Promise<any | undefined> {
const k8sCoreApi = this.kubeConfig.makeApiClient(CustomObjectsApi)

// It is required to patch content-type, otherwise request will be rejected with 415 (Unsupported media type) error.
Expand All @@ -801,7 +801,7 @@ export class KubeHelper {
}

try {
const res = await k8sCoreApi.patchNamespacedCustomObject('org.eclipse.che', 'v1', namespace, 'checlusters', name, patch, undefined, undefined, undefined, requestOptions)
const res = await k8sCoreApi.patchNamespacedCustomObject(resourceAPIGroup, resourceAPIVersion, namespace, resourcePlural, name, patch, undefined, undefined, undefined, requestOptions)
if (res && res.body) {
return res.body
}
Expand Down Expand Up @@ -1622,10 +1622,17 @@ export class KubeHelper {
/**
* Returns `checlusters.org.eclipse.che' in the given namespace.
*/
async getCheCluster(namespace: string): Promise<any | undefined> {
async getCheCluster(cheNamespace: string): Promise<any | undefined> {
return this.getCustomResource(cheNamespace, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
}

/**
* Returns custom resource in the given namespace.
*/
async getCustomResource(namespace: string, resourceAPIGroup: string, resourceAPIVersion: string, resourcePlural: string): Promise<any | undefined> {
const customObjectsApi = this.kubeConfig.makeApiClient(CustomObjectsApi)
try {
const { body } = await customObjectsApi.listNamespacedCustomObject('org.eclipse.che', 'v1', namespace, 'checlusters')
const { body } = await customObjectsApi.listNamespacedCustomObject(resourceAPIGroup, resourceAPIVersion, namespace, resourcePlural)
if (!(body as any).items) {
return
}
Expand All @@ -1634,30 +1641,37 @@ export class KubeHelper {
if (crs.length === 0) {
return
} else if (crs.length !== 1) {
throw new Error(`Too many resources '${CHE_CLUSTER_CRD}' found in the namespace '${namespace}'`)
throw new Error(`Too many resources of type ${resourcePlural}.${resourceAPIGroup} found in the namespace '${namespace}'`)
}

return crs[0]
} catch (e) {
if (e.response.statusCode === 404) {
// There is no CR 'checluster`
// There is no CRD
return
}
throw this.wrapK8sClientError(e)
}
}

/**
* Returns all `checlusters.org.eclipse.che' resources
* Deletes `checlusters.org.eclipse.che' resources in the given namespace.
*/
async getAllCheClusters(): Promise<any[]> {
return this.getAllCustomResources(CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
}

/**
* Returns all custom resources
*/
async getAllCustomResources(resourceAPIGroup: string, resourceAPIVersion: string, resourcePlural: string): Promise<any[]> {
const customObjectsApi = this.kubeConfig.makeApiClient(CustomObjectsApi)
try {
const { body } = await customObjectsApi.listClusterCustomObject('org.eclipse.che', 'v1', 'checlusters')
const { body } = await customObjectsApi.listClusterCustomObject(resourceAPIGroup, resourceAPIVersion, resourcePlural)
return (body as any).items ? (body as any).items : []
} catch (e) {
if (e.response.statusCode === 404) {
// There is no CRD 'checlusters`
// There is no CRD
return []
}
throw this.wrapK8sClientError(e)
Expand All @@ -1668,20 +1682,27 @@ export class KubeHelper {
* Deletes `checlusters.org.eclipse.che' resources in the given namespace.
*/
async deleteCheCluster(namespace: string): Promise<void> {
return this.deleteCustomResource(namespace, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
}

/**
* Deletes custom resources in the given namespace.
*/
async deleteCustomResource(namespace: string, resourceAPIGroup: string, resourceAPIVersion: string, resourcePlural: string): Promise<void> {
const customObjectsApi = this.kubeConfig.makeApiClient(CustomObjectsApi)
try {
const { body } = await customObjectsApi.listNamespacedCustomObject('org.eclipse.che', 'v1', namespace, 'checlusters')
const { body } = await customObjectsApi.listNamespacedCustomObject(resourceAPIGroup, resourceAPIVersion, namespace, resourcePlural)
if (!(body as any).items) {
return
}

const crs = (body as any).items as any[]
for (const cr of crs) {
await customObjectsApi.deleteNamespacedCustomObject('org.eclipse.che', 'v1', namespace, 'checlusters', cr.metadata.name)
await customObjectsApi.deleteNamespacedCustomObject(resourceAPIGroup, resourceAPIVersion, namespace, resourcePlural, cr.metadata.name)
}
} catch (e) {
if (e.response.statusCode === 404) {
// There is no CRD 'checlusters`
// There is no CRD
return
}
throw this.wrapK8sClientError(e)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/server/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default class Delete extends Command {

// Remove devworkspace controller only if there are no more cheClusters after olm/operator tasks
tasks.add({
title: 'Uninstall DevWorkspace Controller',
title: 'Uninstall DevWorkspace Controller and DevWorkspace Che Controller',
task: async (_ctx: any, task: any) => {
const checlusters = await kube.getAllCheClusters()
if (checlusters.length === 0) {
Expand Down
7 changes: 6 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const CHE_TLS_SECRET_NAME = 'che-tls'
export const CHE_ROOT_CA_SECRET_NAME = 'self-signed-certificate'
export const DEFAULT_CA_CERT_FILE_NAME = 'cheCA.crt'
export const CHE_CLUSTER_CR_NAME = 'eclipse-che'
export const CHE_CLUSTER_CRD = 'checlusters.org.eclipse.che'

// operator
export const OPERATOR_DEPLOYMENT_NAME = 'che-operator'
Expand Down Expand Up @@ -77,3 +76,9 @@ export const DEFAULT_ANALYTIC_HOOK_NAME = 'analytics'
// Timeouts
export const DEFAULT_K8S_POD_WAIT_TIMEOUT = 600000
export const DEFAULT_K8S_POD_ERROR_RECHECK_TIMEOUT = 15000

// Custom Resources names
export const CHE_CLUSTER_CRD = 'checlusters.org.eclipse.che'
export const CHE_CLUSTER_API_GROUP = 'org.eclipse.che'
export const CHE_CLUSTER_API_VERSION = 'v1'
export const CHE_CLUSTER_KIND_PLURAL = 'checlusters'
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/

import { cli } from 'cli-ux'
import * as Listr from 'listr'
import * as path from 'path'

Expand Down Expand Up @@ -84,6 +85,14 @@ export class DevWorkspaceTasks {
protected WTOSubscriptionName = 'web-terminal'
protected WTONamespace = 'openshift-operators'

// Devworkspace Che operator namespace
protected devworkspaceCheNamespace = 'devworkspace-che'

// chemanager k8s object info
protected cheManagerApiGroupName = 'che.eclipse.org'
protected cheManagerApiVersionName = 'v1alpha1'
protected cheManagerCRDName = 'chemanagers.che.eclipse.org'
protected cheManagersKindPlural = 'chemanagers'
constructor(private readonly flags: any) {
this.kubeHelper = new KubeHelper(flags)
this.cheHelper = new CheHelper(flags)
Expand Down Expand Up @@ -273,6 +282,41 @@ export class DevWorkspaceTasks {
task.title = await `${task.title}...OK`
}
},
{
title: `Delete the Custom Resource of type ${this.cheManagerCRDName}`,
task: async (_ctx: any, task: any) => {
await this.kubeHelper.deleteCustomResource(this.devworkspaceCheNamespace, this.cheManagerApiGroupName, this.cheManagerApiVersionName, this.cheManagersKindPlural)

// Timeout of 20 seconds to see if CR are removed
for (let index = 0; index < 20; index++) {
await cli.wait(1000)
if (!await this.kubeHelper.getCustomResource(this.devworkspaceCheNamespace, this.cheManagerApiGroupName, this.cheManagerApiVersionName, this.cheManagersKindPlural)) {
task.title = `${task.title}...OK`
return
}
}

// if chemanager instance still exists then remove finalizers and delete again
const chemanager = await this.kubeHelper.getCustomResource(this.devworkspaceCheNamespace, this.cheManagerApiGroupName, this.cheManagerApiVersionName, this.cheManagersKindPlural)
if (chemanager) {
try {
await this.kubeHelper.patchCustomResource(chemanager.metadata.name, this.devworkspaceCheNamespace, { metadata: { finalizers: null } }, this.cheManagerApiGroupName, this.cheManagerApiVersionName, this.cheManagersKindPlural)
} catch (error) {
if (await this.kubeHelper.getCustomResource(this.devworkspaceCheNamespace, this.cheManagerApiGroupName, this.cheManagerApiVersionName, this.cheManagersKindPlural)) {
task.title = `${task.title}...OK`
return // successfully removed
}
throw error
}
}

if (!await this.kubeHelper.getCustomResource(this.devworkspaceCheNamespace, this.cheManagerApiGroupName, this.cheManagerApiVersionName, this.cheManagersKindPlural)) {
task.title = `${task.title}...OK`
} else {
task.title = `${task.title}...Failed`
}
}
},
]
}
}
4 changes: 2 additions & 2 deletions src/tasks/installers/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import * as path from 'path'
import { ChectlContext } from '../../api/context'
import { KubeHelper } from '../../api/kube'
import { VersionHelper } from '../../api/version'
import { CHE_CLUSTER_CRD, CHE_OPERATOR_SELECTOR, OPERATOR_DEPLOYMENT_NAME, OPERATOR_TEMPLATE_DIR } from '../../constants'
import { CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_CRD, CHE_CLUSTER_KIND_PLURAL, CHE_OPERATOR_SELECTOR, OPERATOR_DEPLOYMENT_NAME, OPERATOR_TEMPLATE_DIR } from '../../constants'
import { getImageNameAndTag, safeLoadFromYamlFile } from '../../util'
import { KubeTasks } from '../kube'

Expand Down Expand Up @@ -528,7 +528,7 @@ export class OperatorTasks {
const checluster = await kh.getCheCluster(flags.chenamespace)
if (checluster) {
try {
await kh.patchCheClusterCustomResource(checluster.metadata.name, flags.chenamespace, { metadata: { finalizers: null } })
await kh.patchCustomResource(checluster.metadata.name, flags.chenamespace, { metadata: { finalizers: null } }, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
} catch (error) {
if (await kh.getCheCluster(flags.chenamespace)) {
task.title = `${task.title}...OK`
Expand Down