From f875e8c752105b9da4a8d2ab86e962e4bf7e0137 Mon Sep 17 00:00:00 2001 From: Thuan Vo Date: Tue, 9 Jul 2024 15:28:57 -0700 Subject: [PATCH] fix: consider apiVersion when getting example yaml for crds Solves #56. When getting example YAMLs for CRDs, apiVersion should be considered in cases where a CRD is shipped with >= 2 versions. Signed-off-by: Thuan Vo --- frontend/src/utils/operatorTypes.ts | 1 + frontend/src/utils/operatorUtils.ts | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/frontend/src/utils/operatorTypes.ts b/frontend/src/utils/operatorTypes.ts index abc7ace..53a5458 100644 --- a/frontend/src/utils/operatorTypes.ts +++ b/frontend/src/utils/operatorTypes.ts @@ -198,6 +198,7 @@ export type NormalizedOperatorChannel = { export type NormalizedCrdPreview = { name: string kind: string + version: string displayName: string description: string yamlExample: object | null diff --git a/frontend/src/utils/operatorUtils.ts b/frontend/src/utils/operatorUtils.ts index 6078f17..13ce5db 100644 --- a/frontend/src/utils/operatorUtils.ts +++ b/frontend/src/utils/operatorUtils.ts @@ -40,7 +40,7 @@ const normalizeCapabilityLevel = (capability: string) => { /** * Search for deployment example by kind */ -const getExampleYAML = (kind: string, operator: operatorTypes.Operator): object | null => { +const getExampleYAML = (kind: string, version: string, operator: operatorTypes.Operator): object | null => { const examples = _.get(operator, 'metadata.annotations.alm-examples'); if (!examples) { return null; @@ -53,7 +53,13 @@ const getExampleYAML = (kind: string, operator: operatorTypes.Operator): object yamlExamples = JSON.parse(examples); } - return _.find(yamlExamples, { kind }); + return _.find(yamlExamples, (resource) => { + const apiVersionSplit = resource.apiVersion?.split('/'); + if (apiVersionSplit.length > 2) { + throw new Error("Example resource YAML has invalid apiVersion.") + } + return resource?.kind === kind && (apiVersionSplit.length === 2? apiVersionSplit[1]: resource.apiVersion) === version; + }); } catch (e) { return null; } @@ -75,12 +81,13 @@ export const mergeDescriptions = (operator: operatorTypes.Operator) => { -const normalizeCRD = (crd: operatorTypes.CustomResourceFile, operator: operatorTypes.Operator): operatorTypes.NormalizedCrdPreview => ({ +const normalizeCRD = (crd: operatorTypes.OperatorOwnedCrd, operator: operatorTypes.Operator): operatorTypes.NormalizedCrdPreview => ({ name: _.get(crd, 'name', 'Name Not Available'), kind: crd.kind, + version: crd.version, displayName: _.get(crd, 'displayName', 'Name Not Available'), description: _.get(crd, 'description', 'No description available'), - yamlExample: getExampleYAML(crd.kind, operator) + yamlExample: getExampleYAML(crd.kind, crd.version, operator) }); const normalizeCRDs = (operator: operatorTypes.Operator) => {