forked from che-incubator/chectl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inject.ts
233 lines (204 loc) · 10.4 KB
/
inject.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
/*********************************************************************
* Copyright (c) 2019 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
**********************************************************************/
import { Context } from '@kubernetes/client-node/dist/config_types'
import { Command, flags } from '@oclif/command'
import { string } from '@oclif/parser/lib/flags'
import { cli } from 'cli-ux'
import * as execa from 'execa'
import * as fs from 'fs'
import * as os from 'os'
import * as path from 'path'
import { CheHelper } from '../../api/che'
import { CheApiClient } from '../../api/che-api-client'
import { KubeHelper } from '../../api/kube'
import { accessToken, ACCESS_TOKEN_KEY, cheApiEndpoint, cheNamespace, CHE_API_ENDPOINT_KEY, skipKubeHealthzCheck } from '../../common-flags'
import { getClusterClientCommand, OPENSHIFT_CLI } from '../../util'
export default class Inject extends Command {
static description = 'inject configurations and tokens in a workspace'
static flags: flags.Input<any> = {
help: flags.help({ char: 'h' }),
kubeconfig: flags.boolean({
char: 'k',
description: 'Inject the local Kubernetes configuration',
required: true
}),
workspace: string({
char: 'w',
description: `The workspace id to inject configuration into. It can be omitted if the only one running workspace exists.
Use workspace:list command to get all workspaces and their statuses.`
}),
container: string({
char: 'c',
description: 'The container name. If not specified, configuration files will be injected in all containers of the workspace pod',
required: false
}),
'kube-context': string({
description: 'Kubeconfig context to inject',
required: false
}),
[CHE_API_ENDPOINT_KEY]: cheApiEndpoint,
[ACCESS_TOKEN_KEY]: accessToken,
chenamespace: cheNamespace,
'skip-kubernetes-health-check': skipKubeHealthzCheck
}
// Holds cluster CLI tool name: kubectl or oc
private readonly command = getClusterClientCommand()
async run() {
const { flags } = this.parse(Inject)
const notifier = require('node-notifier')
const cheHelper = new CheHelper(flags)
let cheApiEndpoint = flags[CHE_API_ENDPOINT_KEY]
if (!cheApiEndpoint) {
const kube = new KubeHelper(flags)
if (!await kube.hasReadPermissionsForNamespace(flags.chenamespace)) {
throw new Error(`Eclipse Che API endpoint is required. Use flag --${CHE_API_ENDPOINT_KEY} to provide it.`)
}
cheApiEndpoint = await cheHelper.cheURL(flags.chenamespace) + '/api'
}
const cheApiClient = CheApiClient.getInstance(cheApiEndpoint)
await cheApiClient.checkCheApiEndpointUrl()
if (!flags[ACCESS_TOKEN_KEY] && await cheApiClient.isAuthenticationEnabled()) {
cli.error('Authentication is enabled but \'access-token\' is not provided.\nSee more details with the --help flag.')
}
let workspaceId = flags.workspace
let workspaceNamespace = ''
if (!workspaceId) {
const workspaces = await cheApiClient.getAllWorkspaces(flags[ACCESS_TOKEN_KEY])
const runningWorkspaces = workspaces.filter(w => w.status === 'RUNNING')
if (runningWorkspaces.length === 1) {
workspaceId = runningWorkspaces[0].id
workspaceNamespace = runningWorkspaces[0].attributes!.infrastructureNamespace
} else if (runningWorkspaces.length === 0) {
cli.error('There are no running workspaces. Please start workspace first.')
} else {
cli.error('There are more than 1 running workspaces. Please, specify the workspace id by providing \'--workspace\' flag.\nSee more details with the --help flag.')
}
} else {
const workspace = await cheApiClient.getWorkspaceById(workspaceId, flags[ACCESS_TOKEN_KEY])
if (workspace.status !== 'RUNNING') {
cli.error(`Workspace '${workspaceId}' is not running. Please start workspace first.`)
}
workspaceNamespace = workspace.attributes!.infrastructureNamespace
}
const workspacePodName = await cheHelper.getWorkspacePodName(workspaceNamespace, workspaceId!)
if (flags.container && !await this.containerExists(workspaceNamespace, workspacePodName, flags.container)) {
cli.error(`The specified container '${flags.container}' doesn't exist. The configuration cannot be injected.`)
}
try {
await this.injectKubeconfig(flags, workspaceNamespace, workspacePodName, workspaceId!)
} catch (err) {
this.error(err)
}
notifier.notify({
title: 'chectl',
message: `Command ${this.id} has completed.`
})
}
async injectKubeconfig(flags: any, workspaceNamespace: string, workspacePodName: string, workspaceId: string): Promise<void> {
const kubeContext = flags['kube-context']
let contextToInject: Context | null
const kh = new KubeHelper(flags)
if (kubeContext) {
contextToInject = kh.getContext(kubeContext)
if (!contextToInject) {
this.error(`Context ${kubeContext} is not found in the source kubeconfig`)
}
} else {
const currentContext = await kh.currentContext()
contextToInject = kh.getContext(currentContext)
}
const che = new CheHelper(flags)
const containers = flags.container ? [flags.container] : await che.getWorkspacePodContainers(workspaceNamespace, workspaceId)
for (const container of containers) {
// che-machine-exec container is very limited for a security reason.
// We cannot copy file into it.
if (container.startsWith('che-machine-exec') || container.startsWith('che-jwtproxy')) {
continue
}
try {
if (await this.canInject(workspaceNamespace, workspacePodName, container)) {
await this.doInjectKubeconfig(workspaceNamespace, workspacePodName, container, contextToInject!)
cli.info(`Configuration successfully injected into ${container} container`)
}
} catch (error) {
cli.warn(`Failed to injected configuration into ${container} container.\nError: ${error.message}`)
}
}
}
/**
* Tests whether a file can be injected into the specified container.
*/
private async canInject(namespace: string, pod: string, container: string): Promise<boolean> {
const { exitCode } = await execa(`${this.command} exec ${pod} -n ${namespace} -c ${container} -- tar --version `, { timeout: 10000, reject: false, shell: true })
if (exitCode === 0) { return true } else { return false }
}
/**
* Copies the local kubeconfig into the specified container.
* If returns, it means injection was completed successfully. If throws an error, injection failed
*/
private async doInjectKubeconfig(namespace: string, workspacePod: string, container: string, contextToInject: Context): Promise<void> {
const { stdout } = await execa(`${this.command} exec ${workspacePod} -n ${namespace} -c ${container} env | grep ^HOME=`, { timeout: 10000, shell: true })
let containerHomeDir = stdout.split('=')[1]
if (!containerHomeDir.endsWith('/')) {
containerHomeDir += '/'
}
if (await this.fileExists(namespace, workspacePod, container, `${containerHomeDir}.kube/config`)) {
throw new Error('kubeconfig already exists in the target container')
}
await execa(`${this.command} exec ${workspacePod} -n ${namespace} -c ${container} -- mkdir ${containerHomeDir}.kube -p`, { timeout: 10000, shell: true })
const kubeConfigPath = path.join(os.tmpdir(), 'che-kubeconfig')
const cluster = KubeHelper.KUBE_CONFIG.getCluster(contextToInject.cluster)
if (!cluster) {
throw new Error(`Context ${contextToInject.name} has no cluster object`)
}
const user = KubeHelper.KUBE_CONFIG.getUser(contextToInject.user)
if (!user) {
throw new Error(`Context ${contextToInject.name} has no user object`)
}
// Despite oc has --kubeconfig flag it actually does nothing, so we need to use --config instead
const configPathFlag = this.command === OPENSHIFT_CLI ? '--config' : '--kubeconfig'
const setClusterArgs = ['config', configPathFlag, kubeConfigPath, 'set-cluster', cluster.name, `--server=${cluster.server}`]
// Prepare CA certificate file
if (cluster.caFile) {
setClusterArgs.push(`--certificate-authority=${cluster.caFile}`)
setClusterArgs.push('--embed-certs=true')
} else if (cluster.caData) {
const caFile = path.join(os.tmpdir(), 'cluster-ca-file.pem')
// Write caData into a file and pass it as the parameter
fs.writeFileSync(caFile, cluster.caData, 'utf8')
setClusterArgs.push(`--certificate-authority=${caFile}`)
setClusterArgs.push('--embed-certs=true')
}
await execa(this.command, setClusterArgs, { timeout: 10000 })
const setCredentialsArgs = ['config', configPathFlag, kubeConfigPath, 'set-credentials', user.name]
if (user.certFile) {
setCredentialsArgs.push(`--client-certificate=${user.certFile}`)
}
if (user.keyFile) {
setCredentialsArgs.push(`--client-key=${user.keyFile}`)
}
if (user.certFile || user.keyFile) {
setCredentialsArgs.push('--embed-certs=true')
}
await execa(this.command, setCredentialsArgs, { timeout: 10000 })
await execa(this.command, ['config', configPathFlag, kubeConfigPath, 'set-context', contextToInject.name, `--cluster=${contextToInject.cluster}`, `--user=${contextToInject.user}`, `--namespace=${namespace}`], { timeout: 10000 })
await execa(this.command, ['config', configPathFlag, kubeConfigPath, 'use-context', contextToInject.name], { timeout: 10000 })
await execa(this.command, ['cp', kubeConfigPath, `${namespace}/${workspacePod}:${containerHomeDir}.kube/config`, '-c', container], { timeout: 10000 })
return
}
private async fileExists(namespace: string, pod: string, container: string, file: string): Promise<boolean> {
const { exitCode } = await execa(`${this.command} exec ${pod} -n ${namespace} -c ${container} -- test -e ${file}`, { timeout: 10000, reject: false, shell: true })
if (exitCode === 0) { return true } else { return false }
}
private async containerExists(namespace: string, pod: string, container: string): Promise<boolean> {
const { stdout } = await execa(this.command, ['get', 'pods', `${pod}`, '-n', `${namespace}`, '-o', 'jsonpath={.spec.containers[*].name}'], { timeout: 10000 })
return stdout.split(' ').some(c => c === container)
}
}