Skip to content

Commit 7a5b077

Browse files
committed
polish.
1 parent 8a3cc99 commit 7a5b077

File tree

1 file changed

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

1 file changed

+17
-6
lines changed

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ spec:
7474
- expression: object.spec.template.spec.containers.all(c, !has(c.securityContext) || !has(c.securityContext.Privileged) || !c.securityContext.Privileged)
7575
message: 'all containers must NOT set privileged to true'
7676
```
77-
Create the policy with `kubectl`. Great, no complain so far. But let's take a look at its status.
77+
Create the policy with `kubectl`. Great, no complain so far. But let's get the policy object back and take a look at its status.
7878
```yaml
7979
status:
8080
typeChecking:
@@ -89,7 +89,11 @@ Create the policy with `kubectl`. Great, no complain so far. But let's take a lo
8989
| ...............................................................................................................................^
9090
9191
```
92-
Ah, seems like a copy-paste error. Let's correct it real quick.
92+
The policy was checked against its matched type, which is `apps/v1.Deployment`.
93+
Looking at the `fieldRef`, the problem was with the 3rd expression (index starts with 0)
94+
The expression in question accessed an undefined `Privileged` field.
95+
Ahh, looks like it was a copy-and-paste error. The field name should be in lowercase.
96+
9397
```yaml
9498
apiVersion: admissionregistration.k8s.io/v1
9599
kind: ValidatingAdmissionPolicy
@@ -167,10 +171,17 @@ Warning: Validation failed for ValidatingAdmissionPolicy 'pod-security.policy.ex
167171
Warning: Validation failed for ValidatingAdmissionPolicy 'pod-security.policy.example.com' with binding 'pod-security.policy-binding.example.com': all containers must set privileged to false
168172
Error from server: error when creating "STDIN": admission webhook "cel-shim.example.com" denied the request: [container "nginx" must set RunAsNonRoot to true in its SecurityContext, container "nginx" must set ReadOnlyRootFilesystem to true in its SecurityContext, container "nginx" must NOT set AllowPrivilegeEscalation to true in its SecurityContext, container "nginx" must NOT set Privileged to true in its SecurityContext]
169173
```
170-
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-
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.
174+
Looks great! The policy and the webhook give equivalent results.
175+
After a few other cases, when we are confident with our policy, maybe it is time to do some cleanup.
176+
177+
- For every expression, we repeat access to `object.spec.template.spec.containers` and each `securityContext` in every expression;
178+
- There is a pattern of checking presence of a field and then access it, which looks a bit verbose.
179+
180+
Fortunately, with the release of Kubernetes 1.28, we have new solutions for both issues.
181+
Variable Composition allows us to extract repeated sub-expressions into their own variables.
182+
CEL library has also added support for [CEL optionals](https://github.com/google/cel-spec/wiki/proposal-246), which are excellent to work with fields that are, well, optional.
183+
184+
With both features in mind, let's refactor the policy a bit.
174185
```yaml
175186
apiVersion: admissionregistration.k8s.io/v1
176187
kind: ValidatingAdmissionPolicy

0 commit comments

Comments
 (0)