Last active
August 1, 2025 04:22
-
-
Save rawc0der/040670cd69c4132f7d6aeb56f6668048 to your computer and use it in GitHub Desktop.
Revisions
-
rawc0der revised this gist
Oct 9, 2020 . 1 changed file with 16 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -41,26 +41,37 @@ EOF # optimized version function crd2jsonschemaJq() { set -e local document="$1" # local outputPath="$2" local queryExpression=$(mktemp -u) trap 'rm -f -- "$queryExpression"' INT TERM HUP ERR EXIT # local jsonCRD=$(yq r -j $document) # >/dev/null 2&>1 local jsonCRD=$(yaml2json $document) # >/dev/null 2&>1 local hasSchema=$(echo $jsonCRD | jq -r '.spec.validation.openAPIV3Schema.properties') local version=$(echo $jsonCRD | jq .spec.version | sed 's/"//g') if [[ -n $hasSchema && $version != null ]]; then echo $hasSchema cat <<'EOF' >$queryExpression { properties: .spec.validation.openAPIV3Schema.properties, description: .spec.validation.openAPIV3Schema.description, required: .spec.validation.openAPIV3Schema.required, title: .metadata.name, type: "object", "$schema": "http://json-schema.org/draft/2019-09/schema#", "x-kubernetes-group-version-kind.group": .spec.group, "x-kubernetes-group-version-kind.kind": .spec.names.kind, "x-kubernetes-group-version-kind.version": .spec.version } EOF local basefile=${document##*/} local filename=${basefile%.*} local destinationFile="$filename-$version.crd-schema.yaml" echo "$(date --utc) Writing json-schema file: $PWD/$destinationFile" echo $jsonCRD | jq -S -f "$queryExpression" >$destinationFile fi } -
rawc0der revised this gist
Oct 8, 2020 . No changes.There are no files selected for viewing
-
rawc0der revised this gist
Oct 8, 2020 . 1 changed file with 28 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -38,4 +38,31 @@ EOF } # optimized version function crd2jsonschemaJq() { set -e local document="$1" local queryExpression=$(mktemp -u) trap 'rm -f -- "$queryExpression"' INT TERM HUP ERR EXIT local jsonCRD=$(yq r -j $document) local hasSchema=$(echo $jsonCRD | jq -r '.spec.validation.openAPIV3Schema.properties') if [[ -n $hasSchema ]]; then cat <<'EOF' >$queryExpression { properties: .spec.validation.openAPIV3Schema.properties, title: .metadata.name, type: "object", "$schema": "http://json-schema.org/draft/2019-09/schema#", "x-kubernetes-group-version-kind.group": .spec.group, "x-kubernetes-group-version-kind.kind": .spec.names.kind, "x-kubernetes-group-version-kind.version": .spec.version } EOF echo $jsonCRD | jq -S -f "$queryExpression" fi } crd2jsonschemaJq $1 -
rawc0der revised this gist
Oct 8, 2020 . 1 changed file with 24 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,31 +4,38 @@ set -e function crd2jsonschema() { set -e local xkgroup="x-kubernetes-group-version-kind" local document="$1" local openAPIV3Schema=$(mktemp -u) local baseSchema=$(mktemp -u) local jsonSchema=$(mktemp -u) # clean on exit trap 'rm -f -- "$baseSchema" "$openAPIV3Schema" "$jsonSchema"' INT TERM HUP ERR EXIT # extract openapi schema from crd yq r -j $document 'spec.validation.openAPIV3Schema' >$openAPIV3Schema # check if openAPIV3Schema if [[ -n $(jq -r .properties $openAPIV3Schema) ]]; then # create initial schema file cat <<'EOF' >$baseSchema "$schema": "http://json-schema.org/draft/2019-09/schema#" type: object EOF # add canonical properties to schema yq w $baseSchema 'title' $(yq r $document 'metadata.name') | yq w - --tag='!!map' 'properties' | yq w - "${xkgroup}.group" $(yq r $document 'spec.group') | yq w - "${xkgroup}.kind" $(yq r $document 'spec.names.kind') | yq w -j - "${xkgroup}.version" $(yq r $document 'spec.version') >$jsonSchema # merge files into expected openapi jsonschema echo "$(cat $jsonSchema) $(cat $openAPIV3Schema)" | jq -S -n '[inputs]| add' fi } crd2jsonschema $1 -
rawc0der revised this gist
Oct 8, 2020 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ #!/bin/bash # Small utility function based on yq to extract openAPIV3Schema from CRD # example: crd2jsonschema.sh ./crd-alertmanager.yaml set -e -
rawc0der revised this gist
Oct 8, 2020 . 1 changed file with 8 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,18 +1,18 @@ # Small utility function based on yq to extract openAPIV3Schema from CRD # example: crd2jsonschema.sh ./crd-alertmanager.yaml set -e function crd2jsonschema() { local xkgroup="x-kubernetes-group-version-kind" # local document=$(cat "${@:1}") local document="${1}" local openapiFile=$(mktemp) local tmpfile=$(mktemp) # clean on exit trap 'rm -f -- "$tmpfile" "$openapiFile"' INT TERM HUP ERR EXIT # extract openapi schema from crd yq r $1 'spec.validation.openAPIV3Schema' > $openapiFile # create initial schema file cat << 'EOF' > $tmpfile "$schema": "http://json-schema.org/draft/2019-09/schema#" @@ -21,11 +21,11 @@ EOF # add canonical properties to schema cat $tmpfile | yq w - 'title' $(yq r $document 'metadata.name') | yq w - --tag='!!map' 'properties' | yq w - "${xkgroup}.group" $(yq r $document 'spec.group') | yq w - "${xkgroup}.kind" $(yq r $document 'spec.names.kind') | yq w - "${xkgroup}.version" $(yq r $document 'spec.version') | yq m -x - $openapiFile } -
rawc0der revised this gist
Oct 7, 2020 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,8 +10,8 @@ function crd2jsonschema() { trap 'rm -f -- "$tmpfile" "$openapiFile"' INT TERM HUP ERR EXIT # extract openapi schema from crd echo $document | yq r - 'spec.validation.openAPIV3Schema' > $openapiFile # local openAPIV3Schema=$(cat $openapiFile | yq r -P - ) # create initial schema file cat << 'EOF' > $tmpfile -
rawc0der revised this gist
Oct 7, 2020 . 1 changed file with 14 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,20 +4,30 @@ function crd2jsonschema() { local xkgroup="x-kubernetes-group-version-kind" local document=$(cat "$1") local openapiFile=$(mktemp) local tmpfile=$(mktemp) # clean on exit trap 'rm -f -- "$tmpfile" "$openapiFile"' INT TERM HUP ERR EXIT # extract openapi schema from crd echo $document | yq r - 'spec.validation.openAPIV3Schema.properties' | yq p -P - properties > $openapiFile local openAPIV3Schema=$(cat $openapiFile | yq r -P - properties) # create initial schema file cat << 'EOF' > $tmpfile "$schema": "http://json-schema.org/draft/2019-09/schema#" type: object EOF # add canonical properties to schema cat $tmpfile | yq w - 'title' $(echo $document | yq r - 'metadata.name') | yq w - --tag='!!map' 'properties' | yq w - "${xkgroup}.group" $(echo $document | yq r - 'spec.group') | yq w - "${xkgroup}.kind" $(echo $document | yq r - 'spec.names.kind') | yq w - "${xkgroup}.version" $(echo $document | yq r - 'spec.version') | yq m -x - $openapiFile } crd2jsonschema $1 -
rawc0der revised this gist
Oct 7, 2020 . 1 changed file with 9 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,12 +1,19 @@ # Small utility function based on yq to extract openAPIV3Schema from CRD # example: crd2jsonschema.sh ./crd-alertmanager.yaml function crd2jsonschema() { local xkgroup="x-kubernetes-group-version-kind" local document=$(cat "$1") local openAPIV3Schema=$(echo $document | yq r - 'spec.validation.openAPIV3Schema.properties') local tmpfile=$(mktemp) trap 'rm -f -- "$tmpfile"' INT TERM HUP ERR EXIT cat << 'EOF' > $tmpfile "$schema": "http://json-schema.org/draft/2019-09/schema#" type: object EOF cat $tmpfile | yq w - 'title' $(echo $document | yq r - 'metadata.name') | yq w - 'properties' $openAPIV3Schema | yq w - "${xkgroup}.group" $(echo $document | yq r - 'spec.group') | -
rawc0der created this gist
Oct 7, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ # Small utility function based on yq to extract openAPIV3Schema from CRD # example: crd2jsonschema.sh ./crd-alertmanager.yaml function crd2jsonschema() { local xkgroup="x-kubernetes-group-version-kind" local document=$(cat "$1") local yamlfile="local-schema-for-crd.yaml" local openAPIV3Schema=$(echo $document | yq r - 'spec.validation.openAPIV3Schema.properties') cat $yamlfile | yq w - 'title' $(echo $document | yq r - 'metadata.name') | yq w - 'properties' $openAPIV3Schema | yq w - "${xkgroup}.group" $(echo $document | yq r - 'spec.group') | yq w - "${xkgroup}.kind" $(echo $document | yq r - 'spec.names.kind') | yq w -jP - "${xkgroup}.version" $(echo $document | yq r - 'spec.version') } crd2jsonschema $1