Skip to content

Commit 18adc30

Browse files
author
Alexander Zielenski
committed
refactor: rename TransitionRule to UsesOldSelf
not all rules that use OldSelf are transition rules, and this flag was used to check for oldSelf usage anyway, not specifically whether the rule was a transition rule
1 parent 9747358 commit 18adc30

File tree

4 files changed

+8
-9
lines changed

4 files changed

+8
-9
lines changed

staging/src/k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/validation/validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1182,7 +1182,7 @@ func ValidateCustomResourceDefinitionOpenAPISchema(schema *apiextensions.JSONSch
11821182
}
11831183
}
11841184
}
1185-
if cr.TransitionRule {
1185+
if cr.UsesOldSelf {
11861186
if uncorrelatablePath := ssv.forbidOldSelfValidations(); uncorrelatablePath != nil {
11871187
allErrs.CELErrors = append(allErrs.CELErrors, field.Invalid(fldPath.Child("x-kubernetes-validations").Index(i).Child("rule"), schema.XValidations[i].Rule, fmt.Sprintf("oldSelf cannot be used on the uncorrelatable portion of the schema within %v", uncorrelatablePath)))
11881188
}

staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel/compilation.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ const (
4848
type CompilationResult struct {
4949
Program cel.Program
5050
Error *apiservercel.Error
51-
// If true, the compiled expression contains a reference to the identifier "oldSelf", and its corresponding rule
52-
// is implicitly a transition rule.
53-
TransitionRule bool
51+
// If true, the compiled expression contains a reference to the identifier "oldSelf".
52+
UsesOldSelf bool
5453
// Represents the worst-case cost of the compiled expression in terms of CEL's cost units, as used by cel.EstimateCost.
5554
MaxCost uint64
5655
// MaxCardinality represents the worse case number of times this validation rule could be invoked if contained under an
@@ -190,7 +189,7 @@ func compileRule(s *schema.Structural, rule apiextensions.ValidationRule, envSet
190189
}
191190
for _, ref := range checkedExpr.ReferenceMap {
192191
if ref.Name == OldScopedVarName {
193-
compilationResult.TransitionRule = true
192+
compilationResult.UsesOldSelf = true
194193
break
195194
}
196195
}

staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel/compilation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func transitionRule(t bool) validationMatcher {
140140
}
141141

142142
func (v transitionRuleMatcher) matches(cr CompilationResult) bool {
143-
return cr.TransitionRule == bool(v)
143+
return cr.UsesOldSelf == bool(v)
144144
}
145145

146146
func (v transitionRuleMatcher) String() string {

staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel/validation.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func validator(s *schema.Structural, isResourceRoot bool, declType *cel.DeclType
124124
if len(compiledRules) > 0 || err != nil || itemsValidator != nil || additionalPropertiesValidator != nil || len(propertiesValidators) > 0 {
125125
var activationFactory func(*schema.Structural, interface{}, interface{}) interpreter.Activation = validationActivationWithoutOldSelf
126126
for _, rule := range compiledRules {
127-
if rule.TransitionRule {
127+
if rule.UsesOldSelf {
128128
activationFactory = validationActivationWithOldSelf
129129
break
130130
}
@@ -300,7 +300,7 @@ func (s *Validator) validateExpressions(ctx context.Context, fldPath *field.Path
300300
// rule is empty
301301
continue
302302
}
303-
if compiled.TransitionRule && oldObj == nil {
303+
if compiled.UsesOldSelf && oldObj == nil {
304304
// transition rules are evaluated only if there is a comparable existing value
305305
continue
306306
}
@@ -344,7 +344,7 @@ func (s *Validator) validateExpressions(ctx context.Context, fldPath *field.Path
344344
}
345345

346346
addErr := func(e *field.Error) {
347-
if !compiled.TransitionRule && correlation.shouldRatchetError() {
347+
if !compiled.UsesOldSelf && correlation.shouldRatchetError() {
348348
warning.AddWarning(ctx, "", e.Error())
349349
} else {
350350
errs = append(errs, e)

0 commit comments

Comments
 (0)