Creating extension point for kubernetesServiceLocator

Signed-off-by: Andres Mauricio Gomez P <andmagom@outlook.com>
This commit is contained in:
Andres Mauricio Gomez P
2023-11-27 09:51:32 -05:00
parent aa8ee8559b
commit 673e08a1a3
6 changed files with 99 additions and 40 deletions
@@ -0,0 +1,20 @@
/*
* Copyright 2020 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 type {
AuthenticationStrategy,
KubernetesCredential,
} from '@backstage/plugin-kubernetes-node';
+29 -1
View File
@@ -33,6 +33,9 @@ import {
AuthenticationStrategy,
kubernetesAuthStrategyExtensionPoint,
KubernetesFetcher,
KubernetesServiceLocatorExtensionPoint,
KubernetesServiceLocator,
kubernetesServiceLocatorExtensionPoint,
} from '@backstage/plugin-kubernetes-node';
import {
KubernetesFetcherExtensionPoint,
@@ -90,6 +93,23 @@ class Fetcher implements KubernetesFetcherExtensionPoint {
}
}
class ServiceLocator implements KubernetesServiceLocatorExtensionPoint {
private serviceLocator: KubernetesServiceLocator | undefined;
getServiceLocator() {
return this.serviceLocator;
}
addServiceLocator(serviceLocator: KubernetesServiceLocator) {
if (this.serviceLocator) {
throw new Error(
'Multiple Kubernetes Service Locators is not supported at this time',
);
}
this.serviceLocator = serviceLocator;
}
}
class AuthStrategy implements KubernetesAuthStrategyExtensionPoint {
private authStrategies: Array<{
key: string;
@@ -131,6 +151,8 @@ export const kubernetesPlugin = createBackendPlugin({
const extPointClusterSuplier = new ClusterSuplier();
const extPointAuthStrategy = new AuthStrategy();
const extPointFetcher = new Fetcher();
const extPointServiceLocator = new ServiceLocator();
env.registerExtensionPoint(
kubernetesObjectsProviderExtensionPoint,
extPointObjectsProvider,
@@ -147,6 +169,10 @@ export const kubernetesPlugin = createBackendPlugin({
kubernetesFetcherExtensionPoint,
extPointFetcher,
);
env.registerExtensionPoint(
kubernetesServiceLocatorExtensionPoint,
extPointServiceLocator,
);
env.registerInit({
deps: {
@@ -167,7 +193,9 @@ export const kubernetesPlugin = createBackendPlugin({
})
.setObjectsProvider(extPointObjectsProvider.getObjectsProvider())
.setClusterSupplier(extPointClusterSuplier.getClusterSupplier())
.setFetcher(extPointFetcher.getFetcher());
.setFetcher(extPointFetcher.getFetcher())
.setServiceLocator(extPointServiceLocator.getServiceLocator());
AuthStrategy.addAuthStrategiesFromArray(
extPointAuthStrategy.getAuthenticationStrategies(),
builder,
+3 -35
View File
@@ -14,17 +14,13 @@
* limitations under the License.
*/
import { Entity } from '@backstage/catalog-model';
import { Logger } from 'winston';
import type {
CustomResourceMatcher,
KubernetesRequestBody,
} from '@backstage/plugin-kubernetes-common';
import type { KubernetesRequestBody } from '@backstage/plugin-kubernetes-common';
import { Config } from '@backstage/config';
import {
ClusterDetails,
CustomResource,
KubernetesFetcher,
KubernetesServiceLocator,
ObjectToFetch,
} from '@backstage/plugin-kubernetes-node';
@@ -50,25 +46,6 @@ export type KubernetesObjectTypes =
// If updating this list, also make sure to update
// `objectTypes` and `apiVersionOverrides` in config.d.ts!
/**
* @public
*/
export interface ServiceLocatorRequestContext {
objectTypesToFetch: Set<ObjectToFetch>;
customResources: CustomResourceMatcher[];
}
/**
* Used to locate which cluster(s) a service is running on
* @public
*/
export interface KubernetesServiceLocator {
getClustersByEntity(
entity: Entity,
requestContext: ServiceLocatorRequestContext,
): Promise<{ clusters: ClusterDetails[] }>;
}
/**
*
* @public
@@ -94,13 +71,4 @@ export interface KubernetesObjectsProviderOptions {
*/
export type ObjectsByEntityRequest = KubernetesRequestBody;
export type {
AuthMetadata,
ClusterDetails,
KubernetesClustersSupplier,
ObjectToFetch,
CustomResource,
ObjectFetchParams,
FetchResponseWrapper,
KubernetesFetcher,
} from '@backstage/plugin-kubernetes-node';
export type * from '@backstage/plugin-kubernetes-node';
+26 -4
View File
@@ -14,10 +14,13 @@
* limitations under the License.
*/
import { createExtensionPoint } from '@backstage/backend-plugin-api';
import { KubernetesFetcher } from '@backstage/plugin-kubernetes-backend';
import { AuthenticationStrategy } from '@backstage/plugin-kubernetes-node';
import { KubernetesClustersSupplier } from '@backstage/plugin-kubernetes-node';
import { KubernetesObjectsProvider } from '@backstage/plugin-kubernetes-node';
import {
AuthenticationStrategy,
KubernetesClustersSupplier,
KubernetesFetcher,
KubernetesObjectsProvider,
KubernetesServiceLocator,
} from '@backstage/plugin-kubernetes-node';
/**
* The interface for {@link kubernetesObjectsProviderExtensionPoint}.
@@ -94,3 +97,22 @@ export const kubernetesFetcherExtensionPoint =
createExtensionPoint<KubernetesFetcherExtensionPoint>({
id: 'kubernetes.fetcher',
});
/**
* The interface for {@link kubernetesServiceLocatorExtensionPoint}.
*
* @public
*/
export interface KubernetesServiceLocatorExtensionPoint {
addServiceLocator(serviceLocator: KubernetesServiceLocator): void;
}
/**
* An extension point the exposes the ability to configure a kubernetes service locator.
*
* @public
*/
export const kubernetesServiceLocatorExtensionPoint =
createExtensionPoint<KubernetesServiceLocatorExtensionPoint>({
id: 'kubernetes.service-locator',
});
@@ -212,3 +212,21 @@ export interface KubernetesFetcher {
labelSelector?: string,
): Promise<FetchResponseWrapper>;
}
/**
* @public
*/
export interface ServiceLocatorRequestContext {
objectTypesToFetch: Set<ObjectToFetch>;
customResources: CustomResourceMatcher[];
}
/**
* Used to locate which cluster(s) a service is running on
* @public
*/
export interface KubernetesServiceLocator {
getClustersByEntity(
entity: Entity,
requestContext: ServiceLocatorRequestContext,
): Promise<{ clusters: ClusterDetails[] }>;
}
+3
View File
@@ -7574,6 +7574,8 @@ __metadata:
"@backstage/plugin-catalog-node": "workspace:^"
"@backstage/plugin-kubernetes-common": "workspace:^"
"@backstage/plugin-kubernetes-node": "workspace:^"
"@backstage/plugin-permission-backend": "workspace:^"
"@backstage/plugin-permission-backend-module-allow-all-policy": "workspace:^"
"@backstage/plugin-permission-common": "workspace:^"
"@backstage/plugin-permission-node": "workspace:^"
"@backstage/types": "workspace:^"
@@ -7672,6 +7674,7 @@ __metadata:
"@backstage/backend-plugin-api": "workspace:^"
"@backstage/catalog-model": "workspace:^"
"@backstage/cli": "workspace:^"
"@backstage/plugin-kubernetes-backend": "workspace:^"
"@backstage/plugin-kubernetes-common": "workspace:^"
"@backstage/types": "workspace:^"
languageName: unknown