NodePort. When the precondition is met, the policy validates that spec.externalTrafficPolicy is set to Local.

- Validate Services only when
spec.typeisNodePort. - Enforce
spec.externalTrafficPolicy: Localfor those NodePort Services.
Preconditions are evaluated against the incoming admission request. If preconditions pass, the rule’s
validate/mutate/generate actions run; otherwise the rule is skipped. Use preconditions to make policies highly selective and avoid unnecessary validation on unrelated resources.matchnarrows targets to KubernetesServiceresources.preconditionsfurther limit execution to Services whosespec.typeequalsNodePort.- When the precondition passes, the
validateblock enforces thatspec.externalTrafficPolicyisLocal. - If the precondition does not pass (for example, a
ClusterIPService), the rule is skipped.
- Bad NodePort Service (should be rejected)
The admission webhook will block creation when the policy’s
validationFailureAction is enforce. Inspect Kyverno logs and events if an expected create is denied.- Good NodePort Service (should be accepted)
- ClusterIP Service (precondition not met; policy skipped — should be accepted)
- Use
matchto restrict resources to specific kinds (here:Service). - Use
preconditionsto further filter requests before running policy logic. - The
validateblock runs only when preconditions succeed; otherwise the rule is skipped. - For more details, see the Kyverno documentation:
- Kyverno documentation: https://kyverno.io/docs/
- Kubernetes Services: https://kubernetes.io/docs/concepts/services-networking/service/