Skip to content

Commit

Permalink
restore makeOpenApiInfoDotGo.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 committed Aug 4, 2022
1 parent 7f4ac84 commit f869229
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 52 deletions.
4 changes: 4 additions & 0 deletions kyaml/openapi/Makefile
Expand Up @@ -40,6 +40,10 @@ $(MYGOBIN)/kind:
rm -rf $$d; \
)

.PHONY: kubernetesapi/openapiinfo.go
kubernetesapi/openapiinfo.go:
./scripts/makeOpenApiInfoDotGo.sh

kustomizationapi/swagger.go: $(MYGOBIN)/go-bindata kustomizationapi/swagger.json
$(MYGOBIN)/go-bindata \
--pkg kustomizationapi \
Expand Down
58 changes: 6 additions & 52 deletions kyaml/openapi/README.md
Expand Up @@ -28,65 +28,19 @@ If you'd like to change the default schema version, then in the Makefile in this

You may need to update the version of Kind these scripts use by changing `KIND_VERSION` in the Makefile in this directory. You can find compatibility information in the [kind release notes](https://github.com/kubernetes-sigs/kind/releases).

In this directory, fetch the openapi schema and generate the
corresponding swagger.go for the kubernetes api:
In this directory, fetch the openapi schema, generate the
corresponding swagger.go for the kubernetes api, and update `kubernetesapi/openapiinfo.go`:

```
make all
make kustomizationapi/swagger.go
make kubernetesapi/swagger.go
make kubernetesapi/openapiinfo.go
```

Then, follow the instructions in the next section to make the newly generated schema available for use.

You can optionally delete the old `swagger.pb` and `swagger.go` files if we no longer need to support that kubernetes version of
openapi data.

### Updating the builtin versions

The above command will update the [OpenAPI schema] and the [Kustomization schema]. It will
create a directory kubernetesapi/v1_21_2 and store the resulting
swagger.pb and swagger.go files there. You will then have to manually update
[`kubernetesapi/openapiinfo.go`](https://github.com/kubernetes-sigs/kustomize/blob/master/kyaml/openapi/kubernetesapi/openapiinfo.go).

Here is an example of what it looks like with v1.21.2.

```
package kubernetesapi
import (
"sigs.k8s.io/kustomize/kyaml/openapi/kubernetesapi/v1_21_2"
)
const Info = "{title:Kubernetes,version:v1.21.2}"
var OpenAPIMustAsset = map[string]func(string) []byte{
"v1212": v1_21_2.MustAsset,
}
const DefaultOpenAPI = "v1.21.2"
```

You need to replace the version number in all five places it appears. If you would like to keep the old version as an option,
and just update the default, you can append a new version number to all lists and change the default.

Here is an example of both v1.21.2 and v1.21.5 being available to use, but v1.21.5 being the default:

```
package kubernetesapi
import (
"sigs.k8s.io/kustomize/kyaml/openapi/kubernetesapi/v1_21_2"
"sigs.k8s.io/kustomize/kyaml/openapi/kubernetesapi/v1_21_5"
)
const Info = "{title:Kubernetes,version:v1.21.2},{title:Kubernetes,version:v1.21.5}"
var OpenAPIMustAsset = map[string]func(string) []byte{
"v1.21.2": v1_21_2.MustAsset,
"v1.21.5": v1_21_5.MustAsset,
}
const DefaultOpenAPI = "v1.21.5"
```
openapi data. Make sure you rerun `make kubernetesapi/openapiinfo.go` after deleting any old schemas.


#### Precomputations
Expand Down
2 changes: 2 additions & 0 deletions kyaml/openapi/kubernetesapi/openapiinfo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions kyaml/openapi/scripts/fetchSchemaFromCluster.sh
Expand Up @@ -11,6 +11,7 @@ $MYGOBIN/kind create cluster --image kindest/node:$VERSION --name=getopenapidata

# TODO (natasha41575) Add a `kustomize openapi fetch --proto` option
kubectl proxy &
sleep 5
curl -k -H "Accept: application/com.github.proto-openapi.spec.v2@v1.0+protobuf" http://localhost:8001/openapi/v2 > /tmp/new_swagger.pb

$MYGOBIN/kind delete cluster --name=getopenapidata
Expand Down
81 changes: 81 additions & 0 deletions kyaml/openapi/scripts/makeOpenApiInfoDotGo.sh
@@ -0,0 +1,81 @@
#!/bin/bash
# Copyright 2020 The Kubernetes Authors.
# SPDX-License-Identifier: Apache-2.0

# This will read from the directory kubernetesapi
# and use subdirectory names to generate
# kubernetesapi/openapiinfo.go
#
# This script should only be run after the
# swagger.json and swagger.go files are generated.

set -e

if ! command -v jq &> /dev/null ; then
echo Please install jq
echo on ubuntu: sudo apt-get install jq
exit 1
fi

info_list=()
version_list=()

V=`ls kubernetesapi | grep v.*`
for VERSION in $V
do
openapiinfo="{title:Kubernetes,version:${VERSION//_/.}}"
info_list+=( $openapiinfo )
version_list+=( ${VERSION} )
done


# add imports to openapiinfo.go
cat <<EOF >kubernetesapi/openapiinfo.go
// Copyright 2020 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
// Code generated by $0; DO NOT EDIT.
package kubernetesapi
import (
EOF

for version in ${version_list[@]}
do
cat <<EOF >>kubernetesapi/openapiinfo.go
"sigs.k8s.io/kustomize/kyaml/openapi/kubernetesapi/$version"
EOF
done

# add info string for `kustomize openapi info` command
OPEN_API_INFO=`echo ${info_list[@]} | sed 's/ /\\\n/g'`
cat <<EOF >>kubernetesapi/openapiinfo.go
)
const Info = "$OPEN_API_INFO"
EOF

# add map for `initSchema` in openapi.go to use
cat <<EOF >>kubernetesapi/openapiinfo.go
var OpenAPIMustAsset = map[string]func(string)[]byte{
EOF

latest=""
for version in ${version_list[@]}
do
latest=$version
cat <<EOF >>kubernetesapi/openapiinfo.go
"${version//_/.}": $version.MustAsset,
EOF
done

# add latest version to be used as a default
cat <<EOF >>kubernetesapi/openapiinfo.go
}
const DefaultOpenAPI = "${latest//_/.}"
EOF

gofmt -s -w kubernetesapi/openapiinfo.go

0 comments on commit f869229

Please sign in to comment.