Skip to content

Commit 2079915

Browse files
committed
CEL optionals.
1 parent 7ab710f commit 2079915

File tree

1 file changed

+8
-6
lines changed
  • content/en/blog/_posts/2024-04-01-validating-admission-policy-ga

1 file changed

+8
-6
lines changed

content/en/blog/_posts/2024-04-01-validating-admission-policy-ga/index.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ Warning: Validation failed for ValidatingAdmissionPolicy 'pod-security.policy.ex
168168
Error from server: error when creating "STDIN": admission webhook "webhook.example.com" denied the request: container "nginx" must set RunAsNonRoot to true in its SecurityContext
169169
```
170170
Not quite the exact same behavior but good enough. After a few other cases, when we are confident with our policy, maybe it is time for some refactoring.
171-
We can extract repeated sub-expressions into their own variables.
171+
With Variable Composition introduced in beta, we can extract repeated sub-expressions into their own variables.
172+
Also, In Kubernetes 1.28, the CEL library added support for [CEL optionals](https://github.com/google/cel-spec/wiki/proposal-246).
173+
The final result is as follows.
172174
```yaml
173175
apiVersion: admissionregistration.k8s.io/v1
174176
kind: ValidatingAdmissionPolicy
@@ -186,14 +188,14 @@ spec:
186188
- name: containers
187189
expression: object.spec.template.spec.containers
188190
- name: securityContexts
189-
expression: 'variables.containers.map(c, has(c.securityContext) ? c.securityContext : {})'
191+
expression: 'variables.containers.map(c, c.?securityContext)'
190192
validations:
191-
- expression: variables.securityContexts.all(c, has(c.runAsNonRoot) && c.runAsNonRoot)
193+
- expression: variables.securityContexts.all(c, c.?runAsNonRoot == optional.of(true))
192194
message: 'all containers must set runAsNonRoot to true'
193-
- expression: variables.securityContexts.all(c, has(c.readOnlyRootFilesystem) && c.readOnlyRootFilesystem)
195+
- expression: variables.securityContexts.all(c, c.?readOnlyRootFilesystem == optional.of(true))
194196
message: 'all containers must set readOnlyRootFilesystem to true'
195-
- expression: variables.securityContexts.all(c, !has(c.allowPrivilegeEscalation) || !c.allowPrivilegeEscalation)
197+
- expression: variables.securityContexts.all(c, c.?allowPrivilegeEscalation != optional.of(true))
196198
message: 'all containers must set allowPrivilegeEscalation to false'
197-
- expression: variables.securityContexts.all(c, !has(c.privileged) || !c.privileged)
199+
- expression: variables.securityContexts.all(c, c.?privileged != optional.of(true))
198200
message: 'all containers must set privileged to false'
199201
```

0 commit comments

Comments
 (0)