Skip to content

Commit 40157c8

Browse files
committed
Address comments
1 parent e638ab5 commit 40157c8

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

content/en/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions.md

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ CustomResourceDefinition schemas using the `x-kubernetes-validations` extension.
716716
The Rule is scoped to the location of the `x-kubernetes-validations` extension in the schema.
717717
And `self` variable in the CEL expression is bound to the scoped value.
718718

719-
Note all the validation rules are scoped to the current object, no cross-object or stateful validation rules are supported.
719+
All validation rules are scoped to the current object: no cross-object or stateful validation rules are supported.
720720

721721
For example:
722722

@@ -996,18 +996,18 @@ Here is the declarations type mapping between OpenAPIv3 and CEL type:
996996
xref: [CEL types](https://github.com/google/cel-spec/blob/v0.6.0/doc/langdef.md#values), [OpenAPI
997997
types](https://swagger.io/specification/#data-types), [Kubernetes Structural Schemas](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#specifying-a-structural-schema).
998998

999-
#### Function Library
999+
#### Validation functions {#available-validation-functions}
10001000

10011001
Functions available include:
1002-
- CEL standard functions, defined in the[list of standard definitions](https://github.com/google/cel-spec/blob/v0.7.0/doc/langdef.md#list-of-standard-definitions)
1002+
- CEL standard functions, defined in the [list of standard definitions](https://github.com/google/cel-spec/blob/v0.7.0/doc/langdef.md#list-of-standard-definitions)
10031003
- CEL standard [macros](https://github.com/google/cel-spec/blob/v0.7.0/doc/langdef.md#macros)
10041004
- CEL [extended string function library](https://pkg.go.dev/github.com/google/[email protected]/ext#Strings)
1005-
- Kubernetes [CEL extension library](https://pkg.go.dev/k8s.io/[email protected]-alpha.4/pkg/apiserver/schema/cel/library#pkg-functions)
1005+
- Kubernetes [CEL extension library](https://pkg.go.dev/k8s.io/[email protected]/pkg/apiserver/schema/cel/library#pkg-functions)
10061006

1007-
#### Transition Rules
1007+
#### Transition rules
10081008

10091009
A rule that contains an expression referencing the identifier `oldSelf` is implicitly considered a
1010-
"transition rule". Transition rules allow schema authors to prevent certain transitions between two
1010+
_transition rule_. Transition rules allow schema authors to prevent certain transitions between two
10111011
otherwise valid states. For example:
10121012

10131013
```yaml
@@ -1036,33 +1036,40 @@ Errors will be generated on CRD writes if a schema node contains a transition ru
10361036
applied, e.g. "*path*: update rule *rule* cannot be set on schema because the schema or its parent
10371037
schema is not mergeable".
10381038

1039-
Transition rules are only allowed on "correlatable" portions of a schema.
1039+
Transition rules are only allowed on _correlatable portions_ of a schema.
10401040
A portion of the schema is correlatable if all `array` parent schemas are of type `x-kubernetes-list-type=map`; any `set`or `atomic`array parent schemas make it impossible to unambiguously correlate a `self` with `oldSelf`.
10411041

1042-
##### Use Cases
1042+
Here are some examples for transition rules:
10431043

1044+
{{< table caption="Transition rules examples" >}}
10441045
| Use Case | Rule
10451046
| -------- | --------
10461047
| Immutability | `self.foo == oldSelf.foo`
10471048
| Prevent modification/removal once assigned | `oldSelf != 'bar' \|\| self == 'bar'` or `!has(oldSelf.field) \|\| has(self.field)`
10481049
| Append-only set | `self.all(element, element in oldSelf)`
10491050
| If previous value was X, new value can only be A or B, not Y or Z | `oldSelf != 'X' \|\| self in ['A', 'B']`
1050-
| Nondecreasing counters | `self >= oldSelf`
1051+
| Monotonic (non-decreasing) counters | `self >= oldSelf`
1052+
{{< /table >}}
10511053

1052-
#### Resource Constraints
1054+
#### Resource use by validation functions
10531055

1054-
Resource consumption of validation rules is checked at CustomResourceDefinition creation and update time. If a rule is estimated to be prohibitively expensive to execute, it will result in a validation error. A similar
1055-
system is used at runtime that observes the actions the interpreter takes. If the interpreter executes
1056+
When you create or update a CustomResourceDefinition that uses validation rules,
1057+
the API server checks the likely impact of running those validation rules. If a rule is
1058+
estimated to be prohibitively expensive to execute, the API server rejects the create
1059+
or update operation, and returns an error message.
1060+
A similar system is used at runtime that observes the actions the interpreter takes. If the interpreter executes
10561061
too many instructions, execution of the rule will be halted, and an error will result.
10571062
Each CustomResourceDefinition is also allowed a certain amount of resources to finish executing all of
10581063
its validation rules. If the sum total of its rules are estimated at creation time to go over that limit,
10591064
then a validation error will also occur.
10601065

1061-
In general, both systems will allow rules that do not need to iterate; these rules will
1062-
always take the same amount of time regardless of how large their input is. `self.foo == 1` will be allowed.
1063-
But if `foo` is a string and we instead have `self.foo.contains("someString")`, our rule will take
1064-
longer to execute depending on how long `foo` is. Another example would be if `foo` was an array, and we
1065-
had a rule `self.foo.all(x, x > 5)`. The cost system will always assume the worst-case scenario if
1066+
You are unlikely to encounter issues with the resource budget for validation if you only
1067+
specify rules that always take the same amount of time regardless of how large their input is.
1068+
For example, a rule that asserts that `self.foo == 1` does not by itself have any
1069+
risk of rejection on validation resource budget groups.
1070+
But if `foo` is a string and you define a validation rule `self.foo.contains("someString")`, that rule takes
1071+
longer to execute depending on how long `foo` is.
1072+
Another example would be if `foo` were an array, and you specified a validation rule `self.foo.all(x, x > 5)`. The cost system always assumes the worst-case scenario if
10661073
a limit on the length of `foo` is not given, and this will happen for anything that can be iterated
10671074
over (lists, maps, etc.).
10681075

@@ -1081,17 +1088,18 @@ openAPIV3Schema:
10811088
- rule: "self.all(x, x.contains('a string'))"
10821089
```
10831090

1084-
The cost system will not allow this rule. Using `self.all` means calling `contains` on every string in `foo`,
1085-
which in turn will check the given string to see if it contains `'a string'`. Without limits, this is a very
1086-
expensive rule:
1087-
1091+
then the API server rejects this rule on validation budget grounds with error:
10881092
```
10891093
spec.validation.openAPIV3Schema.properties[spec].properties[foo].x-kubernetes-validations[0].rule: Forbidden:
10901094
CEL rule exceeded budget by more than 100x (try simplifying the rule, or adding maxItems, maxProperties, and
10911095
maxLength where arrays, maps, and strings are used)
10921096
```
10931097

1094-
Without limits being set, the estimated cost of this rule will exceed the per-rule cost limit. But if we
1098+
The rejection happens because `self.all` implies calling `contains()` on every string in `foo`,
1099+
which in turn will check the given string to see if it contains `'a string'`. Without limits, this is a very
1100+
expensive rule.
1101+
1102+
If you do not specify any validation limit, the estimated cost of this rule will exceed the per-rule cost limit. But if you
10951103
add limits in the appropriate places, the rule will be allowed:
10961104

10971105
```yaml

0 commit comments

Comments
 (0)