theshire/.forgejo/workflows/schemas.yaml

136 lines
4.6 KiB
YAML
Raw Normal View History

---
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: "Schemas"
on:
workflow_dispatch:
schedule:
- cron: "0 0 * * *" # Every day at midnight
push:
branches: ["main"]
paths: [".forgejo/workflows/schemas.yaml"]
jobs:
publish:
name: Schemas
2024-07-04 14:34:52 -05:00
runs-on: ["ubuntu-x86_64"]
permissions:
contents: read
packages: write
steps:
- name: Checkout
2024-07-17 10:45:46 -05:00
uses: https://github.com/actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Workflow Tools
shell: bash
2024-05-28 21:12:46 -05:00
run: |
curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
2024-05-28 21:15:05 -05:00
mv kubectl /usr/local/bin/
2024-05-28 21:12:46 -05:00
curl -LO "https://dl.min.io/client/mc/release/linux-amd64/mc"
chmod +x mc
2024-05-28 21:15:05 -05:00
mv mc /usr/local/bin/
- name: Setup Python
2024-05-28 21:24:47 -05:00
run: |
2024-05-28 22:54:26 -05:00
apt-get update
2024-05-29 13:57:11 -05:00
apt-get install -y python3 python3-pip python3-yaml
2024-05-28 21:30:32 -05:00
pip3 install --upgrade pip
- name: Write kubeconfig
id: kubeconfig
2024-07-17 10:45:46 -05:00
uses: https://github.com/timheuer/base64-to-file@v1
with:
encodedString: "${{ secrets.KUBECONFIG }}"
fileName: kubeconfig
2024-07-17 09:36:19 -05:00
fileDir: ${{ env.GITHUB_WORKSPACE }}
- name: Write mc
id: mcconfig
2024-07-17 10:45:46 -05:00
uses: https://github.com/timheuer/base64-to-file@v1
with:
encodedString: "${{ secrets.MCCONFIG }}"
fileName: config.json
2024-05-29 14:55:56 -05:00
fileDir: $HOME/.mc
2024-05-29 13:55:28 -05:00
- name: Extracting CRDs to yaml and converting to JSON schema
2024-05-29 06:59:33 -05:00
env:
KUBECONFIG: "${{ steps.kubeconfig.outputs.filePath }}"
2024-05-28 22:59:20 -05:00
run: |
2024-07-17 09:24:57 -05:00
# kubeconfig
echo "kubeconfig location: $KUBECONFIG"
2024-05-29 12:01:36 -05:00
# Create temp folder for CRDs
TMP_CRD_DIR=$(mktemp -d)
echo "Temp directory: $TMP_CRD_DIR"
# Create final schemas directory
SCHEMAS_DIR=$GITHUB_WORKSPACE/crdSchemas
mkdir -p $SCHEMAS_DIR
2024-05-29 13:55:28 -05:00
echo "Schemas directory: $SCHEMAS_DIR"
2024-05-29 12:01:36 -05:00
2024-05-29 13:55:28 -05:00
# 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
2024-05-29 14:19:46 -05:00
2024-05-29 11:38:21 -05:00
# Extract CRDs from cluster
NUM_OF_CRDS=0
while read -r crd
do
filename=${crd%% *}
kubectl get crds "$filename" -o yaml > "$TMP_CRD_DIR/$filename.yaml" 2>&1
echo "Extracted CRD: $filename"
resourceKind=$(grep "kind:" "$TMP_CRD_DIR/$filename.yaml" | awk 'NR==2{print $2}' | tr '[:upper:]' '[:lower:]')
resourceGroup=$(grep "group:" "$TMP_CRD_DIR/$filename.yaml" | awk 'NR==1{print $2}')
# Save name and group for later directory organization
2024-05-29 14:31:55 -05:00
CRD_GROUPS["$resourceKind"]="$resourceGroup"
2024-05-29 11:38:21 -05:00
let ++NUM_OF_CRDS
done < <(kubectl get crds 2>&1 | sed -n '/NAME/,$p' | tail -n +2)
echo numCRDs: $NUM_OF_CRDS
2024-05-29 15:01:37 -05:00
2024-05-29 13:55:28 -05:00
# Download converter script
curl https://raw.githubusercontent.com/yannh/kubeconform/master/scripts/openapi2jsonschema.py --output $TMP_CRD_DIR/openapi2jsonschema.py 2>/dev/null
2024-05-29 11:38:21 -05:00
2024-05-29 13:55:28 -05:00
# Convert crds to jsonSchema
2024-05-29 15:01:37 -05:00
cd $SCHEMAS_DIR
2024-05-29 13:55:28 -05:00
python3 $TMP_CRD_DIR/openapi2jsonschema.py $TMP_CRD_DIR/*.yaml
conversionResult=$?
# Copy and rename files to support kubeval
rm -rf $SCHEMAS_DIR/master-standalone
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]}
2024-05-29 14:26:24 -05:00
if [ -z $crdGroup ]; then
crdGroup="uncategorized"
echo "CRD kind $crdKind has no group, moving to $crdGroup"
fi
2024-05-29 14:19:46 -05:00
echo making directory $crdGroup
2024-05-29 13:55:28 -05:00
mkdir -p $crdGroup
mv $schema ./$crdGroup
done
fi
rm -rf $TMP_CRD_DIR
- name: Deploy to Cloudflare R2
shell: bash
run: |
2024-07-08 09:19:38 -05:00
mc cp --recursive $GITHUB_WORKSPACE/crdSchemas/ r2-ks/kubernetes-schema