-
Notifications
You must be signed in to change notification settings - Fork 62
/
deploy.ts
450 lines (417 loc) · 20 KB
/
deploy.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/**
* Copyright (c) 2019-2021 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
import { Command, flags } from '@oclif/command'
import { boolean, string } from '@oclif/parser/lib/flags'
import { cli } from 'cli-ux'
import * as Listr from 'listr'
import { ChectlContext, OIDCContextKeys, OLM } from '../../api/context'
import { KubeHelper } from '../../api/kube'
import { batch, cheDeployment, cheDeployVersion, cheNamespace, cheOperatorCRPatchYaml, cheOperatorCRYaml, CHE_OPERATOR_CR_PATCH_YAML_KEY, CHE_OPERATOR_CR_YAML_KEY, CHE_TELEMETRY, DEPLOY_VERSION_KEY, k8sPodDownloadImageTimeout, K8SPODDOWNLOADIMAGETIMEOUT_KEY, k8sPodErrorRecheckTimeout, K8SPODERRORRECHECKTIMEOUT_KEY, k8sPodReadyTimeout, K8SPODREADYTIMEOUT_KEY, k8sPodWaitTimeout, K8SPODWAITTIMEOUT_KEY, listrRenderer, logsDirectory, LOG_DIRECTORY_KEY, skipKubeHealthzCheck as skipK8sHealthCheck } from '../../common-flags'
import { DEFAULT_ANALYTIC_HOOK_NAME, DEFAULT_CHE_NAMESPACE, DEFAULT_OLM_SUGGESTED_NAMESPACE, DOCS_LINK_INSTALL_RUNNING_CHE_LOCALLY, DOC_LINK_CONFIGURE_API_SERVER } from '../../constants'
import { CheTasks } from '../../tasks/che'
import { DevWorkspaceTasks } from '../../tasks/component-installers/devfile-workspace-operator-installer'
import { DexTasks } from '../../tasks/component-installers/dex'
import { createNamespaceTask, getPrintHighlightedMessagesTask, retrieveCheCaCertificateTask } from '../../tasks/installers/common-tasks'
import { InstallerTasks } from '../../tasks/installers/installer'
import { ApiTasks } from '../../tasks/platforms/api'
import { PlatformTasks } from '../../tasks/platforms/platform'
import { askForChectlUpdateIfNeeded, getCommandSuccessMessage, getEmbeddedTemplatesDirectory, getTlsSupport, getWarnVersionFlagMsg, isDevWorkspaceEnabled, isKubernetesPlatformFamily, isOpenshiftPlatformFamily, notifyCommandCompletedSuccessfully, wrapCommandError } from '../../util'
export default class Deploy extends Command {
static description = 'Deploy Eclipse Che server'
static flags: flags.Input<any> = {
help: flags.help({ char: 'h' }),
chenamespace: cheNamespace,
batch,
'listr-renderer': listrRenderer,
'deployment-name': cheDeployment,
cheimage: string({
char: 'i',
description: 'Eclipse Che server container image',
env: 'CHE_CONTAINER_IMAGE',
}),
templates: string({
char: 't',
description: 'Path to the templates folder',
env: 'CHE_TEMPLATES_FOLDER',
exclusive: [DEPLOY_VERSION_KEY],
}),
'devfile-registry-url': string({
description: 'The URL of the external Devfile registry.',
env: 'CHE_WORKSPACE_DEVFILE__REGISTRY__URL',
}),
'plugin-registry-url': string({
description: 'The URL of the external plugin registry.',
env: 'CHE_WORKSPACE_PLUGIN__REGISTRY__URL',
}),
cheboottimeout: string({
char: 'o',
description: 'Eclipse Che server bootstrap timeout (in milliseconds)',
default: '40000',
required: true,
env: 'CHE_SERVER_BOOT_TIMEOUT',
}),
[K8SPODWAITTIMEOUT_KEY]: k8sPodWaitTimeout,
[K8SPODREADYTIMEOUT_KEY]: k8sPodReadyTimeout,
[K8SPODDOWNLOADIMAGETIMEOUT_KEY]: k8sPodDownloadImageTimeout,
[K8SPODERRORRECHECKTIMEOUT_KEY]: k8sPodErrorRecheckTimeout,
[LOG_DIRECTORY_KEY]: logsDirectory,
multiuser: flags.boolean({
char: 'm',
description: 'Deprecated. The flag is ignored. Eclipse Che is always deployed in multi-user mode.',
default: false,
hidden: true,
}),
tls: flags.boolean({
char: 's',
description: `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: ${DOCS_LINK_INSTALL_RUNNING_CHE_LOCALLY}`,
hidden: true,
}),
'self-signed-cert': flags.boolean({
description: 'Deprecated. The flag is ignored. Self signed certificates usage is autodetected now.',
default: false,
hidden: true,
}),
platform: string({
char: 'p',
description: 'Type of Kubernetes platform. Valid values are \"minikube\", \"minishift\", \"k8s (for kubernetes)\", \"openshift\", \"crc (for CodeReady Containers)\", \"microk8s\".',
options: ['minikube', 'minishift', 'k8s', 'openshift', 'microk8s', 'docker-desktop', 'crc'],
}),
installer: string({
char: 'a',
description: 'Installer type. If not set, default is "olm" for OpenShift 4.x platform otherwise "operator".',
options: ['operator', 'olm'],
}),
domain: string({
char: 'b',
description: `Domain of the Kubernetes cluster (e.g. example.k8s-cluster.com or <local-ip>.nip.io)
This flag makes sense only for Kubernetes family infrastructures and will be autodetected for Minikube and MicroK8s in most cases.
However, for Kubernetes cluster it is required to specify.
Please note, that just setting this flag will not likely work out of the box.
According changes should be done in Kubernetes cluster configuration as well.
In case of Openshift, domain adjustment should be done on the cluster configuration level.`,
default: '',
}),
debug: boolean({
description: 'Enables the debug mode for Eclipse Che server. To debug Eclipse Che server from localhost use \'server:debug\' command.',
default: false,
}),
'che-operator-image': string({
description: 'Container image of the operator. This parameter is used only when the installer is the operator or OLM.',
}),
[CHE_OPERATOR_CR_YAML_KEY]: cheOperatorCRYaml,
[CHE_OPERATOR_CR_PATCH_YAML_KEY]: cheOperatorCRPatchYaml,
'workspace-pvc-storage-class-name': string({
description: 'persistent volume(s) storage class name to use to store Eclipse Che workspaces data',
env: 'CHE_INFRA_KUBERNETES_PVC_STORAGE__CLASS__NAME',
default: '',
}),
'postgres-pvc-storage-class-name': string({
description: 'persistent volume storage class name to use to store Eclipse Che postgres database',
default: '',
}),
'skip-version-check': flags.boolean({
description: 'Skip minimal versions check.',
default: false,
}),
'skip-cluster-availability-check': flags.boolean({
description: 'Skip cluster availability check. The check is a simple request to ensure the cluster is reachable.',
default: false,
}),
'skip-oidc-provider-check': flags.boolean({
description: 'Skip OIDC Provider check',
default: false,
}),
'auto-update': flags.boolean({
description: `Auto update approval strategy for installation Eclipse Che.
With this strategy will be provided auto-update Eclipse Che without any human interaction.
By default this flag is enabled.
This parameter is used only when the installer is 'olm'.`,
allowNo: true,
exclusive: ['starting-csv'],
}),
'starting-csv': flags.string({
description: `Starting cluster service version(CSV) for installation Eclipse Che.
Flags uses to set up start installation version Che.
For example: 'starting-csv' provided with value 'eclipse-che.v7.10.0' for stable channel.
Then OLM will install Eclipse Che with version 7.10.0.
Notice: this flag will be ignored with 'auto-update' flag. OLM with auto-update mode installs the latest known version.
This parameter is used only when the installer is 'olm'.`,
}),
'olm-channel': string({
description: `Olm channel to install Eclipse Che, f.e. stable.
If options was not set, will be used default version for package manifest.
This parameter is used only when the installer is the 'olm'.`,
}),
'package-manifest-name': string({
description: `Package manifest name to subscribe to Eclipse Che OLM package manifest.
This parameter is used only when the installer is the 'olm'.`,
}),
'catalog-source-yaml': string({
description: `Path to a yaml file that describes custom catalog source for installation Eclipse Che operator.
Catalog source will be applied to the namespace with Che operator.
Also you need define 'olm-channel' name and 'package-manifest-name'.
This parameter is used only when the installer is the 'olm'.`,
}),
'catalog-source-name': string({
description: `OLM catalog source to install Eclipse Che operator.
This parameter is used only when the installer is the 'olm'.`,
}),
'catalog-source-namespace': string({
description: `Namespace for OLM catalog source to install Eclipse Che operator.
This parameter is used only when the installer is the 'olm'.`,
}),
'cluster-monitoring': boolean({
default: false,
hidden: true,
description: `Enable cluster monitoring to scrape Eclipse Che metrics in Prometheus.
This parameter is used only when the platform is 'openshift'.`,
}),
'olm-suggested-namespace': boolean({
default: true,
allowNo: true,
description: `Indicate to deploy Eclipse Che in OLM suggested namespace: '${DEFAULT_OLM_SUGGESTED_NAMESPACE}'.
Flag 'chenamespace' is ignored in this case
This parameter is used only when the installer is 'olm'.`,
}),
'skip-kubernetes-health-check': skipK8sHealthCheck,
'workspace-engine': string({
description: 'Workspace Engine. If not set, default is "che-server". "dev-workspace" is experimental.',
options: ['che-server', 'dev-workspace'],
default: 'che-server',
hidden: true,
}),
telemetry: CHE_TELEMETRY,
[DEPLOY_VERSION_KEY]: cheDeployVersion,
}
async setPlaformDefaults(flags: any, ctx: any): Promise<void> {
flags.tls = getTlsSupport(ctx)
if (flags['self-signed-cert']) {
this.warn('"self-signed-cert" flag is deprecated and has no effect. Autodetection is used instead.')
}
if (!flags.installer) {
await setDefaultInstaller(flags)
cli.info(`› Installer type is set to: '${flags.installer}'`)
}
if (flags.installer === 'olm' && flags['olm-suggested-namespace']) {
flags.chenamespace = DEFAULT_OLM_SUGGESTED_NAMESPACE
cli.info(` ❕olm-suggested-namespace flag is turned on. Eclipse Che will be deployed in namespace: ${DEFAULT_OLM_SUGGESTED_NAMESPACE}.`)
}
if (!flags.templates) {
// Use build-in templates if no custom templates nor version to deploy specified.
// All flavors should use embedded templates if not custom templates is given.
flags.templates = getEmbeddedTemplatesDirectory()
}
}
private checkCompatibility(flags: any) {
if (flags.installer === 'operator' && flags[CHE_OPERATOR_CR_YAML_KEY]) {
const ignoredFlags = []
flags['plugin-registry-url'] && ignoredFlags.push('--plugin-registry-url')
flags['devfile-registry-url'] && ignoredFlags.push('--devfile-registry-url')
flags['postgres-pvc-storage-class-name'] && ignoredFlags.push('--postgres-pvc-storage-class-name')
flags['workspace-pvc-storage-class-name'] && ignoredFlags.push('--workspace-pvc-storage-class-name')
flags.tls && ignoredFlags.push('--tls')
flags.cheimage && ignoredFlags.push('--cheimage')
flags.debug && ignoredFlags.push('--debug')
flags.domain && ignoredFlags.push('--domain')
if (ignoredFlags.length) {
this.warn(`--${CHE_OPERATOR_CR_YAML_KEY} is used. The following flag(s) will be ignored: ${ignoredFlags.join('\t')}`)
}
}
if (flags.domain && !flags[CHE_OPERATOR_CR_YAML_KEY] && isOpenshiftPlatformFamily(flags.platform)) {
this.warn('"--domain" flag is ignored for Openshift family infrastructures. It should be done on the cluster level.')
}
if (flags.installer === 'olm') {
// OLM installer only checks
if (flags.platform === 'minishift') {
this.error(`🛑 The specified installer ${flags.installer} does not support Minishift`)
}
if (isKubernetesPlatformFamily(flags.platform)) {
this.error(`🛑 The specified installer ${flags.installer} does not support Kubernentes`)
}
if (flags[OLM.CATALOG_SOURCE_NAME] && flags[OLM.CATALOG_SOURCE_YAML]) {
this.error(`should be provided only one argument: "${OLM.CATALOG_SOURCE_NAME}" or "${OLM.CATALOG_SOURCE_YAML}"`)
}
if (!flags[OLM.PACKAGE_MANIFEST_NAME] && flags[OLM.CATALOG_SOURCE_YAML]) {
this.error(`you need to define "${OLM.PACKAGE_MANIFEST_NAME}" flag to use "${OLM.CATALOG_SOURCE_YAML}".`)
}
if (!flags[OLM.CHANNEL] && flags[OLM.CATALOG_SOURCE_YAML]) {
this.error(`you need to define "${OLM.CHANNEL}" flag to use "${OLM.CATALOG_SOURCE_YAML}".`)
}
} else {
// Not OLM installer
if (flags[OLM.STARTING_CSV]) {
this.error(`"${OLM.STARTING_CSV}" flag should be used only with "olm" installer.`)
}
if (flags[OLM.CATALOG_SOURCE_YAML]) {
this.error(`"${OLM.CATALOG_SOURCE_YAML}" flag should be used only with "olm" installer.`)
}
if (flags[OLM.CHANNEL]) {
this.error(`"${OLM.CHANNEL}" flag should be used only with "olm" installer.`)
}
if (flags[OLM.PACKAGE_MANIFEST_NAME]) {
this.error(`"${OLM.PACKAGE_MANIFEST_NAME}" flag should be used only with "olm" installer.`)
}
if (flags[OLM.CATALOG_SOURCE_NAME]) {
this.error(`"${OLM.CATALOG_SOURCE_NAME}" flag should be used only with "olm" installer.`)
}
if (flags[OLM.CATALOG_SOURCE_NAMESPACE]) {
this.error(`"${OLM.CATALOG_SOURCE_NAMESPACE}" flag should be used only with "olm" installer.`)
}
if (flags['cluster-monitoring'] && flags.platform !== 'openshift') {
this.error('"cluster-monitoring" flag should be used only with "olm" installer and "openshift" platform.')
}
}
}
async run() {
const { flags } = this.parse(Deploy)
flags.chenamespace = flags.chenamespace || DEFAULT_CHE_NAMESPACE
const ctx = await ChectlContext.initAndGet(flags, this)
if (!flags.batch && ctx.isChectl) {
await askForChectlUpdateIfNeeded()
}
if (flags.version) {
cli.info(getWarnVersionFlagMsg(flags))
this.exit(1)
}
await this.setPlaformDefaults(flags, ctx)
await this.config.runHook(DEFAULT_ANALYTIC_HOOK_NAME, { command: Deploy.id, flags })
const dexTasks = new DexTasks(flags)
const cheTasks = new CheTasks(flags)
const platformTasks = new PlatformTasks(flags)
const installerTasks = new InstallerTasks()
const apiTasks = new ApiTasks()
const devWorkspaceTasks = new DevWorkspaceTasks(flags)
// Platform Checks
const platformCheckTasks = new Listr(platformTasks.preflightCheckTasks(flags, this), ctx.listrOptions)
// Checks if Eclipse Che is already deployed
const preInstallTasks = new Listr(undefined, ctx.listrOptions)
preInstallTasks.add(apiTasks.testApiTasks(flags))
preInstallTasks.add({
title: '👀 Looking for an already existing Eclipse Che instance',
task: () => new Listr(cheTasks.checkIfCheIsInstalledTasks(flags)),
})
preInstallTasks.add(ensureOIDCProviderInstalled(flags))
preInstallTasks.add({
title: '🧪 DevWorkspace engine',
enabled: () => isDevWorkspaceEnabled(ctx) && !ctx.isOpenShift,
task: () => new Listr(devWorkspaceTasks.getInstallTasks()),
})
const installTasks = new Listr(undefined, ctx.listrOptions)
installTasks.add([createNamespaceTask(flags.chenamespace, this.getNamespaceLabels(flags))])
if (flags.platform === 'minikube' && isDevWorkspaceEnabled(ctx)) {
installTasks.add(dexTasks.getInstallTasks())
}
installTasks.add(await installerTasks.installTasks(flags, this))
// Post Install Checks
const postInstallTasks = new Listr([
{
title: '✅ Post installation checklist',
task: () => new Listr(cheTasks.waitDeployedChe()),
},
retrieveCheCaCertificateTask(flags),
...cheTasks.preparePostInstallationOutput(flags),
getPrintHighlightedMessagesTask(),
], ctx.listrOptions)
const logsTasks = new Listr([{
title: 'Following Eclipse Che logs',
task: () => new Listr(cheTasks.serverLogsTasks(flags, true)),
}], ctx.listrOptions)
try {
await preInstallTasks.run(ctx)
if (ctx.isCheDeployed) {
let message = 'Eclipse Che has been already deployed.'
if (!ctx.isCheReady) {
message += ' Use server:start command to start a stopped Eclipse Che instance.'
}
cli.warn(message)
} else {
this.checkCompatibility(flags)
await platformCheckTasks.run(ctx)
await logsTasks.run(ctx)
await installTasks.run(ctx)
await postInstallTasks.run(ctx)
this.log(getCommandSuccessMessage())
}
} catch (err) {
this.error(wrapCommandError(err))
}
if (!flags.batch) {
notifyCommandCompletedSuccessfully()
}
this.exit(0)
}
private getNamespaceLabels(flags: any): any {
// The label values must be strings
if (flags['cluster-monitoring'] && flags.platform === 'openshift') {
return { 'openshift.io/cluster-monitoring': 'true' }
}
return {}
}
}
function ensureOIDCProviderInstalled(flags: any): Listr.ListrTask {
return {
title: 'Check if OIDC Provider installed',
enabled: ctx => !flags['skip-oidc-provider-check'] && isKubernetesPlatformFamily(flags.platform) && !ctx.isCheDeployed,
skip: () => {
if (flags.platform === 'minikube') {
return 'Dex will be automatically installed as OIDC Identity Provider'
}
},
task: async (_ctx: any, task: any) => {
const kube = new KubeHelper(flags)
const apiServerPods = await kube.getPodListByLabel('kube-system', 'component=kube-apiserver')
for (const pod of apiServerPods) {
if (!pod.spec) {
continue
}
for (const container of pod.spec.containers) {
if (container.command) {
if (container.command.some(value => value.includes(OIDCContextKeys.ISSUER_URL)) && container.command.some(value => value.includes(OIDCContextKeys.CLIENT_ID))) {
task.title = `${task.title}...OK`
return
}
}
if (container.args) {
if (container.args.some(value => value.includes(OIDCContextKeys.ISSUER_URL)) && container.args.some(value => value.includes(OIDCContextKeys.CLIENT_ID))) {
task.title = `${task.title}...OK`
return
}
}
}
}
task.title = `${task.title}...NOT INSTALLED`
throw new Error(`API server is not configured with OIDC Identity Provider, see details ${DOC_LINK_CONFIGURE_API_SERVER}. To bypass OIDC Provider check, use \'--skip-oidc-provider-check\' flag`)
},
}
}
/**
* 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 kubeHelper = new KubeHelper(flags)
const isOlmPreinstalled = await kubeHelper.isPreInstalledOLM()
if ((flags[OLM.CATALOG_SOURCE_NAME] || flags[OLM.CATALOG_SOURCE_YAML]) && isOlmPreinstalled) {
flags.installer = 'olm'
return
}
if (flags.platform === 'openshift' && await kubeHelper.isOpenShift4() && isOlmPreinstalled) {
flags.installer = 'olm'
} else {
flags.installer = 'operator'
}
}