Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: chectl improvements #948

Merged
merged 1 commit into from
Nov 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 30 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ If you're using linux or macOS, here is how to install chectl by using one singl
```
$ bash <(curl -sL https://www.eclipse.org/che/chectl/)
```

- For `next` channel:
```
$ bash <(curl -sL https://www.eclipse.org/che/chectl/) --channel=next
Expand Down Expand Up @@ -372,16 +372,14 @@ USAGE
$ chectl server:debug

OPTIONS
-h, --help show CLI help

-n, --chenamespace=chenamespace [default: che] Kubernetes namespace where Eclipse Che server is supposed to
be deployed
-h, --help show CLI help

--debug-port=debug-port [default: 8000] Eclipse Che server debug port
-n, --chenamespace=chenamespace [default: che] Kubernetes namespace where Eclipse Che server is supposed to be
deployed

--listr-renderer=default|silent|verbose [default: default] Listr renderer
--debug-port=debug-port [default: 8000] Eclipse Che server debug port

--skip-kubernetes-health-check Skip Kubernetes health check
--skip-kubernetes-health-check Skip Kubernetes health check
```

_See code: [src/commands/server/debug.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/server/debug.ts)_
Expand All @@ -400,6 +398,10 @@ OPTIONS
-n, --chenamespace=chenamespace [default: che] Kubernetes namespace where
Eclipse Che server is supposed to be deployed

-y, --yes Automatic yes to prompts; assume "yes" as
answer to all prompts and run
non-interactively

--delete-namespace Indicates that a Eclipse Che namespace will
be deleted as well

Expand All @@ -410,10 +412,6 @@ OPTIONS
parameter is used only when the workspace
engine is the DevWorkspace

--listr-renderer=default|silent|verbose [default: default] Listr renderer

--skip-deletion-check Skip user confirmation on deletion check

--skip-kubernetes-health-check Skip Kubernetes health check
```

Expand Down Expand Up @@ -462,17 +460,6 @@ OPTIONS
Type of Kubernetes platform. Valid values are "minikube", "minishift", "k8s (for kubernetes)", "openshift", "crc
(for CodeReady Containers)", "microk8s".

-s, --tls
Deprecated. Enable TLS encryption.
Note, this option is turned on by default.
To provide own certificate for Kubernetes infrastructure, 'che-tls' secret with TLS certificate
must be pre-created in the configured namespace.
In case of providing own self-signed certificate 'self-signed-certificate' secret should be
also created.
For OpenShift, router will use default cluster certificates.
Please see the docs how to deploy Eclipse Che on different infrastructures:
https://www.eclipse.org/che/docs/che-7/overview/running-che-locally/

-t, --templates=templates
Path to the templates folder

Expand Down Expand Up @@ -534,9 +521,6 @@ OPTIONS
--k8spodwaittimeout=k8spodwaittimeout
[default: 300000] Waiting time for Pod Wait Timeout Kubernetes (in milliseconds)

--listr-renderer=default|silent|verbose
[default: default] Listr renderer

--olm-channel=olm-channel
Olm channel to install Eclipse Che, f.e. stable.
If options was not set, will be used default version for package manifest.
Expand All @@ -552,9 +536,6 @@ OPTIONS
--postgres-pvc-storage-class-name=postgres-pvc-storage-class-name
persistent volume storage class name to use to store Eclipse Che postgres database

--self-signed-cert
Deprecated. The flag is ignored. Self signed certificates usage is autodetected now.

--skip-cluster-availability-check
Skip cluster availability check. The check is a simple request to ensure the cluster is reachable.

Expand Down Expand Up @@ -594,17 +575,15 @@ USAGE
$ chectl server:logs

OPTIONS
-d, --directory=directory Directory to store logs into
-h, --help show CLI help

-n, --chenamespace=chenamespace [default: che] Kubernetes namespace where Eclipse Che server is supposed to
be deployed
-d, --directory=directory Directory to store logs into
-h, --help show CLI help

--deployment-name=deployment-name [default: che] Eclipse Che deployment name
-n, --chenamespace=chenamespace [default: che] Kubernetes namespace where Eclipse Che server is supposed to be
deployed

--listr-renderer=default|silent|verbose [default: default] Listr renderer
--deployment-name=deployment-name [default: che] Eclipse Che deployment name

--skip-kubernetes-health-check Skip Kubernetes health check
--skip-kubernetes-health-check Skip Kubernetes health check
```

