generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcli.js
executable file
·529 lines (494 loc) · 17.5 KB
/
cli.js
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
#!/usr/bin/env node
import modules from './model.js'
import { program, Option } from 'commander'
import proxy from 'express-http-proxy';
import express from 'express';
import open from 'open';
import path from 'path';
import * as url from 'url';
import k8sClient from '@kubernetes/client-node'
import { Agent } from 'undici';
import { Client } from './k8s.js'
import { installedManagers, managedModules } from './module-management.js'
import { table } from 'table'
import { BtpClient } from './btp.js';
import { ServiceManager } from './service-manager.js'
import { defaultConfig, defaultKubeconfig } from './busola.js'
import { spawn } from 'child_process'
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
async function defaultClient(proxyPort) {
if (proxyPort) {
console.log("checking if kubectl proxy is running on port", proxyPort)
try{
await fetch('http://127.0.0.1:' + proxyPort + '/api/v1/namespaces', { timeout: 5000 })
} catch(error) {
console.log("proxy not found, please start kubectl proxy on port", proxyPort, "in another terminal")
process.exit(1)
}
return new Client(`http://127.0.0.1:${proxyPort}`)
}
let kc = new k8sClient.KubeConfig();
kc.loadFromDefault();
const opts = {};
kc.applyToRequest(opts);
opts.dispatcher = new Agent({
connect: {
...opts,
rejectUnauthorized: false,
}
})
return new Client(kc.getCurrentCluster().server, opts)
}
function platformInstances(gas) {
let platforms = []
let clusters = []
for (let ga of gas) {
for (let sa of ga.sAccounts) {
for (let si of sa.instances) {
let plan = sa.plans.find(p => p.id == si.service_plan_id)
if (plan) {
si.service_offering_name = plan.service_offering_name
si.catalog_name = plan.catalog_name
}
if (si.catalog_name == 'service-operator-access') {
si.clusters = []
si.global_account_name = ga.displayName
si.subaccount_name = sa.displayName
platforms.push(si)
}
if (si.context && si.context.clusterid) {
clusters.push({ platform_id: si.platform_id, clusterid: si.context.clusterid })
}
si.bindings = []
for (let bi of sa.bindings) {
if (bi.service_instance_id == si.id) {
si.bindings.push(bi)
si.binding = bi
}
}
}
}
}
for (let cluster of clusters) {
let platform = platforms.find(p => p.id == cluster.platform_id)
if (platform) {
if (!platform.clusters.find(id => id == cluster.clusterid)) {
platform.clusters.push(cluster.clusterid)
}
}
}
return platforms
}
function btpDump(gas) {
let platforms = platformInstances(gas)
for (let ga of gas) {
console.group('global account:', ga.displayName)
for (let sa of ga.sAccounts) {
console.group('sub account:', sa.displayName)
for (let si of sa.instances) {
console.group('instance:', si.name, si.service_offering_name || '', si.catalog_name || '')
for (let bi of si.bindings) {
console.log('binding:', bi.name)
}
console.groupEnd()
}
console.groupEnd()
}
console.groupEnd()
}
logPlatforms(platforms)
}
function logPlatforms(platforms) {
console.group('platforms:')
for (let p of platforms) {
console.group(p.name)
console.log('id:', p.id)
console.log('global account:', p.global_account_name, '(', p.context.global_account_id, ')')
console.log('subaccount:', p.subaccount_name, '(', p.subaccount_id, ')')
console.log('clusterId:', p.clusters.join(', '))
console.log('credentials:', p.binding ? 'ok' : '')
console.groupEnd()
}
console.groupEnd()
}
function createPlatformSecret(platform) {
let cred = platform.binding.credentials
let secret = {
apiVersion: 'v1',
kind: 'Secret',
metadata: {
name: 'sap-btp-manager',
namespace: 'kyma-system',
labels: {
'app.kubernetes.io/managed-by': 'kcp-kyma-environment-broker'
}
},
data: {
'clientid': Buffer.from(cred.clientid).toString('base64'),
'clientsecret': Buffer.from(cred.clientsecret).toString('base64'),
'sm_url': Buffer.from(cred.sm_url).toString('base64'),
'tokenurl': Buffer.from(cred.url).toString('base64'),
'cluster_id': Buffer.from(platform.clusters[0]).toString('base64')
}
}
return secret
}
async function globalAccounts(opts) {
if (opts.fromFile) {
let fs = await import('fs')
return JSON.parse(fs.readFileSync(opts.fromFile).toString())
} else {
let client = new BtpClient(opts)
let { id, url } = client.ssoUrl()
console.log("Please login to your BTP account using this URL:", url)
open(url)
let account = await client.sso(id)
console.log("Logged in as:", account.user)
return await client.dump(opts)
}
}
function smToK8s(instances) {
let resources = []
for (let si of instances) {
let res = {
apiVersion: 'services.cloud.sap.com/v1',
kind: 'ServiceInstance',
metadata: {
name: si.name,
namespace: si.context.namespace
},
spec: {
serviceOfferingName: si.offering_name,
servicePlanName: si.plan_name,
}
}
resources.push(res)
for (let bi of si.bindings) {
res = {
apiVersion: 'services.cloud.sap.com/v1',
kind: 'ServiceBinding',
metadata: {
name: bi.name,
namespace: si.context.namespace
},
spec: {
serviceInstanceName: si.context.instance_name,
}
}
resources.push(res)
}
}
return resources
}
function missingNamespaces(resources, namespaces) {
let missing = {}
for (let r of resources) {
if (!namespaces.find(ns => ns.metadata.name == r.metadata.namespace)) {
missing[r.metadata.namespace] = true
}
}
return Object.keys(missing).map(ns => { return { apiVersion: 'v1', kind: 'Namespace', metadata: { name: ns } } })
}
program.command('restore-services')
.description('restore BTP services instances and binding from the attached btp platform')
.addOption(new Option('-l, --landscape <landscape>', 'live or canary').choices(['live', 'canary']))
.option('-ga, --global-account <string>', 'global account')
.option('-sa, --sub-account <string>', 'sub account')
.addOption(new Option('--proxy-port <proxy-port>', 'connect through kubectl proxy running on this port').env('PROXY_PORT'))
.option('--from-file <filename>', 'json file with dump of BTP services (see btp-dump command)')
.action(async function () {
let clientK8s = await defaultClient(this.opts().proxyPort)
let btpPlatformSecret = await clientK8s.get('/api/v1/namespaces/kyma-system/secrets/sap-btp-manager')
if (!btpPlatformSecret) {
console.error("Platform secret not found. Please attach your cluster to the BTP platform first using the 'attach' command")
return
}
let btpCR = await clientK8s.get('/apis/operator.kyma-project.io/v1alpha1/namespaces/kyma-system/btpoperators/btpoperator')
if (!btpCR) {
console.error("BTP operator CR not found. Please install BTP operator first using the 'deploy' command")
return
}
if (!btpCR.status || !btpCR.status.state || btpCR.status.state != 'Ready') {
console.error("BTP operator not ready. Please wait until BTP operator is ready")
return
}
let sm = new ServiceManager(btpPlatformSecret)
await sm.authenticate()
let si = await sm.serviceInstances()
if (si.length == 0) {
console.log("no BTP services found for this cluster")
}
let resources = smToK8s(si)
let namespaces = await clientK8s.get('/api/v1/namespaces')
let missing = missingNamespaces(resources, namespaces.items)
for (let r of missing) {
await clientK8s.apply(r)
}
for (let r of resources) {
let path = await clientK8s.resPath(r)
let exists = await clientK8s.get(path)
if (exists) {
console.log(r.kind, r.metadata.name, 'already exists')
} else {
// console.log('creating', r.kind, r.metadata.name, 'in namespace', r.metadata.namespace || '-')
await clientK8s.apply(r)
}
}
})
program.command('attach')
.description('attach your cluster to the BTP platform. It requires BTP subaccount with "service-manager" service instance and binding with plan "service-operator-access"')
.addOption(new Option('-l, --landscape <landscape>', 'live or canary').choices(['live', 'canary']))
.option('-ga, --global-account <string>', 'global account')
.option('-sa, --sub-account <string>', 'sub account')
.option('--from-file <filename>', 'json file with dump of BTP services (see btp-dump command)')
.option('--platform <string>', 'platform id, you can skip it if only one platform is available')
.option('--cluster-id <string>', 'cluster id, you can skip it if the platform has only one cluster connected')
.addOption(new Option('--proxy-port <proxy-port>', 'connect through kubectl proxy running on this port').env('PROXY_PORT'))
.action(async function () {
let gas = await globalAccounts(this.opts())
let platforms = platformInstances(gas)
if (this.opts().platform) {
platforms = platforms.filter(p => p.id == this.opts().platform)
}
if (platforms.length == 0) {
console.error("no platforms found")
return
}
if (platforms.length > 1) {
console.error("multiple platforms found, please specify platform id")
logPlatforms(platforms)
return
}
if (!platforms[0].binding) {
console.error("no binding found for platform", platforms[0].name)
logPlatforms([platforms[0]])
return
}
if (platforms[0].clusters.length != 1 && !this.opts().clusterId) {
console.error("Cluster id cannot be determined, provide --cluster-id <string> parameter")
logPlatforms([platforms[0]])
return
}
if (this.opts().clusterId) {
platforms[0].clusters = [this.opts().clusterId]
}
console.log('connecting to platform ', platforms[0].name, 'with cluster id', platforms[0].clusters[0])
let btpPlatformSecret = createPlatformSecret(platforms[0])
let clientK8s = await defaultClient(this.opts().proxyPort)
await clientK8s.apply(btpPlatformSecret)
console.log(`platform secret ${btpPlatformSecret.metadata.name} created in the namespace ${btpPlatformSecret.metadata.namespace}`)
})
program.command('btp-dump')
.description('dump BTP services from your accounts to the json file')
.argument('<filename>', 'filename to dump to')
.addOption(new Option('-l, --landscape <landscape>', 'live or canary').choices(['live', 'canary']))
.option('-ga, --global-account <string>', 'global account to dump')
.option('-sa, --sub-account <string>', 'sub account to dump')
.action(async function () {
let client = new BtpClient(this.opts())
let { id, url } = client.ssoUrl()
console.log("Please login to your BTP account using this URL:", url)
open(url)
let account = await client.sso(id)
console.log("Logged in as:", account.user)
let gas = await client.dump(this.opts())
btpDump(gas)
let fs = await import('fs')
fs.writeFileSync(this.args[0], JSON.stringify(gas, null, 2))
})
program.command('modules')
.description('list modules')
.option('-c, --channel <string>')
.addOption(new Option('--proxy-port <proxy-port>', 'connect through kubectl proxy running on this port').env('PROXY_PORT'))
.action(async function () {
let client = await defaultClient(this.opts().proxyPort)
let filtered = modules.filter(filterFunc(this.opts()))
await installedManagers(filtered, client)
await managedModules(filtered, client)
managedTable(filtered)
userTable(filtered)
availableTable(filtered)
})
program.command('version')
.description('show version')
.action(async () => {
const { default: info } = await import("./package.json", {
assert: {
type: "json",
},
});
console.log(info.version)
})
function configState(m) {
if (!m.config) {
return 'Not configured'
}
if (!m.config.status || !m.config.status.state) {
return 'Applied'
}
return m.config.status.state
}
function managedTable(modules) {
let list = modules.filter(m => m.managed)
if (list.length > 0) {
console.log("Managed modules")
let data = [['name', 'channel', 'version', 'manager', 'ready', 'config']]
list.forEach(m => {
let image = m.managerImage ? m.managerImage.split('/')[m.managerImage.split('/').length - 1] : ''
data.push([m.name, m.channel || '', m.actualVersion || '', image, m.available, configState(m)])
})
console.log(table(data))
}
}
function userTable(modules) {
let list = modules.filter(m => !m.managed && m.actualVersion)
if (list.length > 0) {
console.log("User modules")
let data = [['name', 'version', 'manager', 'ready', 'config']]
list.forEach(m => {
let image = m.managerImage ? m.managerImage.split('/')[m.managerImage.split('/').length - 1] : ''
data.push([m.name, m.actualVersion || '', image, m.available, configState(m)])
})
console.log(table(data))
}
}
function availableTable(modules) {
console.log("Available modules")
let data = [['name', 'versions']]
modules.forEach(m => {
data.push([m.name, moduleVersions(m)])
})
console.log(table(data))
}
program.command('deploy')
.description('deploy modules')
.option('-m, --modules <name:version...>'
, 'install one or more modules; put :<version> after module name to specify version'
, ["istio", "api-gateway", "btp-operator"])
.option('-c, --channel <string>', 'use module version from channel')
.addOption(new Option('--proxy-port <proxy-port>', 'connect through kubectl proxy running on this port').env('PROXY_PORT'))
.option('--customConfig', 'do not apply default module configuration (CR)')
.option('--dry-run', 'do not actually deploy')
.action(async function () {
for (let module of this.opts().modules) {
let { m, v } = findModuleVersion(module, this.opts().channel, modules)
if (!v) {
console.error("module not found", module)
process.exit(1)
}
else {
await applyModuleResource(m, v, this.opts())
}
}
})
program.command('ui')
.description('start web interface')
.option('-p, --port <number>', 'port to listen on', 3001)
.addOption(new Option('--proxy-port <proxy-port>', 'connect through kubectl proxy running on this port').env('PROXY_PORT'))
.action(ui)
program.parse()
function moduleVersions(m) {
return m.versions.map(v => {
return v.channels ? v.version + ' (' + v.channels.join(', ') + ')' : v.version
}).join(', ')
}
async function ui() {
console.log("starting ui on port", this.opts().port)
var app = express();
let defKc = defaultKubeconfig(this.opts().port)
app.get('/kubeconfig/kyma.yaml', (_, res) => {
res.send(defKc)
})
let defCfg = defaultConfig(this.opts().port)
app.get('/config/config.yaml', (_, res) => {
res.send(defCfg)
})
if (this.opts().proxyPort) {
//check if proxy is running with timeout 5 seconds
console.log("checking if kubectl proxy is running on port", this.opts().proxyPort)
try{
await fetch('http://127.0.0.1:' + this.opts().proxyPort + '/api/v1/namespaces', { timeout: 5000 })
} catch(error) {
console.log("starting kubectl proxy on port", this.opts().proxyPort)
spawn('kubectl', ['proxy', '--port', this.opts().proxyPort])
}
app.use('/backend', proxy('http://127.0.0.1:' + this.opts().proxyPort, {
proxyReqOptDecorator: function (proxyReqOpts, originalReq) {
proxyReqOpts.rejectUnauthorized = false
return proxyReqOpts;
}
}));
} else {
let kc = new k8sClient.KubeConfig();
kc.loadFromDefault();
app.use('/backend', proxy(kc.getCurrentCluster().server, {
proxyReqOptDecorator: function (proxyReqOpts, originalReq) {
proxyReqOpts.rejectUnauthorized = false
kc.applyToRequest(proxyReqOpts)
return proxyReqOpts;
}
}));
}
app.use('/modules', express.static(path.resolve(__dirname, "dist/modules")))
app.use(express.static(path.resolve(__dirname, "dist/core-ui")))
app.get('/*', (_, res) =>
res.sendFile(path.join(__dirname + 'dist/core-ui/index.html')),
);
app.listen(this.opts().port);
open('http://127.0.0.1:' + this.opts().port + '/?kubeconfigID=kyma.yaml');
}
function findModuleVersion(module, channel, modules) {
if (module.includes(':')) {
let version = module.split(':')[1]
let name = module.split(':')[0]
let m = modules.find(m => m.name == name)
for (let v of m.versions) {
if (v.version == version) {
return { m, v }
}
}
return {}
} else {
let m = modules.find(m => m.name == module)
if (channel) {
let v = m.versions.find(v => v.channels && v.channels.includes(channel))
if (v) {
return { m, v }
}
}
return { m, v: m.versions[m.versions.length - 1] }
}
}
function filterFunc(options) {
return (m) => {
let ok = true
if (options.channel) {
ok = m.versions.find(v => v.channels && v.channels.includes(options.channel))
}
return ok
}
}
async function applyModuleResource(module, v, opts) {
let client
if (!opts.dryRun) {
client = await defaultClient(opts.proxyPort)
}
if (!opts.dryRun) {
for (let r of v.resources) {
await client.apply(r)
}
} else {
if (v.deploymentYaml) {
console.log('kubectl apply -f ' + v.deploymentYaml)
}
}
if (!opts.customConfig) {
if (!opts.dryRun) {
await client.apply(v.cr)
} else {
if (v.crYaml) {
console.log('kubectl apply -f ' + v.crYaml)
}
}
}
}