From 5818bead7d20bb59b3989ca3f9b1348e9e3fd570 Mon Sep 17 00:00:00 2001 From: Luna Stadler Date: Tue, 29 Mar 2022 11:22:22 +0200 Subject: [PATCH] Ensure GkeClusterLocator only fetches the clusters once This is now necessary because cluster suppliers are called whenever the list of clusters is required and thus should cache their clusters. Signed-off-by: Luna Stadler --- .../src/cluster-locator/GkeClusterLocator.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts index 8b70db40eb..989b69c7c8 100644 --- a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts +++ b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts @@ -17,7 +17,11 @@ import { Config } from '@backstage/config'; import { ForwardedError } from '@backstage/errors'; import * as container from '@google-cloud/container'; -import { GKEClusterDetails, KubernetesClustersSupplier } from '../types/types'; +import { + ClusterDetails, + GKEClusterDetails, + KubernetesClustersSupplier, +} from '../types/types'; type GkeClusterLocatorOptions = { projectId: string; @@ -31,6 +35,7 @@ export class GkeClusterLocator implements KubernetesClustersSupplier { constructor( private readonly options: GkeClusterLocatorOptions, private readonly client: container.v1.ClusterManagerClient, + private clusterDetails: GKEClusterDetails[] | undefined = undefined, ) {} static fromConfigWithClient( @@ -55,8 +60,17 @@ export class GkeClusterLocator implements KubernetesClustersSupplier { ); } + async getClusters(): Promise { + if (this.clusterDetails) { + return this.clusterDetails; + } + + this.clusterDetails = await this.retrieveClusters(); + return this.clusterDetails; + } + // TODO pass caData into the object - async getClusters(): Promise { + async retrieveClusters(): Promise { const { projectId, region,