Skip to content

Commit a5b39fe

Browse files
author
hadar-co
authored
Merge pull request #186 from mhumeSF/crd-extractor-edits
Refactor to handle `kind` `singular` mismatch
2 parents a98dfeb + e999447 commit a5b39fe

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

Utilities/crd-extractor.sh

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,18 @@ fi
4747

4848
# Extract CRDs from cluster
4949
NUM_OF_CRDS=0
50-
while read -r crd
50+
while read -r crd
5151
do
52-
resourceKind=${crd%% *}
53-
kubectl get crds "$resourceKind" -o yaml > "$TMP_CRD_DIR/"$resourceKind".yaml" 2>&1
54-
55-
# Get singular name from crd
56-
singularNameValue=$(grep singular: "$TMP_CRD_DIR/"$resourceKind".yaml" -m 1)
57-
singularName=${singularNameValue##* }
58-
59-
# Get group
60-
resourceGroup=${resourceKind#*.}
52+
filename=${crd%% *}
53+
kubectl get crds "$filename" -o yaml > "$TMP_CRD_DIR/$filename.yaml" 2>&1
54+
55+
resourceKind=$(grep "kind:" "$TMP_CRD_DIR/$filename.yaml" | awk 'NR==2{print $2}' | tr '[:upper:]' '[:lower:]')
56+
resourceGroup=$(grep "group:" "$TMP_CRD_DIR/$filename.yaml" | awk 'NR==1{print $2}')
6157

6258
# Save name and group for later directory organization
63-
CRD_GROUPS["$singularName"]="$resourceGroup"
64-
65-
let NUM_OF_CRDS++
59+
CRD_GROUPS["$resourceKind"]="$resourceGroup"
60+
61+
let ++NUM_OF_CRDS
6662
done < <(kubectl get crds 2>&1 | sed -n '/NAME/,$p' | tail -n +2)
6763

6864
# If no CRDs exist in the cluster, exit
@@ -102,7 +98,7 @@ NC='\033[0m' # No Color
10298

10399
if [ $conversionResult == 0 ]; then
104100
printf "${GREEN}Successfully converted $NUM_OF_CRDS CRDs to JSON schema${NC}\n"
105-
101+
106102
printf "\nTo validate a CR using various tools, run the relevant command:\n"
107103
printf "\n- ${CYAN}datree:${NC}\n\$ datree test /path/to/file\n"
108104
printf "\n- ${CYAN}kubeconform:${NC}\n\$ kubeconform -summary -output json -schema-location default -schema-location '$HOME/.datree/crdSchemas/{{ .ResourceKind }}_{{ .ResourceAPIVersion }}.json' /path/to/file\n"

0 commit comments

Comments
 (0)