diff --git a/.changeset/mighty-tomatoes-visit.md b/.changeset/mighty-tomatoes-visit.md new file mode 100644 index 0000000000..beb37d02ed --- /dev/null +++ b/.changeset/mighty-tomatoes-visit.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-kubernetes-backend': patch +--- + +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 849a7839e2..e6e19a6aaa 100644 --- a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts +++ b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts @@ -18,8 +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 * 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(() => { @@ -485,5 +495,18 @@ describe('GkeClusterLocator', () => { }, ]); }); + it('constructs ClusterManagerClient with identifying metadata', async () => { + const configs: Config = new ConfigReader({ + type: 'gke', + projectId: 'some-project', + }); + + GkeClusterLocator.fromConfig(configs); + + 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 03f5bda103..3b3d74e17f 100644 --- a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts +++ b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts @@ -21,6 +21,7 @@ import * as container from '@google-cloud/container'; import { Duration } from 'luxon'; import { runPeriodically } from '../service/runPeriodically'; import { ClusterDetails, KubernetesClustersSupplier } from '../types/types'; +import packageinfo from '../../package.json'; interface MatchResourceLabelEntry { key: string; @@ -80,13 +81,17 @@ export class GkeClusterLocator implements KubernetesClustersSupplier { return gkeClusterLocator; } + // 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, ): GkeClusterLocator { return GkeClusterLocator.fromConfigWithClient( config, - new container.v1.ClusterManagerClient(), + new container.v1.ClusterManagerClient({ + libName: `backstage/kubernetes-backend.GkeClusterLocator`, + libVersion: packageinfo.version, + }), refreshInterval, ); }