Skip to content

Commit e126d16

Browse files
markmandelRobert Bailey
andauthored
Fix for failing export-openapi.sh on K8s 1.19 (agones-dev#2160)
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 agones-dev#2129 Fixes agones-dev#2159 Co-authored-by: Robert Bailey <robertbailey@google.com>
1 parent 2d135a8 commit e126d16

5 files changed

Lines changed: 1340 additions & 83 deletions

File tree

‎build/README.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,10 @@ Compile the ping binary and then build the docker image
574574
#### `make gen-install`
575575
Generate the `/install/yaml/install.yaml` from the Helm template
576576

577-
#### `gen-embedded-openapi`
578-
Generate the embedded OpenAPI specs for existing Kubernetes Objects, such as `PodTemplateSpec` and `ObjectMeta`
577+
#### `make gen-embedded-openapi`
578+
Generate the embedded OpenAPI specs for existing Kubernetes Objects, such as `PodTemplateSpec` and `ObjectMeta`.
579+
580+
This should be run against a clean or brand new cluster, as external CRD's or schemas could cause errors to occur.
579581

580582
#### `make gen-crd-client`
581583
Generate the Custom Resource Definition client(s)

‎build/export-openapi.sh‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ set -o errexit
2020
set -o nounset
2121
set -o pipefail
2222

23+
echo
24+
echo "⚠ If you run into errors running this script, please remove anything you have installed in your cluster, or create a brand new cluster ⚠"
25+
echo
26+
2327
do_expand() {
2428
echo "Processing $1"
2529
jq '.definitions."'"$1"'"' ./openapi.json >"$1.json"
@@ -53,14 +57,16 @@ chmod +x ./yq
5357

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

5963
# do any editing you need to do to any types here, before they are expanded.
6064
# creationTimestamp is defaulted to it's zero value, which gets serialised as "null", so we have to set it as nullable.
6165
jq '.properties.creationTimestamp.nullable = true' io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta.json | sponge io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta.json
6266
# Make IntOrString type have `x-kubernetes-int-or-string` member
6367
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
68+
# This has a \\ in a description field that is being extra tricky to resolve, so let's just escape it here.
69+
sed -i 's/\\\\/\\\\\\\\/g' io.k8s.api.core.v1.PodSpec.json
6470

6571
# easier debugging if something goes wrong, still have the original
6672
mkdir orig
@@ -74,7 +80,8 @@ for f in *.json; do
7480
# any "foo\nbar" values need their \n escaped
7581
# remove top and bottom line to get rid of first { and last } (we know all are formatted because jq)
7682
# then format for multiline - replace real multilines breaks with \n
77-
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')
83+
# and escape bash and sed special characters, such as $ and &.
84+
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')
7885
ref=$(basename "$f" .json)
7986

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

0 commit comments

Comments
 (0)