Skip to content

Commit

Permalink
feat: update default resource viewer (#266)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ChristopherFry committed May 18, 2023
1 parent 42aac8e commit df9ce8f
Showing 1 changed file with 22 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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('.');
Expand All @@ -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;
};

0 comments on commit df9ce8f

Please sign in to comment.