Merge pull request #105057 from jiahuif-forks/feature/enum-types-feature-enablement

Add Support for OpenAPIEnum in OpenAPI v2
This commit is contained in:
Kubernetes Prow Robot
2021-11-16 16:35:55 -08:00
committed by GitHub
61 changed files with 941 additions and 165 deletions

View File

@@ -87,6 +87,12 @@ var _ = SIGDescribe("CustomResourcePublishOpenAPI [Privileged:ClusterAdmin]", fu
framework.Failf("failed to delete valid CR: %v", err)
}
ginkgo.By("client-side validation (kubectl create and apply) rejects request with value outside defined enum values")
badEnumValueCR := fmt.Sprintf(`{%s,"spec":{"bars":[{"name":"test-bar", "feeling":"NonExistentValue"}]}}`, meta)
if _, err := framework.RunKubectlInput(f.Namespace.Name, badEnumValueCR, ns, "create", "-f", "-"); err == nil || !strings.Contains(err.Error(), `Unsupported value: "NonExistentValue"`) {
framework.Failf("unexpected no error when creating CR with unknown enum value: %v", err)
}
ginkgo.By("client-side validation (kubectl create and apply) rejects request with unknown properties when disallowed by the schema")
unknownCR := fmt.Sprintf(`{%s,"spec":{"foo":true}}`, meta)
if _, err := framework.RunKubectlInput(f.Namespace.Name, unknownCR, ns, "create", "-f", "-"); err == nil || !strings.Contains(err.Error(), `unknown field "foo"`) {
@@ -741,6 +747,12 @@ properties:
age:
description: Age of Bar.
type: string
feeling:
description: Whether Bar is feeling great.
type: string
enum:
- Great
- Down
bazs:
description: List of Bazs.
items:

View File

@@ -0,0 +1,115 @@
/*
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 apiserver
import (
"encoding/json"
"net/http"
"testing"
"k8s.io/apiserver/pkg/features"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/apiserver/pkg/util/openapi"
restclient "k8s.io/client-go/rest"
featuregatetesting "k8s.io/component-base/featuregate/testing"
"k8s.io/kube-openapi/pkg/common"
"k8s.io/kube-openapi/pkg/validation/spec"
generated "k8s.io/kubernetes/pkg/generated/openapi"
"k8s.io/kubernetes/test/integration/framework"
)
func TestEnablingOpenAPIEnumTypes(t *testing.T) {
const typeToAddEnum = "k8s.io/api/core/v1.ContainerPort"
const typeToCheckEnum = "io.k8s.api.core.v1.ContainerPort"
for _, tc := range []struct {
name string
featureEnabled bool
enumShouldExist bool
}{
{
name: "disabled",
featureEnabled: false,
enumShouldExist: false,
},
{
name: "enabled",
featureEnabled: true,
enumShouldExist: true,
},
} {
t.Run(tc.name, func(t *testing.T) {
defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.OpenAPIEnums, tc.featureEnabled)()
controlPlaneConfig := framework.NewIntegrationTestControlPlaneConfigWithOptions(&framework.ControlPlaneConfigOptions{})
controlPlaneConfig.GenericConfig.OpenAPIConfig = framework.DefaultOpenAPIConfig()
controlPlaneConfig.GenericConfig.OpenAPIConfig.GetDefinitions = openapi.GetOpenAPIDefinitionsWithoutDisabledFeatures(func(ref common.ReferenceCallback) map[string]common.OpenAPIDefinition {
defs := generated.GetOpenAPIDefinitions(ref)
def := defs[typeToAddEnum]
// replace protocol to add the would-be enum field.
def.Schema.Properties["protocol"] = spec.Schema{
SchemaProps: spec.SchemaProps{
Description: "Protocol for port. Must be UDP, TCP, or SCTP. Defaults to \\\"TCP\\\".\\n\\nPossible enum values:\\n - `SCTP`: is the SCTP protocol.\\n - `TCP`: is the TCP protocol.\\n - `UDP`: is the UDP protocol.",
Default: "TCP",
Type: []string{"string"},
Format: "",
Enum: []interface{}{"SCTP", "TCP", "UDP"},
},
}
defs[typeToAddEnum] = def
return defs
})
instanceConfig, _, closeFn := framework.RunAnAPIServer(controlPlaneConfig)
defer closeFn()
rt, err := restclient.TransportFor(instanceConfig.GenericAPIServer.LoopbackClientConfig)
if err != nil {
t.Fatal(err)
}
req, err := http.NewRequest("GET", instanceConfig.GenericAPIServer.LoopbackClientConfig.Host+"/openapi/v2", nil)
if err != nil {
t.Fatal(err)
}
resp, err := rt.RoundTrip(req)
if err != nil {
t.Fatal(err)
}
var body struct {
Definitions map[string]struct {
Properties map[string]struct {
Description string `json:"description"`
Type string `json:"type"`
Enum []string `json:"enum"`
} `json:"properties"`
} `json:"definitions"`
}
err = json.NewDecoder(resp.Body).Decode(&body)
if err != nil {
t.Fatal(err)
}
protocol, ok := body.Definitions[typeToCheckEnum].Properties["protocol"]
if !ok {
t.Fatalf("protocol not found in properties in %v", body)
}
if enumExists := len(protocol.Enum) > 0; enumExists != tc.enumShouldExist {
t.Errorf("expect enum exists: %v, but got %v", tc.enumShouldExist, enumExists)
}
})
}
}

View File

@@ -45,6 +45,7 @@ import (
"k8s.io/apiserver/pkg/storage/storagebackend"
utilfeature "k8s.io/apiserver/pkg/util/feature"
utilflowcontrol "k8s.io/apiserver/pkg/util/flowcontrol"
utilopenapi "k8s.io/apiserver/pkg/util/openapi"
"k8s.io/client-go/informers"
clientset "k8s.io/client-go/kubernetes"
restclient "k8s.io/client-go/rest"
@@ -119,7 +120,7 @@ func DefaultOpenAPIConfig() *openapicommon.Config {
Description: "Default Response.",
},
}
openAPIConfig.GetDefinitions = openapi.GetOpenAPIDefinitions
openAPIConfig.GetDefinitions = utilopenapi.GetOpenAPIDefinitionsWithoutDisabledFeatures(openapi.GetOpenAPIDefinitions)
return openAPIConfig
}