Skip to content

Commit a1699dc

Browse files
jpbetzTim Bannister
andauthored
Apply improvements from Tim Bannister
Co-authored-by: Tim Bannister <[email protected]>
1 parent 6423b4e commit a1699dc

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

content/en/blog/_posts/2022-07-27-crd-validation-rules-graduate-to-beta.md

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
layout: blog
33
title: "Kubernetes 1.25: CustomResourceDefinition Validation Rules Graduate to Beta"
44
date: 2022-07-27
5-
slug: tbd
6-
canonicalUrl: tbd
5+
slug: crd-validation-rules-beta
76
---
87

98
**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
4241
| `self.created + self.ttl < self.expired` | Validate that 'expired' date is after a 'create' date plus a 'ttl' duration |
4342

4443

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.
4645

4746
## Why CEL?
4847

@@ -59,21 +58,21 @@ Benefits of using validation rules when compared with validation webhooks:
5958
* Cluster administrators benefit by no longer having to install, upgrade and operate webhooks for the purposes of CRD validation.
6059
* 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.
6160

62-
## Getting Started with Validation Rules
61+
## Getting started with validation rules
6362

64-
### Writing Validation Rules in OpenAPIv3 Schemas
63+
### Writing validation rules in OpenAPIv3 schemas
6564

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.
6766

68-
Best practices for validation rules:
67+
Good practices for CRD validation rules:
6968

7069
* Scope validation rules as close as possible to the fields(s) they validate.
7170
* Use multiple rules when validating independent constraints.
7271
* Do not use validation rules for validations already
7372
* 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.
7473
* Use `x-kubernetes-int-or-string`, `x-kubernetes-embedded-type` and `x-kubernetes-list-type=(set|map)` were appropriate.
7574

76-
Best practice examples:
75+
Examples of good practice:
7776

7877
| Validation | Best Practice | Example(s) |
7978
| - | - | - |
@@ -82,17 +81,17 @@ Best practice examples:
8281
| 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> |
8382
| 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>
8483

85-
## Using Transition Rules
84+
## CRD transition rules
8685

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.
8887

8988
Transition rule examples:
9089

9190
| Transition Rule | Purpose |
9291
| - | - |
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 field never 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). |
9695
| `self >= oldSelf` | Validate that a number is monotonically increasing.|
9796

9897

@@ -115,29 +114,29 @@ Examples of function libraries in use:
115114
| `int(self.find('^[0-9]*')) < 100` | Validate that a string starts with a number less than 100. |
116115
| `self.isSorted()` | Validates that a list is sorted. |
117116

118-
## Resource Limits
117+
## Resource use and limits
119118

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.
121120

122-
### Estimated Cost Limit
121+
### Estimated cost limit
123122

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.
125124

126-
Best Practice:
125+
Good practice:
127126

128127
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.
129128

130-
### Runtime Cost Limit
129+
### Runtime cost limits for CRD validation rules
131130

132131
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.
133132

134133
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.
135134

136135
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.
137136

138-
## Future Work
137+
## Future work
139138

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!
141140

142141
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).
143142

0 commit comments

Comments
 (0)