After upgrading to Kubernetes 1.30, a ValidatingAdmissionPolicy fails with `expression ... no such key: securityContext`
AdvancedTroubleshootingKubernetes admission control and Pod Security
Answer
Likely causes → CEL expressions access optional fields without has(), so pods without that field cause evaluation errors.
Diagnose →
kubectl get validatingadmissionpolicy require-nonroot -o yaml | yq '.spec.validations'
kubectl get validatingadmissionpolicy require-nonroot -o jsonpath='{.status.typeChecking}'
Fix → Guard optional fields:
expression: >
object.spec.containers.all(c, has(c.securityContext) &&
has(c.securityContext.runAsNonRoot) && c.securityContext.runAsNonRoot == true)
Also set failurePolicy deliberately.
Prevent → Test VAPs in a kind cluster in CI with good and bad pod fixtures.
More on Kubernetes admission control and Pod Security
`Error from server: admission webhook "validate.kyverno.svc-fail" denied the request: policy Deployment/prod/api for resource violation: require-run-as-nonroot`BeginnerPods do not appear, but `kubectl apply` of the Deployment succeededBeginner`pods "api-7d9" is forbidden: violates PodSecurity "restricted:latest": allowPrivilegeEscalation != false, unrestricted capabilities, seccompProfile`BeginnerEnabling `enforce=restricted` on a namespace did not evict the existing non-compliant podsBeginner`Error from server (InternalError): Internal error occurred: failed calling webhook "validation.gatekeeper.sh": ... context deadline exceeded` blocks every deployIntermediateKyverno is down and now the whole cluster, including Kyverno's own namespace, cannot be fixed because writes are rejectedAdvanced