@backstage/plugin-kubernetes-backend 0.3.16 → 0.3.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +59 -0
- package/dist/index.cjs.js +227 -140
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +74 -26
- package/package.json +7 -6
- package/schema.d.ts +20 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,64 @@
|
|
|
1
1
|
# @backstage/plugin-kubernetes-backend
|
|
2
2
|
|
|
3
|
+
## 0.3.20
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 65ddccb5e8: Added apiVersionOverrides config to allow for specifying api versions to use for kubernetes objects
|
|
8
|
+
- f6087fc8f8: Query CronJobs from Kubernetes with apiGroup BatchV1beta1
|
|
9
|
+
- Updated dependencies
|
|
10
|
+
- @backstage/backend-common@0.9.12
|
|
11
|
+
- @backstage/plugin-kubernetes-common@0.1.7
|
|
12
|
+
|
|
13
|
+
## 0.3.19
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- 37dc844728: Include CronJobs and Jobs as default objects returned by the kubernetes backend and add/update relevant types.
|
|
18
|
+
- Updated dependencies
|
|
19
|
+
- @backstage/errors@0.1.5
|
|
20
|
+
- @backstage/plugin-kubernetes-common@0.1.6
|
|
21
|
+
- @backstage/backend-common@0.9.11
|
|
22
|
+
|
|
23
|
+
## 0.3.18
|
|
24
|
+
|
|
25
|
+
### Patch Changes
|
|
26
|
+
|
|
27
|
+
- b61c50a12f: Fix Kubernetes plugin custom objects lookup regression
|
|
28
|
+
- c57b075d18: add caData support for kubernetes client config
|
|
29
|
+
- 36e67d2f24: Internal updates to apply more strict checks to throw errors.
|
|
30
|
+
- Updated dependencies
|
|
31
|
+
- @backstage/backend-common@0.9.7
|
|
32
|
+
- @backstage/errors@0.1.3
|
|
33
|
+
- @backstage/catalog-model@0.9.5
|
|
34
|
+
|
|
35
|
+
## 0.3.17
|
|
36
|
+
|
|
37
|
+
### Patch Changes
|
|
38
|
+
|
|
39
|
+
- 89bcf90b66: Refactor kubernetes fetcher to reduce boilerplate code
|
|
40
|
+
- a982e166c5: Enable customization of services used by the kubernetes backend plugin
|
|
41
|
+
|
|
42
|
+
The createRouter function has been deprecated in favor of a KubernetesBuilder object.
|
|
43
|
+
Here's how you should upgrade your projects when configuring the Kubernetes backend plugin.
|
|
44
|
+
in your `packages/backend/src/plugins/kubernetes.ts` file for instance:
|
|
45
|
+
|
|
46
|
+
```typescript
|
|
47
|
+
import { KubernetesBuilder } from '@backstage/plugin-kubernetes-backend';
|
|
48
|
+
import { PluginEnvironment } from '../types';
|
|
49
|
+
|
|
50
|
+
export default async function createPlugin({
|
|
51
|
+
logger,
|
|
52
|
+
config,
|
|
53
|
+
}: PluginEnvironment) {
|
|
54
|
+
const { router } = await KubernetesBuilder.createBuilder({
|
|
55
|
+
logger,
|
|
56
|
+
config,
|
|
57
|
+
}).build();
|
|
58
|
+
return router;
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
3
62
|
## 0.3.16
|
|
4
63
|
|
|
5
64
|
### Patch Changes
|
package/dist/index.cjs.js
CHANGED
|
@@ -4,6 +4,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var express = require('express');
|
|
6
6
|
var Router = require('express-promise-router');
|
|
7
|
+
var errors = require('@backstage/errors');
|
|
7
8
|
var container = require('@google-cloud/container');
|
|
8
9
|
var clientNode = require('@kubernetes/client-node');
|
|
9
10
|
var AWS = require('aws-sdk');
|
|
@@ -51,6 +52,7 @@ class ConfigClusterLocator {
|
|
|
51
52
|
url: c.getString("url"),
|
|
52
53
|
serviceAccountToken: c.getOptionalString("serviceAccountToken"),
|
|
53
54
|
skipTLSVerify: (_a = c.getOptionalBoolean("skipTLSVerify")) != null ? _a : false,
|
|
55
|
+
caData: c.getOptionalString("caData"),
|
|
54
56
|
authProvider
|
|
55
57
|
};
|
|
56
58
|
const dashboardUrl = c.getOptionalString("dashboardUrl");
|
|
@@ -119,7 +121,7 @@ class GkeClusterLocator {
|
|
|
119
121
|
};
|
|
120
122
|
});
|
|
121
123
|
} catch (e) {
|
|
122
|
-
throw new
|
|
124
|
+
throw new errors.ForwardedError(`There was an error retrieving clusters from GKE for projectId=${projectId} region=${region}`, e);
|
|
123
125
|
}
|
|
124
126
|
}
|
|
125
127
|
}
|
|
@@ -156,7 +158,8 @@ class KubernetesClientProvider {
|
|
|
156
158
|
const cluster = {
|
|
157
159
|
name: clusterDetails.name,
|
|
158
160
|
server: clusterDetails.url,
|
|
159
|
-
skipTLSVerify: clusterDetails.skipTLSVerify
|
|
161
|
+
skipTLSVerify: clusterDetails.skipTLSVerify,
|
|
162
|
+
caData: clusterDetails.caData
|
|
160
163
|
};
|
|
161
164
|
const user = {
|
|
162
165
|
name: "backstage",
|
|
@@ -188,6 +191,10 @@ class KubernetesClientProvider {
|
|
|
188
191
|
const kc = this.getKubeConfig(clusterDetails);
|
|
189
192
|
return kc.makeApiClient(clientNode.AutoscalingV1Api);
|
|
190
193
|
}
|
|
194
|
+
getBatchClientByClusterDetails(clusterDetails) {
|
|
195
|
+
const kc = this.getKubeConfig(clusterDetails);
|
|
196
|
+
return kc.makeApiClient(clientNode.BatchV1beta1Api);
|
|
197
|
+
}
|
|
191
198
|
getNetworkingBeta1Client(clusterDetails) {
|
|
192
199
|
const kc = this.getKubeConfig(clusterDetails);
|
|
193
200
|
return kc.makeApiClient(clientNode.NetworkingV1beta1Api);
|
|
@@ -323,13 +330,60 @@ class KubernetesAuthTranslatorGenerator {
|
|
|
323
330
|
}
|
|
324
331
|
|
|
325
332
|
const DEFAULT_OBJECTS = [
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
+
{
|
|
334
|
+
group: "",
|
|
335
|
+
apiVersion: "v1",
|
|
336
|
+
plural: "pods",
|
|
337
|
+
objectType: "pods"
|
|
338
|
+
},
|
|
339
|
+
{
|
|
340
|
+
group: "",
|
|
341
|
+
apiVersion: "v1",
|
|
342
|
+
plural: "services",
|
|
343
|
+
objectType: "services"
|
|
344
|
+
},
|
|
345
|
+
{
|
|
346
|
+
group: "",
|
|
347
|
+
apiVersion: "v1",
|
|
348
|
+
plural: "configmaps",
|
|
349
|
+
objectType: "configmaps"
|
|
350
|
+
},
|
|
351
|
+
{
|
|
352
|
+
group: "apps",
|
|
353
|
+
apiVersion: "v1",
|
|
354
|
+
plural: "deployments",
|
|
355
|
+
objectType: "deployments"
|
|
356
|
+
},
|
|
357
|
+
{
|
|
358
|
+
group: "apps",
|
|
359
|
+
apiVersion: "v1",
|
|
360
|
+
plural: "replicasets",
|
|
361
|
+
objectType: "replicasets"
|
|
362
|
+
},
|
|
363
|
+
{
|
|
364
|
+
group: "autoscaling",
|
|
365
|
+
apiVersion: "v1",
|
|
366
|
+
plural: "horizontalpodautoscalers",
|
|
367
|
+
objectType: "horizontalpodautoscalers"
|
|
368
|
+
},
|
|
369
|
+
{
|
|
370
|
+
group: "batch",
|
|
371
|
+
apiVersion: "v1",
|
|
372
|
+
plural: "jobs",
|
|
373
|
+
objectType: "jobs"
|
|
374
|
+
},
|
|
375
|
+
{
|
|
376
|
+
group: "batch",
|
|
377
|
+
apiVersion: "v1",
|
|
378
|
+
plural: "cronjobs",
|
|
379
|
+
objectType: "cronjobs"
|
|
380
|
+
},
|
|
381
|
+
{
|
|
382
|
+
group: "networking.k8s.io",
|
|
383
|
+
apiVersion: "v1",
|
|
384
|
+
plural: "ingresses",
|
|
385
|
+
objectType: "ingresses"
|
|
386
|
+
}
|
|
333
387
|
];
|
|
334
388
|
class KubernetesFanOutHandler {
|
|
335
389
|
constructor({
|
|
@@ -343,7 +397,7 @@ class KubernetesFanOutHandler {
|
|
|
343
397
|
this.fetcher = fetcher;
|
|
344
398
|
this.serviceLocator = serviceLocator;
|
|
345
399
|
this.customResources = customResources;
|
|
346
|
-
this.objectTypesToFetch = objectTypesToFetch;
|
|
400
|
+
this.objectTypesToFetch = new Set(objectTypesToFetch);
|
|
347
401
|
}
|
|
348
402
|
async getKubernetesObjectsByEntity(requestBody) {
|
|
349
403
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
@@ -360,7 +414,7 @@ class KubernetesFanOutHandler {
|
|
|
360
414
|
return this.fetcher.fetchObjectsForService({
|
|
361
415
|
serviceId: entityName,
|
|
362
416
|
clusterDetails: clusterDetailsItem,
|
|
363
|
-
objectTypesToFetch:
|
|
417
|
+
objectTypesToFetch: this.objectTypesToFetch,
|
|
364
418
|
labelSelector,
|
|
365
419
|
customResources: this.customResources
|
|
366
420
|
}).then((result) => {
|
|
@@ -408,16 +462,6 @@ const statusCodeToErrorType = (statusCode) => {
|
|
|
408
462
|
return "UNKNOWN_ERROR";
|
|
409
463
|
}
|
|
410
464
|
};
|
|
411
|
-
const captureKubernetesErrorsRethrowOthers = (e) => {
|
|
412
|
-
if (e.response && e.response.statusCode) {
|
|
413
|
-
return {
|
|
414
|
-
errorType: statusCodeToErrorType(e.response.statusCode),
|
|
415
|
-
statusCode: e.response.statusCode,
|
|
416
|
-
resourcePath: e.response.request.uri.pathname
|
|
417
|
-
};
|
|
418
|
-
}
|
|
419
|
-
throw e;
|
|
420
|
-
};
|
|
421
465
|
class KubernetesClientBasedFetcher {
|
|
422
466
|
constructor({
|
|
423
467
|
kubernetesClientProvider,
|
|
@@ -427,146 +471,189 @@ class KubernetesClientBasedFetcher {
|
|
|
427
471
|
this.logger = logger;
|
|
428
472
|
}
|
|
429
473
|
fetchObjectsForService(params) {
|
|
430
|
-
const fetchResults = Array.from(params.objectTypesToFetch).map((
|
|
431
|
-
return this.
|
|
474
|
+
const fetchResults = Array.from(params.objectTypesToFetch).concat(params.customResources).map((toFetch) => {
|
|
475
|
+
return this.fetchResource(params.clusterDetails, toFetch, params.labelSelector || `backstage.io/kubernetes-id=${params.serviceId}`, toFetch.objectType).catch(this.captureKubernetesErrorsRethrowOthers.bind(this));
|
|
432
476
|
});
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
resources: r
|
|
444
|
-
}));
|
|
445
|
-
case "configmaps":
|
|
446
|
-
return this.fetchConfigMapsForService(clusterDetails, labelSelector).then((r) => ({type, resources: r}));
|
|
447
|
-
case "deployments":
|
|
448
|
-
return this.fetchDeploymentsForService(clusterDetails, labelSelector).then((r) => ({type, resources: r}));
|
|
449
|
-
case "replicasets":
|
|
450
|
-
return this.fetchReplicaSetsForService(clusterDetails, labelSelector).then((r) => ({type, resources: r}));
|
|
451
|
-
case "services":
|
|
452
|
-
return this.fetchServicesForService(clusterDetails, labelSelector).then((r) => ({type, resources: r}));
|
|
453
|
-
case "horizontalpodautoscalers":
|
|
454
|
-
return this.fetchHorizontalPodAutoscalersForService(clusterDetails, labelSelector).then((r) => ({type, resources: r}));
|
|
455
|
-
case "ingresses":
|
|
456
|
-
return this.fetchIngressesForService(clusterDetails, labelSelector).then((r) => ({type, resources: r}));
|
|
457
|
-
default:
|
|
458
|
-
throw new Error(`unrecognised type=${type}`);
|
|
477
|
+
return Promise.all(fetchResults).then(fetchResultsToResponseWrapper);
|
|
478
|
+
}
|
|
479
|
+
captureKubernetesErrorsRethrowOthers(e) {
|
|
480
|
+
if (e.response && e.response.statusCode) {
|
|
481
|
+
this.logger.info(`statusCode=${e.response.statusCode} for resource ${e.response.request.uri.pathname}`);
|
|
482
|
+
return {
|
|
483
|
+
errorType: statusCodeToErrorType(e.response.statusCode),
|
|
484
|
+
statusCode: e.response.statusCode,
|
|
485
|
+
resourcePath: e.response.request.uri.pathname
|
|
486
|
+
};
|
|
459
487
|
}
|
|
488
|
+
throw e;
|
|
460
489
|
}
|
|
461
|
-
|
|
490
|
+
fetchResource(clusterDetails, resource, labelSelector, objectType) {
|
|
462
491
|
const customObjects = this.kubernetesClientProvider.getCustomObjectsClient(clusterDetails);
|
|
463
|
-
|
|
464
|
-
|
|
492
|
+
customObjects.addInterceptor((requestOptions) => {
|
|
493
|
+
requestOptions.uri = requestOptions.uri.replace("/apis//v1/", "/api/v1/");
|
|
494
|
+
});
|
|
495
|
+
return customObjects.listClusterCustomObject(resource.group, resource.apiVersion, resource.plural, "", "", "", labelSelector).then((r) => {
|
|
496
|
+
return {
|
|
497
|
+
type: objectType,
|
|
498
|
+
resources: r.body.items
|
|
499
|
+
};
|
|
465
500
|
});
|
|
466
501
|
}
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
class KubernetesBuilder {
|
|
505
|
+
constructor(env) {
|
|
506
|
+
this.env = env;
|
|
507
|
+
}
|
|
508
|
+
static createBuilder(env) {
|
|
509
|
+
return new KubernetesBuilder(env);
|
|
510
|
+
}
|
|
511
|
+
async build() {
|
|
512
|
+
var _a, _b, _c, _d;
|
|
513
|
+
const logger = this.env.logger;
|
|
514
|
+
logger.info("Initializing Kubernetes backend");
|
|
515
|
+
const customResources = this.buildCustomResources();
|
|
516
|
+
const fetcher = (_a = this.fetcher) != null ? _a : this.buildFetcher();
|
|
517
|
+
const clusterSupplier = (_b = this.clusterSupplier) != null ? _b : this.buildClusterSupplier();
|
|
518
|
+
const clusterDetails = await this.fetchClusterDetails(clusterSupplier);
|
|
519
|
+
const serviceLocator = (_c = this.serviceLocator) != null ? _c : this.buildServiceLocator(this.getServiceLocatorMethod(), clusterDetails);
|
|
520
|
+
const objectsProvider = (_d = this.objectsProvider) != null ? _d : this.buildObjectsProvider({
|
|
521
|
+
logger,
|
|
522
|
+
fetcher,
|
|
523
|
+
serviceLocator,
|
|
524
|
+
customResources,
|
|
525
|
+
objectTypesToFetch: this.getObjectTypesToFetch()
|
|
475
526
|
});
|
|
527
|
+
const router = this.buildRouter(objectsProvider, clusterDetails);
|
|
528
|
+
return {
|
|
529
|
+
clusterDetails,
|
|
530
|
+
clusterSupplier,
|
|
531
|
+
customResources,
|
|
532
|
+
fetcher,
|
|
533
|
+
objectsProvider,
|
|
534
|
+
router,
|
|
535
|
+
serviceLocator
|
|
536
|
+
};
|
|
476
537
|
}
|
|
477
|
-
|
|
478
|
-
|
|
538
|
+
setClusterSupplier(clusterSupplier) {
|
|
539
|
+
this.clusterSupplier = clusterSupplier;
|
|
540
|
+
return this;
|
|
479
541
|
}
|
|
480
|
-
|
|
481
|
-
|
|
542
|
+
setObjectsProvider(objectsProvider) {
|
|
543
|
+
this.objectsProvider = objectsProvider;
|
|
544
|
+
return this;
|
|
482
545
|
}
|
|
483
|
-
|
|
484
|
-
|
|
546
|
+
setFetcher(fetcher) {
|
|
547
|
+
this.fetcher = fetcher;
|
|
548
|
+
return this;
|
|
485
549
|
}
|
|
486
|
-
|
|
487
|
-
|
|
550
|
+
setServiceLocator(serviceLocator) {
|
|
551
|
+
this.serviceLocator = serviceLocator;
|
|
552
|
+
return this;
|
|
488
553
|
}
|
|
489
|
-
|
|
490
|
-
|
|
554
|
+
buildCustomResources() {
|
|
555
|
+
var _a;
|
|
556
|
+
const customResources = ((_a = this.env.config.getOptionalConfigArray("kubernetes.customResources")) != null ? _a : []).map((c) => ({
|
|
557
|
+
group: c.getString("group"),
|
|
558
|
+
apiVersion: c.getString("apiVersion"),
|
|
559
|
+
plural: c.getString("plural"),
|
|
560
|
+
objectType: "customresources"
|
|
561
|
+
}));
|
|
562
|
+
this.env.logger.info(`action=LoadingCustomResources numOfCustomResources=${customResources.length}`);
|
|
563
|
+
return customResources;
|
|
491
564
|
}
|
|
492
|
-
|
|
493
|
-
|
|
565
|
+
buildClusterSupplier() {
|
|
566
|
+
const config = this.env.config;
|
|
567
|
+
return {
|
|
568
|
+
getClusters() {
|
|
569
|
+
return getCombinedClusterDetails(config);
|
|
570
|
+
}
|
|
571
|
+
};
|
|
494
572
|
}
|
|
495
|
-
|
|
496
|
-
return
|
|
573
|
+
buildObjectsProvider(options) {
|
|
574
|
+
return new KubernetesFanOutHandler(options);
|
|
497
575
|
}
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
case "multiTenant":
|
|
504
|
-
return new MultiTenantServiceLocator(clusterDetails);
|
|
505
|
-
case "http":
|
|
506
|
-
throw new Error("not implemented");
|
|
507
|
-
default:
|
|
508
|
-
throw new Error(`Unsupported kubernetes.clusterLocatorMethod "${serviceLocatorMethod}"`);
|
|
576
|
+
buildFetcher() {
|
|
577
|
+
return new KubernetesClientBasedFetcher({
|
|
578
|
+
kubernetesClientProvider: new KubernetesClientProvider(),
|
|
579
|
+
logger: this.env.logger
|
|
580
|
+
});
|
|
509
581
|
}
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
const response = await kubernetesFanOutHandler.getKubernetesObjectsByEntity(requestBody);
|
|
519
|
-
res.json(response);
|
|
520
|
-
} catch (e) {
|
|
521
|
-
logger.error(`action=retrieveObjectsByServiceId service=${serviceId}, error=${e}`);
|
|
522
|
-
res.status(500).json({error: e.message});
|
|
582
|
+
buildServiceLocator(method, clusterDetails) {
|
|
583
|
+
switch (method) {
|
|
584
|
+
case "multiTenant":
|
|
585
|
+
return this.buildMultiTenantServiceLocator(clusterDetails);
|
|
586
|
+
case "http":
|
|
587
|
+
return this.buildHttpServiceLocator(clusterDetails);
|
|
588
|
+
default:
|
|
589
|
+
throw new Error(`Unsupported kubernetes.clusterLocatorMethod "${method}"`);
|
|
523
590
|
}
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
591
|
+
}
|
|
592
|
+
buildMultiTenantServiceLocator(clusterDetails) {
|
|
593
|
+
return new MultiTenantServiceLocator(clusterDetails);
|
|
594
|
+
}
|
|
595
|
+
buildHttpServiceLocator(_clusterDetails) {
|
|
596
|
+
throw new Error("not implemented");
|
|
597
|
+
}
|
|
598
|
+
buildRouter(objectsProvider, clusterDetails) {
|
|
599
|
+
const logger = this.env.logger;
|
|
600
|
+
const router = Router__default['default']();
|
|
601
|
+
router.use(express__default['default'].json());
|
|
602
|
+
router.post("/services/:serviceId", async (req, res) => {
|
|
603
|
+
const serviceId = req.params.serviceId;
|
|
604
|
+
const requestBody = req.body;
|
|
605
|
+
try {
|
|
606
|
+
const response = await objectsProvider.getKubernetesObjectsByEntity(requestBody);
|
|
607
|
+
res.json(response);
|
|
608
|
+
} catch (e) {
|
|
609
|
+
logger.error(`action=retrieveObjectsByServiceId service=${serviceId}, error=${e}`);
|
|
610
|
+
res.status(500).json({error: e.message});
|
|
611
|
+
}
|
|
532
612
|
});
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
613
|
+
router.get("/clusters", async (_, res) => {
|
|
614
|
+
res.json({
|
|
615
|
+
items: clusterDetails.map((cd) => ({
|
|
616
|
+
name: cd.name,
|
|
617
|
+
dashboardUrl: cd.dashboardUrl,
|
|
618
|
+
authProvider: cd.authProvider
|
|
619
|
+
}))
|
|
620
|
+
});
|
|
621
|
+
});
|
|
622
|
+
return router;
|
|
623
|
+
}
|
|
624
|
+
async fetchClusterDetails(clusterSupplier) {
|
|
625
|
+
const clusterDetails = await clusterSupplier.getClusters();
|
|
626
|
+
this.env.logger.info(`action=loadClusterDetails numOfClustersLoaded=${clusterDetails.length}`);
|
|
627
|
+
return clusterDetails;
|
|
628
|
+
}
|
|
629
|
+
getServiceLocatorMethod() {
|
|
630
|
+
return this.env.config.getString("kubernetes.serviceLocatorMethod.type");
|
|
631
|
+
}
|
|
632
|
+
getObjectTypesToFetch() {
|
|
633
|
+
const objectTypesToFetchStrings = this.env.config.getOptionalStringArray("kubernetes.objectTypes");
|
|
634
|
+
const apiVersionOverrides = this.env.config.getOptionalConfig("kubernetes.apiVersionOverrides");
|
|
635
|
+
let objectTypesToFetch;
|
|
636
|
+
if (objectTypesToFetchStrings) {
|
|
637
|
+
objectTypesToFetch = DEFAULT_OBJECTS.filter((obj) => objectTypesToFetchStrings.includes(obj.objectType));
|
|
638
|
+
}
|
|
639
|
+
if (apiVersionOverrides) {
|
|
640
|
+
objectTypesToFetch = objectTypesToFetch != null ? objectTypesToFetch : DEFAULT_OBJECTS;
|
|
641
|
+
for (const obj of objectTypesToFetch) {
|
|
642
|
+
if (apiVersionOverrides.has(obj.objectType)) {
|
|
643
|
+
obj.apiVersion = apiVersionOverrides.getString(obj.objectType);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
return objectTypesToFetch;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
536
651
|
async function createRouter(options) {
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
logger.info("Initializing Kubernetes backend");
|
|
540
|
-
const customResources = ((_a = options.config.getOptionalConfigArray("kubernetes.customResources")) != null ? _a : []).map((c) => ({
|
|
541
|
-
group: c.getString("group"),
|
|
542
|
-
apiVersion: c.getString("apiVersion"),
|
|
543
|
-
plural: c.getString("plural")
|
|
544
|
-
}));
|
|
545
|
-
logger.info(`action=LoadingCustomResources numOfCustomResources=${customResources.length}`);
|
|
546
|
-
const fetcher = new KubernetesClientBasedFetcher({
|
|
547
|
-
kubernetesClientProvider: new KubernetesClientProvider(),
|
|
548
|
-
logger
|
|
549
|
-
});
|
|
550
|
-
let clusterDetails;
|
|
551
|
-
if (options.clusterSupplier) {
|
|
552
|
-
clusterDetails = await options.clusterSupplier.getClusters();
|
|
553
|
-
} else {
|
|
554
|
-
clusterDetails = await getCombinedClusterDetails(options.config);
|
|
555
|
-
}
|
|
556
|
-
logger.info(`action=loadClusterDetails numOfClustersLoaded=${clusterDetails.length}`);
|
|
557
|
-
const serviceLocator = getServiceLocator(options.config, clusterDetails);
|
|
558
|
-
const objectTypesToFetch = options.config.getOptionalStringArray("kubernetes.objectTypes");
|
|
559
|
-
const kubernetesFanOutHandler = new KubernetesFanOutHandler({
|
|
560
|
-
logger,
|
|
561
|
-
fetcher,
|
|
562
|
-
serviceLocator,
|
|
563
|
-
customResources,
|
|
564
|
-
objectTypesToFetch
|
|
565
|
-
});
|
|
566
|
-
return makeRouter(logger, kubernetesFanOutHandler, clusterDetails);
|
|
652
|
+
const {router} = await KubernetesBuilder.createBuilder(options).setClusterSupplier(options.clusterSupplier).build();
|
|
653
|
+
return router;
|
|
567
654
|
}
|
|
568
655
|
|
|
569
656
|
exports.DEFAULT_OBJECTS = DEFAULT_OBJECTS;
|
|
657
|
+
exports.KubernetesBuilder = KubernetesBuilder;
|
|
570
658
|
exports.createRouter = createRouter;
|
|
571
|
-
exports.makeRouter = makeRouter;
|
|
572
659
|
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":["../src/cluster-locator/ConfigClusterLocator.ts","../src/cluster-locator/GkeClusterLocator.ts","../src/cluster-locator/index.ts","../src/service-locator/MultiTenantServiceLocator.ts","../src/service/KubernetesClientProvider.ts","../src/kubernetes-auth-translator/GoogleKubernetesAuthTranslator.ts","../src/kubernetes-auth-translator/ServiceAccountKubernetesAuthTranslator.ts","../src/kubernetes-auth-translator/AwsIamKubernetesAuthTranslator.ts","../src/kubernetes-auth-translator/KubernetesAuthTranslatorGenerator.ts","../src/service/KubernetesFanOutHandler.ts","../src/service/KubernetesFetcher.ts","../src/service/router.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport { ClusterDetails, KubernetesClustersSupplier } from '../types/types';\n\nexport class ConfigClusterLocator implements KubernetesClustersSupplier {\n private readonly clusterDetails: ClusterDetails[];\n\n constructor(clusterDetails: ClusterDetails[]) {\n this.clusterDetails = clusterDetails;\n }\n\n static fromConfig(config: Config): ConfigClusterLocator {\n // TODO: Add validation that authProvider is required and serviceAccountToken\n // is required if authProvider is serviceAccount\n return new ConfigClusterLocator(\n config.getConfigArray('clusters').map(c => {\n const authProvider = c.getString('authProvider');\n const clusterDetails: ClusterDetails = {\n name: c.getString('name'),\n url: c.getString('url'),\n serviceAccountToken: c.getOptionalString('serviceAccountToken'),\n skipTLSVerify: c.getOptionalBoolean('skipTLSVerify') ?? false,\n authProvider: authProvider,\n };\n const dashboardUrl = c.getOptionalString('dashboardUrl');\n if (dashboardUrl) {\n clusterDetails.dashboardUrl = dashboardUrl;\n }\n const dashboardApp = c.getOptionalString('dashboardApp');\n if (dashboardApp) {\n clusterDetails.dashboardApp = dashboardApp;\n }\n\n switch (authProvider) {\n case 'google': {\n return clusterDetails;\n }\n case 'aws': {\n const assumeRole = c.getOptionalString('assumeRole');\n const externalId = c.getOptionalString('externalId');\n\n return { assumeRole, externalId, ...clusterDetails };\n }\n case 'serviceAccount': {\n return clusterDetails;\n }\n default: {\n throw new Error(\n `authProvider \"${authProvider}\" has no config associated with it`,\n );\n }\n }\n }),\n );\n }\n\n async getClusters(): Promise<ClusterDetails[]> {\n return this.clusterDetails;\n }\n}\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport * as container from '@google-cloud/container';\nimport { GKEClusterDetails, KubernetesClustersSupplier } from '../types/types';\n\ntype GkeClusterLocatorOptions = {\n projectId: string;\n region?: string;\n skipTLSVerify?: boolean;\n};\n\nexport class GkeClusterLocator implements KubernetesClustersSupplier {\n constructor(\n private readonly options: GkeClusterLocatorOptions,\n private readonly client: container.v1.ClusterManagerClient,\n ) {}\n\n static fromConfigWithClient(\n config: Config,\n client: container.v1.ClusterManagerClient,\n ): GkeClusterLocator {\n const options = {\n projectId: config.getString('projectId'),\n region: config.getOptionalString('region') ?? '-',\n skipTLSVerify: config.getOptionalBoolean('skipTLSVerify') ?? false,\n };\n return new GkeClusterLocator(options, client);\n }\n\n static fromConfig(config: Config): GkeClusterLocator {\n return GkeClusterLocator.fromConfigWithClient(\n config,\n new container.v1.ClusterManagerClient(),\n );\n }\n\n async getClusters(): Promise<GKEClusterDetails[]> {\n const { projectId, region, skipTLSVerify } = this.options;\n const request = {\n parent: `projects/${projectId}/locations/${region}`,\n };\n\n try {\n const [response] = await this.client.listClusters(request);\n return (response.clusters ?? []).map(r => ({\n // TODO filter out clusters which don't have name or endpoint\n name: r.name ?? 'unknown',\n url: `https://${r.endpoint ?? ''}`,\n authProvider: 'google',\n skipTLSVerify,\n }));\n } catch (e) {\n throw new Error(\n `There was an error retrieving clusters from GKE for projectId=${projectId} region=${region} : [${e.message}]`,\n );\n }\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport { ClusterDetails } from '../types/types';\nimport { ConfigClusterLocator } from './ConfigClusterLocator';\nimport { GkeClusterLocator } from './GkeClusterLocator';\n\nexport const getCombinedClusterDetails = async (\n rootConfig: Config,\n): Promise<ClusterDetails[]> => {\n return Promise.all(\n rootConfig\n .getConfigArray('kubernetes.clusterLocatorMethods')\n .map(clusterLocatorMethod => {\n const type = clusterLocatorMethod.getString('type');\n switch (type) {\n case 'config':\n return ConfigClusterLocator.fromConfig(\n clusterLocatorMethod,\n ).getClusters();\n case 'gke':\n return GkeClusterLocator.fromConfig(\n clusterLocatorMethod,\n ).getClusters();\n default:\n throw new Error(\n `Unsupported kubernetes.clusterLocatorMethods: \"${type}\"`,\n );\n }\n }),\n )\n .then(res => {\n return res.flat();\n })\n .catch(e => {\n throw e;\n });\n};\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ClusterDetails, KubernetesServiceLocator } from '../types/types';\n\n// This locator assumes that every service is located on every cluster\n// Therefore it will always return all clusters provided\nexport class MultiTenantServiceLocator implements KubernetesServiceLocator {\n private readonly clusterDetails: ClusterDetails[];\n\n constructor(clusterDetails: ClusterDetails[]) {\n this.clusterDetails = clusterDetails;\n }\n\n // As this implementation always returns all clusters serviceId is ignored here\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n async getClustersByServiceId(_serviceId: string): Promise<ClusterDetails[]> {\n return this.clusterDetails;\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n AppsV1Api,\n AutoscalingV1Api,\n CoreV1Api,\n KubeConfig,\n NetworkingV1beta1Api,\n CustomObjectsApi,\n} from '@kubernetes/client-node';\nimport { ClusterDetails } from '../types/types';\n\nexport class KubernetesClientProvider {\n // visible for testing\n getKubeConfig(clusterDetails: ClusterDetails) {\n const cluster = {\n name: clusterDetails.name,\n server: clusterDetails.url,\n skipTLSVerify: clusterDetails.skipTLSVerify,\n };\n\n // TODO configure\n const user = {\n name: 'backstage',\n token: clusterDetails.serviceAccountToken,\n };\n\n const context = {\n name: `${clusterDetails.name}`,\n user: user.name,\n cluster: cluster.name,\n };\n\n const kc = new KubeConfig();\n kc.loadFromOptions({\n clusters: [cluster],\n users: [user],\n contexts: [context],\n currentContext: context.name,\n });\n return kc;\n }\n\n getCoreClientByClusterDetails(clusterDetails: ClusterDetails) {\n const kc = this.getKubeConfig(clusterDetails);\n\n return kc.makeApiClient(CoreV1Api);\n }\n\n getAppsClientByClusterDetails(clusterDetails: ClusterDetails) {\n const kc = this.getKubeConfig(clusterDetails);\n\n return kc.makeApiClient(AppsV1Api);\n }\n\n getAutoscalingClientByClusterDetails(clusterDetails: ClusterDetails) {\n const kc = this.getKubeConfig(clusterDetails);\n\n return kc.makeApiClient(AutoscalingV1Api);\n }\n\n getNetworkingBeta1Client(clusterDetails: ClusterDetails) {\n const kc = this.getKubeConfig(clusterDetails);\n\n return kc.makeApiClient(NetworkingV1beta1Api);\n }\n\n getCustomObjectsClient(clusterDetails: ClusterDetails) {\n const kc = this.getKubeConfig(clusterDetails);\n\n return kc.makeApiClient(CustomObjectsApi);\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { KubernetesAuthTranslator } from './types';\nimport { GKEClusterDetails } from '../types/types';\nimport { KubernetesRequestBody } from '@backstage/plugin-kubernetes-common';\n\nexport class GoogleKubernetesAuthTranslator\n implements KubernetesAuthTranslator\n{\n async decorateClusterDetailsWithAuth(\n clusterDetails: GKEClusterDetails,\n requestBody: KubernetesRequestBody,\n ): Promise<GKEClusterDetails> {\n const clusterDetailsWithAuthToken: GKEClusterDetails = Object.assign(\n {},\n clusterDetails,\n );\n const authToken: string | undefined = requestBody.auth?.google;\n\n if (authToken) {\n clusterDetailsWithAuthToken.serviceAccountToken = authToken;\n } else {\n throw new Error(\n 'Google token not found under auth.google in request body',\n );\n }\n return clusterDetailsWithAuthToken;\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { KubernetesAuthTranslator } from './types';\nimport { ServiceAccountClusterDetails } from '../types/types';\nimport { KubernetesRequestBody } from '@backstage/plugin-kubernetes-common';\n\nexport class ServiceAccountKubernetesAuthTranslator\n implements KubernetesAuthTranslator\n{\n async decorateClusterDetailsWithAuth(\n clusterDetails: ServiceAccountClusterDetails,\n // To ignore TS6133 linting error where it detects 'requestBody' is declared but its value is never read.\n // @ts-ignore-start\n requestBody: KubernetesRequestBody, // eslint-disable-line @typescript-eslint/no-unused-vars\n // @ts-ignore-end\n ): Promise<ServiceAccountClusterDetails> {\n return clusterDetails;\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport AWS, { Credentials } from 'aws-sdk';\nimport { sign } from 'aws4';\nimport { AWSClusterDetails } from '../types/types';\nimport { KubernetesAuthTranslator } from './types';\n\nconst base64 = (str: string) =>\n Buffer.from(str.toString(), 'binary').toString('base64');\nconst prepend = (prep: string) => (str: string) => prep + str;\nconst replace =\n (search: string | RegExp, substitution: string) => (str: string) =>\n str.replace(search, substitution);\nconst pipe =\n (fns: ReadonlyArray<any>) =>\n (thing: string): string =>\n fns.reduce((val, fn) => fn(val), thing);\nconst removePadding = replace(/=+$/, '');\nconst makeUrlSafe = pipe([replace('+', '-'), replace('/', '_')]);\n\ntype SigningCreds = {\n accessKeyId: string | undefined;\n secretAccessKey: string | undefined;\n sessionToken: string | undefined;\n};\n\nexport class AwsIamKubernetesAuthTranslator\n implements KubernetesAuthTranslator\n{\n validCredentials(creds: SigningCreds): boolean {\n return (creds?.accessKeyId &&\n creds?.secretAccessKey &&\n creds?.sessionToken) as unknown as boolean;\n }\n\n awsGetCredentials = async (): Promise<Credentials> => {\n return new Promise((resolve, reject) => {\n AWS.config.getCredentials(err => {\n if (err) {\n return reject(err);\n }\n\n return resolve(AWS.config.credentials as Credentials);\n });\n });\n };\n\n async getCredentials(\n assumeRole?: string,\n externalId?: string,\n ): Promise<SigningCreds> {\n return new Promise<SigningCreds>(async (resolve, reject) => {\n const awsCreds = await this.awsGetCredentials();\n\n if (!(awsCreds instanceof Credentials))\n return reject(Error('No AWS credentials found.'));\n\n let creds: SigningCreds = {\n accessKeyId: awsCreds.accessKeyId,\n secretAccessKey: awsCreds.secretAccessKey,\n sessionToken: awsCreds.sessionToken,\n };\n\n if (!this.validCredentials(creds))\n return reject(Error('Invalid AWS credentials found.'));\n if (!assumeRole) return resolve(creds);\n\n try {\n const params: AWS.STS.Types.AssumeRoleRequest = {\n RoleArn: assumeRole,\n RoleSessionName: 'backstage-login',\n };\n if (externalId) params.ExternalId = externalId;\n\n const assumedRole = await new AWS.STS().assumeRole(params).promise();\n\n if (!assumedRole.Credentials) {\n throw new Error(`No credentials returned for role ${assumeRole}`);\n }\n\n creds = {\n accessKeyId: assumedRole.Credentials.AccessKeyId,\n secretAccessKey: assumedRole.Credentials.SecretAccessKey,\n sessionToken: assumedRole.Credentials.SessionToken,\n };\n } catch (e) {\n console.warn(`There was an error assuming the role: ${e}`);\n return reject(Error(`Unable to assume role: ${e}`));\n }\n return resolve(creds);\n });\n }\n async getBearerToken(\n clusterName: string,\n assumeRole?: string,\n externalId?: string,\n ): Promise<string> {\n const credentials = await this.getCredentials(assumeRole, externalId);\n\n const request = {\n host: `sts.amazonaws.com`,\n path: `/?Action=GetCallerIdentity&Version=2011-06-15&X-Amz-Expires=60`,\n headers: {\n 'x-k8s-aws-id': clusterName,\n },\n signQuery: true,\n };\n\n const signedRequest = sign(request, credentials);\n\n return pipe([\n (signed: any) => `https://${signed.host}${signed.path}`,\n base64,\n removePadding,\n makeUrlSafe,\n prepend('k8s-aws-v1.'),\n ])(signedRequest);\n }\n\n async decorateClusterDetailsWithAuth(\n clusterDetails: AWSClusterDetails,\n ): Promise<AWSClusterDetails> {\n const clusterDetailsWithAuthToken: AWSClusterDetails = Object.assign(\n {},\n clusterDetails,\n );\n\n clusterDetailsWithAuthToken.serviceAccountToken = await this.getBearerToken(\n clusterDetails.name,\n clusterDetails.assumeRole,\n clusterDetails.externalId,\n );\n return clusterDetailsWithAuthToken;\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { KubernetesAuthTranslator } from './types';\nimport { GoogleKubernetesAuthTranslator } from './GoogleKubernetesAuthTranslator';\nimport { ServiceAccountKubernetesAuthTranslator } from './ServiceAccountKubernetesAuthTranslator';\nimport { AwsIamKubernetesAuthTranslator } from './AwsIamKubernetesAuthTranslator';\n\nexport class KubernetesAuthTranslatorGenerator {\n static getKubernetesAuthTranslatorInstance(\n authProvider: string,\n ): KubernetesAuthTranslator {\n switch (authProvider) {\n case 'google': {\n return new GoogleKubernetesAuthTranslator();\n }\n case 'aws': {\n return new AwsIamKubernetesAuthTranslator();\n }\n case 'serviceAccount': {\n return new ServiceAccountKubernetesAuthTranslator();\n }\n default: {\n throw new Error(\n `authProvider \"${authProvider}\" has no KubernetesAuthTranslator associated with it`,\n );\n }\n }\n }\n}\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Logger } from 'winston';\nimport {\n ClusterDetails,\n CustomResource,\n KubernetesFetcher,\n KubernetesObjectTypes,\n KubernetesServiceLocator,\n} from '../types/types';\nimport {\n ClusterObjects,\n KubernetesRequestBody,\n} from '@backstage/plugin-kubernetes-common';\nimport { KubernetesAuthTranslator } from '../kubernetes-auth-translator/types';\nimport { KubernetesAuthTranslatorGenerator } from '../kubernetes-auth-translator/KubernetesAuthTranslatorGenerator';\n\nexport const DEFAULT_OBJECTS: KubernetesObjectTypes[] = [\n 'pods',\n 'services',\n 'configmaps',\n 'deployments',\n 'replicasets',\n 'horizontalpodautoscalers',\n 'ingresses',\n];\n\nexport interface KubernetesFanOutHandlerOptions {\n logger: Logger;\n fetcher: KubernetesFetcher;\n serviceLocator: KubernetesServiceLocator;\n customResources: CustomResource[];\n objectTypesToFetch?: KubernetesObjectTypes[];\n}\n\nexport class KubernetesFanOutHandler {\n private readonly logger: Logger;\n private readonly fetcher: KubernetesFetcher;\n private readonly serviceLocator: KubernetesServiceLocator;\n private readonly customResources: CustomResource[];\n private readonly objectTypesToFetch: KubernetesObjectTypes[];\n\n constructor({\n logger,\n fetcher,\n serviceLocator,\n customResources,\n objectTypesToFetch = DEFAULT_OBJECTS,\n }: KubernetesFanOutHandlerOptions) {\n this.logger = logger;\n this.fetcher = fetcher;\n this.serviceLocator = serviceLocator;\n this.customResources = customResources;\n this.objectTypesToFetch = objectTypesToFetch;\n }\n\n async getKubernetesObjectsByEntity(requestBody: KubernetesRequestBody) {\n const entityName =\n requestBody.entity?.metadata?.annotations?.[\n 'backstage.io/kubernetes-id'\n ] || requestBody.entity?.metadata?.name;\n\n const clusterDetails: ClusterDetails[] =\n await this.serviceLocator.getClustersByServiceId(entityName);\n\n // Execute all of these async actions simultaneously/without blocking sequentially as no common object is modified by them\n const promises: Promise<ClusterDetails>[] = clusterDetails.map(cd => {\n const kubernetesAuthTranslator: KubernetesAuthTranslator =\n KubernetesAuthTranslatorGenerator.getKubernetesAuthTranslatorInstance(\n cd.authProvider,\n );\n return kubernetesAuthTranslator.decorateClusterDetailsWithAuth(\n cd,\n requestBody,\n );\n });\n const clusterDetailsDecoratedForAuth: ClusterDetails[] = await Promise.all(\n promises,\n );\n\n this.logger.info(\n `entity.metadata.name=${entityName} clusterDetails=[${clusterDetailsDecoratedForAuth\n .map(c => c.name)\n .join(', ')}]`,\n );\n\n const labelSelector: string =\n requestBody.entity?.metadata?.annotations?.[\n 'backstage.io/kubernetes-label-selector'\n ] || `backstage.io/kubernetes-id=${entityName}`;\n\n return Promise.all(\n clusterDetailsDecoratedForAuth.map(clusterDetailsItem => {\n return this.fetcher\n .fetchObjectsForService({\n serviceId: entityName,\n clusterDetails: clusterDetailsItem,\n objectTypesToFetch: new Set(this.objectTypesToFetch),\n labelSelector,\n customResources: this.customResources,\n })\n .then(result => {\n const objects: ClusterObjects = {\n cluster: {\n name: clusterDetailsItem.name,\n },\n resources: result.responses,\n errors: result.errors,\n };\n if (clusterDetailsItem.dashboardUrl) {\n objects.cluster.dashboardUrl = clusterDetailsItem.dashboardUrl;\n }\n if (clusterDetailsItem.dashboardApp) {\n objects.cluster.dashboardApp = clusterDetailsItem.dashboardApp;\n }\n return objects;\n });\n }),\n ).then(r => ({\n items: r.filter(\n item =>\n (item.errors !== undefined && item.errors.length >= 1) ||\n (item.resources !== undefined &&\n item.resources.length >= 1 &&\n item.resources.some(fr => fr.resources.length >= 1)),\n ),\n }));\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n AppsV1Api,\n AutoscalingV1Api,\n CoreV1Api,\n ExtensionsV1beta1Ingress,\n NetworkingV1beta1Api,\n V1ConfigMap,\n V1Deployment,\n V1HorizontalPodAutoscaler,\n V1Pod,\n V1ReplicaSet,\n} from '@kubernetes/client-node';\nimport { V1Service } from '@kubernetes/client-node/dist/gen/model/v1Service';\nimport http from 'http';\nimport lodash, { Dictionary } from 'lodash';\nimport { Logger } from 'winston';\nimport {\n ClusterDetails,\n FetchResponseWrapper,\n KubernetesFetcher,\n KubernetesObjectTypes,\n ObjectFetchParams,\n CustomResource,\n} from '../types/types';\nimport {\n FetchResponse,\n KubernetesFetchError,\n KubernetesErrorTypes,\n} from '@backstage/plugin-kubernetes-common';\nimport { KubernetesClientProvider } from './KubernetesClientProvider';\n\nexport interface Clients {\n core: CoreV1Api;\n apps: AppsV1Api;\n autoscaling: AutoscalingV1Api;\n networkingBeta1: NetworkingV1beta1Api;\n}\n\nexport interface KubernetesClientBasedFetcherOptions {\n kubernetesClientProvider: KubernetesClientProvider;\n logger: Logger;\n}\n\ntype FetchResult = FetchResponse | KubernetesFetchError;\n\nconst isError = (fr: FetchResult): fr is KubernetesFetchError =>\n fr.hasOwnProperty('errorType');\n\nfunction fetchResultsToResponseWrapper(\n results: FetchResult[],\n): FetchResponseWrapper {\n const groupBy: Dictionary<FetchResult[]> = lodash.groupBy(results, value => {\n return isError(value) ? 'errors' : 'responses';\n });\n\n return {\n errors: groupBy.errors ?? [],\n responses: groupBy.responses ?? [],\n } as FetchResponseWrapper; // TODO would be nice to get rid of this 'as'\n}\n\nconst statusCodeToErrorType = (statusCode: number): KubernetesErrorTypes => {\n switch (statusCode) {\n case 400:\n return 'BAD_REQUEST';\n case 401:\n return 'UNAUTHORIZED_ERROR';\n case 500:\n return 'SYSTEM_ERROR';\n default:\n return 'UNKNOWN_ERROR';\n }\n};\n\nconst captureKubernetesErrorsRethrowOthers = (e: any): KubernetesFetchError => {\n if (e.response && e.response.statusCode) {\n return {\n errorType: statusCodeToErrorType(e.response.statusCode),\n statusCode: e.response.statusCode,\n resourcePath: e.response.request.uri.pathname,\n };\n }\n throw e;\n};\n\nexport class KubernetesClientBasedFetcher implements KubernetesFetcher {\n private readonly kubernetesClientProvider: KubernetesClientProvider;\n private readonly logger: Logger;\n\n constructor({\n kubernetesClientProvider,\n logger,\n }: KubernetesClientBasedFetcherOptions) {\n this.kubernetesClientProvider = kubernetesClientProvider;\n this.logger = logger;\n }\n\n fetchObjectsForService(\n params: ObjectFetchParams,\n ): Promise<FetchResponseWrapper> {\n const fetchResults = Array.from(params.objectTypesToFetch).map(type => {\n return this.fetchByObjectType(\n params.clusterDetails,\n type,\n params.labelSelector ||\n `backstage.io/kubernetes-id=${params.serviceId}`,\n ).catch(captureKubernetesErrorsRethrowOthers);\n });\n\n const customObjectsFetchResults = params.customResources.map(cr => {\n return this.fetchCustomResource(\n params.clusterDetails,\n cr,\n params.labelSelector ||\n `backstage.io/kubernetes-id=${params.serviceId}`,\n ).catch(captureKubernetesErrorsRethrowOthers);\n });\n\n return Promise.all(fetchResults.concat(customObjectsFetchResults)).then(\n fetchResultsToResponseWrapper,\n );\n }\n\n // TODO could probably do with a tidy up\n private fetchByObjectType(\n clusterDetails: ClusterDetails,\n type: KubernetesObjectTypes,\n labelSelector: string,\n ): Promise<FetchResponse> {\n switch (type) {\n case 'pods':\n return this.fetchPodsForService(clusterDetails, labelSelector).then(\n r => ({\n type: type,\n resources: r,\n }),\n );\n case 'configmaps':\n return this.fetchConfigMapsForService(\n clusterDetails,\n labelSelector,\n ).then(r => ({ type: type, resources: r }));\n case 'deployments':\n return this.fetchDeploymentsForService(\n clusterDetails,\n labelSelector,\n ).then(r => ({ type: type, resources: r }));\n case 'replicasets':\n return this.fetchReplicaSetsForService(\n clusterDetails,\n labelSelector,\n ).then(r => ({ type: type, resources: r }));\n case 'services':\n return this.fetchServicesForService(clusterDetails, labelSelector).then(\n r => ({ type: type, resources: r }),\n );\n case 'horizontalpodautoscalers':\n return this.fetchHorizontalPodAutoscalersForService(\n clusterDetails,\n labelSelector,\n ).then(r => ({ type: type, resources: r }));\n case 'ingresses':\n return this.fetchIngressesForService(\n clusterDetails,\n labelSelector,\n ).then(r => ({ type: type, resources: r }));\n default:\n // unrecognised type\n throw new Error(`unrecognised type=${type}`);\n }\n }\n\n private fetchCustomResource(\n clusterDetails: ClusterDetails,\n customResource: CustomResource,\n labelSelector: string,\n ): Promise<FetchResponse> {\n const customObjects =\n this.kubernetesClientProvider.getCustomObjectsClient(clusterDetails);\n\n return customObjects\n .listClusterCustomObject(\n customResource.group,\n customResource.apiVersion,\n customResource.plural,\n '',\n '',\n '',\n labelSelector,\n )\n .then(r => {\n return { type: 'customresources', resources: (r.body as any).items };\n });\n }\n\n private singleClusterFetch<T>(\n clusterDetails: ClusterDetails,\n fn: (\n client: Clients,\n ) => Promise<{ body: { items: Array<T> }; response: http.IncomingMessage }>,\n ): Promise<Array<T>> {\n const core =\n this.kubernetesClientProvider.getCoreClientByClusterDetails(\n clusterDetails,\n );\n const apps =\n this.kubernetesClientProvider.getAppsClientByClusterDetails(\n clusterDetails,\n );\n const autoscaling =\n this.kubernetesClientProvider.getAutoscalingClientByClusterDetails(\n clusterDetails,\n );\n const networkingBeta1 =\n this.kubernetesClientProvider.getNetworkingBeta1Client(clusterDetails);\n\n this.logger.debug(`calling cluster=${clusterDetails.name}`);\n return fn({ core, apps, autoscaling, networkingBeta1 }).then(({ body }) => {\n return body.items;\n });\n }\n\n private fetchServicesForService(\n clusterDetails: ClusterDetails,\n labelSelector: string,\n ): Promise<Array<V1Service>> {\n return this.singleClusterFetch<V1Service>(clusterDetails, ({ core }) =>\n core.listServiceForAllNamespaces(false, '', '', labelSelector),\n );\n }\n\n private fetchPodsForService(\n clusterDetails: ClusterDetails,\n labelSelector: string,\n ): Promise<Array<V1Pod>> {\n return this.singleClusterFetch<V1Pod>(clusterDetails, ({ core }) =>\n core.listPodForAllNamespaces(false, '', '', labelSelector),\n );\n }\n\n private fetchConfigMapsForService(\n clusterDetails: ClusterDetails,\n labelSelector: string,\n ): Promise<Array<V1ConfigMap>> {\n return this.singleClusterFetch<V1Pod>(clusterDetails, ({ core }) =>\n core.listConfigMapForAllNamespaces(false, '', '', labelSelector),\n );\n }\n\n private fetchDeploymentsForService(\n clusterDetails: ClusterDetails,\n labelSelector: string,\n ): Promise<Array<V1Deployment>> {\n return this.singleClusterFetch<V1Deployment>(clusterDetails, ({ apps }) =>\n apps.listDeploymentForAllNamespaces(false, '', '', labelSelector),\n );\n }\n\n private fetchReplicaSetsForService(\n clusterDetails: ClusterDetails,\n labelSelector: string,\n ): Promise<Array<V1ReplicaSet>> {\n return this.singleClusterFetch<V1ReplicaSet>(clusterDetails, ({ apps }) =>\n apps.listReplicaSetForAllNamespaces(false, '', '', labelSelector),\n );\n }\n\n private fetchHorizontalPodAutoscalersForService(\n clusterDetails: ClusterDetails,\n labelSelector: string,\n ): Promise<Array<V1HorizontalPodAutoscaler>> {\n return this.singleClusterFetch<V1HorizontalPodAutoscaler>(\n clusterDetails,\n ({ autoscaling }) =>\n autoscaling.listHorizontalPodAutoscalerForAllNamespaces(\n false,\n '',\n '',\n labelSelector,\n ),\n );\n }\n\n private fetchIngressesForService(\n clusterDetails: ClusterDetails,\n labelSelector: string,\n ): Promise<Array<ExtensionsV1beta1Ingress>> {\n return this.singleClusterFetch<ExtensionsV1beta1Ingress>(\n clusterDetails,\n ({ networkingBeta1 }) =>\n networkingBeta1.listIngressForAllNamespaces(\n false,\n '',\n '',\n labelSelector,\n ),\n );\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport express from 'express';\nimport Router from 'express-promise-router';\nimport { Logger } from 'winston';\nimport { getCombinedClusterDetails } from '../cluster-locator';\nimport { MultiTenantServiceLocator } from '../service-locator/MultiTenantServiceLocator';\nimport {\n ClusterDetails,\n KubernetesClustersSupplier,\n KubernetesObjectTypes,\n KubernetesServiceLocator,\n ServiceLocatorMethod,\n CustomResource,\n} from '../types/types';\nimport { KubernetesRequestBody } from '@backstage/plugin-kubernetes-common';\nimport { KubernetesClientProvider } from './KubernetesClientProvider';\nimport { KubernetesFanOutHandler } from './KubernetesFanOutHandler';\nimport { KubernetesClientBasedFetcher } from './KubernetesFetcher';\n\nexport interface RouterOptions {\n logger: Logger;\n config: Config;\n clusterSupplier?: KubernetesClustersSupplier;\n}\n\nconst getServiceLocator = (\n config: Config,\n clusterDetails: ClusterDetails[],\n): KubernetesServiceLocator => {\n const serviceLocatorMethod = config.getString(\n 'kubernetes.serviceLocatorMethod.type',\n ) as ServiceLocatorMethod;\n\n switch (serviceLocatorMethod) {\n case 'multiTenant':\n return new MultiTenantServiceLocator(clusterDetails);\n case 'http':\n throw new Error('not implemented');\n default:\n throw new Error(\n `Unsupported kubernetes.clusterLocatorMethod \"${serviceLocatorMethod}\"`,\n );\n }\n};\n\nexport const makeRouter = (\n logger: Logger,\n kubernetesFanOutHandler: KubernetesFanOutHandler,\n clusterDetails: ClusterDetails[],\n): express.Router => {\n const router = Router();\n router.use(express.json());\n\n router.post('/services/:serviceId', async (req, res) => {\n const serviceId = req.params.serviceId;\n const requestBody: KubernetesRequestBody = req.body;\n try {\n const response =\n await kubernetesFanOutHandler.getKubernetesObjectsByEntity(requestBody);\n res.json(response);\n } catch (e) {\n logger.error(\n `action=retrieveObjectsByServiceId service=${serviceId}, error=${e}`,\n );\n res.status(500).json({ error: e.message });\n }\n });\n\n router.get('/clusters', async (_, res) => {\n res.json({\n items: clusterDetails.map(cd => ({\n name: cd.name,\n dashboardUrl: cd.dashboardUrl,\n authProvider: cd.authProvider,\n })),\n });\n });\n return router;\n};\n\nexport async function createRouter(\n options: RouterOptions,\n): Promise<express.Router> {\n const logger = options.logger;\n\n logger.info('Initializing Kubernetes backend');\n\n const customResources: CustomResource[] = (\n options.config.getOptionalConfigArray('kubernetes.customResources') ?? []\n ).map(\n c =>\n ({\n group: c.getString('group'),\n apiVersion: c.getString('apiVersion'),\n plural: c.getString('plural'),\n } as CustomResource),\n );\n\n logger.info(\n `action=LoadingCustomResources numOfCustomResources=${customResources.length}`,\n );\n\n const fetcher = new KubernetesClientBasedFetcher({\n kubernetesClientProvider: new KubernetesClientProvider(),\n logger,\n });\n\n let clusterDetails: ClusterDetails[];\n\n if (options.clusterSupplier) {\n clusterDetails = await options.clusterSupplier.getClusters();\n } else {\n clusterDetails = await getCombinedClusterDetails(options.config);\n }\n\n logger.info(\n `action=loadClusterDetails numOfClustersLoaded=${clusterDetails.length}`,\n );\n\n const serviceLocator = getServiceLocator(options.config, clusterDetails);\n const objectTypesToFetch = options.config.getOptionalStringArray(\n 'kubernetes.objectTypes',\n ) as KubernetesObjectTypes[];\n\n const kubernetesFanOutHandler = new KubernetesFanOutHandler({\n logger,\n fetcher,\n serviceLocator,\n customResources,\n objectTypesToFetch,\n });\n\n return makeRouter(logger, kubernetesFanOutHandler, clusterDetails);\n}\n"],"names":["container","KubeConfig","CoreV1Api","AppsV1Api","AutoscalingV1Api","NetworkingV1beta1Api","CustomObjectsApi","AWS","Credentials","sign","lodash","Router","express"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;2BAmBwE;AAAA,EAGtE,YAAY,gBAAkC;AAC5C,SAAK,iBAAiB;AAAA;AAAA,SAGjB,WAAW,QAAsC;AAGtD,WAAO,IAAI,qBACT,OAAO,eAAe,YAAY,IAAI,OAAK;AA9BjD;AA+BQ,YAAM,eAAe,EAAE,UAAU;AACjC,YAAM,iBAAiC;AAAA,QACrC,MAAM,EAAE,UAAU;AAAA,QAClB,KAAK,EAAE,UAAU;AAAA,QACjB,qBAAqB,EAAE,kBAAkB;AAAA,QACzC,eAAe,QAAE,mBAAmB,qBAArB,YAAyC;AAAA,QACxD;A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AAI;AAAA,IAC9B;AAAA;AAGF,MAAI;AAEJ,MAAI,QAAQ,iBAAiB;AAC3B,qBAAiB,MAAM,QAAQ,gBAAgB;AAAA,SAC1C;AACL,qBAAiB,MAAM,0BAA0B,QAAQ;AAAA;AAG3D,SAAO,KACL,iDAAiD,eAAe;AAGlE,QAAM,iBAAiB,kBAAkB,QAAQ,QAAQ;AACzD,QAAM,qBAAqB,QAAQ,OAAO,uBACxC;AAGF,QAAM,0BAA0B,IAAI,wBAAwB;AAAA,IAC1D;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA;AAGF,SAAO,WAAW,QAAQ,yBAAyB;AAAA;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../src/cluster-locator/ConfigClusterLocator.ts","../src/cluster-locator/GkeClusterLocator.ts","../src/cluster-locator/index.ts","../src/service-locator/MultiTenantServiceLocator.ts","../src/service/KubernetesClientProvider.ts","../src/kubernetes-auth-translator/GoogleKubernetesAuthTranslator.ts","../src/kubernetes-auth-translator/ServiceAccountKubernetesAuthTranslator.ts","../src/kubernetes-auth-translator/AwsIamKubernetesAuthTranslator.ts","../src/kubernetes-auth-translator/KubernetesAuthTranslatorGenerator.ts","../src/service/KubernetesFanOutHandler.ts","../src/service/KubernetesFetcher.ts","../src/service/KubernetesBuilder.ts","../src/service/router.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport { ClusterDetails, KubernetesClustersSupplier } from '../types/types';\n\nexport class ConfigClusterLocator implements KubernetesClustersSupplier {\n private readonly clusterDetails: ClusterDetails[];\n\n constructor(clusterDetails: ClusterDetails[]) {\n this.clusterDetails = clusterDetails;\n }\n\n static fromConfig(config: Config): ConfigClusterLocator {\n // TODO: Add validation that authProvider is required and serviceAccountToken\n // is required if authProvider is serviceAccount\n return new ConfigClusterLocator(\n config.getConfigArray('clusters').map(c => {\n const authProvider = c.getString('authProvider');\n const clusterDetails: ClusterDetails = {\n name: c.getString('name'),\n url: c.getString('url'),\n serviceAccountToken: c.getOptionalString('serviceAccountToken'),\n skipTLSVerify: c.getOptionalBoolean('skipTLSVerify') ?? false,\n caData: c.getOptionalString('caData'),\n authProvider: authProvider,\n };\n const dashboardUrl = c.getOptionalString('dashboardUrl');\n if (dashboardUrl) {\n clusterDetails.dashboardUrl = dashboardUrl;\n }\n const dashboardApp = c.getOptionalString('dashboardApp');\n if (dashboardApp) {\n clusterDetails.dashboardApp = dashboardApp;\n }\n\n switch (authProvider) {\n case 'google': {\n return clusterDetails;\n }\n case 'aws': {\n const assumeRole = c.getOptionalString('assumeRole');\n const externalId = c.getOptionalString('externalId');\n\n return { assumeRole, externalId, ...clusterDetails };\n }\n case 'serviceAccount': {\n return clusterDetails;\n }\n default: {\n throw new Error(\n `authProvider \"${authProvider}\" has no config associated with it`,\n );\n }\n }\n }),\n );\n }\n\n async getClusters(): Promise<ClusterDetails[]> {\n return this.clusterDetails;\n }\n}\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport { ForwardedError } from '@backstage/errors';\nimport * as container from '@google-cloud/container';\nimport { GKEClusterDetails, KubernetesClustersSupplier } from '../types/types';\n\ntype GkeClusterLocatorOptions = {\n projectId: string;\n region?: string;\n skipTLSVerify?: boolean;\n};\n\nexport class GkeClusterLocator implements KubernetesClustersSupplier {\n constructor(\n private readonly options: GkeClusterLocatorOptions,\n private readonly client: container.v1.ClusterManagerClient,\n ) {}\n\n static fromConfigWithClient(\n config: Config,\n client: container.v1.ClusterManagerClient,\n ): GkeClusterLocator {\n const options = {\n projectId: config.getString('projectId'),\n region: config.getOptionalString('region') ?? '-',\n skipTLSVerify: config.getOptionalBoolean('skipTLSVerify') ?? false,\n };\n return new GkeClusterLocator(options, client);\n }\n\n static fromConfig(config: Config): GkeClusterLocator {\n return GkeClusterLocator.fromConfigWithClient(\n config,\n new container.v1.ClusterManagerClient(),\n );\n }\n\n // TODO pass caData into the object\n async getClusters(): Promise<GKEClusterDetails[]> {\n const { projectId, region, skipTLSVerify } = this.options;\n const request = {\n parent: `projects/${projectId}/locations/${region}`,\n };\n\n try {\n const [response] = await this.client.listClusters(request);\n return (response.clusters ?? []).map(r => ({\n // TODO filter out clusters which don't have name or endpoint\n name: r.name ?? 'unknown',\n url: `https://${r.endpoint ?? ''}`,\n authProvider: 'google',\n skipTLSVerify,\n }));\n } catch (e) {\n throw new ForwardedError(\n `There was an error retrieving clusters from GKE for projectId=${projectId} region=${region}`,\n e,\n );\n }\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport { ClusterDetails } from '../types/types';\nimport { ConfigClusterLocator } from './ConfigClusterLocator';\nimport { GkeClusterLocator } from './GkeClusterLocator';\n\nexport const getCombinedClusterDetails = async (\n rootConfig: Config,\n): Promise<ClusterDetails[]> => {\n return Promise.all(\n rootConfig\n .getConfigArray('kubernetes.clusterLocatorMethods')\n .map(clusterLocatorMethod => {\n const type = clusterLocatorMethod.getString('type');\n switch (type) {\n case 'config':\n return ConfigClusterLocator.fromConfig(\n clusterLocatorMethod,\n ).getClusters();\n case 'gke':\n return GkeClusterLocator.fromConfig(\n clusterLocatorMethod,\n ).getClusters();\n default:\n throw new Error(\n `Unsupported kubernetes.clusterLocatorMethods: \"${type}\"`,\n );\n }\n }),\n )\n .then(res => {\n return res.flat();\n })\n .catch(e => {\n throw e;\n });\n};\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ClusterDetails, KubernetesServiceLocator } from '../types/types';\n\n// This locator assumes that every service is located on every cluster\n// Therefore it will always return all clusters provided\nexport class MultiTenantServiceLocator implements KubernetesServiceLocator {\n private readonly clusterDetails: ClusterDetails[];\n\n constructor(clusterDetails: ClusterDetails[]) {\n this.clusterDetails = clusterDetails;\n }\n\n // As this implementation always returns all clusters serviceId is ignored here\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n async getClustersByServiceId(_serviceId: string): Promise<ClusterDetails[]> {\n return this.clusterDetails;\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n AppsV1Api,\n BatchV1beta1Api,\n AutoscalingV1Api,\n CoreV1Api,\n KubeConfig,\n NetworkingV1beta1Api,\n CustomObjectsApi,\n} from '@kubernetes/client-node';\nimport { ClusterDetails } from '../types/types';\n\nexport class KubernetesClientProvider {\n // visible for testing\n getKubeConfig(clusterDetails: ClusterDetails) {\n const cluster = {\n name: clusterDetails.name,\n server: clusterDetails.url,\n skipTLSVerify: clusterDetails.skipTLSVerify,\n caData: clusterDetails.caData,\n };\n\n // TODO configure\n const user = {\n name: 'backstage',\n token: clusterDetails.serviceAccountToken,\n };\n\n const context = {\n name: `${clusterDetails.name}`,\n user: user.name,\n cluster: cluster.name,\n };\n\n const kc = new KubeConfig();\n kc.loadFromOptions({\n clusters: [cluster],\n users: [user],\n contexts: [context],\n currentContext: context.name,\n });\n return kc;\n }\n\n getCoreClientByClusterDetails(clusterDetails: ClusterDetails) {\n const kc = this.getKubeConfig(clusterDetails);\n\n return kc.makeApiClient(CoreV1Api);\n }\n\n getAppsClientByClusterDetails(clusterDetails: ClusterDetails) {\n const kc = this.getKubeConfig(clusterDetails);\n\n return kc.makeApiClient(AppsV1Api);\n }\n\n getAutoscalingClientByClusterDetails(clusterDetails: ClusterDetails) {\n const kc = this.getKubeConfig(clusterDetails);\n\n return kc.makeApiClient(AutoscalingV1Api);\n }\n\n getBatchClientByClusterDetails(clusterDetails: ClusterDetails) {\n const kc = this.getKubeConfig(clusterDetails);\n\n return kc.makeApiClient(BatchV1beta1Api);\n }\n\n getNetworkingBeta1Client(clusterDetails: ClusterDetails) {\n const kc = this.getKubeConfig(clusterDetails);\n\n return kc.makeApiClient(NetworkingV1beta1Api);\n }\n\n getCustomObjectsClient(clusterDetails: ClusterDetails) {\n const kc = this.getKubeConfig(clusterDetails);\n\n return kc.makeApiClient(CustomObjectsApi);\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { KubernetesAuthTranslator } from './types';\nimport { GKEClusterDetails } from '../types/types';\nimport { KubernetesRequestBody } from '@backstage/plugin-kubernetes-common';\n\nexport class GoogleKubernetesAuthTranslator\n implements KubernetesAuthTranslator\n{\n async decorateClusterDetailsWithAuth(\n clusterDetails: GKEClusterDetails,\n requestBody: KubernetesRequestBody,\n ): Promise<GKEClusterDetails> {\n const clusterDetailsWithAuthToken: GKEClusterDetails = Object.assign(\n {},\n clusterDetails,\n );\n const authToken: string | undefined = requestBody.auth?.google;\n\n if (authToken) {\n clusterDetailsWithAuthToken.serviceAccountToken = authToken;\n } else {\n throw new Error(\n 'Google token not found under auth.google in request body',\n );\n }\n return clusterDetailsWithAuthToken;\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { KubernetesAuthTranslator } from './types';\nimport { ServiceAccountClusterDetails } from '../types/types';\nimport { KubernetesRequestBody } from '@backstage/plugin-kubernetes-common';\n\nexport class ServiceAccountKubernetesAuthTranslator\n implements KubernetesAuthTranslator\n{\n async decorateClusterDetailsWithAuth(\n clusterDetails: ServiceAccountClusterDetails,\n // To ignore TS6133 linting error where it detects 'requestBody' is declared but its value is never read.\n // @ts-ignore-start\n requestBody: KubernetesRequestBody, // eslint-disable-line @typescript-eslint/no-unused-vars\n // @ts-ignore-end\n ): Promise<ServiceAccountClusterDetails> {\n return clusterDetails;\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport AWS, { Credentials } from 'aws-sdk';\nimport { sign } from 'aws4';\nimport { AWSClusterDetails } from '../types/types';\nimport { KubernetesAuthTranslator } from './types';\n\nconst base64 = (str: string) =>\n Buffer.from(str.toString(), 'binary').toString('base64');\nconst prepend = (prep: string) => (str: string) => prep + str;\nconst replace =\n (search: string | RegExp, substitution: string) => (str: string) =>\n str.replace(search, substitution);\nconst pipe =\n (fns: ReadonlyArray<any>) =>\n (thing: string): string =>\n fns.reduce((val, fn) => fn(val), thing);\nconst removePadding = replace(/=+$/, '');\nconst makeUrlSafe = pipe([replace('+', '-'), replace('/', '_')]);\n\ntype SigningCreds = {\n accessKeyId: string | undefined;\n secretAccessKey: string | undefined;\n sessionToken: string | undefined;\n};\n\nexport class AwsIamKubernetesAuthTranslator\n implements KubernetesAuthTranslator\n{\n validCredentials(creds: SigningCreds): boolean {\n return (creds?.accessKeyId &&\n creds?.secretAccessKey &&\n creds?.sessionToken) as unknown as boolean;\n }\n\n awsGetCredentials = async (): Promise<Credentials> => {\n return new Promise((resolve, reject) => {\n AWS.config.getCredentials(err => {\n if (err) {\n return reject(err);\n }\n\n return resolve(AWS.config.credentials as Credentials);\n });\n });\n };\n\n async getCredentials(\n assumeRole?: string,\n externalId?: string,\n ): Promise<SigningCreds> {\n return new Promise<SigningCreds>(async (resolve, reject) => {\n const awsCreds = await this.awsGetCredentials();\n\n if (!(awsCreds instanceof Credentials))\n return reject(Error('No AWS credentials found.'));\n\n let creds: SigningCreds = {\n accessKeyId: awsCreds.accessKeyId,\n secretAccessKey: awsCreds.secretAccessKey,\n sessionToken: awsCreds.sessionToken,\n };\n\n if (!this.validCredentials(creds))\n return reject(Error('Invalid AWS credentials found.'));\n if (!assumeRole) return resolve(creds);\n\n try {\n const params: AWS.STS.Types.AssumeRoleRequest = {\n RoleArn: assumeRole,\n RoleSessionName: 'backstage-login',\n };\n if (externalId) params.ExternalId = externalId;\n\n const assumedRole = await new AWS.STS().assumeRole(params).promise();\n\n if (!assumedRole.Credentials) {\n throw new Error(`No credentials returned for role ${assumeRole}`);\n }\n\n creds = {\n accessKeyId: assumedRole.Credentials.AccessKeyId,\n secretAccessKey: assumedRole.Credentials.SecretAccessKey,\n sessionToken: assumedRole.Credentials.SessionToken,\n };\n } catch (e) {\n console.warn(`There was an error assuming the role: ${e}`);\n return reject(Error(`Unable to assume role: ${e}`));\n }\n return resolve(creds);\n });\n }\n async getBearerToken(\n clusterName: string,\n assumeRole?: string,\n externalId?: string,\n ): Promise<string> {\n const credentials = await this.getCredentials(assumeRole, externalId);\n\n const request = {\n host: `sts.amazonaws.com`,\n path: `/?Action=GetCallerIdentity&Version=2011-06-15&X-Amz-Expires=60`,\n headers: {\n 'x-k8s-aws-id': clusterName,\n },\n signQuery: true,\n };\n\n const signedRequest = sign(request, credentials);\n\n return pipe([\n (signed: any) => `https://${signed.host}${signed.path}`,\n base64,\n removePadding,\n makeUrlSafe,\n prepend('k8s-aws-v1.'),\n ])(signedRequest);\n }\n\n async decorateClusterDetailsWithAuth(\n clusterDetails: AWSClusterDetails,\n ): Promise<AWSClusterDetails> {\n const clusterDetailsWithAuthToken: AWSClusterDetails = Object.assign(\n {},\n clusterDetails,\n );\n\n clusterDetailsWithAuthToken.serviceAccountToken = await this.getBearerToken(\n clusterDetails.name,\n clusterDetails.assumeRole,\n clusterDetails.externalId,\n );\n return clusterDetailsWithAuthToken;\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { KubernetesAuthTranslator } from './types';\nimport { GoogleKubernetesAuthTranslator } from './GoogleKubernetesAuthTranslator';\nimport { ServiceAccountKubernetesAuthTranslator } from './ServiceAccountKubernetesAuthTranslator';\nimport { AwsIamKubernetesAuthTranslator } from './AwsIamKubernetesAuthTranslator';\n\nexport class KubernetesAuthTranslatorGenerator {\n static getKubernetesAuthTranslatorInstance(\n authProvider: string,\n ): KubernetesAuthTranslator {\n switch (authProvider) {\n case 'google': {\n return new GoogleKubernetesAuthTranslator();\n }\n case 'aws': {\n return new AwsIamKubernetesAuthTranslator();\n }\n case 'serviceAccount': {\n return new ServiceAccountKubernetesAuthTranslator();\n }\n default: {\n throw new Error(\n `authProvider \"${authProvider}\" has no KubernetesAuthTranslator associated with it`,\n );\n }\n }\n }\n}\n","/*\n * Copyright 2021 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Logger } from 'winston';\nimport {\n ClusterDetails,\n CustomResource,\n KubernetesFetcher,\n KubernetesObjectsProviderOptions,\n KubernetesServiceLocator,\n ObjectsByEntityRequest,\n ObjectToFetch,\n} from '../types/types';\nimport { KubernetesAuthTranslator } from '../kubernetes-auth-translator/types';\nimport { KubernetesAuthTranslatorGenerator } from '../kubernetes-auth-translator/KubernetesAuthTranslatorGenerator';\nimport {\n ClusterObjects,\n ObjectsByEntityResponse,\n} from '@backstage/plugin-kubernetes-common';\n\nexport const DEFAULT_OBJECTS: ObjectToFetch[] = [\n {\n group: '',\n apiVersion: 'v1',\n plural: 'pods',\n objectType: 'pods',\n },\n {\n group: '',\n apiVersion: 'v1',\n plural: 'services',\n objectType: 'services',\n },\n {\n group: '',\n apiVersion: 'v1',\n plural: 'configmaps',\n objectType: 'configmaps',\n },\n {\n group: 'apps',\n apiVersion: 'v1',\n plural: 'deployments',\n objectType: 'deployments',\n },\n {\n group: 'apps',\n apiVersion: 'v1',\n plural: 'replicasets',\n objectType: 'replicasets',\n },\n {\n group: 'autoscaling',\n apiVersion: 'v1',\n plural: 'horizontalpodautoscalers',\n objectType: 'horizontalpodautoscalers',\n },\n {\n group: 'batch',\n apiVersion: 'v1',\n plural: 'jobs',\n objectType: 'jobs',\n },\n {\n group: 'batch',\n apiVersion: 'v1',\n plural: 'cronjobs',\n objectType: 'cronjobs',\n },\n {\n group: 'networking.k8s.io',\n apiVersion: 'v1',\n plural: 'ingresses',\n objectType: 'ingresses',\n },\n];\n\nexport interface KubernetesFanOutHandlerOptions\n extends KubernetesObjectsProviderOptions {}\n\nexport interface KubernetesRequestBody extends ObjectsByEntityRequest {}\n\nexport class KubernetesFanOutHandler {\n private readonly logger: Logger;\n private readonly fetcher: KubernetesFetcher;\n private readonly serviceLocator: KubernetesServiceLocator;\n private readonly customResources: CustomResource[];\n private readonly objectTypesToFetch: Set<ObjectToFetch>;\n\n constructor({\n logger,\n fetcher,\n serviceLocator,\n customResources,\n objectTypesToFetch = DEFAULT_OBJECTS,\n }: KubernetesFanOutHandlerOptions) {\n this.logger = logger;\n this.fetcher = fetcher;\n this.serviceLocator = serviceLocator;\n this.customResources = customResources;\n this.objectTypesToFetch = new Set(objectTypesToFetch);\n }\n\n async getKubernetesObjectsByEntity(\n requestBody: KubernetesRequestBody,\n ): Promise<ObjectsByEntityResponse> {\n const entityName =\n requestBody.entity?.metadata?.annotations?.[\n 'backstage.io/kubernetes-id'\n ] || requestBody.entity?.metadata?.name;\n\n const clusterDetails: ClusterDetails[] =\n await this.serviceLocator.getClustersByServiceId(entityName);\n\n // Execute all of these async actions simultaneously/without blocking sequentially as no common object is modified by them\n const promises: Promise<ClusterDetails>[] = clusterDetails.map(cd => {\n const kubernetesAuthTranslator: KubernetesAuthTranslator =\n KubernetesAuthTranslatorGenerator.getKubernetesAuthTranslatorInstance(\n cd.authProvider,\n );\n return kubernetesAuthTranslator.decorateClusterDetailsWithAuth(\n cd,\n requestBody,\n );\n });\n const clusterDetailsDecoratedForAuth: ClusterDetails[] = await Promise.all(\n promises,\n );\n\n this.logger.info(\n `entity.metadata.name=${entityName} clusterDetails=[${clusterDetailsDecoratedForAuth\n .map(c => c.name)\n .join(', ')}]`,\n );\n\n const labelSelector: string =\n requestBody.entity?.metadata?.annotations?.[\n 'backstage.io/kubernetes-label-selector'\n ] || `backstage.io/kubernetes-id=${entityName}`;\n\n return Promise.all(\n clusterDetailsDecoratedForAuth.map(clusterDetailsItem => {\n return this.fetcher\n .fetchObjectsForService({\n serviceId: entityName,\n clusterDetails: clusterDetailsItem,\n objectTypesToFetch: this.objectTypesToFetch,\n labelSelector,\n customResources: this.customResources,\n })\n .then(result => {\n const objects: ClusterObjects = {\n cluster: {\n name: clusterDetailsItem.name,\n },\n resources: result.responses,\n errors: result.errors,\n };\n if (clusterDetailsItem.dashboardUrl) {\n objects.cluster.dashboardUrl = clusterDetailsItem.dashboardUrl;\n }\n if (clusterDetailsItem.dashboardApp) {\n objects.cluster.dashboardApp = clusterDetailsItem.dashboardApp;\n }\n return objects;\n });\n }),\n ).then(r => ({\n items: r.filter(\n item =>\n (item.errors !== undefined && item.errors.length >= 1) ||\n (item.resources !== undefined &&\n item.resources.length >= 1 &&\n item.resources.some(fr => fr.resources.length >= 1)),\n ),\n }));\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n AppsV1Api,\n AutoscalingV1Api,\n BatchV1beta1Api,\n CoreV1Api,\n NetworkingV1beta1Api,\n} from '@kubernetes/client-node';\nimport lodash, { Dictionary } from 'lodash';\nimport { Logger } from 'winston';\nimport {\n ClusterDetails,\n FetchResponseWrapper,\n KubernetesFetcher,\n KubernetesObjectTypes,\n ObjectFetchParams,\n ObjectToFetch,\n} from '../types/types';\nimport {\n FetchResponse,\n KubernetesFetchError,\n KubernetesErrorTypes,\n} from '@backstage/plugin-kubernetes-common';\nimport { KubernetesClientProvider } from './KubernetesClientProvider';\n\nexport interface Clients {\n core: CoreV1Api;\n apps: AppsV1Api;\n autoscaling: AutoscalingV1Api;\n batch: BatchV1beta1Api;\n networkingBeta1: NetworkingV1beta1Api;\n}\n\nexport interface KubernetesClientBasedFetcherOptions {\n kubernetesClientProvider: KubernetesClientProvider;\n logger: Logger;\n}\n\ntype FetchResult = FetchResponse | KubernetesFetchError;\n\nconst isError = (fr: FetchResult): fr is KubernetesFetchError =>\n fr.hasOwnProperty('errorType');\n\nfunction fetchResultsToResponseWrapper(\n results: FetchResult[],\n): FetchResponseWrapper {\n const groupBy: Dictionary<FetchResult[]> = lodash.groupBy(results, value => {\n return isError(value) ? 'errors' : 'responses';\n });\n\n return {\n errors: groupBy.errors ?? [],\n responses: groupBy.responses ?? [],\n } as FetchResponseWrapper; // TODO would be nice to get rid of this 'as'\n}\n\nconst statusCodeToErrorType = (statusCode: number): KubernetesErrorTypes => {\n switch (statusCode) {\n case 400:\n return 'BAD_REQUEST';\n case 401:\n return 'UNAUTHORIZED_ERROR';\n case 500:\n return 'SYSTEM_ERROR';\n default:\n return 'UNKNOWN_ERROR';\n }\n};\n\nexport class KubernetesClientBasedFetcher implements KubernetesFetcher {\n private readonly kubernetesClientProvider: KubernetesClientProvider;\n private readonly logger: Logger;\n\n constructor({\n kubernetesClientProvider,\n logger,\n }: KubernetesClientBasedFetcherOptions) {\n this.kubernetesClientProvider = kubernetesClientProvider;\n this.logger = logger;\n }\n\n fetchObjectsForService(\n params: ObjectFetchParams,\n ): Promise<FetchResponseWrapper> {\n const fetchResults = Array.from(params.objectTypesToFetch)\n .concat(params.customResources)\n .map(toFetch => {\n return this.fetchResource(\n params.clusterDetails,\n toFetch,\n params.labelSelector ||\n `backstage.io/kubernetes-id=${params.serviceId}`,\n toFetch.objectType,\n ).catch(this.captureKubernetesErrorsRethrowOthers.bind(this));\n });\n\n return Promise.all(fetchResults).then(fetchResultsToResponseWrapper);\n }\n\n private captureKubernetesErrorsRethrowOthers(e: any): KubernetesFetchError {\n if (e.response && e.response.statusCode) {\n this.logger.info(\n `statusCode=${e.response.statusCode} for resource ${e.response.request.uri.pathname}`,\n );\n return {\n errorType: statusCodeToErrorType(e.response.statusCode),\n statusCode: e.response.statusCode,\n resourcePath: e.response.request.uri.pathname,\n };\n }\n throw e;\n }\n\n private fetchResource(\n clusterDetails: ClusterDetails,\n resource: ObjectToFetch,\n labelSelector: string,\n objectType: KubernetesObjectTypes,\n ): Promise<FetchResponse> {\n const customObjects =\n this.kubernetesClientProvider.getCustomObjectsClient(clusterDetails);\n\n customObjects.addInterceptor((requestOptions: any) => {\n requestOptions.uri = requestOptions.uri.replace('/apis//v1/', '/api/v1/');\n });\n\n return customObjects\n .listClusterCustomObject(\n resource.group,\n resource.apiVersion,\n resource.plural,\n '',\n '',\n '',\n labelSelector,\n )\n .then(r => {\n return {\n type: objectType,\n resources: (r.body as any).items,\n };\n });\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\nimport { Config } from '@backstage/config';\nimport express from 'express';\nimport Router from 'express-promise-router';\nimport { Logger } from 'winston';\nimport { getCombinedClusterDetails } from '../cluster-locator';\nimport { MultiTenantServiceLocator } from '../service-locator/MultiTenantServiceLocator';\nimport {\n ClusterDetails,\n KubernetesObjectTypes,\n ServiceLocatorMethod,\n CustomResource,\n KubernetesObjectsProvider,\n ObjectsByEntityRequest,\n KubernetesClustersSupplier,\n KubernetesFetcher,\n KubernetesServiceLocator,\n KubernetesObjectsProviderOptions,\n} from '../types/types';\nimport { KubernetesClientProvider } from './KubernetesClientProvider';\nimport {\n DEFAULT_OBJECTS,\n KubernetesFanOutHandler,\n} from './KubernetesFanOutHandler';\nimport { KubernetesClientBasedFetcher } from './KubernetesFetcher';\n\nexport interface KubernetesEnvironment {\n logger: Logger;\n config: Config;\n}\n\nexport class KubernetesBuilder {\n private clusterSupplier?: KubernetesClustersSupplier;\n private objectsProvider?: KubernetesObjectsProvider;\n private fetcher?: KubernetesFetcher;\n private serviceLocator?: KubernetesServiceLocator;\n\n static createBuilder(env: KubernetesEnvironment) {\n return new KubernetesBuilder(env);\n }\n\n constructor(protected readonly env: KubernetesEnvironment) {}\n\n public async build() {\n const logger = this.env.logger;\n\n logger.info('Initializing Kubernetes backend');\n\n const customResources = this.buildCustomResources();\n\n const fetcher = this.fetcher ?? this.buildFetcher();\n\n const clusterSupplier = this.clusterSupplier ?? this.buildClusterSupplier();\n\n const clusterDetails = await this.fetchClusterDetails(clusterSupplier);\n\n const serviceLocator =\n this.serviceLocator ??\n this.buildServiceLocator(this.getServiceLocatorMethod(), clusterDetails);\n\n const objectsProvider =\n this.objectsProvider ??\n this.buildObjectsProvider({\n logger,\n fetcher,\n serviceLocator,\n customResources,\n objectTypesToFetch: this.getObjectTypesToFetch(),\n });\n\n const router = this.buildRouter(objectsProvider, clusterDetails);\n\n return {\n clusterDetails,\n clusterSupplier,\n customResources,\n fetcher,\n objectsProvider,\n router,\n serviceLocator,\n };\n }\n\n public setClusterSupplier(clusterSupplier?: KubernetesClustersSupplier) {\n this.clusterSupplier = clusterSupplier;\n return this;\n }\n\n public setObjectsProvider(objectsProvider?: KubernetesObjectsProvider) {\n this.objectsProvider = objectsProvider;\n return this;\n }\n\n public setFetcher(fetcher?: KubernetesFetcher) {\n this.fetcher = fetcher;\n return this;\n }\n\n public setServiceLocator(serviceLocator?: KubernetesServiceLocator) {\n this.serviceLocator = serviceLocator;\n return this;\n }\n\n protected buildCustomResources() {\n const customResources: CustomResource[] = (\n this.env.config.getOptionalConfigArray('kubernetes.customResources') ?? []\n ).map(\n c =>\n ({\n group: c.getString('group'),\n apiVersion: c.getString('apiVersion'),\n plural: c.getString('plural'),\n objectType: 'customresources',\n } as CustomResource),\n );\n\n this.env.logger.info(\n `action=LoadingCustomResources numOfCustomResources=${customResources.length}`,\n );\n return customResources;\n }\n\n protected buildClusterSupplier(): KubernetesClustersSupplier {\n const config = this.env.config;\n return {\n getClusters() {\n return getCombinedClusterDetails(config);\n },\n };\n }\n\n protected buildObjectsProvider(\n options: KubernetesObjectsProviderOptions,\n ): KubernetesObjectsProvider {\n return new KubernetesFanOutHandler(options);\n }\n\n protected buildFetcher(): KubernetesFetcher {\n return new KubernetesClientBasedFetcher({\n kubernetesClientProvider: new KubernetesClientProvider(),\n logger: this.env.logger,\n });\n }\n\n protected buildServiceLocator(\n method: ServiceLocatorMethod,\n clusterDetails: ClusterDetails[],\n ): KubernetesServiceLocator {\n switch (method) {\n case 'multiTenant':\n return this.buildMultiTenantServiceLocator(clusterDetails);\n case 'http':\n return this.buildHttpServiceLocator(clusterDetails);\n default:\n throw new Error(\n `Unsupported kubernetes.clusterLocatorMethod \"${method}\"`,\n );\n }\n }\n\n protected buildMultiTenantServiceLocator(\n clusterDetails: ClusterDetails[],\n ): KubernetesServiceLocator {\n return new MultiTenantServiceLocator(clusterDetails);\n }\n\n protected buildHttpServiceLocator(\n _clusterDetails: ClusterDetails[],\n ): KubernetesServiceLocator {\n throw new Error('not implemented');\n }\n\n protected buildRouter(\n objectsProvider: KubernetesObjectsProvider,\n clusterDetails: ClusterDetails[],\n ): express.Router {\n const logger = this.env.logger;\n const router = Router();\n router.use(express.json());\n\n router.post('/services/:serviceId', async (req, res) => {\n const serviceId = req.params.serviceId;\n const requestBody: ObjectsByEntityRequest = req.body;\n try {\n const response = await objectsProvider.getKubernetesObjectsByEntity(\n requestBody,\n );\n res.json(response);\n } catch (e) {\n logger.error(\n `action=retrieveObjectsByServiceId service=${serviceId}, error=${e}`,\n );\n res.status(500).json({ error: e.message });\n }\n });\n\n router.get('/clusters', async (_, res) => {\n res.json({\n items: clusterDetails.map(cd => ({\n name: cd.name,\n dashboardUrl: cd.dashboardUrl,\n authProvider: cd.authProvider,\n })),\n });\n });\n return router;\n }\n\n protected async fetchClusterDetails(\n clusterSupplier: KubernetesClustersSupplier,\n ) {\n const clusterDetails = await clusterSupplier.getClusters();\n\n this.env.logger.info(\n `action=loadClusterDetails numOfClustersLoaded=${clusterDetails.length}`,\n );\n\n return clusterDetails;\n }\n\n protected getServiceLocatorMethod() {\n return this.env.config.getString(\n 'kubernetes.serviceLocatorMethod.type',\n ) as ServiceLocatorMethod;\n }\n\n protected getObjectTypesToFetch() {\n const objectTypesToFetchStrings = this.env.config.getOptionalStringArray(\n 'kubernetes.objectTypes',\n ) as KubernetesObjectTypes[];\n\n const apiVersionOverrides = this.env.config.getOptionalConfig(\n 'kubernetes.apiVersionOverrides',\n );\n\n let objectTypesToFetch;\n\n if (objectTypesToFetchStrings) {\n objectTypesToFetch = DEFAULT_OBJECTS.filter(obj =>\n objectTypesToFetchStrings.includes(obj.objectType),\n );\n }\n\n if (apiVersionOverrides) {\n objectTypesToFetch = objectTypesToFetch ?? DEFAULT_OBJECTS;\n\n for (const obj of objectTypesToFetch) {\n if (apiVersionOverrides.has(obj.objectType)) {\n obj.apiVersion = apiVersionOverrides.getString(obj.objectType);\n }\n }\n }\n\n return objectTypesToFetch;\n }\n}\n","/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport { Logger } from 'winston';\nimport { KubernetesClustersSupplier } from '../types/types';\nimport express from 'express';\nimport { KubernetesBuilder } from './KubernetesBuilder';\n\nexport interface RouterOptions {\n logger: Logger;\n config: Config;\n clusterSupplier?: KubernetesClustersSupplier;\n}\n\n/**\n * creates and configure a new router for handling the kubernetes backend APIs\n * @param options - specifies the options required by this plugin\n * @returns a new router\n * @deprecated Please use the new KubernetesBuilder instead like this\n * ```\n * import { KubernetesBuilder } from '@backstage/plugin-kubernetes-backend';\n * const { router } = await KubernetesBuilder.createBuilder({\n * logger,\n * config,\n * }).build();\n * ```\n */\nexport async function createRouter(\n options: RouterOptions,\n): Promise<express.Router> {\n const { router } = await KubernetesBuilder.createBuilder(options)\n .setClusterSupplier(options.clusterSupplier)\n .build();\n return router;\n}\n"],"names":["container","ForwardedError","KubeConfig","CoreV1Api","AppsV1Api","AutoscalingV1Api","BatchV1beta1Api","NetworkingV1beta1Api","C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AA0B,SAAS,IAAI;AAAA;AAI3C,QAAI,qBAAqB;AACvB,2BAAqB,kDAAsB;AAE3C,iBAAW,OAAO,oBAAoB;AACpC,YAAI,oBAAoB,IAAI,IAAI,aAAa;AAC3C,cAAI,aAAa,oBAAoB,UAAU,IAAI;AAAA;AAAA;AAAA;AAKzD,WAAO;AAAA;AAAA;;4BCjOT,SACyB;AACzB,QAAM,CAAE,UAAW,MAAM,kBAAkB,cAAc,SACtD,mBAAmB,QAAQ,iBAC3B;AACH,SAAO;AAAA;;;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,12 @@
|
|
|
1
1
|
import { Config } from '@backstage/config';
|
|
2
|
-
import express from 'express';
|
|
3
2
|
import { Logger } from 'winston';
|
|
4
|
-
import { KubernetesFetchError, FetchResponse, KubernetesRequestBody,
|
|
3
|
+
import { KubernetesFetchError, FetchResponse, KubernetesRequestBody, ObjectsByEntityResponse } from '@backstage/plugin-kubernetes-common';
|
|
4
|
+
import express from 'express';
|
|
5
5
|
|
|
6
|
-
interface CustomResource {
|
|
7
|
-
group: string;
|
|
8
|
-
apiVersion: string;
|
|
9
|
-
plural: string;
|
|
10
|
-
}
|
|
11
6
|
interface ObjectFetchParams {
|
|
12
7
|
serviceId: string;
|
|
13
8
|
clusterDetails: AWSClusterDetails | GKEClusterDetails | ServiceAccountClusterDetails | ClusterDetails;
|
|
14
|
-
objectTypesToFetch: Set<
|
|
9
|
+
objectTypesToFetch: Set<ObjectToFetch>;
|
|
15
10
|
labelSelector: string;
|
|
16
11
|
customResources: CustomResource[];
|
|
17
12
|
}
|
|
@@ -22,7 +17,16 @@ interface FetchResponseWrapper {
|
|
|
22
17
|
errors: KubernetesFetchError[];
|
|
23
18
|
responses: FetchResponse[];
|
|
24
19
|
}
|
|
25
|
-
|
|
20
|
+
interface ObjectToFetch {
|
|
21
|
+
objectType: KubernetesObjectTypes;
|
|
22
|
+
group: string;
|
|
23
|
+
apiVersion: string;
|
|
24
|
+
plural: string;
|
|
25
|
+
}
|
|
26
|
+
interface CustomResource extends ObjectToFetch {
|
|
27
|
+
objectType: 'customresources';
|
|
28
|
+
}
|
|
29
|
+
declare type KubernetesObjectTypes = 'pods' | 'services' | 'configmaps' | 'deployments' | 'replicasets' | 'horizontalpodautoscalers' | 'jobs' | 'cronjobs' | 'ingresses' | 'customresources';
|
|
26
30
|
interface KubernetesClustersSupplier {
|
|
27
31
|
getClusters(): Promise<ClusterDetails[]>;
|
|
28
32
|
}
|
|
@@ -39,6 +43,7 @@ interface ClusterDetails {
|
|
|
39
43
|
authProvider: string;
|
|
40
44
|
serviceAccountToken?: string | undefined;
|
|
41
45
|
skipTLSVerify?: boolean;
|
|
46
|
+
caData?: string | undefined;
|
|
42
47
|
/**
|
|
43
48
|
* Specifies the link to the Kubernetes dashboard managing this cluster.
|
|
44
49
|
* @remarks
|
|
@@ -73,25 +78,16 @@ interface AWSClusterDetails extends ClusterDetails {
|
|
|
73
78
|
assumeRole?: string;
|
|
74
79
|
externalId?: string;
|
|
75
80
|
}
|
|
76
|
-
|
|
77
|
-
declare const DEFAULT_OBJECTS: KubernetesObjectTypes[];
|
|
78
|
-
interface KubernetesFanOutHandlerOptions {
|
|
81
|
+
interface KubernetesObjectsProviderOptions {
|
|
79
82
|
logger: Logger;
|
|
80
83
|
fetcher: KubernetesFetcher;
|
|
81
84
|
serviceLocator: KubernetesServiceLocator;
|
|
82
85
|
customResources: CustomResource[];
|
|
83
|
-
objectTypesToFetch?:
|
|
84
|
-
}
|
|
85
|
-
declare
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
private readonly serviceLocator;
|
|
89
|
-
private readonly customResources;
|
|
90
|
-
private readonly objectTypesToFetch;
|
|
91
|
-
constructor({ logger, fetcher, serviceLocator, customResources, objectTypesToFetch, }: KubernetesFanOutHandlerOptions);
|
|
92
|
-
getKubernetesObjectsByEntity(requestBody: KubernetesRequestBody): Promise<{
|
|
93
|
-
items: ClusterObjects[];
|
|
94
|
-
}>;
|
|
86
|
+
objectTypesToFetch?: ObjectToFetch[];
|
|
87
|
+
}
|
|
88
|
+
declare type ObjectsByEntityRequest = KubernetesRequestBody;
|
|
89
|
+
interface KubernetesObjectsProvider {
|
|
90
|
+
getKubernetesObjectsByEntity(request: ObjectsByEntityRequest): Promise<ObjectsByEntityResponse>;
|
|
95
91
|
}
|
|
96
92
|
|
|
97
93
|
interface RouterOptions {
|
|
@@ -99,7 +95,59 @@ interface RouterOptions {
|
|
|
99
95
|
config: Config;
|
|
100
96
|
clusterSupplier?: KubernetesClustersSupplier;
|
|
101
97
|
}
|
|
102
|
-
|
|
98
|
+
/**
|
|
99
|
+
* creates and configure a new router for handling the kubernetes backend APIs
|
|
100
|
+
* @param options - specifies the options required by this plugin
|
|
101
|
+
* @returns a new router
|
|
102
|
+
* @deprecated Please use the new KubernetesBuilder instead like this
|
|
103
|
+
* ```
|
|
104
|
+
* import { KubernetesBuilder } from '@backstage/plugin-kubernetes-backend';
|
|
105
|
+
* const { router } = await KubernetesBuilder.createBuilder({
|
|
106
|
+
* logger,
|
|
107
|
+
* config,
|
|
108
|
+
* }).build();
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
103
111
|
declare function createRouter(options: RouterOptions): Promise<express.Router>;
|
|
104
112
|
|
|
105
|
-
|
|
113
|
+
interface KubernetesEnvironment {
|
|
114
|
+
logger: Logger;
|
|
115
|
+
config: Config;
|
|
116
|
+
}
|
|
117
|
+
declare class KubernetesBuilder {
|
|
118
|
+
protected readonly env: KubernetesEnvironment;
|
|
119
|
+
private clusterSupplier?;
|
|
120
|
+
private objectsProvider?;
|
|
121
|
+
private fetcher?;
|
|
122
|
+
private serviceLocator?;
|
|
123
|
+
static createBuilder(env: KubernetesEnvironment): KubernetesBuilder;
|
|
124
|
+
constructor(env: KubernetesEnvironment);
|
|
125
|
+
build(): Promise<{
|
|
126
|
+
clusterDetails: ClusterDetails[];
|
|
127
|
+
clusterSupplier: KubernetesClustersSupplier;
|
|
128
|
+
customResources: CustomResource[];
|
|
129
|
+
fetcher: KubernetesFetcher;
|
|
130
|
+
objectsProvider: KubernetesObjectsProvider;
|
|
131
|
+
router: express.Router;
|
|
132
|
+
serviceLocator: KubernetesServiceLocator;
|
|
133
|
+
}>;
|
|
134
|
+
setClusterSupplier(clusterSupplier?: KubernetesClustersSupplier): this;
|
|
135
|
+
setObjectsProvider(objectsProvider?: KubernetesObjectsProvider): this;
|
|
136
|
+
setFetcher(fetcher?: KubernetesFetcher): this;
|
|
137
|
+
setServiceLocator(serviceLocator?: KubernetesServiceLocator): this;
|
|
138
|
+
protected buildCustomResources(): CustomResource[];
|
|
139
|
+
protected buildClusterSupplier(): KubernetesClustersSupplier;
|
|
140
|
+
protected buildObjectsProvider(options: KubernetesObjectsProviderOptions): KubernetesObjectsProvider;
|
|
141
|
+
protected buildFetcher(): KubernetesFetcher;
|
|
142
|
+
protected buildServiceLocator(method: ServiceLocatorMethod, clusterDetails: ClusterDetails[]): KubernetesServiceLocator;
|
|
143
|
+
protected buildMultiTenantServiceLocator(clusterDetails: ClusterDetails[]): KubernetesServiceLocator;
|
|
144
|
+
protected buildHttpServiceLocator(_clusterDetails: ClusterDetails[]): KubernetesServiceLocator;
|
|
145
|
+
protected buildRouter(objectsProvider: KubernetesObjectsProvider, clusterDetails: ClusterDetails[]): express.Router;
|
|
146
|
+
protected fetchClusterDetails(clusterSupplier: KubernetesClustersSupplier): Promise<ClusterDetails[]>;
|
|
147
|
+
protected getServiceLocatorMethod(): ServiceLocatorMethod;
|
|
148
|
+
protected getObjectTypesToFetch(): ObjectToFetch[] | undefined;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
declare const DEFAULT_OBJECTS: ObjectToFetch[];
|
|
152
|
+
|
|
153
|
+
export { AWSClusterDetails, ClusterDetails, CustomResource, DEFAULT_OBJECTS, FetchResponseWrapper, GKEClusterDetails, KubernetesBuilder, KubernetesClustersSupplier, KubernetesEnvironment, KubernetesFetcher, KubernetesObjectTypes, KubernetesObjectsProvider, KubernetesObjectsProviderOptions, KubernetesServiceLocator, ObjectFetchParams, ObjectToFetch, ObjectsByEntityRequest, RouterOptions, ServiceAccountClusterDetails, ServiceLocatorMethod, createRouter };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backstage/plugin-kubernetes-backend",
|
|
3
3
|
"description": "A Backstage backend plugin that integrates towards Kubernetes",
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.20",
|
|
5
5
|
"main": "dist/index.cjs.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"license": "Apache-2.0",
|
|
@@ -32,10 +32,11 @@
|
|
|
32
32
|
"clean": "backstage-cli clean"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@backstage/backend-common": "^0.9.
|
|
36
|
-
"@backstage/catalog-model": "^0.9.
|
|
35
|
+
"@backstage/backend-common": "^0.9.12",
|
|
36
|
+
"@backstage/catalog-model": "^0.9.7",
|
|
37
37
|
"@backstage/config": "^0.1.10",
|
|
38
|
-
"@backstage/
|
|
38
|
+
"@backstage/errors": "^0.1.5",
|
|
39
|
+
"@backstage/plugin-kubernetes-common": "^0.1.7",
|
|
39
40
|
"@google-cloud/container": "^2.2.0",
|
|
40
41
|
"@kubernetes/client-node": "^0.15.0",
|
|
41
42
|
"@types/express": "^4.17.6",
|
|
@@ -54,7 +55,7 @@
|
|
|
54
55
|
"yn": "^4.0.0"
|
|
55
56
|
},
|
|
56
57
|
"devDependencies": {
|
|
57
|
-
"@backstage/cli": "^0.
|
|
58
|
+
"@backstage/cli": "^0.10.0",
|
|
58
59
|
"@types/aws4": "^1.5.1",
|
|
59
60
|
"aws-sdk-mock": "^5.2.1",
|
|
60
61
|
"bdd-lazy-var": "^2.6.0",
|
|
@@ -64,5 +65,5 @@
|
|
|
64
65
|
"dist",
|
|
65
66
|
"schema.d.ts"
|
|
66
67
|
],
|
|
67
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "a05e7081b805006e3f0b2960a08a7753357f532f"
|
|
68
69
|
}
|
package/schema.d.ts
CHANGED
|
@@ -23,6 +23,8 @@ export interface Config {
|
|
|
23
23
|
| 'deployments'
|
|
24
24
|
| 'replicasets'
|
|
25
25
|
| 'horizontalpodautoscalers'
|
|
26
|
+
| 'jobs'
|
|
27
|
+
| 'cronjobs'
|
|
26
28
|
| 'ingresses'
|
|
27
29
|
>;
|
|
28
30
|
serviceLocatorMethod: {
|
|
@@ -61,5 +63,23 @@ export interface Config {
|
|
|
61
63
|
apiVersion: string;
|
|
62
64
|
plural: string;
|
|
63
65
|
}>;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* (Optional) API Version Overrides
|
|
69
|
+
* If set, the specified api version will be used to make requests for the corresponding object.
|
|
70
|
+
* If running a legacy Kubernetes version, you may use this to override the default api versions
|
|
71
|
+
* that are not supported in your cluster.
|
|
72
|
+
*/
|
|
73
|
+
apiVersionOverrides?: {
|
|
74
|
+
pods?: string;
|
|
75
|
+
services?: string;
|
|
76
|
+
configmaps?: string;
|
|
77
|
+
deployments?: string;
|
|
78
|
+
replicasets?: string;
|
|
79
|
+
horizontalpodautoscalers?: string;
|
|
80
|
+
cronjobs?: string;
|
|
81
|
+
jobs?: string;
|
|
82
|
+
ingresses?: string;
|
|
83
|
+
} & { [pluralKind: string]: string };
|
|
64
84
|
};
|
|
65
85
|
}
|