Skip to content

Commit

Permalink
Apply backup/restore CRDs if available
Browse files Browse the repository at this point in the history
Signed-off-by: Mykola Morhun <[email protected]>
  • Loading branch information
mmorhun committed May 25, 2021
1 parent bf42397 commit 7119128
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion src/tasks/installers/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { createEclipseCheCluster, createNamespaceTask, patchingEclipseCheCluster
export class OperatorTasks {
operatorServiceAccount = 'che-operator'
cheClusterCrd = 'checlusters.org.eclipse.che'
cheClusterBackupCrd = 'checlusterbackups.org.eclipse.che'
cheClusterRestoreCrd = 'checlusterrestores.org.eclipse.che'
cheManagerCRD = 'chemanagers.che.eclipse.org'
dwRoutingCRD = 'devworkspaceroutings.controller.devfile.io'
legacyClusterResourcesName = 'che-operator'
Expand Down Expand Up @@ -184,6 +186,29 @@ export class OperatorTasks {
}
}
},
{
title: 'Create backup and restore CRDs',
task: async (ctx: any, task: any) => {
const backupCrdExist = await kube.getCrd(this.cheClusterBackupCrd)
const restoreCrdExist = await kube.getCrd(this.cheClusterRestoreCrd)
if (backupCrdExist && restoreCrdExist) {
task.title = `${task.title}...skipped.`
return
}

const backupCrdPath = path.join(ctx.resourcesPath, 'crds', 'org.eclipse.che_checlusterbackups_crd.yaml')
if (!backupCrdExist && fs.existsSync(backupCrdPath)) {
await kube.createCrdFromFile(backupCrdPath)
}

const restoreCrdPath = path.join(ctx.resourcesPath, 'crds', 'org.eclipse.che_checlusterrestores_crd.yaml')
if (!restoreCrdExist && fs.existsSync(restoreCrdPath)) {
await kube.createCrdFromFile(restoreCrdPath)
}

task.title = `${task.title}...done.`
}
},
{
title: `Create CRD ${this.cheManagerCRD}`,
task: async (ctx: any, task: any) => {
Expand Down Expand Up @@ -356,6 +381,34 @@ export class OperatorTasks {
}
}
},
{
title: 'Updating backup and restore CRDs',
task: async (ctx: any, task: any) => {
const existedBackupCRD = await kube.getCrd(this.cheClusterBackupCrd)
const newBackupCRDPath = path.join(ctx.resourcesPath, 'crds', 'org.eclipse.che_checlusterbackups_crd.yaml')
if (existedBackupCRD) {
if (!existedBackupCRD.metadata || !existedBackupCRD.metadata.resourceVersion) {
throw new Error(`Fetched CRD ${this.cheClusterBackupCrd} without resource version`)
}
await kube.replaceCrdFromFile(newBackupCRDPath, existedBackupCRD.metadata.resourceVersion)
} else {
await kube.createCrdFromFile(newBackupCRDPath)
}

const existedRestoreCRD = await kube.getCrd(this.cheClusterRestoreCrd)
const newRestoreCRDPath = path.join(ctx.resourcesPath, 'crds', 'org.eclipse.che_checlusterrestores_crd.yaml')
if (existedRestoreCRD) {
if (!existedRestoreCRD.metadata || !existedRestoreCRD.metadata.resourceVersion) {
throw new Error(`Fetched CRD ${this.cheClusterRestoreCrd} without resource version`)
}
await kube.replaceCrdFromFile(newRestoreCRDPath, existedRestoreCRD.metadata.resourceVersion)
task.title = `${task.title}...updated.`
} else {
await kube.createCrdFromFile(newRestoreCRDPath)
task.title = `${task.title}...created new one.`
}
}
},
{
title: `Updating CRD ${this.cheManagerCRD}`,
task: async (ctx: any, task: any) => {
Expand Down Expand Up @@ -492,13 +545,15 @@ export class OperatorTasks {
}
},
{
title: `Delete CRD ${this.cheClusterCrd}`,
title: 'Delete CRDs',
task: async (_ctx: any, task: any) => {
const checlusters = await kh.getAllCheClusters()
if (checlusters.length > 0) {
task.title = `${task.title}...Skipped: another Eclipse Che deployment found.`
} else {
await kh.deleteCrd(this.cheClusterCrd)
await kh.deleteCrd(this.cheClusterBackupCrd)
await kh.deleteCrd(this.cheClusterRestoreCrd)
task.title = `${task.title}...OK`
}
}
Expand Down

0 comments on commit 7119128

Please sign in to comment.