_See code: [src/commands/server/logs.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/server/logs.ts)_
Expand Down Expand Up @@ -658,9 +637,6 @@ OPTIONS
[default: devworkspace-controller] Namespace for the DevWorkspace controller. This parameter is used only when the
workspace engine is the DevWorkspace

--listr-renderer=default|silent|verbose
[default: default] Listr renderer

--skip-kubernetes-health-check
Skip Kubernetes health check
```
Expand All @@ -676,44 +652,27 @@ USAGE
$ chectl server:update

OPTIONS
-a, --installer=operator|olm Installer type. If not set, default is
autodetected depending on previous
installation.

-h, --help show CLI help

-n, --chenamespace=chenamespace [default: che] Kubernetes namespace where
Eclipse Che server is supposed to be
deployed

-p, --platform=minikube|minishift|k8s|openshift|microk8s|docker-desktop|crc Type of Kubernetes platform. Valid values
are "minikube", "minishift", "k8s (for
kubernetes)", "openshift", "crc (for
CodeReady Containers)", "microk8s".
-h, --help show CLI help

-t, --templates=templates [default: templates] Path to the
templates folder
-n, --chenamespace=chenamespace [default: che] Kubernetes namespace where Eclipse Che server
is supposed to be deployed

--che-operator-cr-patch-yaml=che-operator-cr-patch-yaml Path to a yaml file that overrides the
default values in CheCluster CR used by
the operator. This parameter is used only
when the installer is the 'operator' or
the 'olm'.
-t, --templates=templates [default: templates] Path to the templates folder

--che-operator-image=che-operator-image [default:
quay.io/eclipse/che-operator:nightly]
Container image of the operator. This
parameter is used only when the installer
is the operator
-y, --yes Automatic yes to prompts; assume "yes" as answer to all
prompts and run non-interactively

--deployment-name=deployment-name [default: che] Eclipse Che deployment
name
--che-operator-cr-patch-yaml=che-operator-cr-patch-yaml Path to a yaml file that overrides the default values in
CheCluster CR used by the operator. This parameter is used
only when the installer is the 'operator' or the 'olm'.

--listr-renderer=default|silent|verbose [default: default] Listr renderer
--che-operator-image=che-operator-image [default: quay.io/eclipse/che-operator:nightly] Container
image of the operator. This parameter is used only when the
installer is the operator

--skip-kubernetes-health-check Skip Kubernetes health check
--deployment-name=deployment-name [default: che] Eclipse Che deployment name

--skip-version-check Skip user confirmation on version check
--skip-kubernetes-health-check Skip Kubernetes health check
```

