cleanup: remove unused versions of cel validators and structural schemas from CRD strategy

This commit is contained in:
Alexander Zielenski 2023-10-18 15:04:47 -07:00
parent cc0264f7fc
commit d151f22780
4 changed files with 30 additions and 40 deletions

View File

@ -805,7 +805,7 @@ func (r *crdHandler) getOrCreateServingInfoFor(uid types.UID, name string) (*crd
kind, kind,
validator, validator,
statusValidator, statusValidator,
structuralSchemas, structuralSchemas[v.Name],
statusSpec, statusSpec,
scaleSpec, scaleSpec,
), ),

View File

@ -99,21 +99,19 @@ func (a statusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Obj
oldObject = uOld.Object oldObject = uOld.Object
} }
v := obj.GetObjectKind().GroupVersionKind().Version
// ratcheting validation of x-kubernetes-list-type value map and set // 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 newErrs := structurallisttype.ValidateListSetsAndMaps(nil, a.structuralSchema, uNew.Object); len(newErrs) > 0 {
if oldErrs := structurallisttype.ValidateListSetsAndMaps(nil, a.structuralSchemas[v], oldObject); len(oldErrs) == 0 { if oldErrs := structurallisttype.ValidateListSetsAndMaps(nil, a.structuralSchema, oldObject); len(oldErrs) == 0 {
errs = append(errs, newErrs...) errs = append(errs, newErrs...)
} }
} }
// validate x-kubernetes-validations rules // 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 { if has, err := hasBlockingErr(errs); has {
errs = append(errs, err) errs = append(errs, err)
} else { } 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...) errs = append(errs, err...)
} }
} }

View File

@ -198,9 +198,7 @@ func TestStatusStrategyValidateUpdate(t *testing.T) {
} }
strategy.customResourceStrategy.validator.kind = kind strategy.customResourceStrategy.validator.kind = kind
ss, _ := structuralschema.NewStructural(crd.Spec.Versions[0].Schema.OpenAPIV3Schema) ss, _ := structuralschema.NewStructural(crd.Spec.Versions[0].Schema.OpenAPIV3Schema)
strategy.structuralSchemas = map[string]*structuralschema.Structural{ strategy.structuralSchema = ss
crd.Spec.Versions[0].Name: ss,
}
ctx := context.TODO() ctx := context.TODO()

View File

@ -45,29 +45,25 @@ import (
"sigs.k8s.io/structured-merge-diff/v4/fieldpath" "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 { type customResourceStrategy struct {
runtime.ObjectTyper runtime.ObjectTyper
names.NameGenerator names.NameGenerator
namespaceScoped bool namespaceScoped bool
validator customResourceValidator validator customResourceValidator
structuralSchemas map[string]*structuralschema.Structural structuralSchema *structuralschema.Structural
celValidators map[string]*cel.Validator celValidator *cel.Validator
status *apiextensions.CustomResourceSubresourceStatus status *apiextensions.CustomResourceSubresourceStatus
scale *apiextensions.CustomResourceSubresourceScale scale *apiextensions.CustomResourceSubresourceScale
kind schema.GroupVersionKind 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 { func NewStrategy(typer runtime.ObjectTyper, namespaceScoped bool, kind schema.GroupVersionKind, schemaValidator, statusSchemaValidator validation.SchemaValidator, structuralSchema *structuralschema.Structural, status *apiextensions.CustomResourceSubresourceStatus, scale *apiextensions.CustomResourceSubresourceScale) customResourceStrategy {
celValidators := map[string]*cel.Validator{} var celValidator *cel.Validator
if utilfeature.DefaultFeatureGate.Enabled(features.CustomResourceValidationExpressions) { if utilfeature.DefaultFeatureGate.Enabled(features.CustomResourceValidationExpressions) {
for name, s := range structuralSchemas { celValidator = cel.NewValidator(structuralSchema, true, celconfig.PerCallLimit) // CEL programs are compiled and cached here
v := cel.NewValidator(s, true, celconfig.PerCallLimit) // CEL programs are compiled and cached here
if v != nil {
celValidators[name] = v
}
}
} }
return customResourceStrategy{ return customResourceStrategy{
@ -82,9 +78,9 @@ func NewStrategy(typer runtime.ObjectTyper, namespaceScoped bool, kind schema.Gr
schemaValidator: schemaValidator, schemaValidator: schemaValidator,
statusSchemaValidator: statusSchemaValidator, statusSchemaValidator: statusSchemaValidator,
}, },
structuralSchemas: structuralSchemas, structuralSchema: structuralSchema,
celValidators: celValidators, celValidator: celValidator,
kind: kind, kind: kind,
} }
} }
@ -173,18 +169,17 @@ func (a customResourceStrategy) Validate(ctx context.Context, obj runtime.Object
errs = append(errs, a.validator.Validate(ctx, u, a.scale)...) errs = append(errs, a.validator.Validate(ctx, u, a.scale)...)
// validate embedded resources // validate embedded resources
v := obj.GetObjectKind().GroupVersionKind().Version errs = append(errs, schemaobjectmeta.Validate(nil, u.Object, a.structuralSchema, false)...)
errs = append(errs, schemaobjectmeta.Validate(nil, u.Object, a.structuralSchemas[v], false)...)
// validate x-kubernetes-list-type "map" and "set" invariant // 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 // validate x-kubernetes-validations rules
if celValidator, ok := a.celValidators[v]; ok { if celValidator := a.celValidator; celValidator != nil {
if has, err := hasBlockingErr(errs); has { if has, err := hasBlockingErr(errs); has {
errs = append(errs, err) errs = append(errs, err)
} else { } 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...) errs = append(errs, err...)
} }
} }
@ -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)...) 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. // 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.structuralSchema, false)...)
errs = append(errs, schemaobjectmeta.Validate(nil, uNew.Object, a.structuralSchemas[v], false)...)
// ratcheting validation of x-kubernetes-list-type value map and set // ratcheting validation of x-kubernetes-list-type value map and set
if oldErrs := structurallisttype.ValidateListSetsAndMaps(nil, a.structuralSchemas[v], uOld.Object); len(oldErrs) == 0 { if oldErrs := structurallisttype.ValidateListSetsAndMaps(nil, a.structuralSchema, uOld.Object); len(oldErrs) == 0 {
errs = append(errs, structurallisttype.ValidateListSetsAndMaps(nil, a.structuralSchemas[v], uNew.Object)...) errs = append(errs, structurallisttype.ValidateListSetsAndMaps(nil, a.structuralSchema, uNew.Object)...)
} }
// validate x-kubernetes-validations rules // validate x-kubernetes-validations rules
if celValidator, ok := a.celValidators[v]; ok { if celValidator := a.celValidator; celValidator != nil {
if has, err := hasBlockingErr(errs); has { if has, err := hasBlockingErr(errs); has {
errs = append(errs, err) errs = append(errs, err)
} else { } 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...) errs = append(errs, err...)
} }
} }