Skip to content

Commit

Permalink
Fix for failing export-openapi.sh on K8s 1.19
Browse files Browse the repository at this point in the history
Some new documentation in 1.19 meant that there was some extra work to
be done to escape certain strings properly.

This does that, and also provides the updated output for
`PodTemplateSpec` and `ObjectMeta`.

Work on googleforgames#2129
Fixes googleforgames#2159
  • Loading branch information
markmandel committed Jun 25, 2021
1 parent 75db964 commit f332bbc
Show file tree
Hide file tree
Showing 4 changed files with 345 additions and 24 deletions.
6 changes: 4 additions & 2 deletions build/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -574,8 +574,10 @@ Compile the ping binary and then build the docker image
#### `make gen-install`
Generate the `/install/yaml/install.yaml` from the Helm template

#### `gen-embedded-openapi`
Generate the embedded OpenAPI specs for existing Kubernetes Objects, such as `PodTemplateSpec` and `ObjectMeta`
#### `make gen-embedded-openapi`
Generate the embedded OpenAPI specs for existing Kubernetes Objects, such as `PodTemplateSpec` and `ObjectMeta`.

This should be run against a clean or brand new cluster, as external CRD's or schemas could cause errors to occur.

#### `make gen-crd-client`
Generate the Custom Resource Definition client(s)
Expand Down
11 changes: 9 additions & 2 deletions build/export-openapi.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ set -o errexit
set -o nounset
set -o pipefail

echo
echo "⚠ If you run into errors running this script, please remove anything you have installed in your cluster, or create a brand new cluster ⚠"
echo

do_expand() {
echo "Processing $1"
jq '.definitions."'"$1"'"' ./openapi.json >"$1.json"
Expand Down Expand Up @@ -53,14 +57,16 @@ chmod +x ./yq

# cleanup and format
curl http://127.0.0.1:8001/openapi/v2 | jq 'del(.. | .["x-kubernetes-patch-strategy"]?, .["x-kubernetes-patch-merge-key"]?, .["x-kubernetes-list-type"]?)' |
jq 'del(.. | .["x-kubernetes-group-version-kind"]?, .["x-kubernetes-list-map-keys"]? )' >openapi.json
jq 'del(.. | .["x-kubernetes-group-version-kind"]?, .["x-kubernetes-list-map-keys"]?, .["x-kubernetes-unions"]? )' >openapi.json
do_expand "io.k8s.api.core.v1.PodTemplateSpec"

# do any editing you need to do to any types here, before they are expanded.
# creationTimestamp is defaulted to it's zero value, which gets serialised as "null", so we have to set it as nullable.
jq '.properties.creationTimestamp.nullable = true' io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta.json | sponge io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta.json
# Make IntOrString type have `x-kubernetes-int-or-string` member
jq '.["x-kubernetes-int-or-string"] = true' io.k8s.apimachinery.pkg.util.intstr.IntOrString.json | jq 'del(.type)' | sponge io.k8s.apimachinery.pkg.util.intstr.IntOrString.json
# This has a \\ in a description field that is being extra tricky to resolve, so let's just escape it here.
sed -i 's/\\\\/\\\\\\\\/g' io.k8s.api.core.v1.PodSpec.json

# easier debugging if something goes wrong, still have the original
mkdir orig
Expand All @@ -74,7 +80,8 @@ for f in *.json; do
# any "foo\nbar" values need their \n escaped
# remove top and bottom line to get rid of first { and last } (we know all are formatted because jq)
# then format for multiline - replace real multilines breaks with \n
contents=$(cat "$f" | jq 'del(.description)' | sed 's/\\n/\\\\n/g' | sed '$d' | sed '1d' | sed ':a;N;$!ba;s/\n/\\n/g' | sed 's/\$/\\$/g' | sed 's@\"@\\"@g')
# and escape bash and sed special characters, such as $ and &.
contents=$(cat "$f" | jq 'del(.description)' | sed 's/\\n/\\\\n/g' | sed '$d' | sed '1d' | sed ':a;N;$!ba;s/\n/\\n/g' | sed 's/\$/\\$/g' | sed 's/\&/\\&/g' | sed 's@\"@\\"@g')
ref=$(basename "$f" .json)

find -maxdepth 1 -name '*.json' | xargs sed -i 's@"$ref": "#/definitions/'"$ref"'"@'"$contents"'@g'
Expand Down
Loading

0 comments on commit f332bbc

Please sign in to comment.