_See code: [src/commands/server/update.ts](https://github.com/che-incubator/chectl/blob/v0.0.2/src/commands/server/update.ts)_
Expand Down
75 changes: 52 additions & 23 deletions src/api/kube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/

import { ApiextensionsV1beta1Api, ApisApi, AppsV1Api, AuthorizationV1Api, BatchV1Api, CoreV1Api, CustomObjectsApi, ExtensionsV1beta1Api, ExtensionsV1beta1IngressList, KubeConfig, Log, PortForward, RbacAuthorizationV1Api, V1beta1CustomResourceDefinition, V1ClusterRole, V1ClusterRoleBinding, V1ConfigMap, V1ConfigMapEnvSource, V1Container, V1ContainerStateWaiting, V1Deployment, V1DeploymentList, V1DeploymentSpec, V1EnvFromSource, V1Job, V1JobSpec, V1LabelSelector, V1NamespaceList, V1ObjectMeta, V1PersistentVolumeClaimList, V1Pod, V1PodCondition, V1PodList, V1PodSpec, V1PodTemplateSpec, V1PolicyRule, V1Role, V1RoleBinding, V1RoleRef, V1Secret, V1SelfSubjectAccessReview, V1SelfSubjectAccessReviewSpec, V1Service, V1ServiceAccount, V1ServiceList, V1Subject, Watch } from '@kubernetes/client-node'
import { ApiextensionsV1beta1Api, ApisApi, AppsV1Api, AuthorizationV1Api, BatchV1Api, CoreV1Api, CustomObjectsApi, ExtensionsV1beta1Api, ExtensionsV1beta1IngressList, KubeConfig, Log, PortForward, RbacAuthorizationV1Api, V1beta1CustomResourceDefinition, V1ClusterRole, V1ClusterRoleBinding, V1ConfigMap, V1ConfigMapEnvSource, V1Container, V1ContainerStateTerminated, V1ContainerStateWaiting, V1Deployment, V1DeploymentList, V1DeploymentSpec, V1EnvFromSource, V1Job, V1JobSpec, V1LabelSelector, V1NamespaceList, V1ObjectMeta, V1PersistentVolumeClaimList, V1Pod, V1PodCondition, V1PodList, V1PodSpec, V1PodTemplateSpec, V1PolicyRule, V1Role, V1RoleBinding, V1RoleRef, V1Secret, V1SelfSubjectAccessReview, V1SelfSubjectAccessReviewSpec, V1Service, V1ServiceAccount, V1ServiceList, V1Subject, Watch } from '@kubernetes/client-node'
import { Cluster, Context } from '@kubernetes/client-node/dist/config_types'
import axios, { AxiosRequestConfig } from 'axios'
import { cli } from 'cli-ux'
Expand Down Expand Up @@ -635,6 +635,26 @@ export class KubeHelper {
}
}

async patchCheClusterCustomResource(name: string, namespace: string, patch: any): Promise<any | undefined> {
const k8sCoreApi = KubeHelper.KUBE_CONFIG.makeApiClient(CustomObjectsApi)

// It is required to patch content-type, otherwise request will be rejected with 415 (Unsupported media type) error.
const requestOptions = {
headers: {
'content-type': 'application/merge-patch+json'
}
}

try {
const res = await k8sCoreApi.patchNamespacedCustomObject('org.eclipse.che', 'v1', namespace, 'checlusters', name, patch, undefined, undefined, undefined, requestOptions)
if (res && res.body) {
return res.body
}
} catch (e) {
throw this.wrapK8sClientError(e)
}
}

async patchNamespacedPod(name: string, namespace: string, patch: any): Promise<V1Pod | undefined> {
const k8sCoreApi = KubeHelper.KUBE_CONFIG.makeApiClient(CoreV1Api)

Expand Down Expand Up @@ -691,6 +711,26 @@ export class KubeHelper {
}
}

/**
* Returns pod last terminated state.
*/
async getPodLastTerminatedState(namespace: string, selector: string): Promise<V1ContainerStateTerminated | undefined> {
const pods = await this.getPodListByLabel(namespace, selector)
if (!pods.length) {
return
}

for (const pod of pods) {
if (pod.status && pod.status.containerStatuses) {
for (const status of pod.status.containerStatuses) {
if (status.lastState) {
return status.lastState.terminated
}
}
}
}
}

