From a65206d5a4d90f3ca34385862261bd48d28e616e Mon Sep 17 00:00:00 2001 From: Morgan Martinet Date: Sun, 5 Sep 2021 20:02:07 -0400 Subject: [PATCH] minor changes after code review Signed-off-by: Morgan Martinet --- docs/features/kubernetes/configuration.md | 6 +-- plugins/kubernetes-backend/src/types/types.ts | 4 +- plugins/kubernetes-common/src/types.ts | 4 +- plugins/kubernetes/api-report.md | 10 ++-- .../KubernetesDrawer/KubernetesDrawer.tsx | 49 ++++++++++++++++++- .../utils/clusterLinks/formatClusterLink.ts | 6 ++- .../utils/clusterLinks/formatters/aks.test.ts | 2 +- .../src/utils/clusterLinks/formatters/aks.ts | 2 +- .../utils/clusterLinks/formatters/eks.test.ts | 2 +- .../src/utils/clusterLinks/formatters/eks.ts | 2 +- .../utils/clusterLinks/formatters/gke.test.ts | 2 +- .../src/utils/clusterLinks/formatters/gke.ts | 2 +- .../clusterLinks/formatters/openshift.test.ts | 4 +- .../clusterLinks/formatters/openshift.ts | 4 +- .../clusterLinks/formatters/rancher.test.ts | 4 +- .../utils/clusterLinks/formatters/rancher.ts | 4 +- .../utils/clusterLinks/formatters/standard.ts | 4 +- 17 files changed, 83 insertions(+), 28 deletions(-) diff --git a/docs/features/kubernetes/configuration.md b/docs/features/kubernetes/configuration.md index 4bbc28c178..f48faa202d 100644 --- a/docs/features/kubernetes/configuration.md +++ b/docs/features/kubernetes/configuration.md @@ -104,9 +104,9 @@ kubectl -n get secret $(kubectl -n get sa JSX.Element; +// Warning: (ae-forgotten-export) The symbol "FormatClusterLinkOptions" needs to be exported by the entry point index.d.ts // Warning: (ae-missing-release-tag) "formatClusterLink" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) // // @public (undocumented) -export function formatClusterLink(options: { - dashboardUrl?: string; - dashboardApp?: string; - object: any; - kind: string; -}): string | undefined; +export function formatClusterLink( + options: FormatClusterLinkOptions, +): string | undefined; // Warning: (ae-forgotten-export) The symbol "KubernetesAuthProvidersApi" needs to be exported by the entry point index.d.ts // Warning: (ae-missing-release-tag) "KubernetesAuthProviders" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) diff --git a/plugins/kubernetes/src/components/KubernetesDrawer/KubernetesDrawer.tsx b/plugins/kubernetes/src/components/KubernetesDrawer/KubernetesDrawer.tsx index 4a51655484..78f8aa3d5c 100644 --- a/plugins/kubernetes/src/components/KubernetesDrawer/KubernetesDrawer.tsx +++ b/plugins/kubernetes/src/components/KubernetesDrawer/KubernetesDrawer.tsx @@ -36,9 +36,12 @@ import { CodeSnippet, StructuredMetadataTable, Link, + WarningPanel, } from '@backstage/core-components'; import { ClusterContext } from '../../hooks'; import { formatClusterLink } from '../../utils/clusterLinks'; +import { ClusterAttributes } from '@backstage/plugin-kubernetes-common'; +import { FormatClusterLinkOptions } from '../../utils/clusterLinks/formatClusterLink'; const useDrawerStyles = makeStyles((theme: Theme) => createStyles({ @@ -57,6 +60,10 @@ const useDrawerContentStyles = makeStyles((_: Theme) => flexDirection: 'row', justifyContent: 'space-between', }, + errorMessage: { + marginTop: '1em', + marginBottom: '1em', + }, options: { display: 'flex', flexDirection: 'row', @@ -80,6 +87,27 @@ const PodDrawerButton = withStyles({ }, })(Button); +type ErrorPanelProps = { + cluster: ClusterAttributes; + errorMessage?: string; + children?: React.ReactNode; +}; + +export const ErrorPanel = ({ cluster, errorMessage }: ErrorPanelProps) => ( + + {errorMessage && ( + Errors: {errorMessage} + )} + +); + interface KubernetesDrawerable { metadata?: V1ObjectMeta; } @@ -100,6 +128,20 @@ function replaceNullsWithUndefined(someObj: any) { return JSON.parse(JSON.stringify(someObj, replacer)); } +function tryFormatClusterLink(options: FormatClusterLinkOptions) { + try { + return { + clusterLink: formatClusterLink(options), + errorMessage: '', + }; + } catch (err) { + return { + clusterLink: '', + errorMessage: err.message || err.toString(), + }; + } +} + const KubernetesDrawerContent = ({ toggleDrawer, object, @@ -110,7 +152,7 @@ const KubernetesDrawerContent = ({ const classes = useDrawerContentStyles(); const cluster = useContext(ClusterContext); - const clusterLink = formatClusterLink({ + const { clusterLink, errorMessage } = tryFormatClusterLink({ dashboardUrl: cluster.dashboardUrl, dashboardApp: cluster.dashboardApp, object, @@ -146,6 +188,11 @@ const KubernetesDrawerContent = ({ + {errorMessage && ( +
+ +
+ )}
{clusterLink && ( diff --git a/plugins/kubernetes/src/utils/clusterLinks/formatClusterLink.ts b/plugins/kubernetes/src/utils/clusterLinks/formatClusterLink.ts index f04ec1daea..83e970248b 100644 --- a/plugins/kubernetes/src/utils/clusterLinks/formatClusterLink.ts +++ b/plugins/kubernetes/src/utils/clusterLinks/formatClusterLink.ts @@ -16,12 +16,14 @@ import { defaultFormatterName, clusterLinksFormatters } from './formatters'; -export function formatClusterLink(options: { +export type FormatClusterLinkOptions = { dashboardUrl?: string; dashboardApp?: string; object: any; kind: string; -}) { +}; + +export function formatClusterLink(options: FormatClusterLinkOptions) { if (!options.dashboardUrl) { return undefined; } diff --git a/plugins/kubernetes/src/utils/clusterLinks/formatters/aks.test.ts b/plugins/kubernetes/src/utils/clusterLinks/formatters/aks.test.ts index 847cbf83ef..320c06e54c 100644 --- a/plugins/kubernetes/src/utils/clusterLinks/formatters/aks.test.ts +++ b/plugins/kubernetes/src/utils/clusterLinks/formatters/aks.test.ts @@ -28,6 +28,6 @@ describe('clusterLinks - AKS formatter', () => { }, kind: 'Deployment', }), - ).toThrowError('AKS formatter is not yet implemented'); + ).toThrowError('AKS formatter is not yet implemented. Please, contribute!'); }); }); diff --git a/plugins/kubernetes/src/utils/clusterLinks/formatters/aks.ts b/plugins/kubernetes/src/utils/clusterLinks/formatters/aks.ts index ef221b604d..d6f39ab72c 100644 --- a/plugins/kubernetes/src/utils/clusterLinks/formatters/aks.ts +++ b/plugins/kubernetes/src/utils/clusterLinks/formatters/aks.ts @@ -16,5 +16,5 @@ import { ClusterLinksFormatterOptions } from '../../../types/types'; export function aksFormatter(_options: ClusterLinksFormatterOptions): URL { - throw new Error('AKS formatter is not yet implemented'); + throw new Error('AKS formatter is not yet implemented. Please, contribute!'); } diff --git a/plugins/kubernetes/src/utils/clusterLinks/formatters/eks.test.ts b/plugins/kubernetes/src/utils/clusterLinks/formatters/eks.test.ts index 808901e60e..5998bfde5e 100644 --- a/plugins/kubernetes/src/utils/clusterLinks/formatters/eks.test.ts +++ b/plugins/kubernetes/src/utils/clusterLinks/formatters/eks.test.ts @@ -28,6 +28,6 @@ describe('clusterLinks - EKS formatter', () => { }, kind: 'Deployment', }), - ).toThrowError('EKS formatter is not yet implemented'); + ).toThrowError('EKS formatter is not yet implemented. Please, contribute!'); }); }); diff --git a/plugins/kubernetes/src/utils/clusterLinks/formatters/eks.ts b/plugins/kubernetes/src/utils/clusterLinks/formatters/eks.ts index a9fb2f265b..975c39797c 100644 --- a/plugins/kubernetes/src/utils/clusterLinks/formatters/eks.ts +++ b/plugins/kubernetes/src/utils/clusterLinks/formatters/eks.ts @@ -16,5 +16,5 @@ import { ClusterLinksFormatterOptions } from '../../../types/types'; export function eksFormatter(_options: ClusterLinksFormatterOptions): URL { - throw new Error('EKS formatter is not yet implemented'); + throw new Error('EKS formatter is not yet implemented. Please, contribute!'); } diff --git a/plugins/kubernetes/src/utils/clusterLinks/formatters/gke.test.ts b/plugins/kubernetes/src/utils/clusterLinks/formatters/gke.test.ts index 6a842e0c24..e566404daf 100644 --- a/plugins/kubernetes/src/utils/clusterLinks/formatters/gke.test.ts +++ b/plugins/kubernetes/src/utils/clusterLinks/formatters/gke.test.ts @@ -28,6 +28,6 @@ describe('clusterLinks - GKE formatter', () => { }, kind: 'Deployment', }), - ).toThrowError('GKE formatter is not yet implemented'); + ).toThrowError('GKE formatter is not yet implemented. Please, contribute!'); }); }); diff --git a/plugins/kubernetes/src/utils/clusterLinks/formatters/gke.ts b/plugins/kubernetes/src/utils/clusterLinks/formatters/gke.ts index 858b872389..30e7fa3123 100644 --- a/plugins/kubernetes/src/utils/clusterLinks/formatters/gke.ts +++ b/plugins/kubernetes/src/utils/clusterLinks/formatters/gke.ts @@ -16,5 +16,5 @@ import { ClusterLinksFormatterOptions } from '../../../types/types'; export function gkeFormatter(_options: ClusterLinksFormatterOptions): URL { - throw new Error('GKE formatter is not yet implemented'); + throw new Error('GKE formatter is not yet implemented. Please, contribute!'); } diff --git a/plugins/kubernetes/src/utils/clusterLinks/formatters/openshift.test.ts b/plugins/kubernetes/src/utils/clusterLinks/formatters/openshift.test.ts index 67ef5e6c51..df2529a8ac 100644 --- a/plugins/kubernetes/src/utils/clusterLinks/formatters/openshift.test.ts +++ b/plugins/kubernetes/src/utils/clusterLinks/formatters/openshift.test.ts @@ -28,6 +28,8 @@ describe('clusterLinks - OpenShift formatter', () => { }, kind: 'Deployment', }), - ).toThrowError('OpenShift formatter is not yet implemented'); + ).toThrowError( + 'OpenShift formatter is not yet implemented. Please, contribute!', + ); }); }); diff --git a/plugins/kubernetes/src/utils/clusterLinks/formatters/openshift.ts b/plugins/kubernetes/src/utils/clusterLinks/formatters/openshift.ts index acab9ec1cb..bacb747ebb 100644 --- a/plugins/kubernetes/src/utils/clusterLinks/formatters/openshift.ts +++ b/plugins/kubernetes/src/utils/clusterLinks/formatters/openshift.ts @@ -18,5 +18,7 @@ import { ClusterLinksFormatterOptions } from '../../../types/types'; export function openshiftFormatter( _options: ClusterLinksFormatterOptions, ): URL { - throw new Error('OpenShift formatter is not yet implemented'); + throw new Error( + 'OpenShift formatter is not yet implemented. Please, contribute!', + ); } diff --git a/plugins/kubernetes/src/utils/clusterLinks/formatters/rancher.test.ts b/plugins/kubernetes/src/utils/clusterLinks/formatters/rancher.test.ts index 503470acd3..754647d5e9 100644 --- a/plugins/kubernetes/src/utils/clusterLinks/formatters/rancher.test.ts +++ b/plugins/kubernetes/src/utils/clusterLinks/formatters/rancher.test.ts @@ -28,6 +28,8 @@ describe('clusterLinks - Rancher formatter', () => { }, kind: 'Deployment', }), - ).toThrowError('Rancher formatter is not yet implemented'); + ).toThrowError( + 'Rancher formatter is not yet implemented. Please, contribute!', + ); }); }); diff --git a/plugins/kubernetes/src/utils/clusterLinks/formatters/rancher.ts b/plugins/kubernetes/src/utils/clusterLinks/formatters/rancher.ts index 962f494408..491ca032a9 100644 --- a/plugins/kubernetes/src/utils/clusterLinks/formatters/rancher.ts +++ b/plugins/kubernetes/src/utils/clusterLinks/formatters/rancher.ts @@ -16,5 +16,7 @@ import { ClusterLinksFormatterOptions } from '../../../types/types'; export function rancherFormatter(_options: ClusterLinksFormatterOptions): URL { - throw new Error('Rancher formatter is not yet implemented'); + throw new Error( + 'Rancher formatter is not yet implemented. Please, contribute!', + ); } diff --git a/plugins/kubernetes/src/utils/clusterLinks/formatters/standard.ts b/plugins/kubernetes/src/utils/clusterLinks/formatters/standard.ts index 06a8f9e0a8..fb26cf220d 100644 --- a/plugins/kubernetes/src/utils/clusterLinks/formatters/standard.ts +++ b/plugins/kubernetes/src/utils/clusterLinks/formatters/standard.ts @@ -15,7 +15,7 @@ */ import { ClusterLinksFormatterOptions } from '../../../types/types'; -const KindMappings: Record = { +const kindMappings: Record = { deployment: 'deployment', ingress: 'ingress', service: 'service', @@ -26,7 +26,7 @@ export function standardFormatter(options: ClusterLinksFormatterOptions) { const result = new URL(options.dashboardUrl.href); const name = options.object.metadata?.name; const namespace = options.object.metadata?.namespace; - const validKind = KindMappings[options.kind.toLocaleLowerCase()]; + const validKind = kindMappings[options.kind.toLocaleLowerCase('en-US')]; if (namespace) { result.searchParams.set('namespace', namespace); }