Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed crd_to_kcl.sh to work with latest kcl import structure #202

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 31 additions & 37 deletions scripts/crd_to_kcl.sh
Original file line number Diff line number Diff line change
@@ -1,65 +1,59 @@
#!/bin/bash

if [ "$#" -ne 1 ]; then
echo "Usage: $0 <github-repo-url>"
if [ "$#" -lt 1 ] || [ "$#" -gt 2 ]; then
echo "Usage: $0 <github-repo-url> <version-tag>"
exit 1
fi

REPO_URL="$1"
VER="$2"

REPO_URL_SUFFIX=$(echo $REPO_URL | sed -n 's|.*github.com/||p')
OWNER=$(echo $REPO_URL_SUFFIX | cut -d '/' -f 1)
REPO=$(echo $REPO_URL_SUFFIX | cut -d '/' -f 2)
[ -z "$VER" ] && REPOVER="$REPO" || REPOVER="$REPO@$VER"

export KCL_FAST_EVAL=1

if [ -z "$OWNER" ] || [ -z "$REPO" ]; then
echo "Invalid GitHub repository URL."
exit 1
fi

# Init the kcl module
kcl mod init $REPO
if [ -z "$VER" ]; then
kcl mod init $REPO
else
kcl mod init $REPO --version $VER
fi

cd $REPO

# Get the Kubernetes CRD files
wget "https://doc.crds.dev/raw/github.com/$OWNER/$REPO"
wget "https://doc.crds.dev/raw/github.com/$OWNER/$REPOVER"
mkdir -p crds
mv $REPO crds/$REPO.yaml
mv $REPOVER crds/$REPO.yaml

# Import Kubernetes CRD to KCL files
kcl import -m crd -s ./crds/**
rm -rf main.k

# Add the k8s dependency
kcl mod add k8s
rm -rf models/k8s

models_dir="models"
regex="^(.*)_(v[0-9]+(alpha[0-9]+|beta[0-9]+)?)(.*)$"
rm -rf main.k models/{k8s,kcl.mod}

if [ ! -d "$models_dir" ]; then
echo "The directory '$models_dir' does not exist."
exit 1
fi

cd "$models_dir"

for file in *; do
if [ -f "$file" ]; then
if [[ $file =~ $regex ]]; then
version="${BASH_REMATCH[2]}"
mkdir -p "../$version"
mv "$file" "../$version/"
else
echo "File '$file' does not contain a version number."
fi
fi
done

cd -
echo "Files have been grouped by version."
rm -rf models
# Check KCL runs
for version_dir in *; do
if [ -d "$version_dir" ]; then
echo "Contents of '$version_dir':"
kcl run ./$version_dir
for version_dir in models/*/; do
if [ $(basename $version_dir) == "unknown" ]; then
rm -rf $version_dir
continue
fi
echo "Contents of '$version_dir':"
kcl run $version_dir
mv $version_dir .
done
echo "Files have been grouped and listed by version."
echo "Files have been listed by version."

rmdir models || exit 1

kcl doc generate
mv ./docs/*.md README.md