/policy-governance-skills:kyverno-policy-managementfrom policy-governance-skills
Guides writing Kyverno ClusterPolicy/Policy resources using YAML-native validate/mutate/generate rule syntax. Covers audit-vs-enforce rollout, PolicyReport-based reporting, and tradeoffs vs OPA/Gatekeeper.
Ships in policy-governance-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Kyverno is a Kubernetes-native policy engine that expresses admission
policies as plain Kubernetes YAML (ClusterPolicy/Policy custom
resources) instead of a separate policy language — a team that already
reads and writes Kubernetes manifests can read and modify a Kyverno rule
without learning Rego. Beyond validation (the OPA/Gatekeeper-equivalent
"allow or deny this resource"), Kyverno natively supports mutate
rules (rewrite or inject fields into a resource on admission — e.g. add a
sidecar, default a label) and generate rules (auto-create a companion
resource when a triggering resource appears — e.g. a default
NetworkPolicy or ResourceQuota whenever a new Namespace is created),
both of which OPA/Gatekeeper handles less directly. This skill covers
Kyverno's actual rule syntax across all three rule types, its
background/validationFailureAction audit-vs-enforce controls, and
PolicyReport output for visibility — and gives explicit guidance on
when Kyverno's YAML-native approach is the better fit than OPA/Gatekeeper
versus when Rego's generality is worth the learning-curve cost.
ClusterPolicy or namespaced Policy to
validate, mutate, or auto-generate Kubernetes resources, without
writing Rego.mutate rule use case Kyverno
handles more directly than Gatekeeper.NetworkPolicy for every new
Namespace) — a generate rule use case.validate rule that isn't blocking (or is
over-blocking) resources it should, and needs help debugging the
pattern-matching syntax.PolicyReport /
ClusterPolicyReport) surfaced to a dashboard or CI step.kyverno.io/v1 and kyverno.io/v2beta1 API groups have both existed
across recent versions — check which API version the installed
Kyverno release expects, since field names for match/exclude blocks
have shifted between them).pattern/anyPattern blocks, JMESPath for dynamic values) is still
necessary; it is simpler than Rego but not zero-learning-curve.kyverno CLI installed locally for kyverno test (offline unit testing
against sample resources) before deploying policies to a live cluster.validationFailureAction: Audit first, not Enforce — identical
discipline to Gatekeeper's dryrun, and for the same reason (surfacing
what a policy would have blocked before it can break a legitimate
deploy).generate rules specifically: understand that generated resources
can persist after the triggering policy is deleted unless
generateExisting/cleanup behavior is deliberately configured — treat
generate-rule rollout with extra care since it creates new cluster
state, not just gates existing requests.Write a validate rule in Audit mode first, using Kyverno's
pattern block (structural match against the resource) rather than
Rego:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-run-as-non-root
spec:
validationFailureAction: Audit # start here, not Enforce
background: true # also evaluate existing resources, not just new admissions
rules:
- name: check-runAsNonRoot
match:
any:
- resources:
kinds: ["Pod"]
validate:
message: "Containers must set securityContext.runAsNonRoot: true"
pattern:
spec:
securityContext:
runAsNonRoot: true
Use anyPattern for "one of several acceptable shapes" rather than
a single rigid pattern, when more than one configuration is
compliant:
validate:
message: "Must use either a read-only root filesystem or an explicit exception annotation"
anyPattern:
- spec:
containers:
- securityContext:
readOnlyRootFilesystem: true
- metadata:
annotations:
policy.example.com/rootfs-exception: "?*"
Review PolicyReport/ClusterPolicyReport results generated
during the audit period before enforcing:
kubectl get clusterpolicyreport -o wide
kubectl get policyreport -A -o jsonpath='{.items[*].summary}'
Switch to Enforce once audit is clean, with a documented,
time-boxed exclusion for genuine exceptions:
spec:
validationFailureAction: Enforce
rules:
- name: check-runAsNonRoot
match:
any:
- resources:
kinds: ["Pod"]
exclude:
any:
- resources:
namespaces: ["legacy-migration"] # owner: platform-team, review: 2026-10-01
Warning — destructive action risk: switching
validationFailureActiontoEnforceon a broadly-scopedClusterPolicyblocks every matching admission cluster-wide the moment it's applied — not just new deploys from CI, but anykubectl apply. Confirm the audit period covered representative traffic and that a rollback (kubectl patch clusterpolicy <name> --type merge -p '{"spec":{"validationFailureAction":"Audit"}}') is understood by on-call before enforcing against production namespaces.
Use mutate rules to inject or default fields rather than only
rejecting non-compliant resources — useful for defaults that don't
need a human decision (e.g. a default imagePullPolicy, an
observability sidecar):
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: add-default-resources
spec:
rules:
- name: default-resource-limits
match:
any:
- resources:
kinds: ["Pod"]
mutate:
patchStrategicMerge:
spec:
containers:
- (name): "*"
resources:
limits:
+(memory): "512Mi" # only sets if not already present
Test mutate rules especially carefully — unlike validate rules, a mutate bug silently changes what gets deployed rather than rejecting it, which is harder to notice.
Use generate rules for auto-provisioned companion resources,
being deliberate about cleanup behavior:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: default-deny-networkpolicy
spec:
rules:
- name: generate-default-deny
match:
any:
- resources:
kinds: ["Namespace"]
generate:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
name: default-deny-all
namespace: "{{request.object.metadata.name}}"
synchronize: true # keep generated resource in sync with policy changes
data:
spec:
podSelector: {}
policyTypes: ["Ingress", "Egress"]
With synchronize: true, editing or deleting the policy also updates
or removes the generated resource — understand this before rollout, so
deleting the policy doesn't unexpectedly remove protective
NetworkPolicies teams have come to depend on.
Unit test policies offline with kyverno test before applying to
a cluster:
kyverno test policies/require-run-as-non-root/
# policies/require-run-as-non-root/kyverno-test.yaml
name: test-require-non-root
policies:
- ../require-run-as-non-root.yaml
resources:
- resources/pod-root.yaml
- resources/pod-nonroot.yaml
results:
- policy: require-run-as-non-root
rule: check-runAsNonRoot
resource: pod-root
result: fail
- policy: require-run-as-non-root
rule: check-runAsNonRoot
resource: pod-nonroot
result: pass
Wire PolicyReport output into CI or a dashboard so compliance
status is visible without manually querying the cluster:
kubectl get policyreport -A -o json | \
jq '[.items[].results[] | select(.result=="fail")] | length'
Version-control policies alongside application manifests, PR-review
changes, and keep one policy per concern rather than one giant
multi-rule ClusterPolicy covering unrelated checks.
mutate/generate (defaulting fields, auto-provisioning companion
resources) — these are first-class Kyverno concepts that are more
awkward to express in OPA/Gatekeeper. Choose OPA/Gatekeeper
(opa-gatekeeper-policy-authoring)
when policies need to run outside Kubernetes too (CI-time Terraform
plan checks via Conftest, API authorization), when logic is complex
enough that Rego's real programming constructs (helper functions,
recursion, set operations) pay off over pattern-matching YAML, or when
the org has already standardized on OPA elsewhere and wants one engine.pattern/anyPattern blocks for straightforward structural
checks; reach for JMESPath expressions only when a pattern block
genuinely can't express the condition — pattern blocks stay readable to
someone who's never seen Kyverno before, which is the point of choosing
Kyverno.mutate rules more cautiously than validate rules — a
validate-rule bug rejects things it shouldn't (loud, visible failure);
a mutate-rule bug silently changes what gets deployed (quiet,
potentially discovered much later).synchronize: true on generate rules before using it in
production — it means deleting or editing the policy also
deletes/edits the generated resource, which is powerful but surprising
if the team expects generated resources to be independent once
created.Audit → review PolicyReport →
Enforce, identically to the Gatekeeper dryrun → deny pattern —
the two engines differ in syntax, not in the rollout discipline needed.ClusterPolicy per concern (non-root, registry allowlist,
resource defaults) rather than bundling unrelated rules into one
resource, so a scoped exception or a rollback doesn't have to touch
unrelated rules.Symptom: A new ClusterPolicy is applied directly with
validationFailureAction: Enforce and immediately blocks a legitimate
deploy, triggering an incident.
Fix: Always deploy with validationFailureAction: Audit first,
review PolicyReport/ClusterPolicyReport results for a representative
period, then switch to Enforce with documented, time-boxed
exclude blocks for anything legitimate the audit surfaced.
Symptom: A validate rule with a pattern block never fails,
even for a resource that clearly violates the intended rule.
Fix: Kyverno's pattern matching requires the field structure to
match exactly, including array-vs-object shape; a common cause is
writing pattern: {spec: {containers: {securityContext: ...}}}
(treating containers as an object) when it's actually a list and
needs containers: - securityContext: .... Run kyverno test against
a sample resource that should fail and inspect the actual result before
trusting the policy is live.
Symptom: A mutate rule intended to set a default resource limit
ends up overwriting a value teams had deliberately set higher than the
default.
Fix: Use the +() conditional-anchor syntax (+(memory): "512Mi")
which only applies the patch when the field is absent, rather than an
unconditional patch that always overwrites — verify this with
kyverno test cases covering both "field absent" and "field already
set" resources.
Symptom: A generate rule with synchronize: true is deleted as
part of a policy cleanup, and previously-generated NetworkPolicy
resources teams depended on for isolation silently disappear along with
it, temporarily removing network segmentation.
Fix: Before deleting or disabling a generate policy with
synchronize: true, check what resources it has generated
(kubectl get networkpolicy -A -l kyverno.io/generated-by-policy) and decide deliberately whether to detach (patch synchronize: false`
first, which stops further sync but leaves existing generated resources
in place) versus a full removal.
Symptom: Team picked Kyverno for everything, including a CI-time
check on Terraform plans, and finds the policy awkward to express or
simply unsupported.
Fix: Kyverno's design center is Kubernetes admission (and, via
newer ClusterCleanupPolicy/JSON payload support, some non-Kubernetes
JSON validation) — but non-Kubernetes IaC gating (Terraform plan JSON,
arbitrary CI artifacts) is squarely OPA/Conftest's use case as covered
in policy-as-code-guardrails.
Use Kyverno for Kubernetes-native admission policy and OPA/Conftest for
IaC/CI-time checks rather than forcing one engine to do both.
A platform team standardizes on Kyverno (team is Kubernetes-YAML-fluent, no existing Rego investment) for three policies: validating non-root containers, defaulting a memory limit via mutation, and auto-generating a default-deny NetworkPolicy per namespace.
policies/require-non-root.yaml (Audit, then Enforce after a clean week):
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-run-as-non-root
spec:
validationFailureAction: Enforce
background: true
rules:
- name: check-runAsNonRoot
match:
any:
- resources:
kinds: ["Pod"]
exclude:
any:
- resources:
namespaces: ["legacy-migration"] # owner: platform-team, review: 2026-10-01
validate:
message: "Containers must set securityContext.runAsNonRoot: true"
pattern:
spec:
securityContext:
runAsNonRoot: true
policies/default-memory-limit.yaml:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: default-memory-limit
spec:
rules:
- name: set-default-memory-limit
match:
any:
- resources:
kinds: ["Pod"]
mutate:
patchStrategicMerge:
spec:
containers:
- (name): "*"
resources:
limits:
+(memory): "512Mi"
policies/default-deny-netpol.yaml:
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: default-deny-networkpolicy
spec:
rules:
- name: generate-default-deny
match:
any:
- resources:
kinds: ["Namespace"]
generate:
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
name: default-deny-all
namespace: "{{request.object.metadata.name}}"
synchronize: true
data:
spec:
podSelector: {}
policyTypes: ["Ingress", "Egress"]
Offline test before rollout:
kyverno test policies/require-non-root/
# policies/require-non-root/kyverno-test.yaml
name: test-require-non-root
policies:
- ../require-non-root.yaml
resources:
- resources/pod-root.yaml
- resources/pod-nonroot.yaml
results:
- policy: require-run-as-non-root
rule: check-runAsNonRoot
resource: pod-root
result: fail
- policy: require-run-as-non-root
rule: check-runAsNonRoot
resource: pod-nonroot
result: pass
After applying, creating a namespace team-payments automatically
provisions NetworkPolicy/default-deny-all in that namespace, and
kubectl get policyreport -n team-payments confirms the non-root policy
is passing for all existing Pods before the team flips it from Audit to
Enforce.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
kyverno, kubernetes-policy, admission-control, opa-alternative
npx claudepluginhub selvarajmurugesan90/ops-engineering-skills --plugin policy-governance-skills3 skills in this plugin
/fairwinds-polaris-and-goldilocksGuides using Fairwinds Polaris to validate and score Kubernetes workload configuration (resource requests/limits, security context, health checks, image tags) and Fairwinds Goldilocks (VPA-backed) to generate right-sizing recommendations. Use when the user asks to "score our workloads against Kubernetes best practices", "check our manifests for missing resource limits or health checks", "find over/under-provisioned pods", "set up Goldilocks recommendations", "add a lightweight config linter before we write custom OPA/Kyverno policies", or "why is Polaris flagging this deployment".
/opa-gatekeeper-policy-authoringGuides authoring Rego ConstraintTemplates and Constraints for OPA Gatekeeper on Kubernetes — admission webhook enforcement, parameterized policies, dry-run/audit mode before switching to deny, and debugging Rego that under- or over-matches. Use when the user asks to "write a Gatekeeper ConstraintTemplate", "write a Rego policy for Kubernetes admission", "block pods that violate our security baseline with OPA", "add a Constraint with parameters", "why isn't my Gatekeeper policy denying anything", or "should we use OPA/Gatekeeper or Kyverno for cluster policy".
Other plugins' skills for the same job