Merge pull request #15250 from jamieklassen/node-fetch-k8s
[kubernetes] use node-fetch and gracefully surface fetcherrors
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
---
|
||||
'@backstage/plugin-kubernetes-backend': minor
|
||||
'@backstage/plugin-kubernetes-common': minor
|
||||
'@backstage/plugin-kubernetes': patch
|
||||
---
|
||||
|
||||
Kubernetes plugin now gracefully surfaces transport-level errors (like DNS or timeout, or other socket errors) occurring while fetching data. This will be merged into any data that is fetched successfully, fixing a bug where the whole page would be empty if any fetch operation encountered such an error.
|
||||
@@ -5,21 +5,17 @@
|
||||
```ts
|
||||
import { CatalogApi } from '@backstage/catalog-client';
|
||||
import { Config } from '@backstage/config';
|
||||
import { CoreV1Api } from '@kubernetes/client-node';
|
||||
import { Credentials } from 'aws-sdk';
|
||||
import { CustomObjectsApi } from '@kubernetes/client-node';
|
||||
import type { CustomResourceMatcher } from '@backstage/plugin-kubernetes-common';
|
||||
import { Duration } from 'luxon';
|
||||
import { Entity } from '@backstage/catalog-model';
|
||||
import express from 'express';
|
||||
import type { FetchResponse } from '@backstage/plugin-kubernetes-common';
|
||||
import type { JsonObject } from '@backstage/types';
|
||||
import { KubeConfig } from '@kubernetes/client-node';
|
||||
import type { KubernetesFetchError } from '@backstage/plugin-kubernetes-common';
|
||||
import { KubernetesRequestAuth } from '@backstage/plugin-kubernetes-common';
|
||||
import type { KubernetesRequestBody } from '@backstage/plugin-kubernetes-common';
|
||||
import { Logger } from 'winston';
|
||||
import { Metrics } from '@kubernetes/client-node';
|
||||
import type { ObjectsByEntityResponse } from '@backstage/plugin-kubernetes-common';
|
||||
import { PluginEndpointDiscovery } from '@backstage/backend-common';
|
||||
import type { RequestHandler } from 'express';
|
||||
@@ -262,18 +258,6 @@ export type KubernetesBuilderReturn = Promise<{
|
||||
serviceLocator: KubernetesServiceLocator;
|
||||
}>;
|
||||
|
||||
// @alpha (undocumented)
|
||||
export class KubernetesClientProvider {
|
||||
// (undocumented)
|
||||
getCoreClientByClusterDetails(clusterDetails: ClusterDetails): CoreV1Api;
|
||||
// (undocumented)
|
||||
getCustomObjectsClient(clusterDetails: ClusterDetails): CustomObjectsApi;
|
||||
// (undocumented)
|
||||
getKubeConfig(clusterDetails: ClusterDetails): KubeConfig;
|
||||
// (undocumented)
|
||||
getMetricsClient(clusterDetails: ClusterDetails): Metrics;
|
||||
}
|
||||
|
||||
// @alpha
|
||||
export interface KubernetesClustersSupplier {
|
||||
getClusters(): Promise<ClusterDetails[]>;
|
||||
|
||||
@@ -34,7 +34,6 @@ import {
|
||||
ObjectsByEntityRequest,
|
||||
ServiceLocatorMethod,
|
||||
} from '../types/types';
|
||||
import { KubernetesClientProvider } from './KubernetesClientProvider';
|
||||
import {
|
||||
DEFAULT_OBJECTS,
|
||||
KubernetesFanOutHandler,
|
||||
@@ -211,7 +210,6 @@ export class KubernetesBuilder {
|
||||
|
||||
protected buildFetcher(): KubernetesFetcher {
|
||||
this.fetcher = new KubernetesClientBasedFetcher({
|
||||
kubernetesClientProvider: new KubernetesClientProvider(),
|
||||
logger: this.env.logger,
|
||||
});
|
||||
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import '@backstage/backend-common';
|
||||
import { KubernetesClientProvider } from './KubernetesClientProvider';
|
||||
import { ClusterDetails } from '../types/types';
|
||||
import * as https from 'https';
|
||||
import mockFs from 'mock-fs';
|
||||
|
||||
describe('KubernetesClientProvider', () => {
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
afterEach(() => {
|
||||
mockFs.restore();
|
||||
});
|
||||
|
||||
it('can get core client by cluster details', () => {
|
||||
const sut = new KubernetesClientProvider();
|
||||
const getKubeConfig = jest.spyOn(sut, 'getKubeConfig');
|
||||
|
||||
const clusterDetails: ClusterDetails = {
|
||||
name: 'cluster-name',
|
||||
url: 'http://localhost:9999',
|
||||
serviceAccountToken: 'TOKEN',
|
||||
authProvider: 'serviceAccount',
|
||||
};
|
||||
const result = sut.getCoreClientByClusterDetails(clusterDetails);
|
||||
|
||||
expect(result.basePath).toBe('http://localhost:9999');
|
||||
// These fields aren't on the type but are there
|
||||
const auth = (result as any).authentications.default;
|
||||
expect(auth.users[0].token).toBe('TOKEN');
|
||||
expect(auth.clusters[0].name).toBe('cluster-name');
|
||||
expect(auth.clusters[0].skipTLSVerify).toBe(false);
|
||||
|
||||
expect(getKubeConfig).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('can get custom objects client by cluster details', () => {
|
||||
const sut = new KubernetesClientProvider();
|
||||
const getKubeConfig = jest.spyOn(sut, 'getKubeConfig');
|
||||
|
||||
const clusterDetails: ClusterDetails = {
|
||||
name: 'cluster-name',
|
||||
url: 'http://localhost:9999',
|
||||
serviceAccountToken: 'TOKEN',
|
||||
authProvider: 'serviceAccount',
|
||||
skipTLSVerify: false,
|
||||
};
|
||||
const result = sut.getCustomObjectsClient(clusterDetails);
|
||||
|
||||
expect(result.basePath).toBe('http://localhost:9999');
|
||||
// These fields aren't on the type but are there
|
||||
const auth = (result as any).authentications.default;
|
||||
expect(auth.users[0].token).toBe('TOKEN');
|
||||
expect(auth.clusters[0].name).toBe('cluster-name');
|
||||
|
||||
expect(getKubeConfig).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('respects caFile', async () => {
|
||||
mockFs({
|
||||
'/path/to/ca.crt': 'my-ca',
|
||||
});
|
||||
const clusterDetails: ClusterDetails = {
|
||||
name: 'cluster-name',
|
||||
url: 'https://localhost:9999',
|
||||
authProvider: 'serviceAccount',
|
||||
serviceAccountToken: 'TOKEN',
|
||||
caFile: '/path/to/ca.crt',
|
||||
};
|
||||
const kubeConfig = new KubernetesClientProvider().getKubeConfig(
|
||||
clusterDetails,
|
||||
);
|
||||
|
||||
const options: https.RequestOptions = {};
|
||||
await kubeConfig.applytoHTTPSOptions(options);
|
||||
|
||||
expect(options.ca?.toString()).toEqual('my-ca');
|
||||
});
|
||||
});
|
||||
@@ -1,87 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {
|
||||
Cluster,
|
||||
Context,
|
||||
CoreV1Api,
|
||||
CustomObjectsApi,
|
||||
KubeConfig,
|
||||
Metrics,
|
||||
User,
|
||||
} from '@kubernetes/client-node';
|
||||
import { ClusterDetails } from '../types/types';
|
||||
|
||||
/**
|
||||
*
|
||||
* @alpha
|
||||
*/
|
||||
export class KubernetesClientProvider {
|
||||
// visible for testing
|
||||
getKubeConfig(clusterDetails: ClusterDetails): KubeConfig {
|
||||
const cluster: Cluster = {
|
||||
name: clusterDetails.name,
|
||||
server: clusterDetails.url,
|
||||
skipTLSVerify: clusterDetails.skipTLSVerify || false,
|
||||
caData: clusterDetails.caData,
|
||||
caFile: clusterDetails.caFile,
|
||||
};
|
||||
|
||||
// TODO configure
|
||||
const user: User = {
|
||||
name: 'backstage',
|
||||
token: clusterDetails.serviceAccountToken,
|
||||
};
|
||||
|
||||
const context: Context = {
|
||||
name: `${clusterDetails.name}`,
|
||||
user: user.name,
|
||||
cluster: cluster.name,
|
||||
};
|
||||
|
||||
const kc: KubeConfig = new KubeConfig();
|
||||
if (clusterDetails.serviceAccountToken) {
|
||||
kc.loadFromOptions({
|
||||
clusters: [cluster],
|
||||
users: [user],
|
||||
contexts: [context],
|
||||
currentContext: context.name,
|
||||
});
|
||||
} else {
|
||||
kc.loadFromDefault();
|
||||
}
|
||||
|
||||
return kc;
|
||||
}
|
||||
|
||||
getCoreClientByClusterDetails(clusterDetails: ClusterDetails): CoreV1Api {
|
||||
const kc = this.getKubeConfig(clusterDetails);
|
||||
|
||||
return kc.makeApiClient(CoreV1Api);
|
||||
}
|
||||
|
||||
getMetricsClient(clusterDetails: ClusterDetails): Metrics {
|
||||
const kc = this.getKubeConfig(clusterDetails);
|
||||
|
||||
return new Metrics(kc);
|
||||
}
|
||||
|
||||
getCustomObjectsClient(clusterDetails: ClusterDetails): CustomObjectsApi {
|
||||
const kc = this.getKubeConfig(clusterDetails);
|
||||
|
||||
return kc.makeApiClient(CustomObjectsApi);
|
||||
}
|
||||
}
|
||||
@@ -20,8 +20,14 @@ import {
|
||||
CustomResource,
|
||||
FetchResponseWrapper,
|
||||
ObjectFetchParams,
|
||||
KubernetesServiceLocator,
|
||||
} from '../types/types';
|
||||
import { KubernetesFanOutHandler } from './KubernetesFanOutHandler';
|
||||
import { KubernetesClientBasedFetcher } from './KubernetesFetcher';
|
||||
import { rest } from 'msw';
|
||||
import { setupServer } from 'msw/node';
|
||||
import { setupRequestMockHandlers } from '@backstage/backend-test-utils';
|
||||
import { ObjectsByEntityResponse } from '@backstage/plugin-kubernetes-common';
|
||||
|
||||
const fetchObjectsForService = jest.fn();
|
||||
const fetchPodMetricsByNamespaces = jest.fn();
|
||||
@@ -751,6 +757,122 @@ describe('getKubernetesObjectsByEntity', () => {
|
||||
],
|
||||
});
|
||||
});
|
||||
it('fails when fetcher rejects with a non-FetchError', async () => {
|
||||
const nonFetchError = new Error('not a fetch error');
|
||||
getClustersByEntity.mockResolvedValue({
|
||||
clusters: [
|
||||
{
|
||||
name: 'test-cluster',
|
||||
authProvider: 'serviceAccount',
|
||||
skipMetricsLookup: true,
|
||||
},
|
||||
],
|
||||
});
|
||||
fetchObjectsForService.mockRejectedValue(nonFetchError);
|
||||
|
||||
const sut = getKubernetesFanOutHandler([]);
|
||||
|
||||
const result = sut.getKubernetesObjectsByEntity({
|
||||
entity,
|
||||
auth: {},
|
||||
});
|
||||
await expect(result).rejects.toThrow(nonFetchError);
|
||||
});
|
||||
describe('with a real fetcher', () => {
|
||||
const worker = setupServer();
|
||||
setupRequestMockHandlers(worker);
|
||||
it('fetch error short-circuits requests to a single cluster, recovering across the fleet', async () => {
|
||||
const pods = [{ metadata: { name: 'pod-name' } }];
|
||||
const services = [{ metadata: { name: 'service-name' } }];
|
||||
worker.use(
|
||||
rest.get('https://works/api/v1/pods', (_, res, ctx) =>
|
||||
res(ctx.json({ items: pods })),
|
||||
),
|
||||
rest.get('https://works/api/v1/services', (_, res, ctx) =>
|
||||
res(ctx.json({ items: services })),
|
||||
),
|
||||
rest.get('https://fails/api/v1/pods', (_, res) =>
|
||||
res.networkError('socket error'),
|
||||
),
|
||||
rest.get('https://fails/api/v1/services', (_, res, ctx) =>
|
||||
res(ctx.json({ items: services })),
|
||||
),
|
||||
);
|
||||
const fleet: jest.Mocked<KubernetesServiceLocator> = {
|
||||
getClustersByEntity: jest.fn().mockResolvedValue({
|
||||
clusters: [
|
||||
{
|
||||
name: 'works',
|
||||
url: 'https://works',
|
||||
authProvider: 'serviceAccount',
|
||||
serviceAccountToken: 'token',
|
||||
skipMetricsLookup: true,
|
||||
},
|
||||
{
|
||||
name: 'fails',
|
||||
url: 'https://fails',
|
||||
authProvider: 'serviceAccount',
|
||||
serviceAccountToken: 'token',
|
||||
skipMetricsLookup: true,
|
||||
},
|
||||
],
|
||||
}),
|
||||
};
|
||||
const logger = getVoidLogger();
|
||||
const sut = new KubernetesFanOutHandler({
|
||||
logger,
|
||||
fetcher: new KubernetesClientBasedFetcher({ logger }),
|
||||
serviceLocator: fleet,
|
||||
customResources: [],
|
||||
objectTypesToFetch: [
|
||||
{
|
||||
group: '',
|
||||
apiVersion: 'v1',
|
||||
plural: 'pods',
|
||||
objectType: 'pods',
|
||||
},
|
||||
{
|
||||
group: '',
|
||||
apiVersion: 'v1',
|
||||
plural: 'services',
|
||||
objectType: 'services',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
const result = await sut.getKubernetesObjectsByEntity({
|
||||
entity,
|
||||
auth: {},
|
||||
});
|
||||
|
||||
const expected: ObjectsByEntityResponse = {
|
||||
items: [
|
||||
{
|
||||
cluster: { name: 'works' },
|
||||
resources: [
|
||||
{ type: 'pods', resources: pods },
|
||||
{ type: 'services', resources: services },
|
||||
],
|
||||
podMetrics: [],
|
||||
errors: [],
|
||||
},
|
||||
{
|
||||
cluster: { name: 'fails' },
|
||||
resources: [],
|
||||
podMetrics: [],
|
||||
errors: [
|
||||
{
|
||||
errorType: 'FETCH_ERROR',
|
||||
message:
|
||||
'request to https://fails/api/v1/pods?labelSelector=backstage.io/kubernetes-id=test-component failed, reason: socket error',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
expect(result).toStrictEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getCustomResourcesByEntity', () => {
|
||||
|
||||
@@ -274,6 +274,20 @@ export class KubernetesFanOutHandler {
|
||||
namespace,
|
||||
})
|
||||
.then(result => this.getMetricsForPods(clusterDetailsItem, result))
|
||||
.catch(
|
||||
(e): Promise<responseWithMetrics> =>
|
||||
e.name === 'FetchError'
|
||||
? Promise.resolve([
|
||||
{
|
||||
errors: [
|
||||
{ errorType: 'FETCH_ERROR', message: e.message },
|
||||
],
|
||||
responses: [],
|
||||
},
|
||||
[],
|
||||
])
|
||||
: Promise.reject(e),
|
||||
)
|
||||
.then(r => this.toClusterObjects(clusterDetailsItem, r));
|
||||
}),
|
||||
).then(this.toObjectsByEntityResponse);
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -14,16 +14,23 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { topPods } from '@kubernetes/client-node';
|
||||
import {
|
||||
Config,
|
||||
Cluster,
|
||||
CoreV1Api,
|
||||
KubeConfig,
|
||||
Metrics,
|
||||
User,
|
||||
bufferFromFileOrString,
|
||||
topPods,
|
||||
} from '@kubernetes/client-node';
|
||||
import lodash, { Dictionary } from 'lodash';
|
||||
import { Logger } from 'winston';
|
||||
import {
|
||||
ClusterDetails,
|
||||
FetchResponseWrapper,
|
||||
KubernetesFetcher,
|
||||
KubernetesObjectTypes,
|
||||
ObjectFetchParams,
|
||||
ObjectToFetch,
|
||||
} from '../types/types';
|
||||
import {
|
||||
FetchResponse,
|
||||
@@ -31,10 +38,11 @@ import {
|
||||
KubernetesErrorTypes,
|
||||
PodStatusFetchResponse,
|
||||
} from '@backstage/plugin-kubernetes-common';
|
||||
import { KubernetesClientProvider } from './KubernetesClientProvider';
|
||||
import fetch, { RequestInit, Response } from 'node-fetch';
|
||||
import * as https from 'https';
|
||||
import fs from 'fs-extra';
|
||||
|
||||
export interface KubernetesClientBasedFetcherOptions {
|
||||
kubernetesClientProvider: KubernetesClientProvider;
|
||||
logger: Logger;
|
||||
}
|
||||
|
||||
@@ -72,14 +80,9 @@ const statusCodeToErrorType = (statusCode: number): KubernetesErrorTypes => {
|
||||
};
|
||||
|
||||
export class KubernetesClientBasedFetcher implements KubernetesFetcher {
|
||||
private readonly kubernetesClientProvider: KubernetesClientProvider;
|
||||
private readonly logger: Logger;
|
||||
|
||||
constructor({
|
||||
kubernetesClientProvider,
|
||||
logger,
|
||||
}: KubernetesClientBasedFetcherOptions) {
|
||||
this.kubernetesClientProvider = kubernetesClientProvider;
|
||||
constructor({ logger }: KubernetesClientBasedFetcherOptions) {
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
@@ -88,16 +91,27 @@ export class KubernetesClientBasedFetcher implements KubernetesFetcher {
|
||||
): Promise<FetchResponseWrapper> {
|
||||
const fetchResults = Array.from(params.objectTypesToFetch)
|
||||
.concat(params.customResources)
|
||||
.map(toFetch => {
|
||||
return this.fetchResource(
|
||||
.map(({ objectType, group, apiVersion, plural }) =>
|
||||
this.fetchResource(
|
||||
params.clusterDetails,
|
||||
toFetch,
|
||||
group,
|
||||
apiVersion,
|
||||
plural,
|
||||
params.namespace,
|
||||
params.labelSelector ||
|
||||
`backstage.io/kubernetes-id=${params.serviceId}`,
|
||||
toFetch.objectType,
|
||||
params.namespace,
|
||||
).catch(this.captureKubernetesErrorsRethrowOthers.bind(this));
|
||||
});
|
||||
).then(
|
||||
(r: Response): Promise<FetchResult> =>
|
||||
r.ok
|
||||
? r.json().then(
|
||||
({ items }): FetchResponse => ({
|
||||
type: objectType,
|
||||
resources: items,
|
||||
}),
|
||||
)
|
||||
: this.handleUnsuccessfulResponse(params.clusterDetails.name, r),
|
||||
),
|
||||
);
|
||||
|
||||
return Promise.all(fetchResults).then(fetchResultsToResponseWrapper);
|
||||
}
|
||||
@@ -106,93 +120,146 @@ export class KubernetesClientBasedFetcher implements KubernetesFetcher {
|
||||
clusterDetails: ClusterDetails,
|
||||
namespaces: Set<string>,
|
||||
): Promise<FetchResponseWrapper> {
|
||||
const metricsClient =
|
||||
this.kubernetesClientProvider.getMetricsClient(clusterDetails);
|
||||
const coreApi =
|
||||
this.kubernetesClientProvider.getCoreClientByClusterDetails(
|
||||
clusterDetails,
|
||||
);
|
||||
|
||||
const fetchResults = Array.from(namespaces).map(ns =>
|
||||
topPods(coreApi, metricsClient, ns)
|
||||
.then(r => {
|
||||
return {
|
||||
const fetchResults = Array.from(namespaces).map(async ns => {
|
||||
const [podMetrics, podList] = await Promise.all([
|
||||
this.fetchResource(
|
||||
clusterDetails,
|
||||
'metrics.k8s.io',
|
||||
'v1beta1',
|
||||
'pods',
|
||||
ns,
|
||||
),
|
||||
this.fetchResource(clusterDetails, '', 'v1', 'pods', ns),
|
||||
]);
|
||||
if (podMetrics.ok && podList.ok) {
|
||||
return topPods(
|
||||
{
|
||||
listPodForAllNamespaces: () =>
|
||||
podList.json().then(b => ({ body: b })),
|
||||
} as unknown as CoreV1Api,
|
||||
{
|
||||
getPodMetrics: () => podMetrics.json(),
|
||||
} as unknown as Metrics,
|
||||
).then(
|
||||
(resources): PodStatusFetchResponse => ({
|
||||
type: 'podstatus',
|
||||
resources: r,
|
||||
} as PodStatusFetchResponse;
|
||||
})
|
||||
.catch(this.captureKubernetesErrorsRethrowOthers.bind(this)),
|
||||
);
|
||||
resources,
|
||||
}),
|
||||
);
|
||||
} else if (podMetrics.ok) {
|
||||
return this.handleUnsuccessfulResponse(clusterDetails.name, podList);
|
||||
}
|
||||
return this.handleUnsuccessfulResponse(clusterDetails.name, podMetrics);
|
||||
});
|
||||
|
||||
return Promise.all(fetchResults).then(fetchResultsToResponseWrapper);
|
||||
}
|
||||
|
||||
private captureKubernetesErrorsRethrowOthers(e: any): KubernetesFetchError {
|
||||
if (e.response && e.response.statusCode) {
|
||||
this.logger.warn(
|
||||
`statusCode=${e.response.statusCode} for resource ${
|
||||
e.response.request.uri.pathname
|
||||
} body=[${JSON.stringify(e.response.body)}]`,
|
||||
);
|
||||
return {
|
||||
errorType: statusCodeToErrorType(e.response.statusCode),
|
||||
statusCode: e.response.statusCode,
|
||||
resourcePath: e.response.request.uri.pathname,
|
||||
};
|
||||
}
|
||||
throw e;
|
||||
private async handleUnsuccessfulResponse(
|
||||
clusterName: string,
|
||||
res: Response,
|
||||
): Promise<KubernetesFetchError> {
|
||||
const resourcePath = new URL(res.url).pathname;
|
||||
this.logger.warn(
|
||||
`Received ${
|
||||
res.status
|
||||
} status when fetching "${resourcePath}" from cluster "${clusterName}"; body=[${await res.text()}]`,
|
||||
);
|
||||
return {
|
||||
errorType: statusCodeToErrorType(res.status),
|
||||
statusCode: res.status,
|
||||
resourcePath,
|
||||
};
|
||||
}
|
||||
|
||||
private fetchResource(
|
||||
clusterDetails: ClusterDetails,
|
||||
resource: ObjectToFetch,
|
||||
labelSelector: string,
|
||||
objectType: KubernetesObjectTypes,
|
||||
group: string,
|
||||
apiVersion: string,
|
||||
plural: string,
|
||||
namespace?: string,
|
||||
): Promise<FetchResponse> {
|
||||
const customObjects =
|
||||
this.kubernetesClientProvider.getCustomObjectsClient(clusterDetails);
|
||||
|
||||
customObjects.addInterceptor((requestOptions: any) => {
|
||||
requestOptions.uri = requestOptions.uri.replace('/apis//v1/', '/api/v1/');
|
||||
});
|
||||
|
||||
labelSelector?: string,
|
||||
): Promise<Response> {
|
||||
const encode = (s: string) => encodeURIComponent(s);
|
||||
let resourcePath = group
|
||||
? `/apis/${encode(group)}/${encode(apiVersion)}`
|
||||
: `/api/${encode(apiVersion)}`;
|
||||
if (namespace) {
|
||||
return customObjects
|
||||
.listNamespacedCustomObject(
|
||||
resource.group,
|
||||
resource.apiVersion,
|
||||
namespace,
|
||||
resource.plural,
|
||||
'',
|
||||
false,
|
||||
'',
|
||||
'',
|
||||
labelSelector,
|
||||
)
|
||||
.then(r => {
|
||||
return {
|
||||
type: objectType,
|
||||
resources: (r.body as any).items,
|
||||
};
|
||||
});
|
||||
resourcePath += `/namespaces/${encode(namespace)}`;
|
||||
}
|
||||
return customObjects
|
||||
.listClusterCustomObject(
|
||||
resource.group,
|
||||
resource.apiVersion,
|
||||
resource.plural,
|
||||
'',
|
||||
false,
|
||||
'',
|
||||
'',
|
||||
labelSelector,
|
||||
)
|
||||
.then(r => {
|
||||
return {
|
||||
type: objectType,
|
||||
resources: (r.body as any).items,
|
||||
};
|
||||
resourcePath += `/${encode(plural)}`;
|
||||
|
||||
let url: URL;
|
||||
let requestInit: RequestInit;
|
||||
if (clusterDetails.serviceAccountToken) {
|
||||
[url, requestInit] = this.fetchArgsFromClusterDetails(clusterDetails);
|
||||
} else if (fs.pathExistsSync(Config.SERVICEACCOUNT_TOKEN_PATH)) {
|
||||
[url, requestInit] = this.fetchArgsInCluster();
|
||||
} else {
|
||||
return Promise.reject(
|
||||
new Error(
|
||||
`no bearer token for cluster '${clusterDetails.name}' and not running in Kubernetes`,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
url.pathname = resourcePath;
|
||||
if (labelSelector) {
|
||||
url.search = `labelSelector=${labelSelector}`;
|
||||
}
|
||||
|
||||
return fetch(url, requestInit);
|
||||
}
|
||||
|
||||
private fetchArgsFromClusterDetails(
|
||||
clusterDetails: ClusterDetails,
|
||||
): [URL, RequestInit] {
|
||||
const requestInit: RequestInit = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${clusterDetails.serviceAccountToken}`,
|
||||
},
|
||||
};
|
||||
|
||||
const url: URL = new URL(clusterDetails.url);
|
||||
if (url.protocol === 'https:') {
|
||||
requestInit.agent = new https.Agent({
|
||||
ca:
|
||||
bufferFromFileOrString(
|
||||
clusterDetails.caFile,
|
||||
clusterDetails.caData,
|
||||
) ?? undefined,
|
||||
rejectUnauthorized: !clusterDetails.skipTLSVerify,
|
||||
});
|
||||
}
|
||||
return [url, requestInit];
|
||||
}
|
||||
private fetchArgsInCluster(): [URL, RequestInit] {
|
||||
const kc = new KubeConfig();
|
||||
kc.loadFromCluster();
|
||||
// loadFromCluster is guaranteed to populate the cluster/user/context
|
||||
const cluster = kc.getCurrentCluster() as Cluster;
|
||||
const user = kc.getCurrentUser() as User;
|
||||
|
||||
const token = fs.readFileSync(user.authProvider.config.tokenFile);
|
||||
|
||||
const requestInit: RequestInit = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
};
|
||||
|
||||
const url = new URL(cluster.server);
|
||||
if (url.protocol === 'https:') {
|
||||
requestInit.agent = new https.Agent({
|
||||
ca: fs.readFileSync(cluster.caFile as string),
|
||||
});
|
||||
}
|
||||
return [url, requestInit];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
*/
|
||||
|
||||
export * from './KubernetesBuilder';
|
||||
export * from './KubernetesClientProvider';
|
||||
export { DEFAULT_OBJECTS } from './KubernetesFanOutHandler';
|
||||
export { HEADER_KUBERNETES_CLUSTER, KubernetesProxy } from './KubernetesProxy';
|
||||
export * from './router';
|
||||
|
||||
@@ -184,14 +184,7 @@ export type KubernetesErrorTypes =
|
||||
| 'UNKNOWN_ERROR';
|
||||
|
||||
// @public (undocumented)
|
||||
export interface KubernetesFetchError {
|
||||
// (undocumented)
|
||||
errorType: KubernetesErrorTypes;
|
||||
// (undocumented)
|
||||
resourcePath?: string;
|
||||
// (undocumented)
|
||||
statusCode?: number;
|
||||
}
|
||||
export type KubernetesFetchError = StatusError | RawFetchError;
|
||||
|
||||
// @public (undocumented)
|
||||
export interface KubernetesRequestAuth {
|
||||
@@ -241,6 +234,14 @@ export interface PodStatusFetchResponse {
|
||||
type: 'podstatus';
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface RawFetchError {
|
||||
// (undocumented)
|
||||
errorType: 'FETCH_ERROR';
|
||||
// (undocumented)
|
||||
message: string;
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface ReplicaSetsFetchResponse {
|
||||
// (undocumented)
|
||||
@@ -265,6 +266,16 @@ export interface StatefulSetsFetchResponse {
|
||||
type: 'statefulsets';
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface StatusError {
|
||||
// (undocumented)
|
||||
errorType: KubernetesErrorTypes;
|
||||
// (undocumented)
|
||||
resourcePath?: string;
|
||||
// (undocumented)
|
||||
statusCode?: number;
|
||||
}
|
||||
|
||||
// @public (undocumented)
|
||||
export interface WorkloadsByEntityRequest {
|
||||
// (undocumented)
|
||||
|
||||
@@ -223,12 +223,21 @@ export interface PodStatusFetchResponse {
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export interface KubernetesFetchError {
|
||||
export type KubernetesFetchError = StatusError | RawFetchError;
|
||||
|
||||
/** @public */
|
||||
export interface StatusError {
|
||||
errorType: KubernetesErrorTypes;
|
||||
statusCode?: number;
|
||||
resourcePath?: string;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export interface RawFetchError {
|
||||
errorType: 'FETCH_ERROR';
|
||||
message: string;
|
||||
}
|
||||
|
||||
/** @public */
|
||||
export type KubernetesErrorTypes =
|
||||
| 'BAD_REQUEST'
|
||||
|
||||
@@ -20,36 +20,14 @@ import { wrapInTestApp } from '@backstage/test-utils';
|
||||
import { ErrorPanel } from './ErrorPanel';
|
||||
|
||||
describe('ErrorPanel', () => {
|
||||
it('render with error message', async () => {
|
||||
const { getByText } = render(
|
||||
wrapInTestApp(
|
||||
<ErrorPanel
|
||||
entityName="THIS_ENTITY"
|
||||
errorMessage="SOME_ERROR_MESSAGE"
|
||||
/>,
|
||||
),
|
||||
);
|
||||
|
||||
// title
|
||||
expect(
|
||||
getByText(
|
||||
'There was a problem retrieving some Kubernetes resources for the entity: THIS_ENTITY. This could mean that the Error Reporting card is not completely accurate.',
|
||||
),
|
||||
).toBeInTheDocument();
|
||||
|
||||
// message
|
||||
expect(getByText('Errors: SOME_ERROR_MESSAGE')).toBeInTheDocument();
|
||||
});
|
||||
it('render with cluster errors', async () => {
|
||||
it('displays path and status code when a cluster has an HTTP error', async () => {
|
||||
const { getByText } = render(
|
||||
wrapInTestApp(
|
||||
<ErrorPanel
|
||||
entityName="THIS_ENTITY"
|
||||
clustersWithErrors={[
|
||||
{
|
||||
cluster: {
|
||||
name: 'THIS_CLUSTER',
|
||||
},
|
||||
cluster: { name: 'THIS_CLUSTER' },
|
||||
resources: [],
|
||||
podMetrics: [],
|
||||
errors: [
|
||||
@@ -81,4 +59,62 @@ describe('ErrorPanel', () => {
|
||||
),
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
it('displays message for non-HTTP-status-related fetch errors', async () => {
|
||||
const { getByText } = render(
|
||||
wrapInTestApp(
|
||||
<ErrorPanel
|
||||
entityName="THIS_ENTITY"
|
||||
clustersWithErrors={[
|
||||
{
|
||||
cluster: { name: 'THIS_CLUSTER' },
|
||||
resources: [],
|
||||
podMetrics: [],
|
||||
errors: [
|
||||
{
|
||||
errorType: 'FETCH_ERROR',
|
||||
message: 'description of error',
|
||||
},
|
||||
],
|
||||
},
|
||||
]}
|
||||
/>,
|
||||
),
|
||||
);
|
||||
|
||||
// title
|
||||
expect(
|
||||
getByText(
|
||||
'There was a problem retrieving some Kubernetes resources for the entity: THIS_ENTITY. This could mean that the Error Reporting card is not completely accurate.',
|
||||
),
|
||||
).toBeInTheDocument();
|
||||
|
||||
// message
|
||||
expect(getByText('Errors:')).toBeInTheDocument();
|
||||
expect(getByText('Cluster: THIS_CLUSTER')).toBeInTheDocument();
|
||||
expect(
|
||||
getByText(
|
||||
'Error communicating with Kubernetes: FETCH_ERROR, message: description of error',
|
||||
),
|
||||
).toBeInTheDocument();
|
||||
});
|
||||
it('displays error message', async () => {
|
||||
const { getByText } = render(
|
||||
wrapInTestApp(
|
||||
<ErrorPanel
|
||||
entityName="THIS_ENTITY"
|
||||
errorMessage="SOME_ERROR_MESSAGE"
|
||||
/>,
|
||||
),
|
||||
);
|
||||
|
||||
// title
|
||||
expect(
|
||||
getByText(
|
||||
'There was a problem retrieving some Kubernetes resources for the entity: THIS_ENTITY. This could mean that the Error Reporting card is not completely accurate.',
|
||||
),
|
||||
).toBeInTheDocument();
|
||||
|
||||
// message
|
||||
expect(getByText('Errors: SOME_ERROR_MESSAGE')).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -29,7 +29,9 @@ const clustersWithErrorsToErrorMessage = (
|
||||
{c.errors.map((e, j) => {
|
||||
return (
|
||||
<Typography variant="body2" key={j}>
|
||||
{`Error fetching Kubernetes resource: '${e.resourcePath}', error: ${e.errorType}, status code: ${e.statusCode}`}
|
||||
{e.errorType === 'FETCH_ERROR'
|
||||
? `Error communicating with Kubernetes: ${e.errorType}, message: ${e.message}`
|
||||
: `Error fetching Kubernetes resource: '${e.resourcePath}', error: ${e.errorType}, status code: ${e.statusCode}`}
|
||||
</Typography>
|
||||
);
|
||||
})}
|
||||
|
||||
Reference in New Issue
Block a user