Skip to content

Commit

Permalink
update openapi scripts to fetch protobuffer schema
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 committed Apr 14, 2022
1 parent a464ed0 commit 1b23231
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 98 deletions.
5 changes: 0 additions & 5 deletions kyaml/openapi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ MYGOBIN = $(shell go env GOBIN)
ifeq ($(MYGOBIN),)
MYGOBIN = $(shell go env GOPATH)/bin
endif
API_VERSION := "v1.21.2"
KIND_VERSION := "v0.11.1"

.PHONY: all
Expand Down Expand Up @@ -46,10 +45,6 @@ kustomizationapi/swagger.go: $(MYGOBIN)/go-bindata kustomizationapi/swagger.json
-o kustomizationapi/swagger.go \
kustomizationapi/swagger.json

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

.PHONY: kubernetesapi/swagger.json
kubernetesapi/swagger.json: $(MYGOBIN)/kind $(MYGOBIN)/kustomize
./scripts/fetchSchemaFromCluster.sh $(API_VERSION)
Expand Down
20 changes: 17 additions & 3 deletions kyaml/openapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,25 @@ make kubernetesapi/swagger.go API_VERSION=v1.21.2
While the above commands generate the swagger.go files, they
do not make them available for use nor do they update the
info field reported by `kustomize openapi info`. To make the
newly fetched schema and swagger.go available:
newly fetched schema and swagger.go available, update the
file [`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. The version number needs to be updated in all five places that it appears:

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

## Partial regeneration
Expand Down
8 changes: 5 additions & 3 deletions kyaml/openapi/openapi_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"google.golang.org/protobuf/proto"
"k8s.io/kube-openapi/pkg/validation/spec"
"sigs.k8s.io/kustomize/kyaml/openapi/kubernetesapi"
"sigs.k8s.io/kustomize/kyaml/openapi/kubernetesapi/v1218pb"
)

// Benchmark for swagger parsing (UnmarshalJSON)
Expand Down Expand Up @@ -53,12 +52,15 @@ func BenchmarkOpenAPIV2ParseDocument(t *testing.B) {
}

func BenchmarkProtoUnmarshal(t *testing.B) {
version := kubernetesOpenAPIDefaultVersion

// parse the swagger, this should never fail
assetName := filepath.Join(
"kubernetesapi",
"v1218pb",
version,
"swagger.pb")

b := v1218pb.MustAsset(assetName)
b := kubernetesapi.OpenAPIMustAsset[version](assetName)

for i := 0; i < t.N; i++ {
// We parse protobuf and get an openapiv2.Document here.
Expand Down
8 changes: 6 additions & 2 deletions kyaml/openapi/scripts/fetchSchemaFromCluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ VERSION=$1

cp $HOME/.kube/config /tmp/kubeconfig.txt | true
$MYGOBIN/kind create cluster --image kindest/node:$VERSION --name=getopenapidata
$MYGOBIN/kustomize openapi fetch > /tmp/new_swagger.json

# TODO (natasha41575) Add a `kustomize openapi fetch --proto` option
kubectl proxy &
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
cp /tmp/kubeconfig.txt $HOME/.kube/config | true
mkdir -p kubernetesapi/"${VERSION//.}"
cp /tmp/new_swagger.json kubernetesapi/"${VERSION//.}"/swagger.json
cp /tmp/new_swagger.pb kubernetesapi/"${VERSION//.}"/swagger.pb
2 changes: 1 addition & 1 deletion kyaml/openapi/scripts/generateSwaggerDotGo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ VERSION=$1
$MYGOBIN/go-bindata \
--pkg "${VERSION//.}" \
-o kubernetesapi/"${VERSION//.}"/swagger.go \
kubernetesapi/"${VERSION//.}"/swagger.json
kubernetesapi/"${VERSION//.}"/swagger.pb
84 changes: 0 additions & 84 deletions kyaml/openapi/scripts/makeOpenApiInfoDotGo.sh

This file was deleted.

0 comments on commit 1b23231

Please sign in to comment.