Merge pull request #15735 from AmericanAirlines/fix-kubernetes-url

update fetcher to append resource to path instead of replace
This commit is contained in:
Fredrik Adelöw
2023-01-16 13:43:02 +01:00
committed by GitHub
3 changed files with 77 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-kubernetes-backend': patch
---
fixes a bug affecting clusters that have a base path in the URL. The base path was being replaced with the resource path instead of being appended
@@ -170,6 +170,71 @@ describe('KubernetesFetcher', () => {
});
});
it('should support clusters with a base path', async () => {
worker.use(
rest.get(
'http://localhost:9999/k8s/clusters/1234/api/v1/pods',
(req, res, ctx) =>
res(
checkToken(req, ctx, 'token'),
withLabels(req, ctx, {
items: [{ metadata: { name: 'pod-name' } }],
}),
),
),
rest.get(
'http://localhost:9999/k8s/clusters/1234/api/v1/services',
(req, res, ctx) =>
res(
checkToken(req, ctx, 'token'),
withLabels(req, ctx, {
items: [{ metadata: { name: 'service-name' } }],
}),
),
),
);
const result = await sut.fetchObjectsForService({
serviceId: 'some-service',
clusterDetails: {
name: 'cluster1',
url: 'http://localhost:9999/k8s/clusters/1234',
serviceAccountToken: 'token',
authProvider: 'serviceAccount',
},
objectTypesToFetch: OBJECTS_TO_FETCH,
labelSelector: '',
customResources: [],
});
expect(result).toStrictEqual({
errors: [],
responses: [
{
type: 'pods',
resources: [
{
metadata: {
name: 'pod-name',
labels: { 'backstage.io/kubernetes-id': 'some-service' },
},
},
],
},
{
type: 'services',
resources: [
{
metadata: {
name: 'service-name',
labels: { 'backstage.io/kubernetes-id': 'some-service' },
},
},
],
},
],
});
});
it('should return pods, services', async () => {
worker.use(
rest.get('http://localhost:9999/api/v1/pods', (req, res, ctx) =>
@@ -688,6 +753,7 @@ describe('KubernetesFetcher', () => {
expect(agent.options.rejectUnauthorized).toBe(false);
});
});
it('should use namespace if provided', async () => {
worker.use(
rest.get(
@@ -203,7 +203,12 @@ export class KubernetesClientBasedFetcher implements KubernetesFetcher {
);
}
url.pathname = resourcePath;
if (url.pathname === '/') {
url.pathname = resourcePath;
} else {
url.pathname += resourcePath;
}
if (labelSelector) {
url.search = `labelSelector=${labelSelector}`;
}