Skip to content

Commit

Permalink
Improve codebase
Browse files Browse the repository at this point in the history
Signed-off-by: Flavius Lacatusu <[email protected]>
  • Loading branch information
flacatus committed Jun 1, 2021
1 parent f51bf32 commit 3fc6858
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 34 deletions.
4 changes: 2 additions & 2 deletions src/api/che.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import * as rimraf from 'rimraf'
import * as unzipper from 'unzipper'

import { OpenShiftHelper } from '../api/openshift'
import { CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL, CHE_ROOT_CA_SECRET_NAME, DEFAULT_CA_CERT_FILE_NAME, OPERATOR_TEMPLATE_DIR } from '../constants'
import { CHE_ROOT_CA_SECRET_NAME, DEFAULT_CA_CERT_FILE_NAME, OPERATOR_TEMPLATE_DIR } from '../constants'
import { base64Decode, downloadFile } from '../util'

import { CheApiClient } from './che-api-client'
Expand Down Expand Up @@ -214,7 +214,7 @@ export class CheHelper {
let adminUsername
let adminPassword

const cheCluster = await this.kube.getCustomResource(cheNamespace, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
const cheCluster = await this.kube.getCheCluster(cheNamespace)
if (!cheCluster || cheCluster.spec.auth.externalIdentityProvider) {
return []
}
Expand Down
23 changes: 22 additions & 1 deletion 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 { 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 @@ -1619,6 +1619,13 @@ export class KubeHelper {
}
}

/**
* Returns `checlusters.org.eclipse.che' in the given namespace.
*/
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.
*/
Expand Down Expand Up @@ -1647,6 +1654,13 @@ export class KubeHelper {
}
}

/**
* 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
*/
Expand All @@ -1664,6 +1678,13 @@ 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.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/api/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Listr = require('listr')
import * as path from 'path'
import * as semver from 'semver'

import { CHECTL_PROJECT_NAME, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL } from '../constants'
import { CHECTL_PROJECT_NAME } from '../constants'
import { CheTasks } from '../tasks/che'
import { getClusterClientCommand, getProjectName, getProjectVersion } from '../util'

Expand Down Expand Up @@ -161,7 +161,7 @@ export namespace VersionHelper {
export async function getCheVersion(flags: any): Promise<string> {
const kube = new KubeHelper(flags)
const cheTasks = new CheTasks(flags)
const cheCluster = await kube.getCustomResource(flags.chenamespace, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
const cheCluster = await kube.getCheCluster(flags.chenamespace)
if (cheCluster && cheCluster.spec.server.cheFlavor !== 'che') {
return cheCluster.status.cheVersion
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/server/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import Listr = require('listr')
import { ChectlContext } from '../../api/context'
import { KubeHelper } from '../../api/kube'
import { assumeYes, batch, cheDeployment, cheNamespace, CHE_TELEMETRY, listrRenderer, skipKubeHealthzCheck } from '../../common-flags'
import { CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, DEFAULT_ANALYTIC_HOOK_NAME } from '../../constants'
import { DEFAULT_ANALYTIC_HOOK_NAME } from '../../constants'
import { CheTasks } from '../../tasks/che'
import { DevWorkspaceTasks } from '../../tasks/component-installers/devfile-workspace-operator-installer'
import { HelmTasks } from '../../tasks/installers/helm'
Expand Down Expand Up @@ -82,7 +82,7 @@ export default class Delete extends Command {
tasks.add({
title: 'Uninstall DevWorkspace Controller and DevWorkspace Che Controller',
task: async (_ctx: any, task: any) => {
const checlusters = await kube.getAllCustomResources(CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, 'checlusters')
const checlusters = await kube.getAllCheClusters()
if (checlusters.length === 0) {
return new Listr(devWorkspaceTasks.getUninstallTasks())
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/server/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { ChectlContext } from '../../api/context'
import { KubeHelper } from '../../api/kube'
import { VersionHelper } from '../../api/version'
import { cheNamespace, CHE_TELEMETRY } from '../../common-flags'
import { CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL, DEFAULT_ANALYTIC_HOOK_NAME } from '../../constants'
import { DEFAULT_ANALYTIC_HOOK_NAME } from '../../constants'
import { findWorkingNamespace } from '../../util'

export default class Status extends Command {
Expand All @@ -39,7 +39,7 @@ export default class Status extends Command {
let openshiftOauth = 'No'

await this.config.runHook(DEFAULT_ANALYTIC_HOOK_NAME, { command: Status.id, flags })
const cr = await kube.getCustomResource(flags.chenamespace, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
const cr = await kube.getCheCluster(flags.chenamespace)
if (cr && cr.spec && cr.spec.auth && cr.spec.auth.openShiftoAuth && await kube.isOpenShift()) {
openshiftOauth = 'Yes'
}
Expand Down
6 changes: 3 additions & 3 deletions src/commands/server/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import * as semver from 'semver'
import { ChectlContext } from '../../api/context'
import { KubeHelper } from '../../api/kube'
import { assumeYes, batch, cheDeployment, cheDeployVersion, cheNamespace, cheOperatorCRPatchYaml, CHE_OPERATOR_CR_PATCH_YAML_KEY, CHE_TELEMETRY, DEPLOY_VERSION_KEY, listrRenderer, skipKubeHealthzCheck } from '../../common-flags'
import { CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL, DEFAULT_ANALYTIC_HOOK_NAME, DEFAULT_CHE_OPERATOR_IMAGE_NAME, MIN_CHE_OPERATOR_INSTALLER_VERSION, SUBSCRIPTION_NAME } from '../../constants'
import { DEFAULT_ANALYTIC_HOOK_NAME, DEFAULT_CHE_OPERATOR_IMAGE_NAME, MIN_CHE_OPERATOR_INSTALLER_VERSION, SUBSCRIPTION_NAME } from '../../constants'
import { checkChectlAndCheVersionCompatibility, downloadTemplates, getPrintHighlightedMessagesTask } from '../../tasks/installers/common-tasks'
import { InstallerTasks } from '../../tasks/installers/installer'
import { ApiTasks } from '../../tasks/platforms/api'
Expand Down Expand Up @@ -170,7 +170,7 @@ export default class Update extends Command {
*/
private async checkComponentImages(flags: any): Promise<void> {
const kubeHelper = new KubeHelper(flags)
const cheCluster = await kubeHelper.getCustomResource(flags.chenamespace, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
const cheCluster = await kubeHelper.getCheCluster(flags.chenamespace)
if (cheCluster.spec.server.cheImage
|| cheCluster.spec.server.cheImageTag
|| cheCluster.spec.server.devfileRegistryImage
Expand Down Expand Up @@ -364,7 +364,7 @@ export default class Update extends Command {
*/
private async setDomainFlag(flags: any): Promise<void> {
const kubeHelper = new KubeHelper(flags)
const cheCluster = await kubeHelper.getCustomResource(flags.chenamespace, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
const cheCluster = await kubeHelper.getCheCluster(flags.chenamespace)
if (cheCluster && cheCluster.spec.k8s && cheCluster.spec.k8s.ingressDomain) {
flags.domain = cheCluster.spec.k8s.ingressDomain
}
Expand Down
6 changes: 3 additions & 3 deletions src/tasks/che.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { CheServerLoginManager } from '../api/che-login-manager'
import { KubeHelper } from '../api/kube'
import { OpenShiftHelper } from '../api/openshift'
import { VersionHelper } from '../api/version'
import { CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL, CHE_OPERATOR_SELECTOR, DOC_LINK, DOC_LINK_RELEASE_NOTES, OUTPUT_SEPARATOR } from '../constants'
import { CHE_OPERATOR_SELECTOR, DOC_LINK, DOC_LINK_RELEASE_NOTES, OUTPUT_SEPARATOR } from '../constants'
import { base64Decode } from '../util'

import { KubeTasks } from './kube'
Expand Down Expand Up @@ -456,7 +456,7 @@ export class CheTasks {
{
title: `Delete consoleLink ${this.cheConsoleLinkName}`,
task: async (_ctx: any, task: any) => {
const checlusters = await this.kube.getAllCustomResources(CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, 'checlusters')
const checlusters = await this.kube.getAllCheClusters()
// Delete the consoleLink only in case if there no more checluster installed
if (checlusters.length === 0) {
await this.kube.deleteConsoleLink(this.cheConsoleLinkName)
Expand Down Expand Up @@ -669,7 +669,7 @@ export class CheTasks {
const cheUrl = await this.che.cheURL(flags.chenamespace)
messages.push(`Users Dashboard : ${cheUrl}`)

const cr = await this.kube.getCustomResource(flags.chenamespace, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
const cr = await this.kube.getCheCluster(flags.chenamespace)
if (ctx.isOpenShift && cr && cr.spec && cr.spec.auth && cr.spec.auth.openShiftoAuth) {
if (cr.status && cr.status.openShiftOAuthUserCredentialsSecret) {
let user = ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ export class DevWorkspaceTasks {
task: async (_ctx: any, task: any) => {
await this.kubeHelper.deleteCustomResource(this.devworkspaceCheNamespace, this.cheManagerApiGroupName, this.cheManagerApiVersionName, this.cheManagersKindPlural)

// wait 20 seconds, default timeout in che operator
// 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)) {
Expand All @@ -308,9 +308,6 @@ export class DevWorkspaceTasks {
}
throw error
}

// wait 2 seconds
await cli.wait(2000)
}

if (!await this.kubeHelper.getCustomResource(this.devworkspaceCheNamespace, this.cheManagerApiGroupName, this.cheManagerApiVersionName, this.cheManagersKindPlural)) {
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/installers/common-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { ChectlContext } from '../../api/context'
import { CheGithubClient } from '../../api/github-client'
import { KubeHelper } from '../../api/kube'
import { VersionHelper } from '../../api/version'
import { CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_CRD, CHE_CLUSTER_KIND_PLURAL, DOCS_LINK_IMPORT_CA_CERT_INTO_BROWSER, OPERATOR_TEMPLATE_DIR } from '../../constants'
import { CHE_CLUSTER_CRD, DOCS_LINK_IMPORT_CA_CERT_INTO_BROWSER, OPERATOR_TEMPLATE_DIR } from '../../constants'
import { getProjectVersion } from '../../util'

export function createNamespaceTask(namespaceName: string, labels: {}): Listr.ListrTask {
Expand Down Expand Up @@ -165,7 +165,7 @@ export function patchingEclipseCheCluster(flags: any, kube: KubeHelper, command:
title: `Patching the Custom Resource of type '${CHE_CLUSTER_CRD}' in the namespace '${flags.chenamespace}'`,
skip: (ctx: any) => isEmpty(ctx[ChectlContext.CR_PATCH]),
task: async (ctx: any, task: any) => {
const cheCluster = await kube.getCustomResource(flags.chenamespace, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
const cheCluster = await kube.getCheCluster(flags.chenamespace)
if (!cheCluster) {
command.error(`Eclipse Che cluster CR is not found in the namespace '${flags.chenamespace}'`)
}
Expand Down
4 changes: 2 additions & 2 deletions src/tasks/installers/olm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import * as path from 'path'
import { KubeHelper } from '../../api/kube'
import { CatalogSource, Subscription } from '../../api/typings/olm'
import { VersionHelper } from '../../api/version'
import { CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL, CUSTOM_CATALOG_SOURCE_NAME, CVS_PREFIX, DEFAULT_CHE_OLM_PACKAGE_NAME, DEFAULT_OLM_KUBERNETES_NAMESPACE, DEFAULT_OPENSHIFT_MARKET_PLACE_NAMESPACE, KUBERNETES_OLM_CATALOG, NIGHTLY_CATALOG_SOURCE_NAME, OLM_NIGHTLY_CHANNEL_NAME, OLM_STABLE_CHANNEL_NAME, OPENSHIFT_OLM_CATALOG, OPERATOR_GROUP_NAME, SUBSCRIPTION_NAME } from '../../constants'
import { CUSTOM_CATALOG_SOURCE_NAME, CVS_PREFIX, DEFAULT_CHE_OLM_PACKAGE_NAME, DEFAULT_OLM_KUBERNETES_NAMESPACE, DEFAULT_OPENSHIFT_MARKET_PLACE_NAMESPACE, KUBERNETES_OLM_CATALOG, NIGHTLY_CATALOG_SOURCE_NAME, OLM_NIGHTLY_CHANNEL_NAME, OLM_STABLE_CHANNEL_NAME, OPENSHIFT_OLM_CATALOG, OPERATOR_GROUP_NAME, SUBSCRIPTION_NAME } from '../../constants'
import { isKubernetesPlatformFamily } from '../../util'

import { createEclipseCheCluster, createNamespaceTask, patchingEclipseCheCluster } from './common-tasks'
Expand Down Expand Up @@ -198,7 +198,7 @@ export class OLMTasks {
{
title: 'Prepare Eclipse Che cluster CR',
task: async (ctx: any, task: any) => {
const cheCluster = await kube.getCustomResource(flags.chenamespace, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
const cheCluster = await kube.getCheCluster(flags.chenamespace)
if (cheCluster) {
task.title = `${task.title}...It already exists..`
return
Expand Down
16 changes: 8 additions & 8 deletions src/tasks/installers/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export class OperatorTasks {
{
title: 'Prepare Eclipse Che cluster CR',
task: async (ctx: any, task: any) => {
const cheCluster = await kube.getCustomResource(flags.chenamespace, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
const cheCluster = await kube.getCheCluster(flags.chenamespace)
if (cheCluster) {
task.title = `${task.title}...It already exists..`
return
Expand Down Expand Up @@ -502,7 +502,7 @@ export class OperatorTasks {
return [{
title: 'Delete oauthClientAuthorizations',
task: async (_ctx: any, task: any) => {
const checluster = await kh.getCustomResource(flags.chenamespace, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
const checluster = await kh.getCheCluster(flags.chenamespace)
if (checluster && checluster.spec && checluster.spec.auth && checluster.spec.auth.oAuthClientName) {
const oAuthClientAuthorizations = await kh.getOAuthClientAuthorizations(checluster.spec.auth.oAuthClientName)
await kh.deleteOAuthClientAuthorizations(oAuthClientAuthorizations)
Expand All @@ -513,24 +513,24 @@ export class OperatorTasks {
{
title: `Delete the Custom Resource of type ${CHE_CLUSTER_CRD}`,
task: async (_ctx: any, task: any) => {
await kh.deleteCustomResource(flags.chenamespace, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
await kh.deleteCheCluster(flags.chenamespace)

// wait 20 seconds, default timeout in che operator
for (let index = 0; index < 20; index++) {
await cli.wait(1000)
if (!await kh.getCustomResource(flags.chenamespace, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)) {
if (!await kh.getCheCluster(flags.chenamespace)) {
task.title = `${task.title}...OK`
return
}
}

// if checluster still exists then remove finalizers and delete again
const checluster = await kh.getCustomResource(flags.chenamespace, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
const checluster = await kh.getCheCluster(flags.chenamespace)
if (checluster) {
try {
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.getCustomResource(flags.chenamespace, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)) {
if (await kh.getCheCluster(flags.chenamespace)) {
task.title = `${task.title}...OK`
return // successfully removed
}
Expand All @@ -541,7 +541,7 @@ export class OperatorTasks {
await cli.wait(2000)
}

if (!await kh.getCustomResource(flags.chenamespace, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)) {
if (!await kh.getCheCluster(flags.chenamespace)) {
task.title = `${task.title}...OK`
} else {
task.title = `${task.title}...Failed`
Expand All @@ -551,7 +551,7 @@ export class OperatorTasks {
{
title: 'Delete CRDs',
task: async (_ctx: any, task: any) => {
const checlusters = await kh.getAllCustomResources(CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
const checlusters = await kh.getAllCheClusters()
if (checlusters.length > 0) {
task.title = `${task.title}...Skipped: another Eclipse Che deployment found.`
} else {
Expand Down
3 changes: 1 addition & 2 deletions src/tasks/kube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { cli } from 'cli-ux'
import * as Listr from 'listr'

import { KubeHelper } from '../api/kube'
import { CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL } from '../constants'

interface FailState {
reason?: string
Expand Down Expand Up @@ -190,7 +189,7 @@ export class KubeTasks {
}

private async getCheClusterFailState(namespace: string): Promise<FailState | undefined> {
const cheCluster = await this.kubeHelper.getCustomResource(namespace, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
const cheCluster = await this.kubeHelper.getCheCluster(namespace)
if (cheCluster && cheCluster.status && cheCluster.status.reason && cheCluster.status.message) {
return cheCluster.status
}
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export class E2eHelper {

let totalTimeMs = 0
while (totalTimeMs < timeoutMs) {
const cheCR = await this.kubeHelper.getCustomResource(NAMESPACE, CHE_CLUSTER_API_GROUP, CHE_CLUSTER_API_VERSION, CHE_CLUSTER_KIND_PLURAL)
const cheCR = await this.kubeHelper.getCheCluster(NAMESPACE)
if (cheCR && cheCR.status && cheCR.status.cheVersion === version) {
return
}
Expand Down

0 comments on commit 3fc6858

Please sign in to comment.