Skip to content

Commit

Permalink
Merge pull request #153 from datreeio/organizebygroup
Browse files Browse the repository at this point in the history
organize final schemas in dirs by group
  • Loading branch information
hadar-co authored Apr 9, 2023
2 parents 9ddb776 + 710b427 commit 6f8bca9
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions Utilities/crd-extractor.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash

# Check if python3 is installed
if ! command -v python3 &> /dev/null; then
Expand All @@ -16,7 +16,6 @@ fi
# Check if the pyyaml module is installed
if ! echo 'import yaml' | python3 &> /dev/null; then
printf "the python3 module 'yaml' is required, and is not installed on your machine.\n"
#printf "would you like to install it? y/n"

while true; do
read -p "Do you wish to install this program? (y/n) " yn
Expand All @@ -33,12 +32,36 @@ fi
TMP_CRD_DIR=$HOME/.datree/crds
mkdir -p $TMP_CRD_DIR

# Create final schemas directory
SCHEMAS_DIR=$HOME/.datree/crdSchemas
mkdir -p $SCHEMAS_DIR
cd $SCHEMAS_DIR

# Create array to store CRD kinds and groups
ORGANIZE_BY_GROUP=true
declare -A CRD_GROUPS 2>/dev/null
if [ $? -ne 0 ]; then
# Array creation failed, signal to skip organization by group
ORGANIZE_BY_GROUP=false
fi

# Extract CRDs from cluster
NUM_OF_CRDS=0
while read -r crd
do
resourceKind=${crd%% *}
kubectl get crds "$resourceKind" -o yaml > "$TMP_CRD_DIR/"$resourceKind".yaml" 2>&1

# Get singular name from crd
singularNameValue=$(grep singular: "$TMP_CRD_DIR/"$resourceKind".yaml" -m 1)
singularName=${singularNameValue##* }

# Get group
resourceGroup=${resourceKind#*.}

# Save name and group for later directory organization
CRD_GROUPS["$singularName"]="$resourceGroup"

let NUM_OF_CRDS++
done < <(kubectl get crds 2>&1 | sed -n '/NAME/,$p' | tail -n +2)

Expand All @@ -51,11 +74,6 @@ fi
# Download converter script
curl https://raw.githubusercontent.com/yannh/kubeconform/master/scripts/openapi2jsonschema.py --output $TMP_CRD_DIR/openapi2jsonschema.py 2>/dev/null

# Create final schemas directory
SCHEMAS_DIR=$HOME/.datree/crdSchemas
mkdir -p $SCHEMAS_DIR
cd $SCHEMAS_DIR

# Convert crds to jsonSchema
python3 $TMP_CRD_DIR/openapi2jsonschema.py $TMP_CRD_DIR/*.yaml
conversionResult=$?
Expand All @@ -66,6 +84,18 @@ mkdir -p $SCHEMAS_DIR/master-standalone
cp $SCHEMAS_DIR/*.json $SCHEMAS_DIR/master-standalone
find $SCHEMAS_DIR/master-standalone -name '*json' -exec bash -c ' mv -f $0 ${0/\_/-stable-}' {} \;

# Organize schemas by group
if [ $ORGANIZE_BY_GROUP == true ]; then
for schema in $SCHEMAS_DIR/*.json
do
crdFileName=$(basename $schema .json)
crdKind=${crdFileName%%_*}
crdGroup=${CRD_GROUPS[$crdKind]}
mkdir -p $crdGroup
mv $schema ./$crdGroup
done
fi

CYAN='\033[0;36m'
GREEN='\033[0;32m'
NC='\033[0m' # No Color
Expand Down

0 comments on commit 6f8bca9

Please sign in to comment.