Skip to content

Commit 499e28a

Browse files
KEP-2876: Add messageExpression (kubernetes#3747)
* Update with messageExpression info. * Add information on variable availability/runtime cost. * Allow both message and messageExpression to be present. * CRD writes will succeed if both message and messageExpression are given. * Add information on string.format checks. * Fail if runtime cost limit is exceeded. * Remove mention of admission policy KEP update. * Discard messageExpression if it doesn't statically eval to a string. * messageExpression must statically evaluate to a string, and also any runtime messageExpression errors are logged. * Reorganize `messageExpression` rules section. * Indent messageExpression-related topics. * Update messageExpression example to use proper string.format syntax.
1 parent fceeb86 commit 499e28a

File tree

1 file changed

+19
-2
lines changed
  • keps/sig-api-machinery/2876-crd-validation-expression-language

1 file changed

+19
-2
lines changed

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ kind: CustomResourceDefinition
230230
spec:
231231
x-kubernetes-validations:
232232
- rule: "self.minReplicas <= self.maxReplicas"
233-
message: "minReplicas cannot be larger than maxReplicas"
233+
messageExpression: "'minReplicas (%d) cannot be larger than maxReplicas (%d)'.format([self.minReplicas, self.maxReplicas])"
234234
type: object
235235
properties:
236236
minReplicas:
@@ -258,9 +258,26 @@ Example Validation Rules:
258258

259259

260260
- Each validator may have multiple validation rules.
261-
262261
- Each validation rule has an optional 'message' field for the error message that
263262
will be surfaced when the validation rule evaluates to false.
263+
- As an alternative to the `message` field, there is also a
264+
`messageExpression` field that represents a CEL expression that evaluates to a
265+
string that will be used when the validation rule evaluates to false.
266+
- There are several validation rules for the `messageExpression` field:
267+
- The `messageExpression` field must statically evaluate to a string.
268+
- There are several compile-time checks performed on CEL's `string.format` function that can affect `messageExpression`:
269+
- If the formatting string passed to `string.format` is a constant, then the formatting string will be checked at
270+
compile-time that it parses correctly.
271+
- If the `arg` array passed to `string.format` is a literal, and the formatting string is a constant, then
272+
at compile-time `arg`'s cardinality will be checked against the formatting string. Passing too few/too many arguments
273+
compared to what the formatting string expects is not allowed.
274+
- Type checking of each individual argument in `arg` will be done against what is expected in the formatting string
275+
to ensure the proper formatting clauses are used on each member of arg.
276+
- The runtime cost limit must not be exceeded during `messageExpression` execution (executing `messageExpression` counts towards that limit).
277+
- If `messageExpression` results in a runtime error, then `message` will be used instead if non-empty. If `message`
278+
is empty, then the default error message is used instead. In either case (if `message` or the default error message is
279+
used) the CEL error that `messageExpression` failed with will be logged.
280+
- If both `message` and `messageExpression` are present, `messageExpression` will take priority.
264281

265282
- The validator will be scoped to the location of the `x-kubernetes-validations`
266283
extension in the schema. In the above example, the validator is scoped to the

0 commit comments

Comments
 (0)