Skip to content

Commit

Permalink
fix: Don't remove CRD if another deployments found (#828)
Browse files Browse the repository at this point in the history
* Don't remove CRD if another deployments found

Signed-off-by: Anatolii Bazko <abazko@redhat.com>
  • Loading branch information
tolusha committed Aug 28, 2020
1 parent 535a609 commit 90f4700
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
18 changes: 15 additions & 3 deletions src/api/kube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1193,11 +1193,10 @@ export class KubeHelper {
}
}

async deleteCrd(name = '') {
async deleteCrd(name: string): Promise<void> {
const k8sApiextensionsApi = KubeHelper.KUBE_CONFIG.makeApiClient(ApiextensionsV1beta1Api)
try {
const options = new V1DeleteOptions()
await k8sApiextensionsApi.deleteCustomResourceDefinition(name, undefined, options)
await k8sApiextensionsApi.deleteCustomResourceDefinition(name)
} catch (e) {
throw this.wrapK8sClientError(e)
}
Expand Down Expand Up @@ -1309,6 +1308,19 @@ export class KubeHelper {
}
}

/**
* Returns all `checlusters.org.eclipse.che' resources
*/
async getAllCheCluster(): Promise<any[]> {
const customObjectsApi = KubeHelper.KUBE_CONFIG.makeApiClient(CustomObjectsApi)
try {
const { body } = await customObjectsApi.listClusterCustomObject('org.eclipse.che', 'v1', 'checlusters')
return body.items ? body.items : []
} catch (e) {
throw this.wrapK8sClientError(e)
}
}

/**
* Deletes `checlusters.org.eclipse.che' resources in the given namespace.
*/
Expand Down
24 changes: 19 additions & 5 deletions src/tasks/installers/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export class OperatorTasks {
command.error('ERROR: It looks like you don\'t have enough privileges. You need to grant more privileges to current user or use a different user. If you are using minishift you can "oc login -u system:admin"')
}
task.title = `${task.title}...updated.`
// it is needed to check the legacy cluster object name to be compatible with previous installations
// it is needed to check the legacy cluster object name to be compatible with previous installations
} else if (legacyClusterRoleExists) {
const statusCode = await kube.replaceClusterRoleFromFile(yamlFilePath, this.operatorClusterRole)
if (statusCode === 403) {
Expand Down Expand Up @@ -266,7 +266,7 @@ export class OperatorTasks {
if (clusterRoleBindExists) {
await kube.replaceClusterRoleBinding(clusterRoleBindingName, this.operatorServiceAccount, flags.chenamespace, clusterRoleName)
task.title = `${task.title}...updated.`
// it is needed to check the legacy cluster object name to be compatible with previous installations
// it is needed to check the legacy cluster object name to be compatible with previous installations
} else if (legacyClusterRoleBindExists) {
await kube.replaceClusterRoleBinding(this.operatorClusterRoleBinding, this.operatorServiceAccount, flags.chenamespace, this.operatorClusterRole)
task.title = `Updating ClusterRoleBinding ${this.operatorClusterRoleBinding}...updated.`
Expand Down Expand Up @@ -335,10 +335,24 @@ export class OperatorTasks {
title: `Delete the Custom Resource of type ${CHE_CLUSTER_CRD}`,
task: async (_ctx: any, task: any) => {
await kh.deleteCheCluster(flags.chenamespace)
await cli.wait(2000) //wait a couple of secs for the finalizers to be executed
do {
await cli.wait(2000) //wait a couple of secs for the finalizers to be executed
} while (await kh.getCheCluster(flags.chenamespace))
task.title = await `${task.title}...OK`
}
},
{
title: `Delete CRD ${this.cheClusterCrd}`,
task: async (_ctx: any, task: any) => {
const checlusters = await kh.getAllCheCluster()
if (checlusters.length > 0) {
task.title = await `${task.title}...Skipped: another Eclipse Che deployment found.`
} else {
await kh.deleteCrd(this.cheClusterCrd)
task.title = await `${task.title}...OK`
}
}
},
{
title: `Delete role binding ${this.operatorRoleBinding}`,
task: async (_ctx: any, task: any) => {
Expand All @@ -365,7 +379,7 @@ export class OperatorTasks {
if (clusterRoleBindExists) {
await kh.deleteClusterRoleBinding(clusterRoleBindingName)
task.title = await `${task.title}...OK`
// it is needed to check the legacy cluster object name to be compatible with previous installations
// it is needed to check the legacy cluster object name to be compatible with previous installations
} else if (legacyClusterRoleBindExists) {
await kh.deleteClusterRoleBinding(this.operatorClusterRoleBinding)
task.title = await `Delete cluster role binding ${this.operatorClusterRoleBinding}...OK`
Expand All @@ -380,7 +394,7 @@ export class OperatorTasks {
if (clusterRoleExists) {
await kh.deleteClusterRole(clusterRoleName)
task.title = await `${task.title}...OK`
// it is needed to check the legacy cluster object name to be compatible with previous installations
// it is needed to check the legacy cluster object name to be compatible with previous installations
} else if (legacyClusterRoleExists) {
await kh.deleteClusterRole(this.operatorClusterRole)
task.title = await `Delete cluster role ${this.operatorClusterRole}...OK`
Expand Down

0 comments on commit 90f4700

Please sign in to comment.