You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/en/blog/_posts/2022-07-27-crd-validation-rules-graduate-to-beta.md
+20-21Lines changed: 20 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,8 +2,7 @@
2
2
layout: blog
3
3
title: "Kubernetes 1.25: CustomResourceDefinition Validation Rules Graduate to Beta"
4
4
date: 2022-07-27
5
-
slug: tbd
6
-
canonicalUrl: tbd
5
+
slug: crd-validation-rules-beta
7
6
---
8
7
9
8
**Authors:** Joe Betz (Google), Kermit Alexander (Google)
@@ -42,7 +41,7 @@ Validation rules support a wide range of use cases. To get a sense of some of th
42
41
|`self.created + self.ttl < self.expired`| Validate that 'expired' date is after a 'create' date plus a 'ttl' duration |
43
42
44
43
45
-
Validation rules are expressive and flexible. See the [Validation Rules documentation](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation-rules) to learn more about what validation rules are capable of.
44
+
Validation rules are expressive and flexible. See the [Validation Rules documentation](/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#validation-rules) to learn more about what validation rules are capable of.
46
45
47
46
## Why CEL?
48
47
@@ -59,21 +58,21 @@ Benefits of using validation rules when compared with validation webhooks:
59
58
* Cluster administrators benefit by no longer having to install, upgrade and operate webhooks for the purposes of CRD validation.
60
59
* Cluster operability improves because CRD validation no longer requires a remote call to a webhook endpoint, eliminating a potential point of failure in the request-serving-path of the Kubernetes API server. This allows clusters to retain high availability while scaling to larger amounts of installed CRD extensions, since expected control plane availability would otherwise decrease with each additional webhook installed.
61
60
62
-
## Getting Started with Validation Rules
61
+
## Getting started with validation rules
63
62
64
-
### Writing Validation Rules in OpenAPIv3 Schemas
63
+
### Writing validation rules in OpenAPIv3 schemas
65
64
66
-
Validation rules can be declared at any level of a CRD's OpenAPIv3 schema and are scoped to their location in the schema they are declared.
65
+
You can define validation rules for any level of a CRD's OpenAPIv3 schema. Validation rules are automatically scoped to their location in the schema where they are declared.
67
66
68
-
Best practices for validation rules:
67
+
Good practices for CRD validation rules:
69
68
70
69
* Scope validation rules as close as possible to the fields(s) they validate.
71
70
* Use multiple rules when validating independent constraints.
72
71
* Do not use validation rules for validations already
73
72
* Use OpenAPIv3 [value validations](https://swagger.io/specification/#properties) (`maxLength`, `maxItems`, `maxProperties`, `required`, `enum`, `minimum`, `maximum`, ..) and [string formats](https://swagger.io/docs/specification/data-models/data-types/#format) where available.
74
73
* Use `x-kubernetes-int-or-string`, `x-kubernetes-embedded-type` and `x-kubernetes-list-type=(set|map)` were appropriate.
75
74
76
-
Best practice examples:
75
+
Examples of good practice:
77
76
78
77
| Validation | Best Practice | Example(s) |
79
78
| - | - | - |
@@ -82,17 +81,17 @@ Best practice examples:
82
81
| Require a date-time be more recent than a particular timestamp. | Use OpenAPIv3 string formats to declare that the field is a date-time. Use validation rules to compare it to a particular timestamp. | <pre>type: string<br>format: date-time<br>x-kubernetes-validations:<br> - rule: "self >= timestamp('2000-01-01T00:00:00.000Z')"</pre> |
83
82
| Require two sets to be disjoint. | Use x-kubernetes-list-type to validate that the arrays are sets. <br>Use validation rules to validate the sets are disjoint. | <pre>type: object<br>properties:<br> set1:<br> type: array<br> x-kubernetes-list-type: set<br> set2: ...<br> x-kubernetes-validations:<br> - rule: "!self.set1.all(e, !(e in self.set2))"</pre>
84
83
85
-
## Using Transition Rules
84
+
## CRD transition rules
86
85
87
-
[Transition Rules](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#transition-rules) make it possible to compare the new state against the old state of a resource in validation rules. Any validation rule that uses 'oldSelf' is a transition rule. Transition rules are only evaluated when an old value and new value exist.
86
+
[Transition Rules](/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#transition-rules) make it possible to compare the new state against the old state of a resource in validation rules. You use transition rules to make sure that the cluster's API server does not accept invalid state transitions. A transition rule is a validation rule that references 'oldSelf'. The API server only evaluates transition rules when both an old value and new value exist.
88
87
89
88
Transition rule examples:
90
89
91
90
| Transition Rule | Purpose |
92
91
| - | - |
93
-
|`self == oldSelf`|Validate that a required field is immutable once it is set. This does not prevent an optional field from transitioning from unset to set or set to unset.|
94
-
| on parent of field:`has(self.field) == has(oldSelf.field)`<br>on field: `self == oldSelf`|Validate that an optional fieldnever changes after the resource is created.|
95
-
|`self.all(x, x in oldSelf)`|Validate that a set is append-only.|
92
+
|`self == oldSelf`|For a required field, make that field immutable once it is set. For an optional field, only allow transitioning from unset to set, or from set to unset.|
93
+
|(on parent of field)`has(self.field) == has(oldSelf.field)`<br>on field: `self == oldSelf`|Make a field immutable: validate that a field, even if optional, never changes after the resource is created (for a required field, the previous rule is simpler). |
94
+
|`self.all(x, x in oldSelf)`|Only allow adding items to a field that represents a set (prevent removals). |
96
95
|`self >= oldSelf`| Validate that a number is monotonically increasing.|
97
96
98
97
@@ -115,29 +114,29 @@ Examples of function libraries in use:
115
114
|`int(self.find('^[0-9]*')) < 100`| Validate that a string starts with a number less than 100. |
116
115
|`self.isSorted()`| Validates that a list is sorted. |
117
116
118
-
## Resource Limits
117
+
## Resource use and limits
119
118
120
-
To prevent CEL evaluation from consuming excessive compute resources, validation rules impose some limits. These limits are based on CEL "cost units", a platform and machine independent measure of execution cost. As a result, the limits are the same regardless of where they are enforced.
119
+
To prevent CEL evaluation from consuming excessive compute resources, validation rules impose some limits. These limits are based on CEL _cost units_, a platform and machine independent measure of execution cost. As a result, the limits are the same regardless of where they are enforced.
121
120
122
-
### Estimated Cost Limit
121
+
### Estimated cost limit
123
122
124
-
CEL is, by design, non-Turing-complete in such a way that the halting problem isn’t a concern. CEL takes advantage of this design choice to include an "estimated cost" subsystem that can statically compute the worst case run time cost of any CEL expression. Validation rules are [integrated with the estimated cost system](https://kubernetes.io/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#resource-use-by-validation-functions) and disallow CEL expressions from being included in CRDs if they have a sufficiently poor (high) estimated cost. The estimated cost limit is set quite high and typically requires an O(n^2) or worse operation, across something of unbounded size, to be exceeded. Fortunately the fix is usually quite simple: because the cost system is aware of size limits declared in the CRD's schema, CRD authors can add size limits to the CRD's schema (`maxItems` for arrays, `maxProperties` for maps, `maxLength` for strings) to reduce the estimated cost.
123
+
CEL is, by design, non-Turing-complete in such a way that the halting problem isn’t a concern. CEL takes advantage of this design choice to include an "estimated cost" subsystem that can statically compute the worst case run time cost of any CEL expression. Validation rules are [integrated with the estimated cost system](o/docs/tasks/extend-kubernetes/custom-resources/custom-resource-definitions/#resource-use-by-validation-functions) and disallow CEL expressions from being included in CRDs if they have a sufficiently poor (high) estimated cost. The estimated cost limit is set quite high and typically requires an O(n^2) or worse operation, across something of unbounded size, to be exceeded. Fortunately the fix is usually quite simple: because the cost system is aware of size limits declared in the CRD's schema, CRD authors can add size limits to the CRD's schema (`maxItems` for arrays, `maxProperties` for maps, `maxLength` for strings) to reduce the estimated cost.
125
124
126
-
Best Practice:
125
+
Good practice:
127
126
128
127
Set `maxItems`, `maxProperties` and `maxLength` on all array, map (`object` with `additionalProperties`) and string types in CRD schemas! This results in lower and more accurate estimated costs and generally makes a CRD safer to use.
129
128
130
-
### Runtime Cost Limit
129
+
### Runtime cost limits for CRD validation rules
131
130
132
131
In addition to the estimated cost limit, CEL keeps track of actual cost while evaluating a CEL expression and will halt execution of the expression if a limit is exceeded.
133
132
134
133
With the estimated cost limit already in place, the runtime cost limit is rarely encountered. But it is possible. For example, it might be encountered for a large resource composed entirely of a single large list and a validation rule that is either evaluated on each element in the list, or traverses the entire list.
135
134
136
135
CRD authors can ensure the runtime cost limit will not be exceeded in much the same way the estimated cost limit is avoided: by setting `maxItems`, `maxProperties` and `maxLength` on array, map and string types.
137
136
138
-
## Future Work
137
+
## Future work
139
138
140
-
We look forward to working with the community on the adoption of Validation Rules and hope to see it promoted to GA in the near future!
139
+
We look forward to working with the community on the adoption of CRD Validation Rules, and hope to see this feature promoted to general availability in an upcoming Kubernetes release!
141
140
142
141
There is a growing community of Kubernetes contributors thinking about how to make it possible to write extensible admission controllers using CEL as a substitute for admission webhooks for policy enforcement use cases. Anyone interested should reach out to us on the usual [SIG API Machinery](https://github.com/kubernetes/community/tree/master/sig-api-machinery) channels or via slack at [#sig-api-machinery-cel-dev](https://kubernetes.slack.com/archives/C02TTBG6LF4).
0 commit comments