mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-09 12:07:47 +00:00
refactor: cleanup to NewSchemaValidator takes JSONSchemaProps
avoids caller having to unnecessarily wrap
This commit is contained in:
parent
ba58130021
commit
4da418aba7
@ -825,7 +825,7 @@ func validateCustomResourceDefinitionValidation(ctx context.Context, customResou
|
|||||||
|
|
||||||
// if validation passed otherwise, make sure we can actually construct a schema validator from this custom resource validation.
|
// if validation passed otherwise, make sure we can actually construct a schema validator from this custom resource validation.
|
||||||
if len(allErrs) == 0 {
|
if len(allErrs) == 0 {
|
||||||
if _, _, err := apiservervalidation.NewSchemaValidator(customResourceValidation); err != nil {
|
if _, _, err := apiservervalidation.NewSchemaValidator(customResourceValidation.OpenAPIV3Schema); err != nil {
|
||||||
allErrs = append(allErrs, field.Invalid(fldPath, "", fmt.Sprintf("error building validator: %v", err)))
|
allErrs = append(allErrs, field.Invalid(fldPath, "", fmt.Sprintf("error building validator: %v", err)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -739,14 +739,15 @@ func (r *crdHandler) getOrCreateServingInfoFor(uid types.UID, name string) (*crd
|
|||||||
utilruntime.HandleError(err)
|
utilruntime.HandleError(err)
|
||||||
return nil, fmt.Errorf("the server could not properly serve the CR schema")
|
return nil, fmt.Errorf("the server could not properly serve the CR schema")
|
||||||
}
|
}
|
||||||
var internalValidationSchema *apiextensionsinternal.CustomResourceValidation
|
var internalSchemaProps *apiextensionsinternal.JSONSchemaProps
|
||||||
if validationSchema != nil {
|
if validationSchema != nil {
|
||||||
internalValidationSchema = &apiextensionsinternal.CustomResourceValidation{}
|
internalValidationSchema := &apiextensionsinternal.CustomResourceValidation{}
|
||||||
if err := apiextensionsv1.Convert_v1_CustomResourceValidation_To_apiextensions_CustomResourceValidation(validationSchema, internalValidationSchema, nil); err != nil {
|
if err := apiextensionsv1.Convert_v1_CustomResourceValidation_To_apiextensions_CustomResourceValidation(validationSchema, internalValidationSchema, nil); err != nil {
|
||||||
return nil, fmt.Errorf("failed to convert CRD validation to internal version: %v", err)
|
return nil, fmt.Errorf("failed to convert CRD validation to internal version: %v", err)
|
||||||
}
|
}
|
||||||
|
internalSchemaProps = internalValidationSchema.OpenAPIV3Schema
|
||||||
}
|
}
|
||||||
validator, _, err := apiservervalidation.NewSchemaValidator(internalValidationSchema)
|
validator, _, err := apiservervalidation.NewSchemaValidator(internalSchemaProps)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -30,12 +30,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// NewSchemaValidator creates an openapi schema validator for the given CRD validation.
|
// NewSchemaValidator creates an openapi schema validator for the given CRD validation.
|
||||||
func NewSchemaValidator(customResourceValidation *apiextensions.CustomResourceValidation) (*validate.SchemaValidator, *spec.Schema, error) {
|
func NewSchemaValidator(customResourceValidation *apiextensions.JSONSchemaProps) (*validate.SchemaValidator, *spec.Schema, error) {
|
||||||
// Convert CRD schema to openapi schema
|
// Convert CRD schema to openapi schema
|
||||||
openapiSchema := &spec.Schema{}
|
openapiSchema := &spec.Schema{}
|
||||||
if customResourceValidation != nil {
|
if customResourceValidation != nil {
|
||||||
// TODO: replace with NewStructural(...).ToGoOpenAPI
|
// TODO: replace with NewStructural(...).ToGoOpenAPI
|
||||||
if err := ConvertJSONSchemaPropsWithPostProcess(customResourceValidation.OpenAPIV3Schema, openapiSchema, StripUnsupportedFormatsPostProcess); err != nil {
|
if err := ConvertJSONSchemaPropsWithPostProcess(customResourceValidation, openapiSchema, StripUnsupportedFormatsPostProcess); err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -601,7 +601,7 @@ func TestValidateCustomResource(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
validator, _, err := NewSchemaValidator(&apiextensions.CustomResourceValidation{OpenAPIV3Schema: &tt.schema})
|
validator, _, err := NewSchemaValidator(&tt.schema)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -689,7 +689,7 @@ func TestItemsProperty(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
validator, _, err := NewSchemaValidator(&apiextensions.CustomResourceValidation{OpenAPIV3Schema: &tt.args.schema})
|
validator, _, err := NewSchemaValidator(&tt.args.schema)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user