Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

test: Ensure defaults from JSON schema are respected #9

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions common/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ require (
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.14.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion common/pkg/testutils/openapi/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
)

// convertToAPIExtensionsJSONSchemaProps converts a clusterv1.JSONSchemaProps to apiextensions.JSONSchemaProp.
// ConvertToAPIExtensionsJSONSchemaProps converts a clusterv1.JSONSchemaProps to apiextensions.JSONSchemaProp.
// NOTE: This is used whenever we want to use one of the upstream libraries, as they use apiextensions.JSONSchemaProp.
// NOTE: If new fields are added to clusterv1.JSONSchemaProps (e.g. to support complex types), the corresponding
// schema validation must be added to validateRootSchema too.
Expand Down
13 changes: 13 additions & 0 deletions common/pkg/testutils/openapi/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions"
structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema"
"k8s.io/apiextensions-apiserver/pkg/apiserver/schema/defaulting"
structuralpruning "k8s.io/apiextensions-apiserver/pkg/apiserver/schema/pruning"
"k8s.io/apiextensions-apiserver/pkg/apiserver/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
Expand Down Expand Up @@ -62,6 +63,18 @@ func ValidateClusterVariable(
)}
}

s, err := structuralschema.NewStructural(apiExtensionsSchema)
if err != nil {
return field.ErrorList{field.InternalError(fldPath,
fmt.Errorf(
"failed to create structural schema for variable %q; ClusterClass should be checked: %v",
value.Name,
err,
),
)}
}
defaulting.Default(variableValue, s)

// Validate variable against the schema.
// NOTE: We're reusing a library func used in CRD validation.
if err := validation.ValidateCustomResource(fldPath, variableValue, validator); err != nil {
Expand Down