mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
edited kyaml libraries to use the openapi field from the kustomization file
This commit is contained in:
@@ -15,7 +15,7 @@ import (
|
|||||||
|
|
||||||
const Info = "{title:Kubernetes,version:v1.18.4}\n{title:Kubernetes,version:v1.18.6}\n{title:Kubernetes,version:v1.18.8}\n{title:Kubernetes,version:v1.19.0}\n{title:Kubernetes,version:v1.19.1}"
|
const Info = "{title:Kubernetes,version:v1.18.4}\n{title:Kubernetes,version:v1.18.6}\n{title:Kubernetes,version:v1.18.8}\n{title:Kubernetes,version:v1.19.0}\n{title:Kubernetes,version:v1.19.1}"
|
||||||
|
|
||||||
var OpenApiMustAsset = map[string]func(string) []byte{
|
var OpenAPIMustAsset = map[string]func(string) []byte{
|
||||||
"v1184": v1184.MustAsset,
|
"v1184": v1184.MustAsset,
|
||||||
"v1186": v1186.MustAsset,
|
"v1186": v1186.MustAsset,
|
||||||
"v1188": v1188.MustAsset,
|
"v1188": v1188.MustAsset,
|
||||||
@@ -23,4 +23,4 @@ var OpenApiMustAsset = map[string]func(string) []byte{
|
|||||||
"v1191": v1191.MustAsset,
|
"v1191": v1191.MustAsset,
|
||||||
}
|
}
|
||||||
|
|
||||||
const DefaultOpenApi = "v1191"
|
const DefaultOpenAPI = "v1191"
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/go-openapi/spec"
|
"github.com/go-openapi/spec"
|
||||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||||
@@ -22,14 +21,30 @@ import (
|
|||||||
// globalSchema contains global state information about the openapi
|
// globalSchema contains global state information about the openapi
|
||||||
var globalSchema openapiData
|
var globalSchema openapiData
|
||||||
|
|
||||||
|
// kubernetesOpenAPIVersion specifies which builtin kubernetes schema to use
|
||||||
|
var kubernetesOpenAPIVersion string
|
||||||
|
|
||||||
// openapiData contains the parsed openapi state. this is in a struct rather than
|
// openapiData contains the parsed openapi state. this is in a struct rather than
|
||||||
// a list of vars so that it can be reset from tests.
|
// a list of vars so that it can be reset from tests.
|
||||||
type openapiData struct {
|
type openapiData struct {
|
||||||
setup sync.Once
|
// schema holds the OpenAPI schema data
|
||||||
schema spec.Schema
|
schema spec.Schema
|
||||||
schemaByResourceType map[yaml.TypeMeta]*spec.Schema
|
|
||||||
|
// schemaForResourceType is a map of Resource types to their schemas
|
||||||
|
schemaByResourceType map[yaml.TypeMeta]*spec.Schema
|
||||||
|
|
||||||
|
// namespaceabilityByResourceType stores whether a given Resource type
|
||||||
|
// is namespaceable or not
|
||||||
namespaceabilityByResourceType map[yaml.TypeMeta]bool
|
namespaceabilityByResourceType map[yaml.TypeMeta]bool
|
||||||
noUseBuiltInSchema bool
|
|
||||||
|
// noUseBuiltInSchema stores whether we want to prevent using the built-n
|
||||||
|
// Kubernetes schema as part of the global schema
|
||||||
|
noUseBuiltInSchema bool
|
||||||
|
|
||||||
|
// currentOpenAPIVersion stores the version if the kubernetes openapi data
|
||||||
|
// that is currently stored as the schema, so that we only reparse the
|
||||||
|
// schema when necessary (to speed up performance)
|
||||||
|
currentOpenAPIVersion string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ResourceSchema wraps the OpenAPI Schema.
|
// ResourceSchema wraps the OpenAPI Schema.
|
||||||
@@ -387,9 +402,9 @@ func (rs *ResourceSchema) PatchStrategyAndKey() (string, string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// kubernetesAPIDefaultVersion is the latest version number of the statically compiled in
|
// kubernetesOpenAPIDefaultVersion is the latest version number of the statically compiled in
|
||||||
// OpenAPI schema for kubernetes built-in types
|
// OpenAPI schema for kubernetes built-in types
|
||||||
kubernetesAPIDefaultVersion = kubernetesapi.DefaultOpenApi
|
kubernetesOpenAPIDefaultVersion = kubernetesapi.DefaultOpenAPI
|
||||||
|
|
||||||
// kustomizationAPIAssetName is the name of the asset containing the statically compiled in
|
// kustomizationAPIAssetName is the name of the asset containing the statically compiled in
|
||||||
// OpenAPI definitions for Kustomization built-in types
|
// OpenAPI definitions for Kustomization built-in types
|
||||||
@@ -419,29 +434,65 @@ const (
|
|||||||
kindKey = "kind"
|
kindKey = "kind"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// SetSchemaVersion sets the kubernetes OpenAPI schema version to use
|
||||||
|
func SetSchemaVersion(openAPIField map[string]string, reset bool) error {
|
||||||
|
// this should only be set once
|
||||||
|
if kubernetesOpenAPIVersion != "" && !reset {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
kubernetesOpenAPIVersion = strings.ReplaceAll(openAPIField["version"], ".", "")
|
||||||
|
if kubernetesOpenAPIVersion == "" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if _, ok := kubernetesapi.OpenAPIMustAsset[kubernetesOpenAPIVersion]; !ok {
|
||||||
|
return fmt.Errorf("the specified OpenAPI version is not built in")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSchemaVersion returns what kubernetes OpenAPI version is being used
|
||||||
|
func GetSchemaVersion() string {
|
||||||
|
if kubernetesOpenAPIVersion == "" {
|
||||||
|
return kubernetesOpenAPIDefaultVersion
|
||||||
|
}
|
||||||
|
return kubernetesOpenAPIVersion
|
||||||
|
}
|
||||||
|
|
||||||
// initSchema parses the json schema
|
// initSchema parses the json schema
|
||||||
func initSchema() {
|
func initSchema() {
|
||||||
globalSchema.setup.Do(func() {
|
currentVersion := kubernetesOpenAPIVersion
|
||||||
if globalSchema.noUseBuiltInSchema {
|
if currentVersion == "" {
|
||||||
// don't parse the built in schema
|
currentVersion = kubernetesOpenAPIDefaultVersion
|
||||||
return
|
}
|
||||||
}
|
if globalSchema.currentOpenAPIVersion != currentVersion {
|
||||||
|
parseSchema(currentVersion)
|
||||||
|
}
|
||||||
|
globalSchema.currentOpenAPIVersion = currentVersion
|
||||||
|
}
|
||||||
|
|
||||||
// parse the swagger, this should never fail
|
// parseSchema calls parse to parse the json schemas
|
||||||
assetName := filepath.Join(
|
func parseSchema(version string) {
|
||||||
"kubernetesapi",
|
if globalSchema.noUseBuiltInSchema {
|
||||||
kubernetesAPIDefaultVersion,
|
// don't parse the built in schema
|
||||||
"swagger.json")
|
return
|
||||||
if err := parse(kubernetesapi.OpenApiMustAsset[kubernetesAPIDefaultVersion](assetName)); err != nil {
|
}
|
||||||
// this should never happen
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := parse(kustomizationapi.MustAsset(kustomizationAPIAssetName)); err != nil {
|
// parse the swagger, this should never fail
|
||||||
// this should never happen
|
assetName := filepath.Join(
|
||||||
panic(err)
|
"kubernetesapi",
|
||||||
}
|
version,
|
||||||
})
|
"swagger.json")
|
||||||
|
|
||||||
|
if err := parse(kubernetesapi.OpenAPIMustAsset[version](assetName)); err != nil {
|
||||||
|
// this should never happen
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := parse(kustomizationapi.MustAsset(kustomizationAPIAssetName)); err != nil {
|
||||||
|
// this should never happen
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// parse parses and indexes a single json schema
|
// parse parses and indexes a single json schema
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ EOF
|
|||||||
# add map for `initSchema` in openapi.go to use
|
# add map for `initSchema` in openapi.go to use
|
||||||
cat <<EOF >>kubernetesapi/openapiinfo.go
|
cat <<EOF >>kubernetesapi/openapiinfo.go
|
||||||
|
|
||||||
var OpenApiMustAsset = map[string]func(string)[]byte{
|
var OpenAPIMustAsset = map[string]func(string)[]byte{
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
latest=""
|
latest=""
|
||||||
@@ -78,7 +78,7 @@ done
|
|||||||
cat <<EOF >>kubernetesapi/openapiinfo.go
|
cat <<EOF >>kubernetesapi/openapiinfo.go
|
||||||
}
|
}
|
||||||
|
|
||||||
const DefaultOpenApi = "$latest"
|
const DefaultOpenAPI = "$latest"
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user