Merge pull request #22614 from armandocomellas1/master

[Google Kubernetes Engine] Add x-goog-api-client header with custom values
This commit is contained in:
Jamie Klassen
2024-02-07 22:16:24 -05:00
committed by GitHub
3 changed files with 34 additions and 1 deletions
+5
View File
@@ -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.
@@ -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),
});
});
});
});
@@ -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,
);
}