@@ -21,14 +21,10 @@ import (
21
21
"reflect"
22
22
"strings"
23
23
24
- "github.com/go-openapi/strfmt"
25
- govalidate "github.com/go-openapi/validate"
26
- schemaobjectmeta "k8s.io/apiextensions-apiserver/pkg/apiserver/schema/objectmeta"
27
-
28
24
"k8s.io/apiextensions-apiserver/pkg/apihelpers"
25
+ structuraldefaulting "k8s.io/apiextensions-apiserver/pkg/apiserver/schema/defaulting"
29
26
apiequality "k8s.io/apimachinery/pkg/api/equality"
30
27
genericvalidation "k8s.io/apimachinery/pkg/api/validation"
31
- "k8s.io/apimachinery/pkg/runtime"
32
28
"k8s.io/apimachinery/pkg/runtime/schema"
33
29
"k8s.io/apimachinery/pkg/util/sets"
34
30
utilvalidation "k8s.io/apimachinery/pkg/util/validation"
@@ -39,7 +35,6 @@ import (
39
35
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
40
36
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
41
37
structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
42
- "k8s.io/apiextensions-apiserver/pkg/apiserver/schema/pruning"
43
38
apiservervalidation "k8s.io/apiextensions-apiserver/pkg/apiserver/validation"
44
39
apiextensionsfeatures "k8s.io/apiextensions-apiserver/pkg/features"
45
40
)
@@ -659,6 +654,7 @@ func validateCustomResourceDefinitionValidation(customResourceValidation *apiext
659
654
allowDefaults : opts .allowDefaults ,
660
655
requireValidPropertyType : opts .requireValidPropertyType ,
661
656
}
657
+
662
658
allErrs = append (allErrs , ValidateCustomResourceDefinitionOpenAPISchema (schema , fldPath .Child ("openAPIV3Schema" ), openAPIV3Schema , true )... )
663
659
664
660
if opts .requireStructuralSchema {
@@ -667,8 +663,13 @@ func validateCustomResourceDefinitionValidation(customResourceValidation *apiext
667
663
if len (allErrs ) == 0 {
668
664
allErrs = append (allErrs , field .Invalid (fldPath .Child ("openAPIV3Schema" ), "" , err .Error ()))
669
665
}
666
+ } else if validationErrors := structuralschema .ValidateStructural (fldPath .Child ("openAPIV3Schema" ), ss ); len (validationErrors ) > 0 {
667
+ allErrs = append (allErrs , validationErrors ... )
668
+ } else if validationErrors , err := structuraldefaulting .ValidateDefaults (fldPath .Child ("openAPIV3Schema" ), ss , true ); err != nil {
669
+ // this should never happen
670
+ allErrs = append (allErrs , field .Invalid (fldPath .Child ("openAPIV3Schema" ), "" , err .Error ()))
670
671
} else {
671
- allErrs = append (allErrs , structuralschema . ValidateStructural ( ss , fldPath . Child ( "openAPIV3Schema" )) ... )
672
+ allErrs = append (allErrs , validationErrors ... )
672
673
}
673
674
}
674
675
}
@@ -682,7 +683,7 @@ func validateCustomResourceDefinitionValidation(customResourceValidation *apiext
682
683
return allErrs
683
684
}
684
685
685
- var metaFields = sets .NewString ("metadata" , "apiVersion " , "kind " )
686
+ var metaFields = sets .NewString ("metadata" , "kind " , "apiVersion " )
686
687
687
688
// ValidateCustomResourceDefinitionOpenAPISchema statically validates
688
689
func ValidateCustomResourceDefinitionOpenAPISchema (schema * apiextensions.JSONSchemaProps , fldPath * field.Path , ssv specStandardValidator , isRoot bool ) field.ErrorList {
@@ -726,6 +727,7 @@ func ValidateCustomResourceDefinitionOpenAPISchema(schema *apiextensions.JSONSch
726
727
if len (schema .Properties ) != 0 {
727
728
for property , jsonSchema := range schema .Properties {
728
729
subSsv := ssv
730
+
729
731
if (isRoot || schema .XEmbeddedResource ) && metaFields .Has (property ) {
730
732
// we recurse into the schema that applies to ObjectMeta.
731
733
subSsv = ssv .withInsideResourceMeta ()
@@ -825,43 +827,12 @@ func (v *specStandardValidatorV3) validate(schema *apiextensions.JSONSchemaProps
825
827
allErrs = append (allErrs , field .NotSupported (fldPath .Child ("type" ), schema .Type , openapiV3Types .List ()))
826
828
}
827
829
828
- if schema .Default != nil {
829
- if v .allowDefaults {
830
- if s , err := structuralschema .NewStructural (schema ); err == nil {
831
- // ignore errors here locally. They will show up for the root of the schema.
832
-
833
- clone := runtime .DeepCopyJSONValue (interface {}(* schema .Default ))
834
- if ! v .isInsideResourceMeta {
835
- // If we are under metadata, there are implicitly specified fields like kind, apiVersion, metadata, labels.
836
- // We cannot prune as they are pruned as well. This allows more defaults than we would like to.
837
- // TODO: be precise about pruning under metadata
838
- pruning .Prune (clone , s , s .XEmbeddedResource )
839
-
840
- // TODO: coerce correctly if we are not at the object root, but somewhere below.
841
- if err := schemaobjectmeta .Coerce (fldPath , clone , s , s .XEmbeddedResource , false ); err != nil {
842
- allErrs = append (allErrs , err )
843
- }
844
-
845
- if ! reflect .DeepEqual (clone , interface {}(* schema .Default )) {
846
- allErrs = append (allErrs , field .Invalid (fldPath .Child ("default" ), schema .Default , "must not have unknown fields" ))
847
- } else if s .XEmbeddedResource {
848
- // validate an embedded resource
849
- schemaobjectmeta .Validate (fldPath , interface {}(* schema .Default ), nil , true )
850
- }
851
- }
852
-
853
- // validate the default value with user the provided schema.
854
- validator := govalidate .NewSchemaValidator (s .ToGoOpenAPI (), nil , "" , strfmt .Default )
855
-
856
- allErrs = append (allErrs , apiservervalidation .ValidateCustomResource (fldPath .Child ("default" ), interface {}(* schema .Default ), validator )... )
857
- }
858
- } else {
859
- detail := "must not be set"
860
- if len (v .disallowDefaultsReason ) > 0 {
861
- detail += " " + v .disallowDefaultsReason
862
- }
863
- allErrs = append (allErrs , field .Forbidden (fldPath .Child ("default" ), detail ))
830
+ if schema .Default != nil && ! v .allowDefaults {
831
+ detail := "must not be set"
832
+ if len (v .disallowDefaultsReason ) > 0 {
833
+ detail += " " + v .disallowDefaultsReason
864
834
}
835
+ allErrs = append (allErrs , field .Forbidden (fldPath .Child ("default" ), detail ))
865
836
}
866
837
867
838
if schema .ID != "" {
@@ -1212,7 +1183,7 @@ func schemaIsNonStructural(schema *apiextensions.JSONSchemaProps) bool {
1212
1183
if err != nil {
1213
1184
return true
1214
1185
}
1215
- return len (structuralschema .ValidateStructural (ss , nil )) > 0
1186
+ return len (structuralschema .ValidateStructural (nil , ss )) > 0
1216
1187
}
1217
1188
1218
1189
// requireValidPropertyType returns true if valid openapi v3 types should be required for the given API version
0 commit comments