Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 committed Jun 7, 2022
1 parent 3072068 commit 47db706
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 38 deletions.
9 changes: 5 additions & 4 deletions kyaml/openapi/Makefile
Expand Up @@ -6,6 +6,7 @@ ifeq ($(MYGOBIN),)
MYGOBIN = $(shell go env GOPATH)/bin
endif
KIND_VERSION := "v0.11.1"
API_VERSION ?= "v1.21.2"

.PHONY: all
all: \
Expand All @@ -27,7 +28,7 @@ nuke: clean
rm -r kubernetesapi/*

$(MYGOBIN)/go-bindata:
go install github.com/go-bindata/go-bindata/v3/go-bindata
go install github.com/go-bindata/go-bindata/v3/go-bindata@latest

$(MYGOBIN)/kind:
( \
Expand All @@ -45,12 +46,12 @@ kustomizationapi/swagger.go: $(MYGOBIN)/go-bindata kustomizationapi/swagger.json
-o kustomizationapi/swagger.go \
kustomizationapi/swagger.json

.PHONY: kubernetesapi/swagger.json
kubernetesapi/swagger.json: $(MYGOBIN)/kind $(MYGOBIN)/kustomize
.PHONY: kubernetesapi/swagger.pb
kubernetesapi/swagger.pb: $(MYGOBIN)/kind $(MYGOBIN)/kustomize
./scripts/fetchSchemaFromCluster.sh $(API_VERSION)

.PHONY: kubernetesapi/swagger.go
kubernetesapi/swagger.go: $(MYGOBIN)/go-bindata kubernetesapi/swagger.json
kubernetesapi/swagger.go: $(MYGOBIN)/go-bindata kubernetesapi/swagger.pb
./scripts/generateSwaggerDotGo.sh $(API_VERSION)

$(MYGOBIN)/kustomize:
Expand Down
75 changes: 44 additions & 31 deletions kyaml/openapi/README.md
Expand Up @@ -22,9 +22,9 @@ make nuke

The compiled-in schema version should maximize API availability with respect to all actively supported Kubernetes versions. For example, while 1.20, 1.21 and 1.22 are the actively supported versions, 1.21 is the best choice. This is because 1.21 introduces at least one new API and does not remove any, while 1.22 removes a large set of long-deprecated APIs that are still supported in 1.20/1.21.

### Update the built-in schema to a new version
### Generating additional schema

In the Makefile in this directory, update the `API_VERSION` to your desired version.
If you'd like to change the default schema version, then in the Makefile in this directory, update the `API_VERSION` to your desired version.

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).

Expand All @@ -35,59 +35,72 @@ corresponding swagger.go for the kubernetes api:
make all
```

The above command will update the [OpenAPI schema] and the [Kustomization schema]. It will
create a directory kubernetesapi/v1212 and store the resulting
swagger.json and swagger.go files there.

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

To avoid expensive schema lookups, some functions have precomputed results based on the schema. Unit tests
ensure these are kept in sync with the schema; if these tests fail you will need to follow the suggested diff
to update the precomputed results.
### Updating the builtin versions

### Run all tests
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).

At the top of the repository, run the tests.
Here is an example of what it looks like with v1.21.2.

```
make prow-presubmit-check >& /tmp/k.txt; echo $?
```
package kubernetesapi
The exit code should be zero; if not, examine `/tmp/k.txt`.
import (
"sigs.k8s.io/kustomize/kyaml/openapi/kubernetesapi/v1212"
)
## Generating additional schemas
const Info = "{title:Kubernetes,version:v1.21.2}"
Instead of replacing the default version, you can specify a desired version as part of the make invocation:
var OpenAPIMustAsset = map[string]func(string) []byte{
"v1212": v1212.MustAsset,
}
```
rm kubernetesapi/swagger.go
make kubernetesapi/swagger.go API_VERSION=v1.21.2
const DefaultOpenAPI = "v1212"
```

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, update the
file [`kubernetesapi/openapiinfo.go`](https://github.com/kubernetes-sigs/kustomize/blob/master/kyaml/openapi/kubernetesapi/openapiinfo.go).
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 what it looks like with v1.21.2. The version number needs to be updated in all five places that it appears:
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/v1212"
"sigs.k8s.io/kustomize/kyaml/openapi/kubernetesapi/v1215"
)
const Info = "{title:Kubernetes,version:v1.21.2}"
const Info = "{title:Kubernetes,version:v1.21.2},{title:Kubernetes,version:v1.21.5}"
var OpenAPIMustAsset = map[string]func(string) []byte{
"v1212": v1212.MustAsset,
"v1215": v1215.MustAsset,
}
const DefaultOpenAPI = "v1212"
const DefaultOpenAPI = "v1215"
```


#### Precomputations

To avoid expensive schema lookups, some functions have precomputed results based on the schema. Unit tests
ensure these are kept in sync with the schema; if these tests fail you will need to follow the suggested diff
to update the precomputed results.

### Run all tests

At the top of the repository, run the tests.

```
make prow-presubmit-check >& /tmp/k.txt; echo $?
```

The exit code should be zero; if not, examine `/tmp/k.txt`.

## Partial regeneration

You can also regenerate the kubernetes api schemas specifically with:
Expand All @@ -101,8 +114,8 @@ To fetch the schema without generating the swagger.go, you can
run:

```
rm kubernetesapi/swagger.json
make kubernetesapi/swagger.json
rm kubernetesapi/swagger.pb
make kubernetesapi/swagger.pb
```

Note that generating the swagger.go will re-fetch the schema.
6 changes: 3 additions & 3 deletions kyaml/openapi/openapi.go
Expand Up @@ -400,7 +400,7 @@ func SuppressBuiltInSchemaUse() {

// Elements returns the Schema for the elements of an array.
func (rs *ResourceSchema) Elements() *ResourceSchema {
// load the schema from swagger.json
// load the schema from swagger files
initSchema()

if len(rs.Schema.Type) != 1 || rs.Schema.Type[0] != "array" {
Expand Down Expand Up @@ -446,7 +446,7 @@ func (rs *ResourceSchema) Lookup(path ...string) *ResourceSchema {

// Field returns the Schema for a field.
func (rs *ResourceSchema) Field(field string) *ResourceSchema {
// load the schema from swagger.json
// load the schema from swagger files
initSchema()

// locate the Schema
Expand All @@ -459,7 +459,7 @@ func (rs *ResourceSchema) Field(field string) *ResourceSchema {
// (the key doesn't matter, they all have the same value type)
s = *rs.Schema.AdditionalProperties.Schema
default:
// no Schema found from either swagger.json or line comments
// no Schema found from either swagger files or line comments
return nil
}

Expand Down

0 comments on commit 47db706

Please sign in to comment.