Skip to content

Commit f875e8c

Browse files
committed
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 <thuan.votann@gmail.com>
1 parent cf6c505 commit f875e8c

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

frontend/src/utils/operatorTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ export type NormalizedOperatorChannel = {
198198
export type NormalizedCrdPreview = {
199199
name: string
200200
kind: string
201+
version: string
201202
displayName: string
202203
description: string
203204
yamlExample: object | null

frontend/src/utils/operatorUtils.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const normalizeCapabilityLevel = (capability: string) => {
4040
/**
4141
* Search for deployment example by kind
4242
*/
43-
const getExampleYAML = (kind: string, operator: operatorTypes.Operator): object | null => {
43+
const getExampleYAML = (kind: string, version: string, operator: operatorTypes.Operator): object | null => {
4444
const examples = _.get(operator, 'metadata.annotations.alm-examples');
4545
if (!examples) {
4646
return null;
@@ -53,7 +53,13 @@ const getExampleYAML = (kind: string, operator: operatorTypes.Operator): object
5353
yamlExamples = JSON.parse(examples);
5454
}
5555

56-
return _.find(yamlExamples, { kind });
56+
return _.find(yamlExamples, (resource) => {
57+
const apiVersionSplit = resource.apiVersion?.split('/');
58+
if (apiVersionSplit.length > 2) {
59+
throw new Error("Example resource YAML has invalid apiVersion.")
60+
}
61+
return resource?.kind === kind && (apiVersionSplit.length === 2? apiVersionSplit[1]: resource.apiVersion) === version;
62+
});
5763
} catch (e) {
5864
return null;
5965
}
@@ -75,12 +81,13 @@ export const mergeDescriptions = (operator: operatorTypes.Operator) => {
7581

7682

7783

78-
const normalizeCRD = (crd: operatorTypes.CustomResourceFile, operator: operatorTypes.Operator): operatorTypes.NormalizedCrdPreview => ({
84+
const normalizeCRD = (crd: operatorTypes.OperatorOwnedCrd, operator: operatorTypes.Operator): operatorTypes.NormalizedCrdPreview => ({
7985
name: _.get(crd, 'name', 'Name Not Available'),
8086
kind: crd.kind,
87+
version: crd.version,
8188
displayName: _.get(crd, 'displayName', 'Name Not Available'),
8289
description: _.get(crd, 'description', 'No description available'),
83-
yamlExample: getExampleYAML(crd.kind, operator)
90+
yamlExample: getExampleYAML(crd.kind, crd.version, operator)
8491
});
8592

8693
const normalizeCRDs = (operator: operatorTypes.Operator) => {

0 commit comments

Comments
 (0)