diff --git a/src/commands/server/start.ts b/src/commands/server/start.ts index 832f9fb7f..448bef837 100644 --- a/src/commands/server/start.ts +++ b/src/commands/server/start.ts @@ -10,6 +10,7 @@ import { Command, flags } from '@oclif/command' import { boolean, string } from '@oclif/parser/lib/flags' +import { cli } from 'cli-ux' import * as fs from 'fs-extra' import * as yaml from 'js-yaml' import * as Listr from 'listr' @@ -17,6 +18,7 @@ import * as notifier from 'node-notifier' import * as os from 'os' import * as path from 'path' +import { KubeHelper } from '../../api/kube' import { cheDeployment, cheNamespace, listrRenderer, skipKubeHealthzCheck as skipK8sHealthCheck } from '../../common-flags' import { DEFAULT_CHE_IMAGE, DEFAULT_CHE_OPERATOR_IMAGE, DOCS_LINK_INSTALL_TLS_WITH_SELF_SIGNED_CERT } from '../../constants' import { CheTasks } from '../../tasks/che' @@ -24,7 +26,7 @@ import { getRetrieveKeycloakCredentialsTask, retrieveCheCaCertificateTask } from import { InstallerTasks } from '../../tasks/installers/installer' import { ApiTasks } from '../../tasks/platforms/api' import { PlatformTasks } from '../../tasks/platforms/platform' -import { isOpenshiftPlatformFamily, setDefaultInstaller } from '../../util' +import { isOpenshiftPlatformFamily } from '../../util' export default class Start extends Command { static description = 'start Eclipse Che server' @@ -192,7 +194,7 @@ export default class Start extends Command { flags.tls = await this.checkTlsMode(flags) if (!flags.installer) { - await setDefaultInstaller(flags) + await this.setDefaultInstaller(flags) } if (!flags.templates) { @@ -417,4 +419,19 @@ export default class Start extends Command { this.exit(0) } + + /** + * Sets default installer which is `olm` for OpenShift 4 with stable version of chectl + * and `operator` for other cases. + */ + async setDefaultInstaller(flags: any): Promise { + const cheVersion = DEFAULT_CHE_OPERATOR_IMAGE.split(':')[1] + const kubeHelper = new KubeHelper(flags) + if (flags.platform === 'openshift' && await kubeHelper.isOpenShift4() && cheVersion !== 'nightly' && cheVersion !== 'latest') { + flags.installer = 'olm' + } else { + flags.installer = 'operator' + cli.info(`› Installer type is set to: '${flags.installer}'`) + } + } } diff --git a/src/commands/server/update.ts b/src/commands/server/update.ts index 5bae999af..9924df062 100644 --- a/src/commands/server/update.ts +++ b/src/commands/server/update.ts @@ -21,9 +21,10 @@ import { cheDeployment, cheNamespace, listrRenderer, skipKubeHealthzCheck } from import { CHE_CLUSTER_CR_NAME, DEFAULT_CHE_OPERATOR_IMAGE } from '../../constants' import { CheTasks } from '../../tasks/che' import { InstallerTasks } from '../../tasks/installers/installer' +import { OLMTasks } from '../../tasks/installers/olm' import { ApiTasks } from '../../tasks/platforms/api' import { PlatformTasks } from '../../tasks/platforms/platform' -import { isKubernetesPlatformFamily, setDefaultInstaller } from '../../util' +import { isKubernetesPlatformFamily } from '../../util' export default class Update extends Command { static description = 'update Eclipse Che server' @@ -75,7 +76,7 @@ export default class Update extends Command { async checkIfInstallerSupportUpdating(flags: any) { // matrix checks if (!flags.installer) { - await setDefaultInstaller(flags) + await this.setDefaultInstaller(flags) } if (flags.installer === 'operator' || flags.installer === 'olm') { @@ -167,4 +168,15 @@ export default class Update extends Command { flags.domain = cheCluster.spec.k8s.ingressDomain } } + + async setDefaultInstaller(flags: any): Promise { + const kubeHelper = new KubeHelper(flags) + try { + await kubeHelper.getOperatorSubscription(OLMTasks.SUBSCRIPTION_NAME, flags.chenamespace) + flags.installer = 'olm' + } catch { + flags.installer = 'operator' + } + cli.info(`› Installer type is set to: '${flags.installer}'`) + } } diff --git a/src/tasks/che.ts b/src/tasks/che.ts index 231932911..29be886b3 100644 --- a/src/tasks/che.ts +++ b/src/tasks/che.ts @@ -492,7 +492,7 @@ export class CheTasks { return [ { title: `${follow ? 'Start following' : 'Read'} Operator logs`, - skip: () => flags.installer && flags.installer !== 'operator', + skip: () => flags.installer !== 'operator' && flags.installer !== 'olm', task: async (ctx: any, task: any) => { await this.che.readPodLog(flags.chenamespace, this.cheOperatorSelector, ctx.directory, follow) task.title = `${task.title}...done` diff --git a/src/tasks/installers/olm.ts b/src/tasks/installers/olm.ts index 898d0bae9..d10b12f5d 100644 --- a/src/tasks/installers/olm.ts +++ b/src/tasks/installers/olm.ts @@ -20,9 +20,9 @@ import { isKubernetesPlatformFamily } from '../../util' import { checkTlsCertificate, copyOperatorResources, createEclipseCheCluster, createNamespaceTask } from './common-tasks' export class OLMTasks { - private readonly customCatalogSourceName = 'eclipse-che-custom-catalog-source' - private readonly subscriptionName = 'eclipse-che-subscription' - private readonly operatorGroupName = 'che-operator-group' + public static readonly CUSTOM_CATALOG_SOURCE_NAME = 'eclipse-che-custom-catalog-source' + public static readonly SUBSCRIPTION_NAME = 'eclipse-che-subscription' + public static readonly OPERATOR_GROUP_NAME = 'che-operator-group' /** * Returns list of tasks which perform preflight platform checks. @@ -40,10 +40,10 @@ export class OLMTasks { { title: 'Create operator group', task: async (_ctx: any, task: any) => { - if (await kube.operatorGroupExists(this.operatorGroupName, flags.chenamespace)) { + if (await kube.operatorGroupExists(OLMTasks.OPERATOR_GROUP_NAME, flags.chenamespace)) { task.title = `${task.title}...It already exists.` } else { - await kube.createOperatorGroup(this.operatorGroupName, flags.chenamespace) + await kube.createOperatorGroup(OLMTasks.OPERATOR_GROUP_NAME, flags.chenamespace) task.title = `${task.title}...created new one.` } } @@ -57,7 +57,7 @@ export class OLMTasks { ctx.approvalStarategy = flags['auto-update'] ? 'Automatic' : 'Manual' - ctx.sourceName = this.customCatalogSourceName + ctx.sourceName = OLMTasks.CUSTOM_CATALOG_SOURCE_NAME task.title = `${task.title}...done.` } @@ -66,13 +66,13 @@ export class OLMTasks { enabled: () => flags['catalog-source-yaml'], title: 'Create custom catalog source from file', task: async (ctx: any, task: any) => { - if (!await kube.catalogSourceExists(this.customCatalogSourceName, flags.chenamespace)) { + if (!await kube.catalogSourceExists(OLMTasks.CUSTOM_CATALOG_SOURCE_NAME, flags.chenamespace)) { const customCatalogSource: CatalogSource = kube.readCatalogSourceFromFile(flags['catalog-source-yaml']) customCatalogSource.metadata.name = ctx.sourceName customCatalogSource.metadata.namespace = flags.chenamespace await kube.createCatalogSource(customCatalogSource) - await kube.waitCatalogSource(flags.chenamespace, this.customCatalogSourceName) - task.title = `${task.title}...created new one, with name ${this.customCatalogSourceName} in the namespace ${flags.chenamespace}.` + await kube.waitCatalogSource(flags.chenamespace, OLMTasks.CUSTOM_CATALOG_SOURCE_NAME) + task.title = `${task.title}...created new one, with name ${OLMTasks.CUSTOM_CATALOG_SOURCE_NAME} in the namespace ${flags.chenamespace}.` } else { task.title = `${task.title}...It already exists.` } @@ -81,14 +81,14 @@ export class OLMTasks { { title: 'Create operator subscription', task: async (ctx: any, task: any) => { - if (await kube.operatorSubscriptionExists(this.subscriptionName, flags.chenamespace)) { + if (await kube.operatorSubscriptionExists(OLMTasks.SUBSCRIPTION_NAME, flags.chenamespace)) { task.title = `${task.title}...It already exists.` } else { let subscription: Subscription if (!flags['catalog-source-yaml']) { - subscription = this.createSubscription(this.subscriptionName, DEFAULT_CHE_OLM_PACKAGE_NAME, flags.chenamespace, ctx.defaultCatalogSourceNamespace, OLM_STABLE_CHANNEL_NAME, ctx.catalogSourceNameStable, ctx.approvalStarategy, flags['starting-csv']) + subscription = this.createSubscription(OLMTasks.SUBSCRIPTION_NAME, DEFAULT_CHE_OLM_PACKAGE_NAME, flags.chenamespace, ctx.defaultCatalogSourceNamespace, OLM_STABLE_CHANNEL_NAME, ctx.catalogSourceNameStable, ctx.approvalStarategy, flags['starting-csv']) } else { - subscription = this.createSubscription(this.subscriptionName, flags['package-manifest-name'], flags.chenamespace, flags.chenamespace, flags['olm-channel'], ctx.sourceName, ctx.approvalStarategy, flags['starting-csv']) + subscription = this.createSubscription(OLMTasks.SUBSCRIPTION_NAME, flags['package-manifest-name'], flags.chenamespace, flags.chenamespace, flags['olm-channel'], ctx.sourceName, ctx.approvalStarategy, flags['starting-csv']) } await kube.createOperatorSubscription(subscription) task.title = `${task.title}...created new one.` @@ -98,7 +98,7 @@ export class OLMTasks { { title: 'Wait while subscription is ready', task: async (ctx: any, task: any) => { - const installPlan = await kube.waitOperatorSubscriptionReadyForApproval(flags.chenamespace, this.subscriptionName, 600) + const installPlan = await kube.waitOperatorSubscriptionReadyForApproval(flags.chenamespace, OLMTasks.SUBSCRIPTION_NAME, 600) ctx.installPlanName = installPlan.name task.title = `${task.title}...done.` } @@ -129,8 +129,8 @@ export class OLMTasks { { title: 'Check if operator group exists', task: async (_ctx: any, task: any) => { - if (!await kube.operatorGroupExists(this.operatorGroupName, flags.chenamespace)) { - command.error(`Unable to find operator group ${this.operatorGroupName}`) + if (!await kube.operatorGroupExists(OLMTasks.OPERATOR_GROUP_NAME, flags.chenamespace)) { + command.error(`Unable to find operator group ${OLMTasks.OPERATOR_GROUP_NAME}`) } task.title = `${task.title}...done.` } @@ -138,8 +138,8 @@ export class OLMTasks { { title: 'Check if operator subscription exists', task: async (_ctx: any, task: any) => { - if (!await kube.operatorSubscriptionExists(this.subscriptionName, flags.chenamespace)) { - command.error(`Unable to find operator subscription ${this.subscriptionName}`) + if (!await kube.operatorSubscriptionExists(OLMTasks.SUBSCRIPTION_NAME, flags.chenamespace)) { + command.error(`Unable to find operator subscription ${OLMTasks.SUBSCRIPTION_NAME}`) } task.title = `${task.title}...done.` } @@ -153,7 +153,7 @@ export class OLMTasks { { title: 'Get operator installation plan', task: async (ctx: any, task: any) => { - const subscription: Subscription = await kube.getOperatorSubscription(this.subscriptionName, flags.chenamespace) + const subscription: Subscription = await kube.getOperatorSubscription(OLMTasks.SUBSCRIPTION_NAME, flags.chenamespace) if (subscription.status) { if (subscription.status.state === 'AtLatestKnown') { @@ -203,11 +203,11 @@ export class OLMTasks { } }, { - title: `Delete(OLM) operator subscription ${this.subscriptionName}`, + title: `Delete(OLM) operator subscription ${OLMTasks.SUBSCRIPTION_NAME}`, enabled: ctx => ctx.isPreInstalledOLM, task: async (_ctx: any, task: any) => { - if (await kube.operatorSubscriptionExists(this.subscriptionName, flags.chenamespace)) { - await kube.deleteOperatorSubscription(this.subscriptionName, flags.chenamespace) + if (await kube.operatorSubscriptionExists(OLMTasks.SUBSCRIPTION_NAME, flags.chenamespace)) { + await kube.deleteOperatorSubscription(OLMTasks.SUBSCRIPTION_NAME, flags.chenamespace) } task.title = `${task.title}...OK` } @@ -223,20 +223,20 @@ export class OLMTasks { } }, { - title: `Delete(OLM) operator group ${this.operatorGroupName}`, + title: `Delete(OLM) operator group ${OLMTasks.OPERATOR_GROUP_NAME}`, enabled: ctx => ctx.isPreInstalledOLM, task: async (_ctx: any, task: any) => { - if (await kube.operatorGroupExists(this.operatorGroupName, flags.chenamespace)) { - await kube.deleteOperatorGroup(this.operatorGroupName, flags.chenamespace) + if (await kube.operatorGroupExists(OLMTasks.OPERATOR_GROUP_NAME, flags.chenamespace)) { + await kube.deleteOperatorGroup(OLMTasks.OPERATOR_GROUP_NAME, flags.chenamespace) } task.title = `${task.title}...OK` } }, { - title: `Delete(OLM) custom catalog source ${this.customCatalogSourceName}`, + title: `Delete(OLM) custom catalog source ${OLMTasks.CUSTOM_CATALOG_SOURCE_NAME}`, task: async (_ctx: any, task: any) => { - if (await kube.catalogSourceExists(this.customCatalogSourceName, flags.chenamespace)) { - await kube.deleteCatalogSource(flags.chenamespace, this.customCatalogSourceName) + if (await kube.catalogSourceExists(OLMTasks.CUSTOM_CATALOG_SOURCE_NAME, flags.chenamespace)) { + await kube.deleteCatalogSource(flags.chenamespace, OLMTasks.CUSTOM_CATALOG_SOURCE_NAME) } task.title = `${task.title}...OK` } diff --git a/src/util.ts b/src/util.ts index b643e85ff..3b7e8fa46 100644 --- a/src/util.ts +++ b/src/util.ts @@ -8,12 +8,8 @@ * SPDX-License-Identifier: EPL-2.0 **********************************************************************/ -import { cli } from 'cli-ux' import * as commandExists from 'command-exists' -import { KubeHelper } from './api/kube' -import { DEFAULT_CHE_OPERATOR_IMAGE } from './constants' - export const KUBERNETES_CLI = 'kubectl' export const OPENSHIFT_CLI = 'oc' @@ -68,19 +64,3 @@ export function generatePassword(passwodLength: number, charactersSet = '') { export function base64Decode(arg: string): string { return Buffer.from(arg, 'base64').toString('ascii') } - -/** - * Sets default installer which is `olm` for OpenShift 4 with stable version of chectl - * and `operator` for other cases. - */ -export async function setDefaultInstaller(flags: any): Promise { - const cheVersion = DEFAULT_CHE_OPERATOR_IMAGE.split(':')[1] - const kubeHelper = new KubeHelper(flags) - if (flags.platform === 'openshift' && await kubeHelper.isOpenShift4() && cheVersion !== 'nightly' && cheVersion !== 'latest') { - flags.installer = 'olm' - cli.info('OLM installer is used for OpenShift v4.x') - } else { - flags.installer = 'operator' - cli.info('Operator installer is used by default') - } -}