Creating extension point for kubernetesClusterSupplier

Signed-off-by: Andres Mauricio Gomez P <andmagom@outlook.com>
This commit is contained in:
Andres Mauricio Gomez P
2023-11-21 10:59:31 -05:00
parent 8563ee2b8f
commit 6010564860
9 changed files with 143 additions and 83 deletions
+6
View File
@@ -0,0 +1,6 @@
---
'@backstage/plugin-kubernetes-node': minor
'@backstage/plugin-kubernetes-backend': patch
---
The `kubernetes-node` plugin has been modified to house a new extension points for Kubernetes backend plugin; `KubernetesClusterSupplierExtensionPoint` is introduced . The `kubernetes-backend` plugin was modified to use this new extension point.
+29 -3
View File
@@ -26,6 +26,9 @@ import {
KubernetesObjectsProviderExtensionPoint,
kubernetesObjectsProviderExtensionPoint,
KubernetesObjectsProvider,
KubernetesClusterSupplierExtensionPoint,
kubernetesClusterSupplierExtensionPoint,
KubernetesClustersSupplier,
} from '@backstage/plugin-kubernetes-node';
class ObjectsProvider implements KubernetesObjectsProviderExtensionPoint {
@@ -45,6 +48,23 @@ class ObjectsProvider implements KubernetesObjectsProviderExtensionPoint {
}
}
class ClusterSuplier implements KubernetesClusterSupplierExtensionPoint {
private clusterSupplier: KubernetesClustersSupplier | undefined;
getClusterSupplier() {
return this.clusterSupplier;
}
addClusterSupplier(clusterSupplier: KubernetesClustersSupplier) {
if (this.clusterSupplier) {
throw new Error(
'Multiple Kubernetes Cluster Suppliers is not supported at this time',
);
}
this.clusterSupplier = clusterSupplier;
}
}
/**
* This is the backend plugin that provides the Kubernetes integration.
* @alpha
@@ -53,10 +73,15 @@ class ObjectsProvider implements KubernetesObjectsProviderExtensionPoint {
export const kubernetesPlugin = createBackendPlugin({
pluginId: 'kubernetes',
register(env) {
const extensionPoint = new ObjectsProvider();
const extPointObjectsProvider = new ObjectsProvider();
const extPointClusterSuplier = new ClusterSuplier();
env.registerExtensionPoint(
kubernetesObjectsProviderExtensionPoint,
extensionPoint,
extPointObjectsProvider,
);
env.registerExtensionPoint(
kubernetesClusterSupplierExtensionPoint,
extPointClusterSuplier,
);
env.registerInit({
@@ -76,7 +101,8 @@ export const kubernetesPlugin = createBackendPlugin({
catalogApi,
permissions,
})
.setObjectsProvider(extensionPoint.getObjectsProvider())
.setObjectsProvider(extPointObjectsProvider.getObjectsProvider())
.setClusterSupplier(extPointClusterSuplier.getClusterSupplier())
.build();
http.use(router);
},
@@ -17,7 +17,10 @@
export * from './types';
export type {
AuthMetadata,
ClusterDetails,
CustomResourcesByEntity,
KubernetesClustersSupplier,
KubernetesObjectsByEntity,
KubernetesObjectsProvider,
} from '@backstage/plugin-kubernetes-node';
+1 -79
View File
@@ -16,7 +16,6 @@
import { Entity } from '@backstage/catalog-model';
import { Logger } from 'winston';
import type { JsonObject } from '@backstage/types';
import type {
CustomResourceMatcher,
FetchResponse,
@@ -25,6 +24,7 @@ import type {
} from '@backstage/plugin-kubernetes-common';
import { Config } from '@backstage/config';
import { KubernetesCredential } from '../auth/types';
import { ClusterDetails } from '@backstage/plugin-kubernetes-node';
/**
*
@@ -107,20 +107,6 @@ export type KubernetesObjectTypes =
// If updating this list, also make sure to update
// `objectTypes` and `apiVersionOverrides` in config.d.ts!
/**
* Used to load cluster details from different sources
* @public
*/
export interface KubernetesClustersSupplier {
/**
* Returns the cached list of clusters.
*
* Implementations _should_ cache the clusters and refresh them periodically,
* as getClusters is called whenever the list of clusters is needed.
*/
getClusters(): Promise<ClusterDetails[]>;
}
/**
* @public
*/
@@ -146,70 +132,6 @@ export interface KubernetesServiceLocator {
*/
export type ServiceLocatorMethod = 'multiTenant' | 'singleTenant' | 'http'; // TODO implement http
/**
* Provider-specific authentication configuration
* @public
*/
export type AuthMetadata = Record<string, string>;
/**
*
* @public
*/
export interface ClusterDetails {
/**
* Specifies the name of the Kubernetes cluster.
*/
name: string;
url: string;
authMetadata: AuthMetadata;
skipTLSVerify?: boolean;
/**
* Whether to skip the lookup to the metrics server to retrieve pod resource usage.
* It is not guaranteed that the Kubernetes distro has the metrics server installed.
*/
skipMetricsLookup?: boolean;
caData?: string | undefined;
caFile?: string | undefined;
/**
* Specifies the link to the Kubernetes dashboard managing this cluster.
* @remarks
* Note that you should specify the app used for the dashboard
* using the dashboardApp property, in order to properly format
* links to kubernetes resources, otherwise it will assume that you're running the standard one.
* @see dashboardApp
* @see dashboardParameters
*/
dashboardUrl?: string;
/**
* Specifies the app that provides the Kubernetes dashboard.
* This will be used for formatting links to kubernetes objects inside the dashboard.
* @remarks
* The existing apps are: standard, rancher, openshift, gke, aks, eks
* Note that it will default to the regular dashboard provided by the Kubernetes project (standard).
* Note that you can add your own formatter by registering it to the clusterLinksFormatters dictionary.
* @defaultValue standard
* @see dashboardUrl
* @example
* ```ts
* import { clusterLinksFormatters } from '@backstage/plugin-kubernetes';
* clusterLinksFormatters.myDashboard = (options) => ...;
* ```
*/
dashboardApp?: string;
/**
* Specifies specific parameters used by some dashboard URL formatters.
* This is used by the GKE formatter which requires the project, region and cluster name.
* @see dashboardApp
*/
dashboardParameters?: JsonObject;
/**
* Specifies which custom resources to look for when returning an entity's
* Kubernetes resources.
*/
customResources?: CustomResourceMatcher[];
}
/**
*
* @public
+2 -1
View File
@@ -30,6 +30,7 @@
"dependencies": {
"@backstage/backend-plugin-api": "workspace:^",
"@backstage/catalog-model": "workspace:^",
"@backstage/plugin-kubernetes-common": "workspace:^"
"@backstage/plugin-kubernetes-common": "workspace:^",
"@backstage/types": "workspace:^"
}
}
+20
View File
@@ -14,6 +14,7 @@
* limitations under the License.
*/
import { createExtensionPoint } from '@backstage/backend-plugin-api';
import { KubernetesClustersSupplier } from '@backstage/plugin-kubernetes-node';
import { KubernetesObjectsProvider } from '@backstage/plugin-kubernetes-node';
/**
@@ -34,3 +35,22 @@ export const kubernetesObjectsProviderExtensionPoint =
createExtensionPoint<KubernetesObjectsProviderExtensionPoint>({
id: 'kubernetes.objects-provider',
});
/**
* The interface for {@link kubernetesClusterSupplierExtensionPoint}.
*
* @public
*/
export interface KubernetesClusterSupplierExtensionPoint {
addClusterSupplier(clusterSupplier: KubernetesClustersSupplier): void;
}
/**
* An extension point the exposes the ability to configure a cluster supplier.
*
* @public
*/
export const kubernetesClusterSupplierExtensionPoint =
createExtensionPoint<KubernetesClusterSupplierExtensionPoint>({
id: 'kubernetes.cluster-supplier',
});
+2
View File
@@ -32,6 +32,8 @@
export {
kubernetesObjectsProviderExtensionPoint,
type KubernetesObjectsProviderExtensionPoint,
kubernetesClusterSupplierExtensionPoint,
type KubernetesClusterSupplierExtensionPoint,
} from './extensions';
export * from './types';
@@ -19,6 +19,7 @@ import {
KubernetesRequestAuth,
ObjectsByEntityResponse,
} from '@backstage/plugin-kubernetes-common';
import { JsonObject } from '@backstage/types';
/**
*
@@ -51,3 +52,81 @@ export interface KubernetesObjectsByEntity {
export interface CustomResourcesByEntity extends KubernetesObjectsByEntity {
customResources: CustomResourceMatcher[];
}
/**
* Provider-specific authentication configuration
* @public
*/
export type AuthMetadata = Record<string, string>;
/**
*
* @public
*/
export interface ClusterDetails {
/**
* Specifies the name of the Kubernetes cluster.
*/
name: string;
url: string;
authMetadata: AuthMetadata;
skipTLSVerify?: boolean;
/**
* Whether to skip the lookup to the metrics server to retrieve pod resource usage.
* It is not guaranteed that the Kubernetes distro has the metrics server installed.
*/
skipMetricsLookup?: boolean;
caData?: string | undefined;
caFile?: string | undefined;
/**
* Specifies the link to the Kubernetes dashboard managing this cluster.
* @remarks
* Note that you should specify the app used for the dashboard
* using the dashboardApp property, in order to properly format
* links to kubernetes resources, otherwise it will assume that you're running the standard one.
* @see dashboardApp
* @see dashboardParameters
*/
dashboardUrl?: string;
/**
* Specifies the app that provides the Kubernetes dashboard.
* This will be used for formatting links to kubernetes objects inside the dashboard.
* @remarks
* The existing apps are: standard, rancher, openshift, gke, aks, eks
* Note that it will default to the regular dashboard provided by the Kubernetes project (standard).
* Note that you can add your own formatter by registering it to the clusterLinksFormatters dictionary.
* @defaultValue standard
* @see dashboardUrl
* @example
* ```ts
* import { clusterLinksFormatters } from '@backstage/plugin-kubernetes';
* clusterLinksFormatters.myDashboard = (options) => ...;
* ```
*/
dashboardApp?: string;
/**
* Specifies specific parameters used by some dashboard URL formatters.
* This is used by the GKE formatter which requires the project, region and cluster name.
* @see dashboardApp
*/
dashboardParameters?: JsonObject;
/**
* Specifies which custom resources to look for when returning an entity's
* Kubernetes resources.
*/
customResources?: CustomResourceMatcher[];
}
/**
* Used to load cluster details from different sources
* @public
*/
export interface KubernetesClustersSupplier {
/**
* Returns the cached list of clusters.
*
* Implementations _should_ cache the clusters and refresh them periodically,
* as getClusters is called whenever the list of clusters is needed.
*/
getClusters(): Promise<ClusterDetails[]>;
}
+1
View File
@@ -7673,6 +7673,7 @@ __metadata:
"@backstage/catalog-model": "workspace:^"
"@backstage/cli": "workspace:^"
"@backstage/plugin-kubernetes-common": "workspace:^"
"@backstage/types": "workspace:^"
languageName: unknown
linkType: soft