Merge pull request #19348 from Sabrina0614/szlo/aws-eks-cluster-call-back

feat(catalog-backend-module-aws): AwsEksClusterProcessor adds ability to pass in entity and pass in region to EKS client callback function
This commit is contained in:
Elon Jefferson
2023-10-16 16:22:39 -04:00
committed by GitHub
9 changed files with 179 additions and 39 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-catalog-backend-module-aws': minor
---
AwsEksClusterProcessor supports Entity callback function and passes in region when initialize EKS cluster
@@ -8,7 +8,9 @@ import { AwsCredentialsManager } from '@backstage/integration-aws-node';
import { CatalogProcessor } from '@backstage/plugin-catalog-node';
import { CatalogProcessorEmit } from '@backstage/plugin-catalog-node';
import { CatalogProcessorParser } from '@backstage/plugin-catalog-node';
import type { Cluster } from '@aws-sdk/client-eks';
import { Config } from '@backstage/config';
import type { Entity } from '@backstage/catalog-model';
import { EntityProvider } from '@backstage/plugin-catalog-node';
import { EntityProviderConnection } from '@backstage/plugin-catalog-node';
import { LocationSpec } from '@backstage/plugin-catalog-common';
@@ -17,6 +19,12 @@ import { PluginTaskScheduler } from '@backstage/backend-tasks';
import { TaskRunner } from '@backstage/backend-tasks';
import { UrlReader } from '@backstage/backend-common';
// @public
export const ANNOTATION_AWS_ACCOUNT_ID: string;
// @public
export const ANNOTATION_AWS_ARN: string;
// @public
export type AWSCredentialFactory = (
awsAccountId: string,
@@ -27,14 +35,18 @@ export class AwsEKSClusterProcessor implements CatalogProcessor {
constructor(options: {
credentialsFactory?: AWSCredentialFactory;
credentialsManager?: AwsCredentialsManager;
clusterEntityTransformer?: EksClusterEntityTransformer;
});
// (undocumented)
static fromConfig(configRoot: Config): AwsEKSClusterProcessor;
static fromConfig(
configRoot: Config,
options?: {
clusterEntityTransformer?: EksClusterEntityTransformer;
},
): AwsEKSClusterProcessor;
// (undocumented)
getProcessorName(): string;
// (undocumented)
normalizeName(name: string): string;
// (undocumented)
readLocation(
location: LocationSpec,
_optional: boolean,
@@ -93,4 +105,10 @@ export class AwsS3EntityProvider implements EntityProvider {
// (undocumented)
refresh(logger: Logger): Promise<void>;
}
// @public
export type EksClusterEntityTransformer = (
cluster: Cluster,
accountId: string,
) => Promise<Entity>;
```
@@ -0,0 +1,28 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Annotation for specifying AWS account id
*
* @public
*/
export const ANNOTATION_AWS_ACCOUNT_ID: string = 'amazonaws.com/account-id';
/**
* Annotation for specifying AWS arn
*
* @public
*/
export const ANNOTATION_AWS_ARN: string = 'amazonaws.com/arn';
@@ -23,3 +23,4 @@
export * from './processors';
export * from './providers';
export * from './types';
export * from './constants';
@@ -0,0 +1,59 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { type Cluster } from '@aws-sdk/client-eks';
import {
ANNOTATION_KUBERNETES_API_SERVER,
ANNOTATION_KUBERNETES_API_SERVER_CA,
ANNOTATION_KUBERNETES_AUTH_PROVIDER,
} from '@backstage/plugin-kubernetes-common';
import type { EksClusterEntityTransformer } from '../processors/types';
import { ANNOTATION_AWS_ACCOUNT_ID, ANNOTATION_AWS_ARN } from '../constants';
/**
* Default transformer for EKS Cluster to Resource Entity
* @public
*/
export const defaultEksClusterEntityTransformer: EksClusterEntityTransformer =
async (cluster: Cluster, accountId: string) => {
const { arn, endpoint, certificateAuthority, name } = cluster;
return {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Resource',
metadata: {
annotations: {
[ANNOTATION_AWS_ACCOUNT_ID]: accountId,
[ANNOTATION_AWS_ARN]: arn || '',
[ANNOTATION_KUBERNETES_API_SERVER]: endpoint || '',
[ANNOTATION_KUBERNETES_API_SERVER_CA]:
certificateAuthority?.data || '',
[ANNOTATION_KUBERNETES_AUTH_PROVIDER]: 'aws',
},
name: normalizeName(name as string),
namespace: 'default',
},
spec: {
type: 'kubernetes-cluster',
owner: 'unknown',
},
};
};
function normalizeName(name: string): string {
return name
.trim()
.toLocaleLowerCase('en-US')
.replace(/[^a-zA-Z0-9\-]/g, '-');
}
@@ -0,0 +1,16 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export * from './defaultTransformers';
@@ -19,11 +19,6 @@ import {
CatalogProcessorEmit,
} from '@backstage/plugin-catalog-node';
import { LocationSpec } from '@backstage/plugin-catalog-common';
import {
ANNOTATION_KUBERNETES_API_SERVER,
ANNOTATION_KUBERNETES_API_SERVER_CA,
ANNOTATION_KUBERNETES_AUTH_PROVIDER,
} from '@backstage/plugin-kubernetes-common';
import { EKS } from '@aws-sdk/client-eks';
import { AWSCredentialFactory } from '../types';
import { AwsCredentialIdentity, Provider } from '@aws-sdk/types';
@@ -33,8 +28,8 @@ import {
} from '@backstage/integration-aws-node';
import { Config } from '@backstage/config';
const ACCOUNTID_ANNOTATION: string = 'amazonaws.com/account-id';
const ARN_ANNOTATION: string = 'amazonaws.com/arn';
import type { EksClusterEntityTransformer } from './types';
import { defaultEksClusterEntityTransformer } from '../lib';
/**
* A processor for automatic discovery of resources from EKS clusters. Handles the
@@ -46,34 +41,39 @@ const ARN_ANNOTATION: string = 'amazonaws.com/arn';
export class AwsEKSClusterProcessor implements CatalogProcessor {
private credentialsFactory?: AWSCredentialFactory;
private credentialsManager?: AwsCredentialsManager;
private readonly clusterEntityTransformer: EksClusterEntityTransformer;
static fromConfig(configRoot: Config): AwsEKSClusterProcessor {
static fromConfig(
configRoot: Config,
options?: {
clusterEntityTransformer?: EksClusterEntityTransformer;
},
): AwsEKSClusterProcessor {
const awsCredentaislManager =
DefaultAwsCredentialsManager.fromConfig(configRoot);
return new AwsEKSClusterProcessor({
credentialsManager: awsCredentaislManager,
...options,
});
}
constructor(options: {
credentialsFactory?: AWSCredentialFactory;
credentialsManager?: AwsCredentialsManager;
clusterEntityTransformer?: EksClusterEntityTransformer;
}) {
this.credentialsFactory = options.credentialsFactory;
this.credentialsManager = options.credentialsManager;
// If the callback function is not passed in, then default to the one upstream is using
this.clusterEntityTransformer =
options.clusterEntityTransformer || defaultEksClusterEntityTransformer;
}
getProcessorName(): string {
return 'aws-eks';
}
normalizeName(name: string): string {
return name
.trim()
.toLocaleLowerCase('en-US')
.replace(/[^a-zA-Z0-9\-]/g, '-');
}
async readLocation(
location: LocationSpec,
_optional: boolean,
@@ -108,6 +108,7 @@ export class AwsEKSClusterProcessor implements CatalogProcessor {
const eksClient = new EKS({
credentials,
credentialDefaultProvider: providerFunction,
region,
});
const clusters = await eksClient.listClusters({});
if (clusters.clusters === undefined) {
@@ -119,27 +120,11 @@ export class AwsEKSClusterProcessor implements CatalogProcessor {
.map(async describedClusterPromise => {
const describedCluster = await describedClusterPromise;
if (describedCluster.cluster) {
const entity = {
apiVersion: 'backstage.io/v1alpha1',
kind: 'Resource',
metadata: {
annotations: {
[ACCOUNTID_ANNOTATION]: accountId,
[ARN_ANNOTATION]: describedCluster.cluster.arn || '',
[ANNOTATION_KUBERNETES_API_SERVER]:
describedCluster.cluster.endpoint || '',
[ANNOTATION_KUBERNETES_API_SERVER_CA]:
describedCluster.cluster.certificateAuthority?.data || '',
[ANNOTATION_KUBERNETES_AUTH_PROVIDER]: 'aws',
},
name: this.normalizeName(describedCluster.cluster.name as string),
namespace: 'default',
},
spec: {
type: 'kubernetes-cluster',
owner: 'unknown',
},
};
const entity = await this.clusterEntityTransformer(
describedCluster.cluster,
accountId,
);
emit({
type: 'entity',
entity,
@@ -17,3 +17,4 @@
export { AwsEKSClusterProcessor } from './AwsEKSClusterProcessor';
export { AwsOrganizationCloudAccountProcessor } from './AwsOrganizationCloudAccountProcessor';
export { AwsS3DiscoveryProcessor } from './AwsS3DiscoveryProcessor';
export * from './types';
@@ -0,0 +1,27 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import type { Cluster } from '@aws-sdk/client-eks';
import type { Entity } from '@backstage/catalog-model';
/**
* Options for the EKS cluster entity callback function
*
* @public
*/
export type EksClusterEntityTransformer = (
cluster: Cluster,
accountId: string,
) => Promise<Entity>;