Skip to content

Commit 3172343

Browse files
Update for inclusion of string.format in CEL.
1 parent 9d63597 commit 3172343

File tree

1 file changed

+13
-7
lines changed
  • keps/sig-api-machinery/3488-cel-admission-control

1 file changed

+13
-7
lines changed

keps/sig-api-machinery/3488-cel-admission-control/README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ spec:
407407
validations:
408408
- name: max-replicas
409409
expression: "object.spec.replicas <= params.maxReplicas"
410-
messageExpression: "'object.spec.replicas must be no greater than ' + string(params.maxReplicas)"
410+
messageExpression: "'object.spec.replicas must be no greater than {}'.format([params.maxReplicas])"
411411
reason: Invalid
412412
# ...other rule related fields here...
413413
```
@@ -850,6 +850,9 @@ Policy definitions:
850850
- Each validation may define a message:
851851
- `message` - plain string message
852852
- `messageExpression: "<cel expression>"` (mutually exclusive with `message`)
853+
- As part of [the KEP update to add expression composition](https://github.com/kubernetes/enhancements/pull/3669/files),
854+
expressions defined under `variables` will be accessible from `messageExpression`
855+
- While messageExpression is a CEL expression, it does not factor into the runtime cost limit.
853856
- If `message` and `messageExpression` are absent, `expression` and `name`
854857
will be included in the failure message
855858
- If `messageExpression` results in an error: `expression` and `name` will be
@@ -871,7 +874,7 @@ spec:
871874
validations:
872875
- expression: "self.name.startsWith('xyz-')"
873876
name: name-prefix
874-
messageExpression: "self.name + ' must start with xyz-'"
877+
messageExpression: "'{} must start with xyz-'.format([self.name])"
875878
reason: Unauthorized
876879
- expression: "self.name.contains('bad')"
877880
name: bad-name
@@ -880,7 +883,7 @@ spec:
880883
reason: Invalid
881884
- expression: "self.name.contains('suspicious')"
882885
name: suspicious-name
883-
messageExpression: "self.name + ' contains suspicious'"
886+
messageExpression: "'{} contains suspicious'.format([self.name])"
884887
code: 400
885888
reason: Invalid
886889
```
@@ -1223,7 +1226,10 @@ Plan:
12231226
To consider:
12241227

12251228
- labelSelector evaluation functions or other match evaluator functions ([original comment thread](https://github.com/kubernetes/enhancements/pull/3492#discussion_r981747317))
1226-
- `string.format(string, list(dyn))` to make `messageExpression` more convenient.
1229+
1230+
To implement:
1231+
1232+
- `string.format` for CEL/cel-go ([tracking PR](https://github.com/google/cel-go/pull/617))
12271233

12281234
#### Audit Annotations
12291235

@@ -2872,7 +2878,7 @@ For example, to validate all containers:
28722878
validations:
28732879
- scope: "spec.containers[*]"
28742880
expression: "scope.name.startsWith('xyz-')"
2875-
messageExpression: "scope.name + 'does not start with \'xyz\''"
2881+
messageExpression: "'{} does not start with \'xyz\'-'.format([scope.name])"
28762882
```
28772883

28782884
To make it possible to access the path information in the scope, we can offer a
@@ -2886,7 +2892,7 @@ spec.x[xKey].y[yIndex].field
28862892
validations:
28872893
- scope: "x[xKey].y[yIndex].field"
28882894
expression: "scope.startsWith('xyz-')"
2889-
messageExpression: "scopePath.xKey + ', ' + scopePath.yIndex + ': some problem'"
2895+
messageExpression: "'{}, {}: some problem'.format([scopePath.xKey, scopePath.yIndex])"
28902896
```
28912897

28922898
Prior art:
@@ -2907,7 +2913,7 @@ Note: We considered extending to a list of scopes, e.g.:
29072913
validations:
29082914
- scopes: ["spec.containers[*]", "initContainers[*]", "spec.ephemeralContainers[*]"]
29092915
expression: "scope.name.startsWith('xyz-')"
2910-
messageExpression: "scope.name + ' does not start with \'xyz\''"
2916+
messageExpression: "'{} does not start with \'xyz\''.format([scope.name])"
29112917
```
29122918

29132919
But feedback was this is signficantly more difficult to understand.

0 commit comments

Comments
 (0)