You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
+
93
97
```yaml
94
98
apiVersion: admissionregistration.k8s.io/v1
95
99
kind: ValidatingAdmissionPolicy
@@ -167,10 +171,17 @@ Warning: Validation failed for ValidatingAdmissionPolicy 'pod-security.policy.ex
167
171
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
168
172
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]
169
173
```
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.
0 commit comments