Skip to content

Commit e73e7e3

Browse files
committed
Add more examples, remove obsolete info, add note about oldSelf in graduation criteria
1 parent 645a7ec commit e73e7e3

File tree

1 file changed

+14
-15
lines changed
  • keps/sig-api-machinery/2876-crd-validation-expression-language

1 file changed

+14
-15
lines changed

keps/sig-api-machinery/2876-crd-validation-expression-language/README.md

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ Example Validation Rules:
232232
| `self.minReplicas <= self.replicas <= self.maxReplicas` | Validate that the three fields defining replicas are ordered appropriately |
233233
| `'Available' in self.stateCounts` | Validate that an entry with the 'Available' key exists in a map |
234234
| `(size(self.list1) == 0) != (size(self.list2) == 0)` | Validate that one of two lists is non-empty, but not both |
235+
| `!('MY_ENV' in self.envars) || self['MY_ENV'].matches('^[a-zA-Z]*$')` | Validate the value of a map for a specific key, if it is in the map |
235236
| `has(self.expired) && self.created + self.ttl < self.expired` | Validate that 'expired' date is after a 'create' date plus a 'ttl' duration |
236237
| `self.health.startsWith('ok')` | Validate a 'health' string field has the prefix 'ok' |
237238
| `self.widgets.exists(w, w.key == 'x' && w.foo < 10)` | Validate that the 'foo' property of a listMap item with a key 'x' is less than 10 |
@@ -257,7 +258,7 @@ is scoped to.
257258
name. As the example showing below. When we want to put cel validation on ToySpec, the field
258259
name as `spec` has not been identified yet which makes rule hard to define.
259260

260-
```azure
261+
```
261262
// +kubebuilder:validation:XValidator=
262263
type ToySpec struct {
263264
fieldSample string `json:"fieldSample"`
@@ -307,30 +308,27 @@ like the `all` macro, e.g. `self.all(listItem, <predicate>)` or `self.all(mapKey
307308
it will be escaped by prepending a _ prefix. To prevent this from causing a subsequent collision, properties named with a CEL keyword and a `_` prefix will be
308309
prefixed by `__` (generally, N+1 the existing number of `_`s).
309310

310-
- Only property names of the form `[a-zA-Z_.-/][a-zA-Z0-9_.-/]*` are accessible.
311-
If a property name is "self" or matches with a [reserved language identifier](https://github.com/google/cel-spec/blob/v0.6.0/doc/langdef.md#values)
312-
(`int`, `uint`, `double`, `bool`, `string`, `bytes`, `list`, `map`, `null_type`, `type`), it is
313-
not escaped, but it is excluded from the bound variables and can only be accessed via
314-
"self.{property name}". All other accessible property names are escaped according to the following rules
315-
when accessed in the expression:
311+
- Only property names of the form `[a-zA-Z_.-/][a-zA-Z0-9_.-/]*` are accessible and are escaped
312+
according to the following rules when accessed in the expression:
316313
- '__' escapes to '__underscores__'
317314
- '.' escapes to '__dot__'
318315
- '-' escapes to '__dash__'
319316
- '/' escapes to '__slash__'
320-
- CEL RESERVED keywords escape to '__{keyword}__'. The keywords are: "true", "false", "null",
321-
"in", "as", "break", "const", "continue", "else", "for", "function", "if", "import", "let",
322-
"loop", "package", "namespace", "return".
317+
- Property names that match a CEL RESERVED keyword exactly escape to '__{keyword}__'. The
318+
keywords are: "true", "false", "null", "in", "as", "break", "const", "continue", "else", "for",
319+
"function", "if", "import", "let", "loop", "package", "namespace", "return".
323320

324321
- Rules may be written at the root of an object, and may make field selection into any fields
325322
declared in the OpenAPIv3 schema of the CRD as well as `apiVersion`, `kind`, `metadata.name` and
326323
`metadata.generateName`. This includes selection of fields in both the `spec` and `status` in the
327-
same expression, e.g. `self.status.quantity <= self.spec.maxQuantity`. Because CRDs only allow the `name`
328-
and `generateName` to be declared in the `metadata` of an object, these are the only metadata
329-
fields that may be validated using CEL validator rules. For example,
324+
same expression, e.g. `self.status.quantity <= self.spec.maxQuantity`. Because CRDs only allow the
325+
`name` and `generateName` to be declared in the `metadata` of an object, these are the only
326+
metadata fields that may be validated using CEL validator rules. For example,
330327
`self.metadata.name.endsWith('mySuffix')` is allowed, but `size(self.metadata.labels) < 3` it not
331328
allowed. The limit on which `metadata` fields may be validated is an intentional design choice
332-
(that aims to keep metadata behavior uniform across types) and applies to all validation
333-
mechanisms (e.g. the OpenAPIV3 `maxItems` restriction), not just CEL validator rules.
329+
(that aims to allow for generic access to labels and annotations across all kinds) and applies to
330+
all validation mechanisms (e.g. the OpenAPIV3 `maxItems` restriction), not just CEL validator
331+
rules. xref rule 4 in [specifying a structural schema](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#specifying-a-structural-schema).
334332

335333
- We plan to allow access to the current state of the object to allow validation rules to check the
336334
new value against the current value, e.g. for immutability checks (for validation racheting we would
@@ -601,6 +599,7 @@ developers to test their validation rules.
601599

602600
#### Beta
603601

602+
- Resolve topic of what support we should provide for access to the previous versions of object (ie. 'oldSelf' feature)
604603
- x-kubernetes-int-or-string is upgraded to use a union type of just int or string, not a dynamic type (CEL go support is planned in lates 2021)
605604
- Understanding of upper bounds of CPU/memory usage and appropriate limits set to prevent abuse.
606605
- Build-in macro/function library is comprehensive and stable (any changes to this will be a breaking change)

0 commit comments

Comments
 (0)