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 <luc@spreadshirt.net>
This commit is contained in:
Luna Stadler
2022-03-29 11:22:22 +02:00
parent 3d45427666
commit 5818bead7d
@@ -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<ClusterDetails[]> {
if (this.clusterDetails) {
return this.clusterDetails;
}
this.clusterDetails = await this.retrieveClusters();
return this.clusterDetails;
}
// TODO pass caData into the object
async getClusters(): Promise<GKEClusterDetails[]> {
async retrieveClusters(): Promise<GKEClusterDetails[]> {
const {
projectId,
region,