Merge pull request #25636 from JounQin/feat/k8s_objects

feat(kubernetes): support fetch pod metrics with custom resources
This commit is contained in:
Fredrik Adelöw
2024-12-12 20:08:06 +01:00
committed by GitHub
3 changed files with 84 additions and 1 deletions
@@ -31,6 +31,7 @@ import {
registerMswTestHooks,
} from '@backstage/backend-test-utils';
import {
CustomResourceFetchResponse,
FetchResponse,
KubernetesRequestAuth,
ObjectsByEntityResponse,
@@ -1428,5 +1429,78 @@ describe('KubernetesFanOutHandler', () => {
}),
);
});
it('fetch pod metrics when pods used', async () => {
getClustersByEntity.mockImplementation(() =>
Promise.resolve({
clusters: [
{
name: 'test-cluster',
title: 'cluster-title',
url: '',
authMetadata: {},
},
],
}),
);
sut = getKubernetesFanOutHandler([]);
const resources: CustomResourceFetchResponse[] = [
{
type: 'customresources',
resources: [
{
apiVersion: 'v1',
kind: 'Pod',
metadata: {
namespace: `ns-test-component-test-cluster`,
},
},
],
},
];
fetchObjectsForService.mockImplementation(async () => ({
responses: resources,
errors: [],
}));
const result = await sut.getCustomResourcesByEntity(
{
entity,
auth: {},
customResources: [
{
group: '',
apiVersion: 'v1',
plural: 'pods',
},
],
},
{ credentials: mockCredentials },
);
expect(fetchObjectsForService).toHaveBeenCalledTimes(1);
expect(fetchPodMetricsByNamespaces).toHaveBeenCalledTimes(1);
expect(fetchPodMetricsByNamespaces).toHaveBeenCalledWith(
expect.anything(),
{ type: 'anonymous' },
new Set(['ns-test-component-test-cluster']),
expect.anything(),
);
expect(result).toStrictEqual<ObjectsByEntityResponse>({
items: [
{
cluster: {
name: 'test-cluster',
title: 'cluster-title',
},
errors: [],
podMetrics: [POD_METRICS_FIXTURE],
resources,
},
],
});
});
});
});
@@ -147,7 +147,11 @@ export interface KubernetesFanOutHandlerOptions
export interface KubernetesRequestBody extends ObjectsByEntityRequest {}
const isPodFetchResponse = (fr: FetchResponse): fr is PodFetchResponse =>
fr.type === 'pods';
fr.type === 'pods' ||
(fr.type === 'customresources' &&
fr.resources.length > 0 &&
fr.resources[0].apiVersion === 'v1' &&
fr.resources[0].kind === 'Pod');
const isString = (str: string | undefined): str is string => str !== undefined;
const numberOrBigIntToNumberOrString = (