From 81a73b655930f24cbeae71a4e0446a8b506201b1 Mon Sep 17 00:00:00 2001 From: armandocomellas1 Date: Wed, 7 Feb 2024 14:42:51 -0600 Subject: [PATCH] adds a x-goog-api-client header to existing API requests in this plugin to clearly identify API requests from this GKE plugin Signed-off-by: armandocomellas1 --- .changeset/mighty-tomatoes-visit.md | 4 +-- .../cluster-locator/GkeClusterLocator.test.ts | 25 ++++++++++++------- .../src/cluster-locator/GkeClusterLocator.ts | 4 +-- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/.changeset/mighty-tomatoes-visit.md b/.changeset/mighty-tomatoes-visit.md index 7c12a0fa00..beb37d02ed 100644 --- a/.changeset/mighty-tomatoes-visit.md +++ b/.changeset/mighty-tomatoes-visit.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-kubernetes-backend': minor +'@backstage/plugin-kubernetes-backend': patch --- -At Line 91, in the second parameter enter a value for the property `libName` and `libVersion` to post headers to the key `x-goog-api-client` +adds a x-goog-api-client header to existing API requests in this plugin to clearly identify API requests from this GKE plugin. headers are formatted as follows where `libVersion` represents the current dotted version number of the Backstage GKE plugin and `libName` represent the current Google API used at backstage. diff --git a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts index de77000249..e6e19a6aaa 100644 --- a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts +++ b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts @@ -18,9 +18,18 @@ import { ANNOTATION_KUBERNETES_AUTH_PROVIDER } from '@backstage/plugin-kubernete import '@backstage/backend-common'; import { ConfigReader, Config } from '@backstage/config'; import { GkeClusterLocator } from './GkeClusterLocator'; -import { Duration } from 'luxon'; +import * as container from '@google-cloud/container'; const mockedListClusters = jest.fn(); +jest.mock('@google-cloud/container', () => { + return { + v1: { + ClusterManagerClient: jest.fn().mockImplementation(() => { + mockedListClusters(); + }), + }, + }; +}); describe('GkeClusterLocator', () => { beforeEach(() => { @@ -486,20 +495,18 @@ describe('GkeClusterLocator', () => { }, ]); }); - it('Check if new container.v1.ClusterManagerClient has key and values as parameter', async () => { + it('constructs ClusterManagerClient with identifying metadata', async () => { const configs: Config = new ConfigReader({ type: 'gke', projectId: 'some-project', }); - const refreshIntervals: Duration | undefined = undefined; - const getHeaders: GkeClusterLocator = GkeClusterLocator.fromConfig( - configs, - refreshIntervals, - ); + GkeClusterLocator.fromConfig(configs); - expect(getHeaders).toHaveProperty('client._opts.libName'); - expect(getHeaders).toHaveProperty('client._opts.libVersion'); + expect(container.v1.ClusterManagerClient).toHaveBeenCalledWith({ + libName: 'backstage/kubernetes-backend.GkeClusterLocator', + libVersion: expect.any(String), + }); }); }); }); diff --git a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts index 33b9ee74b9..3b3d74e17f 100644 --- a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts +++ b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts @@ -81,7 +81,7 @@ export class GkeClusterLocator implements KubernetesClustersSupplier { return gkeClusterLocator; } - // At Line 91, in the second parameter enter a value for the property libName and libVersion to post headers to the key 'x-goog-api-client' + // Added an `x-goog-api-client` header to API requests made by the GKE cluster locator to clearly identify API requests from this plugin. static fromConfig( config: Config, refreshInterval: Duration | undefined = undefined, @@ -89,7 +89,7 @@ export class GkeClusterLocator implements KubernetesClustersSupplier { return GkeClusterLocator.fromConfigWithClient( config, new container.v1.ClusterManagerClient({ - libName: 'backstage/gke', + libName: `backstage/kubernetes-backend.GkeClusterLocator`, libVersion: packageinfo.version, }), refreshInterval,