Skip to content

Commit

Permalink
fix: Detect olm installer type for server:update command (#701)
Browse files Browse the repository at this point in the history
* Detect olm installer type for server:update command

Signed-off-by: Anatoliy Bazko <abazko@redhat.com>
  • Loading branch information
tolusha authored May 18, 2020
1 parent 96decfd commit e980051
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 52 deletions.
21 changes: 19 additions & 2 deletions src/commands/server/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

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'
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'
Expand All @@ -25,7 +27,7 @@ import { InstallerTasks } from '../../tasks/installers/installer'
import { ApiTasks } from '../../tasks/platforms/api'
import { CommonPlatformTasks } from '../../tasks/platforms/common-platform-tasks'
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'
Expand Down Expand Up @@ -193,7 +195,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) {
Expand Down Expand Up @@ -406,4 +408,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<void> {
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}'`)
}
}
}
16 changes: 14 additions & 2 deletions src/commands/server/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ import { CHE_CLUSTER_CR_NAME, DEFAULT_CHE_OPERATOR_IMAGE } from '../../constants
import { CheTasks } from '../../tasks/che'
import { getPrintHighlightedMessagesTask } from '../../tasks/installers/common-tasks'
import { InstallerTasks } from '../../tasks/installers/installer'
import { OLMTasks } from '../../tasks/installers/olm'
import { ApiTasks } from '../../tasks/platforms/api'
import { CommonPlatformTasks } from '../../tasks/platforms/common-platform-tasks'
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'
Expand Down Expand Up @@ -77,7 +78,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') {
Expand Down Expand Up @@ -177,4 +178,15 @@ export default class Update extends Command {
flags.domain = cheCluster.spec.k8s.ingressDomain
}
}

async setDefaultInstaller(flags: any): Promise<void> {
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}'`)
}
}
2 changes: 1 addition & 1 deletion src/tasks/che.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
54 changes: 27 additions & 27 deletions src/tasks/installers/olm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.`
}
}
Expand All @@ -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.`
}
Expand All @@ -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.`
}
Expand All @@ -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.`
Expand All @@ -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.`
}
Expand Down Expand Up @@ -129,17 +129,17 @@ 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.`
}
},
{
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.`
}
Expand All @@ -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') {
Expand Down Expand Up @@ -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`
}
Expand All @@ -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`
}
Expand Down
20 changes: 0 additions & 20 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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<void> {
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')
}
}

0 comments on commit e980051

Please sign in to comment.