Make Kubernetes cluster refresh more explicit

The `refreshClusters` method is now part of the
`KubernetesClustersSupplier` interface definition and also documented.

All existing cluster suppliers now implement it as well and the examples
in the docs and in the CHANGELOG have been adjusted.

Signed-off-by: Luna Stadler <luc@spreadshirt.net>
This commit is contained in:
Luna Stadler
2022-04-04 14:26:11 +02:00
parent 38ea1f136c
commit 8d050f714d
13 changed files with 166 additions and 76 deletions
+6 -13
View File
@@ -21,12 +21,11 @@ For example, here's a simple example of this in `packages/backend/src/plugins/ku
+} from '@backstage/plugin-kubernetes-backend';
import { Router } from 'express';
import { PluginEnvironment } from '../types';
+import { Duration } from 'luxon';
+
+export class CustomClustersSupplier implements KubernetesClustersSupplier {
+ private clusterDetails: ClusterDetails[] = [];
+
+ async retrieveClusters() {
+ async refreshClusters(): Promise<void> {
+ this.clusterDetails = []; // fetch from somewhere
+ }
+
@@ -34,7 +33,7 @@ For example, here's a simple example of this in `packages/backend/src/plugins/ku
+ return this.clusterDetails;
+ }
+}
+
export default async function createPlugin(
env: PluginEnvironment,
): Promise<Router> {
@@ -46,18 +45,12 @@ For example, here's a simple example of this in `packages/backend/src/plugins/ku
+ });
+
+ const clusterSupplier = new CustomClustersSupplier();
+ env.scheduler
+ .createScheduledTaskRunner({
+ frequency: Duration.fromObject({ minutes: 60 }),
+ timeout: Duration.fromObject({ minutes: 15 }),
+ })
+ .run({
+ id: 'refresh-kubernetes-clusters',
+ fn: clusterSupplier.retrieveClusters,
+ });
+ builder.setClusterSupplier(clusterSupplier);
+
+ const { router } = await builder.build();
return router;
}
```
If you need to adjust the refresh interval from the default once per hour
you can call `builder.setClusterRefreshInterval`.