Skip to content

Commit

Permalink
refactor: add get group version kind function (#265)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherFry committed Apr 28, 2023
1 parent eacc442 commit 42aac8e
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from '@material-ui/core';
import React, { Fragment, useCallback, useEffect, useState } from 'react';
import { KubernetesResource } from '../../types/KubernetesResource';
import { getGroupVersionKind } from '../../utils/kubernetesResource';
import { PackageResource } from '../../utils/packageRevisionResources';
import { loadYaml } from '../../utils/yaml';
import { YamlViewer } from '../Controls';
Expand Down Expand Up @@ -63,9 +64,7 @@ export const ResourceEditorDialog = ({
const [isNamedEditor, setIsNamedEditor] = useState<boolean>(true);

const resourceYaml = loadYaml(yaml) as KubernetesResource;

const resourceApiVersion = resourceYaml && resourceYaml.apiVersion;
const kind = resourceYaml && resourceYaml.kind;
const groupVersionKind = resourceYaml && getGroupVersionKind(resourceYaml);

useEffect(() => {
if (open) {
Expand Down Expand Up @@ -112,8 +111,7 @@ export const ResourceEditorDialog = ({
>
{!showYamlView && isNamedEditor ? (
<FirstClassEditorSelector
apiVersion={resourceApiVersion}
kind={kind}
groupVersionKind={groupVersionKind}
yaml={latestYaml}
packageResources={packageResources}
onNoNamedEditor={handleNoNamedEditor}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,20 @@ type OnUpdatedYamlFn = (yaml: string) => void;
type OnNoNamedEditorFn = () => void;

type FirstClassEditorSelectorProps = {
apiVersion: string;
kind: string;
groupVersionKind: string;
yaml: string;
onUpdatedYaml: OnUpdatedYamlFn;
onNoNamedEditor: OnNoNamedEditorFn;
packageResources: PackageResource[];
};

export const FirstClassEditorSelector = ({
apiVersion,
kind,
groupVersionKind,
yaml,
onUpdatedYaml,
onNoNamedEditor,
packageResources,
}: FirstClassEditorSelectorProps) => {
const groupVersionKind = `${apiVersion}/${kind}`;
const isNamedEditor = useRef<boolean>(true);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from '@material-ui/core';
import React, { Fragment, useEffect, useState } from 'react';
import { KubernetesResource } from '../../types/KubernetesResource';
import { getGroupVersionKind } from '../../utils/kubernetesResource';
import { loadYaml } from '../../utils/yaml';
import { YamlViewer } from '../Controls';
import { FirstClassViewerSelector } from './components/FirstClassViewerSelector';
Expand Down Expand Up @@ -74,8 +75,9 @@ export const ResourceViewerDialog = ({
setShowYamlView(!showYamlView);
};

const { kind, apiVersion } = resourceYaml;
const kind = resourceYaml.kind;
const resourceName = resourceYaml.metadata.name;
const groupVersionKind = getGroupVersionKind(resourceYaml);

const displayYamlHeight = (thisYaml.split('\n').length + 1) * 18;

Expand All @@ -98,8 +100,7 @@ export const ResourceViewerDialog = ({
/>
) : (
<FirstClassViewerSelector
apiVersion={apiVersion}
kind={kind}
groupVersionKind={groupVersionKind}
yaml={yaml}
originalYaml={originalYaml}
showDiff={showDiff}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ import {
} from './FirstClassViewers/StructuredMetadata/StructuredMetadata';

type FirstClassViewerSelectorProps = {
apiVersion: string;
kind: string;
groupVersionKind: string;
yaml?: string;
originalYaml?: string;
showDiff?: boolean;
Expand Down Expand Up @@ -117,14 +116,11 @@ const getCustomMetadataFn = (
};

export const FirstClassViewerSelector = ({
apiVersion,
kind,
groupVersionKind,
yaml,
originalYaml,
showDiff,
}: FirstClassViewerSelectorProps) => {
const groupVersionKind = `${apiVersion}/${kind}`;

const customMetadataFn = getCustomMetadataFn(groupVersionKind);

return (
Expand Down
4 changes: 4 additions & 0 deletions plugins/cad/src/utils/kubernetesResource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@ export const removeInternalKptAnnotations = (
}
}
};

export const getGroupVersionKind = (resource: KubernetesResource): string => {
return `${resource.apiVersion}/${resource.kind}`;
};

0 comments on commit 42aac8e

Please sign in to comment.