mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-21 19:01:49 +00:00
Fix kubectl apply skew test with extra properties
This commit is contained in:
parent
ef7808fec5
commit
044e92b5cf
@ -42,6 +42,7 @@ go_library(
|
|||||||
"//test/utils/crd:go_default_library",
|
"//test/utils/crd:go_default_library",
|
||||||
"//test/utils/image:go_default_library",
|
"//test/utils/image:go_default_library",
|
||||||
"//vendor/github.com/elazarl/goproxy:go_default_library",
|
"//vendor/github.com/elazarl/goproxy:go_default_library",
|
||||||
|
"//vendor/github.com/googleapis/gnostic/OpenAPIv2:go_default_library",
|
||||||
"//vendor/github.com/onsi/ginkgo:go_default_library",
|
"//vendor/github.com/onsi/ginkgo:go_default_library",
|
||||||
"//vendor/github.com/onsi/gomega:go_default_library",
|
"//vendor/github.com/onsi/gomega:go_default_library",
|
||||||
"//vendor/golang.org/x/net/websocket:go_default_library",
|
"//vendor/golang.org/x/net/websocket:go_default_library",
|
||||||
|
@ -40,6 +40,8 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/elazarl/goproxy"
|
"github.com/elazarl/goproxy"
|
||||||
|
openapi_v2 "github.com/googleapis/gnostic/OpenAPIv2"
|
||||||
|
|
||||||
v1 "k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
rbacv1beta1 "k8s.io/api/rbac/v1beta1"
|
rbacv1beta1 "k8s.io/api/rbac/v1beta1"
|
||||||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
|
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
|
||||||
@ -851,6 +853,56 @@ metadata:
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// definitionMatchesGVK returns true if the specified GVK is listed as an x-kubernetes-group-version-kind extension
|
||||||
|
definitionMatchesGVK := func(extensions []*openapi_v2.NamedAny, desiredGVK schema.GroupVersionKind) bool {
|
||||||
|
for _, extension := range extensions {
|
||||||
|
if extension.GetValue().GetYaml() == "" ||
|
||||||
|
extension.GetName() != "x-kubernetes-group-version-kind" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var values []map[string]string
|
||||||
|
err := yaml.Unmarshal([]byte(extension.GetValue().GetYaml()), &values)
|
||||||
|
if err != nil {
|
||||||
|
e2elog.Logf("%v\n%s", err, string(extension.GetValue().GetYaml()))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, value := range values {
|
||||||
|
if value["group"] != desiredGVK.Group {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if value["version"] != desiredGVK.Version {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if value["kind"] != desiredGVK.Kind {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// schemaForGVK returns a schema (if defined) for the specified GVK
|
||||||
|
schemaForGVK := func(desiredGVK schema.GroupVersionKind) *openapi_v2.Schema {
|
||||||
|
d, err := f.ClientSet.Discovery().OpenAPISchema()
|
||||||
|
if err != nil {
|
||||||
|
framework.Failf("%v", err)
|
||||||
|
}
|
||||||
|
if d == nil || d.Definitions == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
for _, p := range d.Definitions.AdditionalProperties {
|
||||||
|
if p == nil || p.Value == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !definitionMatchesGVK(p.Value.VendorExtension, desiredGVK) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return p.Value
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
framework.KubeDescribe("Kubectl client-side validation", func() {
|
framework.KubeDescribe("Kubectl client-side validation", func() {
|
||||||
ginkgo.It("should create/apply a CR with unknown fields for CRD with no validation schema", func() {
|
ginkgo.It("should create/apply a CR with unknown fields for CRD with no validation schema", func() {
|
||||||
ginkgo.By("create CRD with no validation schema")
|
ginkgo.By("create CRD with no validation schema")
|
||||||
@ -877,6 +929,7 @@ metadata:
|
|||||||
if err := yaml.Unmarshal(schemaFoo, props); err != nil {
|
if err := yaml.Unmarshal(schemaFoo, props); err != nil {
|
||||||
framework.Failf("failed to unmarshal schema: %v", err)
|
framework.Failf("failed to unmarshal schema: %v", err)
|
||||||
}
|
}
|
||||||
|
crd.Spec.Validation = &v1beta1.CustomResourceValidation{OpenAPIV3Schema: props}
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
framework.Failf("failed to create test CRD: %v", err)
|
framework.Failf("failed to create test CRD: %v", err)
|
||||||
@ -900,6 +953,7 @@ metadata:
|
|||||||
if err := yaml.Unmarshal(schemaFoo, props); err != nil {
|
if err := yaml.Unmarshal(schemaFoo, props); err != nil {
|
||||||
framework.Failf("failed to unmarshal schema: %v", err)
|
framework.Failf("failed to unmarshal schema: %v", err)
|
||||||
}
|
}
|
||||||
|
crd.Spec.Validation = &v1beta1.CustomResourceValidation{OpenAPIV3Schema: props}
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
framework.Failf("failed to create test CRD: %v", err)
|
framework.Failf("failed to create test CRD: %v", err)
|
||||||
@ -909,11 +963,29 @@ metadata:
|
|||||||
ginkgo.By("sleep for 10s to wait for potential crd openapi publishing alpha feature")
|
ginkgo.By("sleep for 10s to wait for potential crd openapi publishing alpha feature")
|
||||||
time.Sleep(10 * time.Second)
|
time.Sleep(10 * time.Second)
|
||||||
|
|
||||||
|
publishedSchema := schemaForGVK(schema.GroupVersionKind{Group: crd.Crd.Spec.Group, Version: crd.Crd.Spec.Version, Kind: crd.Crd.Spec.Names.Kind})
|
||||||
|
expectSuccess := false
|
||||||
|
if publishedSchema == nil || publishedSchema.Properties == nil || publishedSchema.Properties.AdditionalProperties == nil || len(publishedSchema.Properties.AdditionalProperties) == 0 {
|
||||||
|
// expect success in the following cases:
|
||||||
|
// - no schema was published
|
||||||
|
// - a schema was published with no properties
|
||||||
|
expectSuccess = true
|
||||||
|
e2elog.Logf("no schema with properties found, expect apply with extra properties to succeed")
|
||||||
|
} else {
|
||||||
|
e2elog.Logf("schema with properties found, expect apply with extra properties to fail")
|
||||||
|
}
|
||||||
|
|
||||||
meta := fmt.Sprintf(metaPattern, crd.Crd.Spec.Names.Kind, crd.Crd.Spec.Group, crd.Crd.Spec.Versions[0].Name, "test-cr")
|
meta := fmt.Sprintf(metaPattern, crd.Crd.Spec.Names.Kind, crd.Crd.Spec.Group, crd.Crd.Spec.Versions[0].Name, "test-cr")
|
||||||
validArbitraryCR := fmt.Sprintf(`{%s,"spec":{"bars":[{"name":"test-bar"}],"extraProperty":"arbitrary-value"}}`, meta)
|
validArbitraryCR := fmt.Sprintf(`{%s,"spec":{"bars":[{"name":"test-bar"}],"extraProperty":"arbitrary-value"}}`, meta)
|
||||||
if err := createApplyCustomResource(validArbitraryCR, f.Namespace.Name, "test-cr", crd); err != nil {
|
if err := createApplyCustomResource(validArbitraryCR, f.Namespace.Name, "test-cr", crd); err != nil {
|
||||||
|
if expectSuccess {
|
||||||
framework.Failf("%v", err)
|
framework.Failf("%v", err)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if !expectSuccess {
|
||||||
|
framework.Failf("expected error, got none")
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user