async getPodCondition(namespace: string, selector: string, conditionType: string): Promise<V1PodCondition[]> {
const k8sCoreApi = KubeHelper.KUBE_CONFIG.makeApiClient(CoreV1Api)
let res
Expand Down Expand Up @@ -1336,8 +1376,8 @@ export class KubeHelper {
}

// override default values
if (ctx.CRPatch) {
merge(cheClusterCR, ctx.CRPatch)
if (ctx.crPatch) {
merge(cheClusterCR, ctx.crPatch)
}

// Back off some configuration properties(chectl estimated them like not working or not desired)
Expand Down Expand Up @@ -1443,7 +1483,7 @@ export class KubeHelper {
}
}

async getAmoutUsers(): Promise<number> {
async getUsersNumber(): Promise<number> {
const customObjectsApi = KubeHelper.KUBE_CONFIG.makeApiClient(CustomObjectsApi)
let amountOfUsers: number
try {
Expand Down Expand Up @@ -1956,23 +1996,15 @@ export class KubeHelper {
}

async isOpenShift(): Promise<boolean> {
const k8sApiApi = KubeHelper.KUBE_CONFIG.makeApiClient(ApisApi)
let res
const k8sCoreApi = KubeHelper.KUBE_CONFIG.makeApiClient(ApisApi)

try {
res = await k8sApiApi.getAPIVersions()
const res = await k8sCoreApi.getAPIVersions()
return res && res.body && res.body.groups &&
res.body.groups.some(group => group.name === 'apps.openshift.io')
} catch (e) {
throw this.wrapK8sClientError(e)
}
if (!res || !res.body) {
throw new Error('Get API versions returned an invalid response')
}
const v1APIGroupList = res.body
for (const v1APIGroup of v1APIGroupList.groups) {
if (v1APIGroup.name === 'apps.openshift.io') {
return true
}
}
return false
}

async getIngressHost(name = '', namespace = ''): Promise<string> {
Expand Down Expand Up @@ -2026,12 +2058,9 @@ export class KubeHelper {

try {
const res = await k8sCoreApi.getAPIVersions()
if (res && res.body && res.body.groups) {
return res.body.groups.some(group => group.name === 'route.openshift.io')
&& res.body.groups.some(group => group.name === 'config.openshift.io')
} else {
return false
}
return res && res.body && res.body.groups &&
res.body.groups.some(group => group.name === 'route.openshift.io') &&
res.body.groups.some(group => group.name === 'config.openshift.io')
} catch (e) {
throw this.wrapK8sClientError(e)
}
Expand Down
11 changes: 6 additions & 5 deletions src/api/openshift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
import execa = require('execa')

export class OpenShiftHelper {
async status(): Promise<boolean> {
const command = 'oc'
const args = ['status']
const { exitCode } = await execa(command, args, { timeout: 60000, reject: false })
if (exitCode === 0) { return true } else { return false }
/**
* Check status on existed `default` namespace.
*/
async isOpenShiftRunning(): Promise<boolean> {
const { exitCode } = await execa('oc', ['status', '--namespace', 'default'], { timeout: 60000, reject: false })
return exitCode === 0
}
async getRouteHost(name: string, namespace = ''): Promise<string> {
const command = 'oc'
Expand Down
5 changes: 2 additions & 3 deletions src/api/version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,11 @@ export namespace VersionHelper {
export function getOpenShiftCheckVersionTask(flags: any): Listr.ListrTask {
return {
title: 'Check OpenShift version',
task: async (_ctx: any, task: any) => {
const kubeHelper = new KubeHelper(flags)
task: async (ctx: any, task: any) => {
const actualVersion = await getOpenShiftVersion()
if (actualVersion) {
task.title = `${task.title}: ${actualVersion}.`
} else if (await kubeHelper.isOpenShift4()) {
} else if (ctx.isOpenShift4) {
task.title = `${task.title}: 4.x`
} else {
task.title = `${task.title}: Unknown`
Expand Down
3 changes: 2 additions & 1 deletion src/commands/server/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import * as Listr from 'listr'
import { cheNamespace, listrRenderer, skipKubeHealthzCheck } from '../../common-flags'
import { CheTasks } from '../../tasks/che'
import { ApiTasks } from '../../tasks/platforms/api'
import { initializeContext } from '../../util'

export default class Debug extends Command {
static description = 'Enable local debug of Eclipse Che server'
Expand All @@ -32,7 +33,7 @@ export default class Debug extends Command {

async run() {
const { flags } = this.parse(Debug)
const ctx: any = {}
const ctx = await initializeContext(flags)

const cheTasks = new CheTasks(flags)
const apiTasks = new ApiTasks()
Expand Down
Loading