fix: Add default failure policy for K8sNativeValidation templates#4568
fix: Add default failure policy for K8sNativeValidation templates#4568JaydipGabani with Copilot wants to merge 6 commits into
Conversation
Agent-Logs-Url: https://github.com/open-policy-agent/gatekeeper/sessions/d4a5e66c-314f-4cec-9b97-cec82e05f12f Co-authored-by: JaydipGabani <20255485+JaydipGabani@users.noreply.github.com>
JaydipGabani
left a comment
There was a problem hiding this comment.
@copilot address these changes at all relavent code changes
| The K8s Validating Admission Controller requires both the Validating Admission Policy (VAP) and Validating Admission Policy Binding (VAPB) resources to exist to enforce a policy. Gatekeeper can be configured to generate both of these resources. To generate VAP Bindings for all Constraints, ensure the Gatekeeper | ||
| `--default-create-vap-binding-for-constraints` flag is set to `true`. To generate VAP as part of all Constraint Templates with the VAP CEL engine `K8sNativeValidation`, ensure the Gatekeeper `--default-create-vap-for-templates=true` flag is set to `true`. By default both flags are set to `true` now that the feature is in beta. | ||
|
|
||
| If a K8sNativeValidation source omits `failurePolicy`, Gatekeeper uses `--default-failure-policy`, which defaults to `Fail`, for both Gatekeeper's CEL evaluation and generated VAP resources. |
There was a problem hiding this comment.
This should not be added to versioned documents since this is a new change
There was a problem hiding this comment.
Removed the new documentation from versioned docs in 25a18c4.
| "k8s.io/apiserver/pkg/admission/plugin/webhook/matchconditions" | ||
| ) | ||
|
|
||
| var DefaultFailurePolicy = flag.String("default-failure-policy", string(admissionv1.Fail), "(beta) Failure policy to use when a K8sNativeValidation source omits failurePolicy. Allowed values are Fail or Ignore.") |
There was a problem hiding this comment.
lets rename this flag here and everywhere else
| var DefaultFailurePolicy = flag.String("default-failure-policy", string(admissionv1.Fail), "(beta) Failure policy to use when a K8sNativeValidation source omits failurePolicy. Allowed values are Fail or Ignore.") | |
| var DefaultFailurePolicy = flag.String("default-failure-policy-for-cel-engine", string(admissionv1.Fail), "(beta) Failure policy to use when a K8sNativeValidation source omits failurePolicy. Allowed values are Fail or Ignore.") |
There was a problem hiding this comment.
Renamed the flag to --default-failure-policy-for-cel-engine in code and current docs in 25a18c4.
Agent-Logs-Url: https://github.com/open-policy-agent/gatekeeper/sessions/35837324-09cf-4f4d-8b5c-095e242264bd Co-authored-by: JaydipGabani <20255485+JaydipGabani@users.noreply.github.com>
JaydipGabani
left a comment
There was a problem hiding this comment.
Rename the flag to something explicit--default-k8s-native-validation-failure-policy.
Validate the flag at startup, not lazily when a template is parsed.
Update docs to explicitly contrast this with validatingWebhookFailurePolicy.
| "k8s.io/apiserver/pkg/admission/plugin/webhook/matchconditions" | ||
| ) | ||
|
|
||
| var DefaultFailurePolicy = flag.String("default-failure-policy-for-cel-engine", string(admissionv1.Fail), "(beta) Failure policy to use when a K8sNativeValidation source omits failurePolicy. Allowed values are Fail or Ignore.") |
There was a problem hiding this comment.
lets rename this flag and variable here and everywhere else
| var DefaultFailurePolicy = flag.String("default-failure-policy-for-cel-engine", string(admissionv1.Fail), "(beta) Failure policy to use when a K8sNativeValidation source omits failurePolicy. Allowed values are Fail or Ignore.") | |
| var DefaultFailurePolicyForK8sNativeValidation = flag.String("default-k8s-native-validation-failure-policy", string(admissionv1.Fail), "(beta) Failure policy to use when a K8sNativeValidation source omits failurePolicy. Allowed values are Fail or Ignore.") |
There was a problem hiding this comment.
Renamed the flag and variable to --default-k8s-native-validation-failure-policy / DefaultFailurePolicyForK8sNativeValidation in 373e1c9.
Agent-Logs-Url: https://github.com/open-policy-agent/gatekeeper/sessions/77583064-3ed1-4ba0-b77b-664c7df23201 Co-authored-by: JaydipGabani <20255485+JaydipGabani@users.noreply.github.com>
Implemented in 373e1c9: renamed the flag to |
There was a problem hiding this comment.
Pull request overview
Adds a configurable runtime flag --default-k8s-native-validation-failure-policy that supplies a default failurePolicy for K8sNativeValidation sources when omitted, applied consistently to Gatekeeper's CEL evaluation and generated VAP resources. Default remains Fail for backward compatibility.
Changes:
- Introduce the new flag and a
ValidateDefaultFailurePolicyForK8sNativeValidationstartup check inmain.go;Source.GetFailurePolicy/GetV1Beta1FailurePolicynow fall back to the flag whenFailurePolicyis nil, whileSource.Validateonly validates explicit values. - Add unit tests covering default behavior, startup validation, and VAP generation using the default.
- Document the new flag in runtime flags and the VAP docs, clarifying it is distinct from
validatingWebhookFailurePolicy.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/drivers/k8scel/schema/schema.go | Adds flag, refactors failure policy resolution/validation. |
| pkg/drivers/k8scel/schema/schema_test.go | Adds tests for default policy resolution and startup validation. |
| pkg/drivers/k8scel/transform/make_vap_objects_test.go | Verifies generated VAP uses default failure policy. |
| main.go | Validates flag value at startup, fails fast on invalid input. |
| website/docs/runtime-flags.md | Documents the new flag. |
| website/docs/validating-admission-policy.md | Clarifies default behavior and distinction from webhook failure policy. |
Signed-off-by: Jaydip Gabani <gabanijaydip@gmail.com>
Signed-off-by: Jaydip Gabani <gabanijaydip@gmail.com>
ConstraintTemplates that omit
failurePolicyneed a configurable default applied consistently to Gatekeeper CEL evaluation and generated VAP resources. The default remains fail-closed for backward compatibility.Runtime flag
--default-k8s-native-validation-failure-policy, defaulting toFail.FailorIgnore.Failure policy defaulting
source.failurePolicyis omitted.failurePolicyvalues.ValidatingAdmissionPolicyspecs.Coverage and docs
validatingWebhookFailurePolicy.Example: