Skip to content

Commit

Permalink
cleanup: remove unused versions of cel validators and structural sche…
Browse files Browse the repository at this point in the history
…mas from CRD strategy
  • Loading branch information
alexzielenski committed Oct 18, 2023
1 parent cc0264f commit d151f22
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 40 deletions.
Expand Up @@ -805,7 +805,7 @@ func (r *crdHandler) getOrCreateServingInfoFor(uid types.UID, name string) (*crd
kind,
validator,
statusValidator,
structuralSchemas,
structuralSchemas[v.Name],
statusSpec,
scaleSpec,
),
Expand Down
Expand Up @@ -99,21 +99,19 @@ func (a statusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Obj
oldObject = uOld.Object
}

v := obj.GetObjectKind().GroupVersionKind().Version

// ratcheting validation of x-kubernetes-list-type value map and set
if newErrs := structurallisttype.ValidateListSetsAndMaps(nil, a.structuralSchemas[v], uNew.Object); len(newErrs) > 0 {
if oldErrs := structurallisttype.ValidateListSetsAndMaps(nil, a.structuralSchemas[v], oldObject); len(oldErrs) == 0 {
if newErrs := structurallisttype.ValidateListSetsAndMaps(nil, a.structuralSchema, uNew.Object); len(newErrs) > 0 {
if oldErrs := structurallisttype.ValidateListSetsAndMaps(nil, a.structuralSchema, oldObject); len(oldErrs) == 0 {
errs = append(errs, newErrs...)
}
}

// validate x-kubernetes-validations rules
if celValidator, ok := a.customResourceStrategy.celValidators[v]; ok {
if celValidator := a.customResourceStrategy.celValidator; celValidator != nil {
if has, err := hasBlockingErr(errs); has {
errs = append(errs, err)
} else {
err, _ := celValidator.Validate(ctx, nil, a.customResourceStrategy.structuralSchemas[v], uNew.Object, oldObject, celconfig.RuntimeCELCostBudget)
err, _ := celValidator.Validate(ctx, nil, a.customResourceStrategy.structuralSchema, uNew.Object, oldObject, celconfig.RuntimeCELCostBudget)
errs = append(errs, err...)
}
}
Expand Down
Expand Up @@ -198,9 +198,7 @@ func TestStatusStrategyValidateUpdate(t *testing.T) {
}
strategy.customResourceStrategy.validator.kind = kind
ss, _ := structuralschema.NewStructural(crd.Spec.Versions[0].Schema.OpenAPIV3Schema)
strategy.structuralSchemas = map[string]*structuralschema.Structural{
crd.Spec.Versions[0].Name: ss,
}
strategy.structuralSchema = ss

ctx := context.TODO()

Expand Down
Expand Up @@ -45,29 +45,25 @@ import (
"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
)

// customResourceStrategy implements behavior for CustomResources.
// customResourceStrategy implements behavior for CustomResources for a single
// version
type customResourceStrategy struct {
runtime.ObjectTyper
names.NameGenerator

namespaceScoped bool
validator customResourceValidator
structuralSchemas map[string]*structuralschema.Structural
celValidators map[string]*cel.Validator
status *apiextensions.CustomResourceSubresourceStatus
scale *apiextensions.CustomResourceSubresourceScale
kind schema.GroupVersionKind
namespaceScoped bool
validator customResourceValidator
structuralSchema *structuralschema.Structural
celValidator *cel.Validator
status *apiextensions.CustomResourceSubresourceStatus
scale *apiextensions.CustomResourceSubresourceScale
kind schema.GroupVersionKind
}

func NewStrategy(typer runtime.ObjectTyper, namespaceScoped bool, kind schema.GroupVersionKind, schemaValidator, statusSchemaValidator validation.SchemaValidator, structuralSchemas map[string]*structuralschema.Structural, status *apiextensions.CustomResourceSubresourceStatus, scale *apiextensions.CustomResourceSubresourceScale) customResourceStrategy {
celValidators := map[string]*cel.Validator{}
func NewStrategy(typer runtime.ObjectTyper, namespaceScoped bool, kind schema.GroupVersionKind, schemaValidator, statusSchemaValidator validation.SchemaValidator, structuralSchema *structuralschema.Structural, status *apiextensions.CustomResourceSubresourceStatus, scale *apiextensions.CustomResourceSubresourceScale) customResourceStrategy {
var celValidator *cel.Validator
if utilfeature.DefaultFeatureGate.Enabled(features.CustomResourceValidationExpressions) {
for name, s := range structuralSchemas {
v := cel.NewValidator(s, true, celconfig.PerCallLimit) // CEL programs are compiled and cached here
if v != nil {
celValidators[name] = v
}
}
celValidator = cel.NewValidator(structuralSchema, true, celconfig.PerCallLimit) // CEL programs are compiled and cached here
}

return customResourceStrategy{
Expand All @@ -82,9 +78,9 @@ func NewStrategy(typer runtime.ObjectTyper, namespaceScoped bool, kind schema.Gr
schemaValidator: schemaValidator,
statusSchemaValidator: statusSchemaValidator,
},
structuralSchemas: structuralSchemas,
celValidators: celValidators,
kind: kind,
structuralSchema: structuralSchema,
celValidator: celValidator,
kind: kind,
}
}

Expand Down Expand Up @@ -173,18 +169,17 @@ func (a customResourceStrategy) Validate(ctx context.Context, obj runtime.Object
errs = append(errs, a.validator.Validate(ctx, u, a.scale)...)

// validate embedded resources
v := obj.GetObjectKind().GroupVersionKind().Version
errs = append(errs, schemaobjectmeta.Validate(nil, u.Object, a.structuralSchemas[v], false)...)
errs = append(errs, schemaobjectmeta.Validate(nil, u.Object, a.structuralSchema, false)...)

// validate x-kubernetes-list-type "map" and "set" invariant
errs = append(errs, structurallisttype.ValidateListSetsAndMaps(nil, a.structuralSchemas[v], u.Object)...)
errs = append(errs, structurallisttype.ValidateListSetsAndMaps(nil, a.structuralSchema, u.Object)...)

// validate x-kubernetes-validations rules
if celValidator, ok := a.celValidators[v]; ok {
if celValidator := a.celValidator; celValidator != nil {
if has, err := hasBlockingErr(errs); has {
errs = append(errs, err)
} else {
err, _ := celValidator.Validate(ctx, nil, a.structuralSchemas[v], u.Object, nil, celconfig.RuntimeCELCostBudget)
err, _ := celValidator.Validate(ctx, nil, a.structuralSchema, u.Object, nil, celconfig.RuntimeCELCostBudget)
errs = append(errs, err...)
}
}
Expand Down Expand Up @@ -251,20 +246,19 @@ func (a customResourceStrategy) ValidateUpdate(ctx context.Context, obj, old run
errs = append(errs, a.validator.ValidateUpdate(ctx, uNew, uOld, a.scale)...)

// Checks the embedded objects. We don't make a difference between update and create for those.
v := obj.GetObjectKind().GroupVersionKind().Version
errs = append(errs, schemaobjectmeta.Validate(nil, uNew.Object, a.structuralSchemas[v], false)...)
errs = append(errs, schemaobjectmeta.Validate(nil, uNew.Object, a.structuralSchema, false)...)

// ratcheting validation of x-kubernetes-list-type value map and set
if oldErrs := structurallisttype.ValidateListSetsAndMaps(nil, a.structuralSchemas[v], uOld.Object); len(oldErrs) == 0 {
errs = append(errs, structurallisttype.ValidateListSetsAndMaps(nil, a.structuralSchemas[v], uNew.Object)...)
if oldErrs := structurallisttype.ValidateListSetsAndMaps(nil, a.structuralSchema, uOld.Object); len(oldErrs) == 0 {
errs = append(errs, structurallisttype.ValidateListSetsAndMaps(nil, a.structuralSchema, uNew.Object)...)
}

// validate x-kubernetes-validations rules
if celValidator, ok := a.celValidators[v]; ok {
if celValidator := a.celValidator; celValidator != nil {
if has, err := hasBlockingErr(errs); has {
errs = append(errs, err)
} else {
err, _ := celValidator.Validate(ctx, nil, a.structuralSchemas[v], uNew.Object, uOld.Object, celconfig.RuntimeCELCostBudget)
err, _ := celValidator.Validate(ctx, nil, a.structuralSchema, uNew.Object, uOld.Object, celconfig.RuntimeCELCostBudget)
errs = append(errs, err...)
}
}
Expand Down

0 comments on commit d151f22

Please sign in to comment.