From df9ce8fe9a241ae6f9042cc5e7a32e1f6bd923c4 Mon Sep 17 00:00:00 2001 From: Christopher Fry Date: Wed, 17 May 2023 18:46:20 -0700 Subject: [PATCH] feat: update default resource viewer (#266) This change updates the default resource viewer to treat all fields in the spec field as top-level fields. This allows for richer metadata to be shown for any resources that do not have a named viewer. --- .../StructuredMetadata/resources/default.ts | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/plugins/cad/src/components/ResourceViewerDialog/components/FirstClassViewers/StructuredMetadata/resources/default.ts b/plugins/cad/src/components/ResourceViewerDialog/components/FirstClassViewers/StructuredMetadata/resources/default.ts index 3bde75ec..6e3704d9 100644 --- a/plugins/cad/src/components/ResourceViewerDialog/components/FirstClassViewers/StructuredMetadata/resources/default.ts +++ b/plugins/cad/src/components/ResourceViewerDialog/components/FirstClassViewers/StructuredMetadata/resources/default.ts @@ -55,25 +55,26 @@ const setMetadata = ( } }; -export const getDefaultStructuredMetadata = ( - resource: KubernetesResource, -): Metadata => { +const populateMetadata = ( + metadata: Metadata, + object: any, + depth: number, +): void => { const STANDARD_K8S_FIELDS = ['apiVersion', 'kind', 'metadata']; - const firstLevelFields = Object.keys(resource).filter( - key => !STANDARD_K8S_FIELDS.includes(key), + const firstLevelFields = Object.keys(object).filter( + key => !STANDARD_K8S_FIELDS.includes(key) || depth !== 1, ); - const anyResource = resource as unknown as any; - - const metadata: Metadata = {}; - for (const fieldName of firstLevelFields) { const newMetadata: Metadata = {}; - const displayName = fieldName === 'spec' ? resource.kind : fieldName; + if (fieldName === 'spec' && depth === 1) { + populateMetadata(metadata, object.spec, depth + 1); + continue; + } - setMetadata(newMetadata, anyResource[fieldName], displayName); + setMetadata(newMetadata, object[fieldName], fieldName); for (const thisKey of Object.keys(newMetadata)) { const isPrefix = thisKey.includes('.'); @@ -93,6 +94,16 @@ export const getDefaultStructuredMetadata = ( ); } } +}; + +export const getDefaultStructuredMetadata = ( + resource: KubernetesResource, +): Metadata => { + const anyResource = resource as unknown as any; + + const metadata: Metadata = {}; + + populateMetadata(metadata, anyResource, 1); return metadata; };