From 5d06333af2899e3057d2b9909704afbc06ed8d54 Mon Sep 17 00:00:00 2001 From: armandocomellas1 Date: Mon, 15 Jan 2024 19:27:09 -0600 Subject: [PATCH] Add a descriptive description at the changeset and add some unit test for three different scenarios Signed-off-by: armandocomellas1 --- .changeset/many-pugs-change.md | 2 +- .../cluster-locator/GkeClusterLocator.test.ts | 103 ++++++++++++++++++ .../src/cluster-locator/GkeClusterLocator.ts | 17 +-- 3 files changed, 109 insertions(+), 13 deletions(-) diff --git a/.changeset/many-pugs-change.md b/.changeset/many-pugs-change.md index f16efb85aa..318c571891 100644 --- a/.changeset/many-pugs-change.md +++ b/.changeset/many-pugs-change.md @@ -2,4 +2,4 @@ '@backstage/plugin-kubernetes-backend': patch --- -Change add some lines in the script GkeClusterLocator.ts, the implementation consist in adding a piece of code starts in 58 adding some config.getString() method to find out if the app-config.yaml kubernetes config includes the type of Auth as googleServiceAccount +The purpose of this patch is to add a new login method which is `googleServiceAccount` configuring the kubernetes properties in the app-config.yaml file with authProvider key diff --git a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts index f919561490..849a7839e2 100644 --- a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts +++ b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts @@ -382,5 +382,108 @@ describe('GkeClusterLocator', () => { parent: 'projects/some-project/locations/some-region', }); }); + it('return google login when no authProvider is specified', async () => { + mockedListClusters.mockReturnValueOnce([ + { + clusters: [ + { + name: 'some-cluster', + endpoint: '1.2.3.4', + }, + ], + }, + ]); + + const config: Config = new ConfigReader({ + type: 'gke', + projectId: 'some-project', + }); + + const sut = GkeClusterLocator.fromConfigWithClient(config, { + listClusters: mockedListClusters, + } as any); + + const result = await sut.getClusters(); + + expect(result).toStrictEqual([ + { + name: 'some-cluster', + url: 'https://1.2.3.4', + authMetadata: { [ANNOTATION_KUBERNETES_AUTH_PROVIDER]: 'google' }, + skipTLSVerify: false, + skipMetricsLookup: false, + }, + ]); + }); + it('return googleServiceAccount login when authProvider is specified', async () => { + mockedListClusters.mockReturnValueOnce([ + { + clusters: [ + { + name: 'some-cluster', + endpoint: '1.2.3.4', + }, + ], + }, + ]); + + const config: Config = new ConfigReader({ + type: 'gke', + projectId: 'some-project', + authProvider: 'googleServiceAccount', + }); + + const sut = GkeClusterLocator.fromConfigWithClient(config, { + listClusters: mockedListClusters, + } as any); + + const result = await sut.getClusters(); + + expect(result).toStrictEqual([ + { + name: 'some-cluster', + url: 'https://1.2.3.4', + authMetadata: { + [ANNOTATION_KUBERNETES_AUTH_PROVIDER]: 'googleServiceAccount', + }, + skipTLSVerify: false, + skipMetricsLookup: false, + }, + ]); + }); + it('return google login when authProvider property has invalid value', async () => { + mockedListClusters.mockReturnValueOnce([ + { + clusters: [ + { + name: 'some-cluster', + endpoint: '1.2.3.4', + }, + ], + }, + ]); + + const config: Config = new ConfigReader({ + type: 'gke', + projectId: 'some-project', + authProvider: 'differentValue', + }); + + const sut = GkeClusterLocator.fromConfigWithClient(config, { + listClusters: mockedListClusters, + } as any); + + const result = await sut.getClusters(); + + expect(result).toStrictEqual([ + { + name: 'some-cluster', + url: 'https://1.2.3.4', + authMetadata: { [ANNOTATION_KUBERNETES_AUTH_PROVIDER]: 'google' }, + skipTLSVerify: false, + skipMetricsLookup: false, + }, + ]); + }); }); }); diff --git a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts index a3b39f2b08..03f5bda103 100644 --- a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts +++ b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts @@ -55,18 +55,11 @@ export class GkeClusterLocator implements KubernetesClustersSupplier { return { key: mrl.getString('key'), value: mrl.getString('value') }; }) ?? []; - let storeAuthProviderString: string; - let getGkeProperty; - try { - getGkeProperty = config.getString('authProvider'); - } catch (err) { - getGkeProperty = 'google'; - } - if (getGkeProperty === 'googleServiceAccount') { - storeAuthProviderString = 'googleServiceAccount'; - } else { - storeAuthProviderString = 'google'; - } + const storeAuthProviderString = + config.getOptionalString('authProvider') === 'googleServiceAccount' + ? 'googleServiceAccount' + : 'google'; + const options = { projectId: config.getString('projectId'), authProvider: storeAuthProviderString,