Skip to content

Commit

Permalink
feat & refactor: TMP
Browse files Browse the repository at this point in the history
Signed-off-by: Sergii Leshchenko <sleshche@redhat.com>
  • Loading branch information
sleshchenko committed Sep 9, 2019
1 parent cbd8828 commit 8076cd0
Show file tree
Hide file tree
Showing 35 changed files with 1,181 additions and 781 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
"axios": "^0.19.0",
"cli-ux": "^5.3.2",
"command-exists": "^1.2.8",
"debug": "^4.1.1",
"eclipse-che": "git://github.com/eclipse/che#master",
"eclipse-che-minishift": "git://github.com/minishift/minishift#master",
"eclipse-che-minishift": "git://github.com/sleshchenko/minishift#updateChe",
"eclipse-che-operator": "git://github.com/eclipse/che-operator#master",
"esprima": "^4.0.1",
"execa": "^2.0.0",
Expand Down
8 changes: 6 additions & 2 deletions src/api/che.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ import { cli } from 'cli-ux'
import * as fs from 'fs'
import * as yaml from 'js-yaml'

import { KubeHelper } from '../api/kube'
import { OpenShiftHelper } from '../api/openshift'

import { Devfile } from './devfile'
import { KubeHelper } from './kube'

export class CheHelper {
defaultCheResponseTimeoutMs = 3000
kc = new KubeConfig()
kube = new KubeHelper()
kube: KubeHelper
oc = new OpenShiftHelper()

constructor(flags: any) {
this.kube = new KubeHelper(flags)
}

async cheServerPodExist(namespace: string): Promise<boolean> {
const kc = new KubeConfig()
kc.loadFromDefault()
Expand Down
25 changes: 25 additions & 0 deletions src/api/kube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,31 @@ export class KubeHelper {
}
}

async deploymentReady(name = '', namespace = ''): Promise<boolean> {
const k8sApi = this.kc.makeApiClient(Apps_v1Api)
try {
const res = await k8sApi.readNamespacedDeployment(name, namespace)
return ((res && res.body &&
res.body.status && res.body.status.readyReplicas
&& res.body.status.readyReplicas > 0) as boolean)
} catch {
return false
}
}

async deploymentStopped(name = '', namespace = ''): Promise<boolean> {
const k8sApi = this.kc.makeApiClient(Apps_v1Api)
try {
const res = await k8sApi.readNamespacedDeployment(name, namespace)
if (res && res.body && res.body.spec && res.body.spec.replicas) {
throw new Error(`Deployment '${name}' without replicas in spec is fetched`)
}
return res.body.spec.replicas === 0
} catch {
return false
}
}

async isDeploymentPaused(name = '', namespace = ''): Promise<boolean> {
const k8sApi = this.kc.makeApiClient(Apps_v1Api)
try {
Expand Down
11 changes: 0 additions & 11 deletions src/api/openshift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,6 @@ export class OpenShiftHelper {
const args = ['delete', 'route', '--all', '--namespace', namespace]
await execa(command, args, { timeout: 60000 })
}
async deploymentConfigExist(name = '', namespace = ''): Promise<boolean> {
const command = 'oc'
const args = ['get', 'deploymentconfig', '--namespace', namespace, '-o', `jsonpath={range.items[?(.metadata.name=='${name}')]}{.metadata.name}{end}`]
const { stdout } = await execa(command, args, { timeout: 60000 })
return stdout.trim().includes(name)
}
async scaleDeploymentConfig(name = '', namespace = '', replicas: number) {
const command = 'oc'
const args = ['scale', 'deploymentconfig', '--namespace', namespace, name, `--replicas=${replicas}`]
await execa(command, args, { timeout: 60000 })
}
async deleteAllDeploymentConfigs(namespace = '') {
const command = 'oc'
const args = ['delete', 'deploymentconfig', '--all', '--namespace', namespace]
Expand Down
216 changes: 23 additions & 193 deletions src/commands/server/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
**********************************************************************/

import { Command, flags } from '@oclif/command'
import { cli } from 'cli-ux'
import * as commandExists from 'command-exists'
import * as Listrq from 'listr'

import { KubeHelper } from '../../api/kube'
import { OpenShiftHelper } from '../../api/openshift'
import { cheNamespace, listrRenderer } from '../../common-flags'
import { HelmHelper } from '../../installers/helm'
import { MinishiftAddonHelper } from '../../installers/minishift-addon'
import { CheTasks } from '../../tasks/che'
import { HelmTasks } from '../../tasks/installers/helm'
import { MinishiftAddonTasks } from '../../tasks/installers/minishift-addon'
import { OperatorTasks } from '../../tasks/installers/operator'
import { K8sTasks } from '../../tasks/platforms/k8s'

export default class Delete extends Command {
static description = 'delete any Che related resource: Kubernetes/OpenShift/Helm'
Expand All @@ -29,194 +29,24 @@ export default class Delete extends Command {

async run() {
const { flags } = this.parse(Delete)
const Listr = require('listr')

const notifier = require('node-notifier')
const kh = new KubeHelper(flags)
const oh = new OpenShiftHelper()
const helm = new HelmHelper()
const msAddon = new MinishiftAddonHelper()
const tasks = new Listr([
{
title: 'Verify Kubernetes API',
task: async (ctx: any, task: any) => {
try {
await kh.checkKubeApi()
ctx.isOpenShift = await kh.isOpenShift()
task.title = await `${task.title}...OK`
if (ctx.isOpenShift) {
task.title = await `${task.title} (it's OpenShift)`
}
} catch (error) {
this.error(`Failed to connect to Kubernetes API. ${error.message}`)
}
}
},
{
title: 'Delete the CR eclipse-che of type checlusters.org.eclipse.che',
task: async (_ctx: any, task: any) => {
if (await kh.crdExist('checlusters.org.eclipse.che') &&
await kh.cheClusterExist('eclipse-che', flags.chenamespace)) {
await kh.deleteCheCluster('eclipse-che', flags.chenamespace)
await cli.wait(2000) //wait a couple of secs for the finalizers to be executed
task.title = await `${task.title}...OK`
} else {
task.title = await `${task.title}...CR not found`
}
}
},
{
title: 'Delete all deployment configs',
enabled: (ctx: any) => ctx.isOpenShift,
task: async (_ctx: any, task: any) => {
await oh.deleteAllDeploymentConfigs(flags.chenamespace)
task.title = await `${task.title}...OK`
}
},
{
title: 'Delete all deployments',
task: async (_ctx: any, task: any) => {
await kh.deleteAllDeployments(flags.chenamespace)
task.title = await `${task.title}...OK`
}
},
{
title: 'Delete CRD checlusters.org.eclipse.che',
task: async (_ctx: any, task: any) => {
if (await kh.crdExist('checlusters.org.eclipse.che')) {
await kh.deleteCrd('checlusters.org.eclipse.che')
}
task.title = await `${task.title}...OK`
}
},
{
title: 'Delete all services',
task: async (_ctx: any, task: any) => {
await kh.deleteAllServices(flags.chenamespace)
task.title = await `${task.title}...OK`
}
},
{
title: 'Delete all ingresses',
enabled: (ctx: any) => !ctx.isOpenShift,
task: async (_ctx: any, task: any) => {
await kh.deleteAllIngresses(flags.chenamespace)
task.title = await `${task.title}...OK`
}
},
{
title: 'Delete all routes',
enabled: (ctx: any) => ctx.isOpenShift,
task: async (_ctx: any, task: any) => {
await oh.deleteAllRoutes(flags.chenamespace)
task.title = await `${task.title}...OK`
}
},
{
title: 'Delete configmaps che and che-operator',
task: async (_ctx: any, task: any) => {
if (await kh.configMapExist('che', flags.chenamespace)) {
await kh.deleteConfigMap('che', flags.chenamespace)
}
if (await kh.configMapExist('che-operator', flags.chenamespace)) {
await kh.deleteConfigMap('che-operator', flags.chenamespace)
}
task.title = await `${task.title}...OK`
}
},
{
title: 'Delete role che-operator',
task: async (_ctx: any, task: any) => {
if (await kh.roleExist('che-operator', flags.chenamespace)) {
await kh.deleteRole('che-operator', flags.chenamespace)
}
task.title = await `${task.title}...OK`
}
},
{
title: 'Delete cluster role che-operator',
task: async (_ctx: any, task: any) => {
if (await kh.clusterRoleExist('che-operator')) {
await kh.deleteClusterRole('che-operator')
}
task.title = await `${task.title}...OK`
}
},
{
title: 'Delete rolebindings che, che-operator, che-workspace-exec and che-workspace-view',
task: async (_ctx: any, task: any) => {
if (await kh.roleBindingExist('che', flags.chenamespace)) {
await kh.deleteRoleBinding('che', flags.chenamespace)
}
if (await kh.roleBindingExist('che-operator', flags.chenamespace)) {
await kh.deleteRoleBinding('che-operator', flags.chenamespace)
}
if (await kh.roleBindingExist('che-workspace-exec', flags.chenamespace)) {
await kh.deleteRoleBinding('che-workspace-exec', flags.chenamespace)
}
if (await kh.roleBindingExist('che-workspace-view', flags.chenamespace)) {
await kh.deleteRoleBinding('che-workspace-view', flags.chenamespace)
}
task.title = await `${task.title}...OK`
}
},
{
title: 'Delete cluster role binding che-operator',
task: async (_ctx: any, task: any) => {
if (await kh.clusterRoleBindingExist('che-operator')) {
await kh.deleteClusterRoleBinding('che-operator')
}
task.title = await `${task.title}...OK`
}
},
{
title: 'Delete service accounts che, che-operator, che-workspace',
task: async (_ctx: any, task: any) => {
if (await kh.serviceAccountExist('che', flags.chenamespace)) {
await kh.deleteServiceAccount('che', flags.chenamespace)
}
if (await kh.roleBindingExist('che-operator', flags.chenamespace)) {
await kh.deleteServiceAccount('che-operator', flags.chenamespace)
}
if (await kh.roleBindingExist('che-workspace', flags.chenamespace)) {
await kh.deleteServiceAccount('che-workspace', flags.chenamespace)
}
task.title = await `${task.title}...OK`
}
},
{
title: 'Delete PVC postgres-data and che-data-volume',
task: async (_ctx: any, task: any) => {
if (await kh.persistentVolumeClaimExist('che-operator', flags.chenamespace)) {
await kh.deletePersistentVolumeClaim('postgres-data', flags.chenamespace)
}
task.title = await `${task.title}...OK`
}
},
{
title: 'Purge che Helm chart',
enabled: (ctx: any) => !ctx.isOpenShift,
task: async (_ctx: any, task: any) => {
if (await !commandExists.sync('helm')) {
task.title = await `${task.title}...OK (Helm not found)`
} else {
await helm.purgeHelmChart('che')
task.title = await `${task.title}...OK`
}
}
},
{
title: 'Remove Che minishift addon',
enabled: (ctx: any) => ctx.isOpenShift,
task: async (_ctx: any, task: any) => {
if (!commandExists.sync('minishift')) {
task.title = await `${task.title}...OK (minishift not found)`
} else {
await msAddon.removeAddon()
task.title = await `${task.title}...OK`
}
}
},
], { renderer: flags['listr-renderer'] as any })

const k8sTasks = new K8sTasks()
const helmTasks = new HelmTasks()
const msAddonTasks = new MinishiftAddonTasks()
const operatorTasks = new OperatorTasks()
const cheTasks = new CheTasks(flags)

let tasks = new Listrq(undefined,
{ renderer: flags['listr-renderer'] as any }
)

tasks.add(k8sTasks.testApiTasks(flags, this))
tasks.add(operatorTasks.deleteTasks(flags))
tasks.add(cheTasks.deleteTasks(flags))
tasks.add(helmTasks.deleteTasks(flags))
tasks.add(msAddonTasks.deleteTasks(flags))

await tasks.run()

Expand Down
Loading

0 comments on commit 8076cd0

Please sign in to comment.