Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 committed Aug 4, 2022
1 parent 08bb228 commit 599acda
Show file tree
Hide file tree
Showing 10 changed files with 30,320 additions and 30,314 deletions.
4 changes: 2 additions & 2 deletions api/krusty/openapitests/openapicustomschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ spec:
- image: nginx
name: server
`)
assert.Equal(t, "v1212", openapi.GetSchemaVersion())
assert.Equal(t, "v1.21.2", openapi.GetSchemaVersion())
})
}

Expand Down Expand Up @@ -580,7 +580,7 @@ spec:
containers:
- image: whatever
`)
assert.Equal(t, "v1212", openapi.GetSchemaVersion())
assert.Equal(t, "v1.21.2", openapi.GetSchemaVersion())
})
}

Expand Down
18 changes: 11 additions & 7 deletions kyaml/openapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ make all

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
Expand All @@ -50,16 +53,16 @@ Here is an example of what it looks like with v1.21.2.
package kubernetesapi
import (
"sigs.k8s.io/kustomize/kyaml/openapi/kubernetesapi/v1212"
"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": v1212.MustAsset,
"v1212": v1_21_2.MustAsset,
}
const DefaultOpenAPI = "v1212"
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,
Expand All @@ -71,17 +74,18 @@ Here is an example of both v1.21.2 and v1.21.5 being available to use, but v1.21
package kubernetesapi
import (
"sigs.k8s.io/kustomize/kyaml/openapi/kubernetesapi/v1215"
"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{
"v1212": v1212.MustAsset,
"v1215": v1215.MustAsset,
"v1.21.2": v1_21_2.MustAsset,
"v1.21.5": v1_21_5.MustAsset,
}
const DefaultOpenAPI = "v1215"
const DefaultOpenAPI = "v1.21.5"
```


Expand Down
6 changes: 3 additions & 3 deletions kyaml/openapi/kubernetesapi/openapiinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
package kubernetesapi

import (
"sigs.k8s.io/kustomize/kyaml/openapi/kubernetesapi/v1212"
"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": v1212.MustAsset,
"v1.21.2": v1_21_2.MustAsset,
}

const DefaultOpenAPI = "v1212"
const DefaultOpenAPI = "v1.21.2"
249 changes: 0 additions & 249 deletions kyaml/openapi/kubernetesapi/v1212/swagger.go

This file was deleted.

249 changes: 249 additions & 0 deletions kyaml/openapi/kubernetesapi/v1_21_2/swagger.go

Large diffs are not rendered by default.

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions kyaml/openapi/openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,12 +557,13 @@ func SetSchema(openAPIField map[string]string, schema []byte, reset bool) error
return nil
}

version, exists := openAPIField["version"]
if exists && schema != nil {
return fmt.Errorf("builtin version and custom schema provided, cannot use both")
}
version, versionProvided := openAPIField["version"]

if schema != nil { // use custom schema
// use custom schema
if schema != nil {
if versionProvided {
return fmt.Errorf("builtin version and custom schema provided, cannot use both")
}
customSchema = schema
kubernetesOpenAPIVersion = "custom"
// if the schema is changed, initSchema should parse the new schema
Expand All @@ -571,11 +572,12 @@ func SetSchema(openAPIField map[string]string, schema []byte, reset bool) error
}

// use builtin version
kubernetesOpenAPIVersion = strings.ReplaceAll(version, ".", "")
if kubernetesOpenAPIVersion == "" {
return nil
kubernetesOpenAPIVersion = version
if version == "" {
version = kubernetesapi.DefaultOpenAPI
}
if _, ok := kubernetesapi.OpenAPIMustAsset[kubernetesOpenAPIVersion]; !ok {

if _, ok := kubernetesapi.OpenAPIMustAsset[version]; !ok {
return fmt.Errorf("the specified OpenAPI version is not built in")
}
customSchema = nil
Expand Down Expand Up @@ -629,11 +631,10 @@ func parseBuiltinSchema(version string) {
// don't parse the built in schema
return
}

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

if err := parse(kubernetesapi.OpenAPIMustAsset[version](assetName), Proto); err != nil {
Expand Down
3 changes: 2 additions & 1 deletion kyaml/openapi/openapi_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package openapi

import (
"path/filepath"
"strings"
"testing"

openapi_v2 "github.com/google/gnostic/openapiv2"
Expand All @@ -18,7 +19,7 @@ func BenchmarkProtoUnmarshal(t *testing.B) {
// parse the swagger, this should never fail
assetName := filepath.Join(
"kubernetesapi",
version,
strings.ReplaceAll(version, ".", "_"),
"swagger.pb")

b := kubernetesapi.OpenAPIMustAsset[version](assetName)
Expand Down
4 changes: 2 additions & 2 deletions kyaml/openapi/scripts/fetchSchemaFromCluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ curl -k -H "Accept: application/com.github.proto-openapi.spec.v2@v1.0+protobuf"

$MYGOBIN/kind delete cluster --name=getopenapidata
cp /tmp/kubeconfig.txt $HOME/.kube/config | true
mkdir -p kubernetesapi/"${VERSION//.}"
cp /tmp/new_swagger.pb kubernetesapi/"${VERSION//.}"/swagger.pb
mkdir -p kubernetesapi/"${VERSION//./_}"
cp /tmp/new_swagger.pb kubernetesapi/"${VERSION//./_}"/swagger.pb
6 changes: 3 additions & 3 deletions kyaml/openapi/scripts/generateSwaggerDotGo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ MYGOBIN="${MYGOBIN:-$(go env GOPATH)/bin}"
VERSION=$1

$MYGOBIN/go-bindata \
--pkg "${VERSION//.}" \
-o kubernetesapi/"${VERSION//.}"/swagger.go \
kubernetesapi/"${VERSION//.}"/swagger.pb
--pkg "${VERSION//./_}" \
-o kubernetesapi/"${VERSION//./_}"/swagger.go \
kubernetesapi/"${VERSION//./_}"/swagger.pb

0 comments on commit 599acda

Please sign in to comment.