Skip to content

Commit 6f8bca9

Browse files
author
hadar-co
authored
Merge pull request #153 from datreeio/organizebygroup
organize final schemas in dirs by group
2 parents 9ddb776 + 710b427 commit 6f8bca9

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

Utilities/crd-extractor.sh

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

33
# Check if python3 is installed
44
if ! command -v python3 &> /dev/null; then
@@ -16,7 +16,6 @@ fi
1616
# Check if the pyyaml module is installed
1717
if ! 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
@@ -33,12 +32,36 @@ fi
3332
TMP_CRD_DIR=$HOME/.datree/crds
3433
mkdir -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
3749
NUM_OF_CRDS=0
3850
while read -r crd
3951
do
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++
4366
done < <(kubectl get crds 2>&1 | sed -n '/NAME/,$p' | tail -n +2)
4467

@@ -51,11 +74,6 @@ fi
5174
# Download converter script
5275
curl 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
6078
python3 $TMP_CRD_DIR/openapi2jsonschema.py $TMP_CRD_DIR/*.yaml
6179
conversionResult=$?
@@ -66,6 +84,18 @@ mkdir -p $SCHEMAS_DIR/master-standalone
6684
cp $SCHEMAS_DIR/*.json $SCHEMAS_DIR/master-standalone
6785
find $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+
6999
CYAN='\033[0;36m'
70100
GREEN='\033[0;32m'
71101
NC='\033[0m' # No Color

0 commit comments

Comments
 (0)