1- #! /bin/bash
1+ #! /usr/ bin/env bash
22
33# Check if python3 is installed
44if ! command -v python3 & > /dev/null; then
1616# Check if the pyyaml module is installed
1717if ! echo ' import yaml' | python3 & > /dev/null; then
1818 printf " the python3 module 'yaml' is required, and is not installed on your machine.\n"
19- # printf "would you like to install it? y/n"
2019
2120 while true ; do
2221 read -p " Do you wish to install this program? (y/n) " yn
3332TMP_CRD_DIR=$HOME /.datree/crds
3433mkdir -p $TMP_CRD_DIR
3534
35+ # Create final schemas directory
36+ SCHEMAS_DIR=$HOME /.datree/crdSchemas
37+ mkdir -p $SCHEMAS_DIR
38+ cd $SCHEMAS_DIR
39+
40+ # Create array to store CRD kinds and groups
41+ ORGANIZE_BY_GROUP=true
42+ declare -A CRD_GROUPS 2> /dev/null
43+ if [ $? -ne 0 ]; then
44+ # Array creation failed, signal to skip organization by group
45+ ORGANIZE_BY_GROUP=false
46+ fi
47+
3648# Extract CRDs from cluster
3749NUM_OF_CRDS=0
3850while read -r crd
3951do
4052 resourceKind=${crd%% * }
4153 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#* .}
61+
62+ # Save name and group for later directory organization
63+ CRD_GROUPS[" $singularName " ]=" $resourceGroup "
64+
4265 let NUM_OF_CRDS++
4366done < <( kubectl get crds 2>&1 | sed -n ' /NAME/,$p' | tail -n +2)
4467
5174# Download converter script
5275curl https://raw.githubusercontent.com/yannh/kubeconform/master/scripts/openapi2jsonschema.py --output $TMP_CRD_DIR /openapi2jsonschema.py 2> /dev/null
5376
54- # Create final schemas directory
55- SCHEMAS_DIR=$HOME /.datree/crdSchemas
56- mkdir -p $SCHEMAS_DIR
57- cd $SCHEMAS_DIR
58-
5977# Convert crds to jsonSchema
6078python3 $TMP_CRD_DIR /openapi2jsonschema.py $TMP_CRD_DIR /* .yaml
6179conversionResult=$?
@@ -66,6 +84,18 @@ mkdir -p $SCHEMAS_DIR/master-standalone
6684cp $SCHEMAS_DIR /* .json $SCHEMAS_DIR /master-standalone
6785find $SCHEMAS_DIR /master-standalone -name ' *json' -exec bash -c ' mv -f $0 ${0/\_/-stable-}' {} \;
6886
87+ # Organize schemas by group
88+ if [ $ORGANIZE_BY_GROUP == true ]; then
89+ for schema in $SCHEMAS_DIR /* .json
90+ do
91+ crdFileName=$( basename $schema .json)
92+ crdKind=${crdFileName%% _* }
93+ crdGroup=${CRD_GROUPS[$crdKind]}
94+ mkdir -p $crdGroup
95+ mv $schema ./$crdGroup
96+ done
97+ fi
98+
6999CYAN=' \033[0;36m'
70100GREEN=' \033[0;32m'
71101NC=' \033[0m' # No Color
0 commit comments