Skip to content

Commit

Permalink
fix: consider apiVersion when getting example yaml for crds
Browse files Browse the repository at this point in the history
Solves k8s-operatorhub#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 <[email protected]>
  • Loading branch information
tthvo committed Jul 9, 2024
1 parent cf6c505 commit f875e8c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions frontend/src/utils/operatorTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ export type NormalizedOperatorChannel = {
export type NormalizedCrdPreview = {
name: string
kind: string
version: string
displayName: string
description: string
yamlExample: object | null
Expand Down
15 changes: 11 additions & 4 deletions frontend/src/utils/operatorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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) => {
Expand Down

0 comments on commit f875e8c

Please sign in to comment.