Skip to content

Instantly share code, notes, and snippets.

@rawc0der
Last active August 1, 2025 04:22
Show Gist options
  • Select an option

  • Save rawc0der/040670cd69c4132f7d6aeb56f6668048 to your computer and use it in GitHub Desktop.

Select an option

Save rawc0der/040670cd69c4132f7d6aeb56f6668048 to your computer and use it in GitHub Desktop.

Revisions

  1. rawc0der revised this gist Oct 9, 2020. 1 changed file with 16 additions and 5 deletions.
    21 changes: 16 additions & 5 deletions crd2jsonschema.sh
    Original file line number Diff line number Diff line change
    @@ -41,26 +41,37 @@ EOF

    # optimized version
    function crd2jsonschemaJq() {
    set -e
    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)
    # 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')
    if [[ -n $hasSchema ]]; then
    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
    "x-kubernetes-group-version-kind.version": .spec.version
    }
    EOF
    echo $jsonCRD | jq -S -f "$queryExpression"
    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
    }

  2. rawc0der revised this gist Oct 8, 2020. No changes.
  3. rawc0der revised this gist Oct 8, 2020. 1 changed file with 28 additions and 1 deletion.
    29 changes: 28 additions & 1 deletion crd2jsonschema.sh
    Original file line number Diff line number Diff line change
    @@ -38,4 +38,31 @@ EOF

    }

    crd2jsonschema $1

    # 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
  4. rawc0der revised this gist Oct 8, 2020. 1 changed file with 24 additions and 17 deletions.
    41 changes: 24 additions & 17 deletions crd2jsonschema.sh
    Original 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=$(cat "${@:1}")
    local document="${1}"
    local openapiFile=$(mktemp)
    local tmpfile=$(mktemp)
    local document="$1"
    local openAPIV3Schema=$(mktemp -u)
    local baseSchema=$(mktemp -u)
    local jsonSchema=$(mktemp -u)
    # clean on exit
    trap 'rm -f -- "$tmpfile" "$openapiFile"' INT TERM HUP ERR EXIT
    trap 'rm -f -- "$baseSchema" "$openAPIV3Schema" "$jsonSchema"' INT TERM HUP ERR EXIT

    # extract openapi schema from crd
    yq r $1 'spec.validation.openAPIV3Schema' > $openapiFile
    # create initial schema file
    cat << 'EOF' > $tmpfile
    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
    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

    # 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
  5. rawc0der revised this gist Oct 8, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions crd2jsonschema.sh
    Original 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
  6. rawc0der revised this gist Oct 8, 2020. 1 changed file with 8 additions and 8 deletions.
    16 changes: 8 additions & 8 deletions crd2jsonschema.sh
    Original 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=$(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
    echo $document | yq r - 'spec.validation.openAPIV3Schema' > $openapiFile
    # local openAPIV3Schema=$(cat $openapiFile | yq r -P - )

    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' $(echo $document | yq r - 'metadata.name') |
    yq w - 'title' $(yq r $document '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 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

    }
  7. rawc0der revised this gist Oct 7, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions crd2jsonschema.sh
    Original 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.properties' | yq p -P - properties > $openapiFile
    local openAPIV3Schema=$(cat $openapiFile | yq r -P - properties)
    echo $document | yq r - 'spec.validation.openAPIV3Schema' > $openapiFile
    # local openAPIV3Schema=$(cat $openapiFile | yq r -P - )

    # create initial schema file
    cat << 'EOF' > $tmpfile
  8. rawc0der revised this gist Oct 7, 2020. 1 changed file with 14 additions and 4 deletions.
    18 changes: 14 additions & 4 deletions crd2jsonschema.sh
    Original 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 openAPIV3Schema=$(echo $document | yq r - 'spec.validation.openAPIV3Schema.properties')
    local openapiFile=$(mktemp)
    local tmpfile=$(mktemp)
    trap 'rm -f -- "$tmpfile"' INT TERM HUP ERR EXIT
    # 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 - 'properties' $openAPIV3Schema |
    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 -jP - "${xkgroup}.version" $(echo $document | yq r - 'spec.version')
    yq w - "${xkgroup}.version" $(echo $document | yq r - 'spec.version') |
    yq m -x - $openapiFile

    }

    crd2jsonschema $1
  9. rawc0der revised this gist Oct 7, 2020. 1 changed file with 9 additions and 2 deletions.
    11 changes: 9 additions & 2 deletions crd2jsonschema.sh
    Original 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 yamlfile="local-schema-for-crd.yaml"
    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 $yamlfile |
    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') |
  10. rawc0der created this gist Oct 7, 2020.
    16 changes: 16 additions & 0 deletions crd2jsonschema.sh
    Original 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