prune enum when OpenAPIEnums is disabled.
This commit is contained in:
parent
73ffb49203
commit
9c05de2f31
@ -49,6 +49,7 @@ import (
|
|||||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||||
utilflowcontrol "k8s.io/apiserver/pkg/util/flowcontrol"
|
utilflowcontrol "k8s.io/apiserver/pkg/util/flowcontrol"
|
||||||
"k8s.io/apiserver/pkg/util/notfoundhandler"
|
"k8s.io/apiserver/pkg/util/notfoundhandler"
|
||||||
|
"k8s.io/apiserver/pkg/util/openapi"
|
||||||
"k8s.io/apiserver/pkg/util/webhook"
|
"k8s.io/apiserver/pkg/util/webhook"
|
||||||
clientgoinformers "k8s.io/client-go/informers"
|
clientgoinformers "k8s.io/client-go/informers"
|
||||||
clientgoclientset "k8s.io/client-go/kubernetes"
|
clientgoclientset "k8s.io/client-go/kubernetes"
|
||||||
@ -396,8 +397,9 @@ func buildGenericConfig(
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// wrap the definitions to revert any changes from disabled features
|
||||||
genericConfig.OpenAPIConfig = genericapiserver.DefaultOpenAPIConfig(generatedopenapi.GetOpenAPIDefinitions, openapinamer.NewDefinitionNamer(legacyscheme.Scheme, extensionsapiserver.Scheme, aggregatorscheme.Scheme))
|
getOpenAPIDefinitions := openapi.GetOpenAPIDefinitionsWithoutDisabledFeatures(generatedopenapi.GetOpenAPIDefinitions)
|
||||||
|
genericConfig.OpenAPIConfig = genericapiserver.DefaultOpenAPIConfig(getOpenAPIDefinitions, openapinamer.NewDefinitionNamer(legacyscheme.Scheme, extensionsapiserver.Scheme, aggregatorscheme.Scheme))
|
||||||
genericConfig.OpenAPIConfig.Info.Title = "Kubernetes"
|
genericConfig.OpenAPIConfig.Info.Title = "Kubernetes"
|
||||||
genericConfig.LongRunningFunc = filters.BasicLongRunningRequestCheck(
|
genericConfig.LongRunningFunc = filters.BasicLongRunningRequestCheck(
|
||||||
sets.NewString("watch", "proxy"),
|
sets.NewString("watch", "proxy"),
|
||||||
|
@ -41,6 +41,7 @@ import (
|
|||||||
"k8s.io/apiserver/pkg/endpoints/openapi"
|
"k8s.io/apiserver/pkg/endpoints/openapi"
|
||||||
"k8s.io/apiserver/pkg/features"
|
"k8s.io/apiserver/pkg/features"
|
||||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||||
|
utilopenapi "k8s.io/apiserver/pkg/util/openapi"
|
||||||
openapibuilder "k8s.io/kube-openapi/pkg/builder"
|
openapibuilder "k8s.io/kube-openapi/pkg/builder"
|
||||||
"k8s.io/kube-openapi/pkg/common"
|
"k8s.io/kube-openapi/pkg/common"
|
||||||
"k8s.io/kube-openapi/pkg/util"
|
"k8s.io/kube-openapi/pkg/util"
|
||||||
@ -434,7 +435,7 @@ func withDescription(s spec.Schema, desc string) spec.Schema {
|
|||||||
|
|
||||||
func buildDefinitionsFunc() {
|
func buildDefinitionsFunc() {
|
||||||
namer = openapi.NewDefinitionNamer(runtime.NewScheme())
|
namer = openapi.NewDefinitionNamer(runtime.NewScheme())
|
||||||
definitions = generatedopenapi.GetOpenAPIDefinitions(func(name string) spec.Ref {
|
definitions = utilopenapi.GetOpenAPIDefinitionsWithoutDisabledFeatures(generatedopenapi.GetOpenAPIDefinitions)(func(name string) spec.Ref {
|
||||||
defName, _ := namer.GetDefinitionName(name)
|
defName, _ := namer.GetDefinitionName(name)
|
||||||
return spec.MustCreateRef(definitionPrefix + common.EscapeJsonPointer(defName))
|
return spec.MustCreateRef(definitionPrefix + common.EscapeJsonPointer(defName))
|
||||||
})
|
})
|
||||||
@ -490,7 +491,7 @@ func (b *builder) getOpenAPIConfig() *common.Config {
|
|||||||
return namer.GetDefinitionName(name)
|
return namer.GetDefinitionName(name)
|
||||||
},
|
},
|
||||||
GetDefinitions: func(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition {
|
GetDefinitions: func(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition {
|
||||||
def := generatedopenapi.GetOpenAPIDefinitions(ref)
|
def := utilopenapi.GetOpenAPIDefinitionsWithoutDisabledFeatures(generatedopenapi.GetOpenAPIDefinitions)(ref)
|
||||||
def[fmt.Sprintf("%s/%s.%s", b.group, b.version, b.kind)] = common.OpenAPIDefinition{
|
def[fmt.Sprintf("%s/%s.%s", b.group, b.version, b.kind)] = common.OpenAPIDefinition{
|
||||||
Schema: *b.schema,
|
Schema: *b.schema,
|
||||||
}
|
}
|
||||||
|
83
staging/src/k8s.io/apiserver/pkg/util/openapi/enablement.go
Normal file
83
staging/src/k8s.io/apiserver/pkg/util/openapi/enablement.go
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2021 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package openapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
genericfeatures "k8s.io/apiserver/pkg/features"
|
||||||
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||||
|
"k8s.io/kube-openapi/pkg/common"
|
||||||
|
"k8s.io/kube-openapi/pkg/schemamutation"
|
||||||
|
"k8s.io/kube-openapi/pkg/validation/spec"
|
||||||
|
)
|
||||||
|
|
||||||
|
// enumTypeDescriptionHeader is the header of enum section in schema description.
|
||||||
|
const enumTypeDescriptionHeader = "Possible enum values:"
|
||||||
|
|
||||||
|
// GetOpenAPIDefinitionsWithoutDisabledFeatures wraps a GetOpenAPIDefinitions to revert
|
||||||
|
// any change to the schema that was made by disabled features.
|
||||||
|
func GetOpenAPIDefinitionsWithoutDisabledFeatures(GetOpenAPIDefinitions common.GetOpenAPIDefinitions) common.GetOpenAPIDefinitions {
|
||||||
|
return func(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition {
|
||||||
|
defs := GetOpenAPIDefinitions(ref)
|
||||||
|
restoreDefinitions(defs)
|
||||||
|
return defs
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// restoreDefinitions restores any changes by disabled features from definition map.
|
||||||
|
func restoreDefinitions(defs map[string]common.OpenAPIDefinition) {
|
||||||
|
// revert changes from OpenAPIEnums
|
||||||
|
if !utilfeature.DefaultFeatureGate.Enabled(genericfeatures.OpenAPIEnums) {
|
||||||
|
for gvk, def := range defs {
|
||||||
|
orig := &def.Schema
|
||||||
|
if ret := pruneEnums(orig); ret != orig {
|
||||||
|
def.Schema = *ret
|
||||||
|
defs[gvk] = def
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func pruneEnums(schema *spec.Schema) *spec.Schema {
|
||||||
|
walker := schemamutation.Walker{
|
||||||
|
SchemaCallback: func(schema *spec.Schema) *spec.Schema {
|
||||||
|
orig := schema
|
||||||
|
clone := func() {
|
||||||
|
if orig == schema { // if schema has not been mutated yet
|
||||||
|
schema = new(spec.Schema)
|
||||||
|
*schema = *orig // make a clone from orig to schema
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if headerIndex := strings.Index(schema.Description, enumTypeDescriptionHeader); headerIndex != -1 {
|
||||||
|
// remove the enum section from description.
|
||||||
|
// note that the new lines before the header should be removed too,
|
||||||
|
// thus the slice range.
|
||||||
|
clone()
|
||||||
|
schema.Description = schema.Description[:headerIndex]
|
||||||
|
}
|
||||||
|
if len(schema.Enum) != 0 {
|
||||||
|
// remove the enum field
|
||||||
|
clone()
|
||||||
|
schema.Enum = nil
|
||||||
|
}
|
||||||
|
return schema
|
||||||
|
},
|
||||||
|
RefCallback: schemamutation.RefCallbackNoop,
|
||||||
|
}
|
||||||
|
return walker.WalkSchema(schema)
|
||||||
|
}
|
106
staging/src/k8s.io/apiserver/pkg/util/openapi/enablement_test.go
Normal file
106
staging/src/k8s.io/apiserver/pkg/util/openapi/enablement_test.go
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2021 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package openapi
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"k8s.io/kube-openapi/pkg/common"
|
||||||
|
"k8s.io/kube-openapi/pkg/validation/spec"
|
||||||
|
)
|
||||||
|
|
||||||
|
func schema_k8sio_api_apps_v1_DeploymentCondition(ref common.ReferenceCallback) common.OpenAPIDefinition {
|
||||||
|
return common.OpenAPIDefinition{
|
||||||
|
Schema: spec.Schema{
|
||||||
|
SchemaProps: spec.SchemaProps{
|
||||||
|
Description: "DeploymentCondition describes the state of a deployment at a certain point.",
|
||||||
|
Type: []string{"object"},
|
||||||
|
Properties: map[string]spec.Schema{
|
||||||
|
"type": {
|
||||||
|
SchemaProps: spec.SchemaProps{
|
||||||
|
Description: "Type of deployment condition.\n\nPossible enum values:\n - `Available`: Available means the deployment is available, ie. at least the minimum available replicas required are up and running for at least minReadySeconds.\n - `Progressing`: Progressing means the deployment is progressing. Progress for a deployment is considered when a new replica set is created or adopted, and when new pods scale up or old pods scale down. Progress is not estimated for paused deployments or when progressDeadlineSeconds is not specified.\n - `ReplicaFailure`: ReplicaFailure is added in a deployment when one of its pods fails to be created or deleted.",
|
||||||
|
Default: "",
|
||||||
|
Type: []string{"string"},
|
||||||
|
Format: "",
|
||||||
|
Enum: []interface{}{"Available", "Progressing", "ReplicaFailure"}},
|
||||||
|
},
|
||||||
|
"status": {
|
||||||
|
SchemaProps: spec.SchemaProps{
|
||||||
|
Description: "Status of the condition, one of True, False, Unknown.\n\nPossible enum values:\n - `False`:\n - `True`:\n - `Unknown`:",
|
||||||
|
Default: "",
|
||||||
|
Type: []string{"string"},
|
||||||
|
Format: "",
|
||||||
|
Enum: []interface{}{"False", "True", "Unknown"}},
|
||||||
|
},
|
||||||
|
"lastUpdateTime": {
|
||||||
|
SchemaProps: spec.SchemaProps{
|
||||||
|
Description: "The last time this condition was updated.",
|
||||||
|
Default: map[string]interface{}{},
|
||||||
|
Ref: ref("k8s.io/apimachinery/pkg/apis/meta/v1.Time"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"lastTransitionTime": {
|
||||||
|
SchemaProps: spec.SchemaProps{
|
||||||
|
Description: "Last time the condition transitioned from one status to another.",
|
||||||
|
Default: map[string]interface{}{},
|
||||||
|
Ref: ref("k8s.io/apimachinery/pkg/apis/meta/v1.Time"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"reason": {
|
||||||
|
SchemaProps: spec.SchemaProps{
|
||||||
|
Description: "The reason for the condition's last transition.",
|
||||||
|
Type: []string{"string"},
|
||||||
|
Format: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
"message": {
|
||||||
|
SchemaProps: spec.SchemaProps{
|
||||||
|
Description: "A human readable message indicating details about the transition.",
|
||||||
|
Type: []string{"string"},
|
||||||
|
Format: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Required: []string{"type", "status"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Dependencies: []string{
|
||||||
|
"k8s.io/apimachinery/pkg/apis/meta/v1.Time"},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var getOpenAPIDefs common.GetOpenAPIDefinitions = func(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition {
|
||||||
|
return map[string]common.OpenAPIDefinition{
|
||||||
|
"k8s.io/api/apps/v1.DeploymentCondition": schema_k8sio_api_apps_v1_DeploymentCondition(ref),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetOpenAPIDefinitionsWithoutDisabledFeatures(t *testing.T) {
|
||||||
|
defs := GetOpenAPIDefinitionsWithoutDisabledFeatures(getOpenAPIDefs)(func(path string) spec.Ref {
|
||||||
|
return spec.Ref{}
|
||||||
|
})
|
||||||
|
def := defs["k8s.io/api/apps/v1.DeploymentCondition"]
|
||||||
|
for _, prop := range def.Schema.Properties {
|
||||||
|
if strings.Contains(prop.Description, "enum") {
|
||||||
|
t.Errorf("enum in description: %s", prop.Description)
|
||||||
|
}
|
||||||
|
if len(prop.Enum) != 0 {
|
||||||
|
t.Errorf("unexpected enum: %v", prop.Enum)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -45,6 +45,7 @@ import (
|
|||||||
"k8s.io/apiserver/pkg/storage/storagebackend"
|
"k8s.io/apiserver/pkg/storage/storagebackend"
|
||||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||||
utilflowcontrol "k8s.io/apiserver/pkg/util/flowcontrol"
|
utilflowcontrol "k8s.io/apiserver/pkg/util/flowcontrol"
|
||||||
|
utilopenapi "k8s.io/apiserver/pkg/util/openapi"
|
||||||
"k8s.io/client-go/informers"
|
"k8s.io/client-go/informers"
|
||||||
clientset "k8s.io/client-go/kubernetes"
|
clientset "k8s.io/client-go/kubernetes"
|
||||||
restclient "k8s.io/client-go/rest"
|
restclient "k8s.io/client-go/rest"
|
||||||
@ -119,7 +120,7 @@ func DefaultOpenAPIConfig() *openapicommon.Config {
|
|||||||
Description: "Default Response.",
|
Description: "Default Response.",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
openAPIConfig.GetDefinitions = openapi.GetOpenAPIDefinitions
|
openAPIConfig.GetDefinitions = utilopenapi.GetOpenAPIDefinitionsWithoutDisabledFeatures(openapi.GetOpenAPIDefinitions)
|
||||||
|
|
||||||
return openAPIConfig
|
return openAPIConfig
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user