skip to content

Kyverno

Kyverno states policy as Kubernetes resources: validate, mutate, generate and verifyImages rules, plus PolicyExceptions, background scans and reports. Interviewers want a rule you could write.

on this pageshow

explore

questions

page 1 of 2

What does the Kyverno CLI command kyverno apply do with a policy and a manifest file?

level: juniorimportance: must knowfreq 62%

answer

  1. runs on your laptop, not a cluster
  2. feed it YAML of two kinds
  3. policy file plus resource file
  4. prints pass, fail, skip, warn
  5. a dry run, nothing is admitted

basics

~20 s

kyverno apply reads policy files and resource files from disk and evaluates the rules against them locally, printing pass, fail, skip and warn results. It is a dry run: no cluster is contacted and nothing is admitted.

solid answer

~40 s

It is Kyverno's offline evaluator. You point it at one or more policy files and one or more resource manifests (`kyverno apply policy.yaml --resource deployment.yaml`) and it runs the same rules the in-cluster engine would run, but against the YAML on disk, printing a per-rule result and a pass/fail/skip/warn summary. It exits non-zero when rules fail, which is what lets a CI job or a pre-commit hook gate on it, and it can emit the findings in policy-report form for another tool to read. For a mutate policy it prints the patched resource so you can diff it. Nothing is created: it never talks to an API server, never admits anything, and never writes PolicyReport objects into a cluster.

go deeper

for a junior

Be ready to say what you feed it (policy YAML plus resource YAML), what it prints (pass, fail, skip, warn plus a summary) and that no cluster is involved. Knowing it exits non-zero on failure is enough to explain how CI uses it.

for a middle

Explain the difference between a fail and a skip, and what each tells you about your match block. Be able to say why the file the CLI evaluates is not the object admission validates.

for a senior

Show where this sits in a real workflow: rendered output rather than templates, running in CI on the merge request, and knowing which classes of rule cannot be judged offline at all.

for a principal

Own the position that offline evaluation is cheap confidence and nothing more. Decide what your organisation gates on locally versus what it can only learn from admission and background scans, and be honest about the gap.

## What the command is for A Kyverno policy is a Kubernetes custom resource whose rules match resources and then `validate`, `mutate` or `generate`. Once installed, those rules decide at admission — when someone submits an object to the API server — and they are also re-run in the background against objects that are already stored. Neither of those is a good place to find out that your brand-new rule matches the wrong kind or denies everything. `kyverno apply` is the escape hatch: it runs the policy engine as a local process over YAML files. ``` kyverno apply policy.yaml --resource deployment.yaml ``` You can pass several policies, several resources, or directories of each. The engine loads them, works out which rules match which resources, evaluates the rule bodies, and prints what happened. ## What comes back For each rule/resource pair you get one of a small set of outcomes: | result | meaning | | --- | --- | | pass | the rule matched the resource and the resource satisfied it | | fail | the rule matched and the resource violated it | | skip | the rule did not apply to this resource (match/exclude, preconditions) | | warn | a violation from a rule configured to advise rather than block | | error | the rule could not be evaluated — bad variable, missing context | plus a summary of the counts. The distinction between **fail** and **skip** is the one beginners misread: a long list of `skip` usually means your `match` block names a kind, a selector or an API group the resource does not have, so the rule you are proud of never ran at all. A wall of `error` usually means the rule references something that only exists at admission time. The process exits non-zero when there are failures, so it drops straight into CI: render your manifests, run the policies over them, fail the job. It can also emit its findings in the policy-report shape rather than as human text, which is handy when you want to feed the results into a dashboard instead of a terminal. For a `mutate` rule the CLI prints the resource after the patch, which is the fastest way to check that a mutation does what you meant rather than what you typed. ## Where it sits relative to the other two evaluation points Think of three places the same rule can run: 1. **The CLI, over files.** Before anything exists. No cluster, no state, no request. This is the dry run. 2. **Admission, over a request.** When someone submits an object. There is a requesting user, an operation, and for an update an old object; the answer is allow, deny or mutate. 3. **The background scan, over stored objects.** On an interval, against what is already in the cluster. Nobody is blocked; results are written into `PolicyReport` and `ClusterPolicyReport` objects. The CLI is the only one you can run on a laptop or in a pipeline that has no cluster, and the only one whose input you fully control. That is its value and also its limitation: it evaluates the file exactly as written, and neither of the other two positions ever sees exactly that. ## What a green run does and does not tell you It tells you the policy parses, the match block selects the resources you think it selects, and the rule body reaches the verdict you expect for the inputs you supplied. That is genuinely most of the mistakes. It does not tell you that admission will agree. The API server applies defaults to an object before validating it, and mutating admission — including Kyverno's own mutate rules and anyone else's webhooks — runs before validating admission, so the object that gets validated in a real cluster is not the file you handed the CLI. Rules that look up a ConfigMap, call the API, or read the requesting user have nothing to read offline unless you stub it with a values file. It also says nothing about objects already in the cluster. A rule can be perfectly green over every manifest in your repository while hundreds of stored objects violate it, because those objects were created before the rule existed and no one is resubmitting them. Finding those is the background scan's job, not the CLI's. ## Related tooling Alongside `apply` the CLI has `kyverno test`, which reads a test manifest declaring policies, resources and the result expected for each pair, and reports mismatches — the same engine, driven by declared expectations instead of eyeballed output. Designing those expectation sets is a testing discipline of its own; the point here is only that `apply` is the interactive form and `test` is the asserted form of the same offline evaluation.

  • Can it evaluate rendered Helm or Kustomize output rather than hand-written manifests?
    Yes — it reads resource YAML from files, and it does not care how that YAML was produced. Render the chart or the kustomization to a file first and point the CLI at the output. That is the right order anyway, because the rendered object is what would actually be submitted, and a rule about a field in the rendered result cannot be judged from the template.
  • Does a passing run mean admission will accept the same manifest?
    No. The API server defaults fields before validation, and mutating admission runs before validating admission, so the object validated in-cluster differs from the file. Rules that consult cluster state or the requesting identity also have nothing to read offline. A green CLI run means the rule is well-formed and matches what you expect; it is not a prediction of the admission verdict.
  • Does the CLI create PolicyReport objects?
    No. It prints results, and it can render them in policy-report shape as output for another tool to parse, but it never writes anything into a cluster. PolicyReport and ClusterPolicyReport resources are created by the in-cluster controllers from admission results and background scans, not by a local run.

It is a spell-checker run over the document before you email it: it catches what is wrong with the text you wrote, but it cannot know what the recipient's mail client will do to the formatting.

saying these in an interview costs you the question

  • Thinks the CLI contacts the cluster to reach its verdict
  • Treats a green local run as proof admission will accept it
  • Confuses the CLI dry run with a background scan of stored objects
  • Expects the CLI to create PolicyReport objects in a cluster
  • Reads a wall of skip results as success rather than a broken match block

context

open as a page

What does a Kyverno PolicyException name, and what does it not switch off?

level: juniorimportance: must knowfreq 60%

basics

~20 s

A Kyverno PolicyException names the policy and the specific rule names it waives, plus a match block selecting which resources the waiver covers. It never disables the policy itself; anything the match block misses is still enforced.

open as a page

What does a Kyverno generate rule do that a validate rule cannot?

level: juniorimportance: must knowfreq 60%

basics

~20 s

A Kyverno generate rule creates a companion object when a trigger appears, such as a default-deny NetworkPolicy in every new namespace. A validate rule can only accept or reject the request; generate supplies the resource instead of demanding it.

open as a page

In a Kyverno mutate rule, what does the +() add-if-not-present anchor do?

level: juniorimportance: must knowfreq 64%

basics

~20 s

The +() anchor in a Kyverno strategic-merge mutate patch sets a field only when the incoming object does not already have it. If the field is already present, the existing value is left alone rather than overwritten.

open as a page

In Kyverno, what does a validate.deny conditions block do that validate.pattern cannot?

level: juniorimportance: must knowfreq 62%

basics

~20 s

A deny block evaluates boolean conditions with operators over the admission request and blocks when they are true. A pattern only asserts the shape a resource must match, so it cannot use operators, compare two fields, or read request metadata.

open as a page

In a Kyverno policy, what does the context block do and how do you use its value?

level: juniorimportance: must knowfreq 64%

basics

~20 s

A Kyverno rule's context block fetches data the request itself does not carry - a ConfigMap, a live API call, or image registry metadata - binds each source to a name, and the rule then references it as a double-brace variable.

open as a page

In Kyverno, a validate rule matches only Pods - why does it still refuse a Deployment?

level: juniorimportance: must knowfreq 72%

basics

~20 s

Kyverno's Pod controller autogen copies a Pod rule into equivalent rules for Deployment, StatefulSet, DaemonSet, Job and CronJob, rewriting field paths into each one's Pod template. The Deployment is refused by a generated rule, not the one you wrote.

open as a page

In a Kyverno verifyImages rule, what does imageReferences select and what happens to an image it does not match?

level: juniorimportance: must knowfreq 60%

basics

~20 s

imageReferences is a list of glob patterns matched against every image string in the pod spec, including initContainers and ephemeral containers. An image matching no pattern is never evaluated by that rule, so it is admitted unverified.

open as a page

In a Kyverno validate.pattern, what happens if a field in the pattern is missing from the resource?

level: juniorimportance: must knowfreq 70%

basics

~10 s

An unanchored field in a Kyverno validate.pattern is mandatory: if the resource does not have it, the pattern fails and the request is rejected. Give it the value ?* to demand any non-empty value.

open as a page

What is the difference between a Kyverno Policy and a ClusterPolicy?

level: juniorimportance: must knowfreq 68%

basics

~10 s

A Kyverno ClusterPolicy is cluster-scoped: its rules can match resources in any namespace and cluster-scoped kinds. A namespaced Policy applies only inside its own namespace. The rule syntax is identical in both.

open as a page

In a Kyverno validate.deny block, how do the any and all condition lists decide whether a request is blocked?

level: middleimportance: must knowfreq 55%

basics

~20 s

Under all, every condition must be true; under any, at least one must be true. When the block comes out true the request is denied with the rule's message. If both lists appear in the same block, both have to be satisfied.

open as a page

In a Kyverno pattern, why does an anchored (resources) key let every container through?

level: middleimportance: must knowfreq 62%

basics

~10 s

A conditional anchor is a condition, not an assertion. (resources) only says: if resources matches, check my peer keys. With no peer keys there is nothing to check, so every container is admitted.

open as a page

In a Kyverno rule, how do the match and exclude blocks decide which resources it applies to?

level: middleimportance: must knowfreq 74%

basics

~10 s

The match block selects which resources and requesters a Kyverno rule applies to; the exclude block subtracts from that set. Anything matching both is skipped, so exclude always wins.

open as a page

How do you show an auditor every live Kyverno PolicyException and who can create one?

level: seniorimportance: must knowfreq 50%

basics

~20 s

List PolicyException objects across all namespaces, map each to the policy and rules it waives and the resources it covers, then show who holds create on that kind. The object records no creator; that comes from audit logs.

open as a page

In Kyverno, how do you block an image whose vulnerability-scan attestation is older than 14 days?

level: seniorimportance: must knowfreq 45%

basics

~10 s

Add an attestations entry naming the scan predicate type, give it the attestors whose signature you accept, and condition on the predicate's timestamp field. No verifiable attestation of that type blocks the image.

open as a page

Why can a Kyverno policy pass a local kyverno apply run yet decide differently in the cluster?

level: middleimportance: should knowfreq 44%

basics

~20 s

Because the CLI evaluates the raw file, while admission judges the object after API-server defaulting and mutating admission, and rules that read cluster state or the requesting user get nothing offline unless a values file supplies it.

open as a page

Your Kyverno PolicyException exempts a Pod but the Deployment is still blocked. Why?

level: middleimportance: should knowfreq 44%

basics

~10 s

Kyverno auto-generates pod-controller versions of a Pod rule under autogen- prefixed names. The Deployment is denied by autogen-check-base-image, a rule name the exception never listed, and by a kind its match block never included.

open as a page

In a Kyverno generate rule, what does synchronize: true actually guarantee?

level: middleimportance: should knowfreq 56%

basics

~20 s

Kyverno keeps the generated copy in step with its source: hand edits to the downstream are reverted and changes to the rule body or the cloned source propagate. With synchronize false, the object is created once and never touched again.

open as a page

When does a Kyverno mutate rule need patchesJson6902 instead of patchStrategicMerge?

level: middleimportance: should knowfreq 57%

basics

~20 s

Reach for patchesJson6902 when you must remove an element, append to an unkeyed list, or patch a custom resource. Strategic merge stays the default because it merges by field, matching a container by name rather than by index.

open as a page

In a Kyverno variable, why does an unquoted nodeSelector key containing dots fail to resolve?

level: middleimportance: should knowfreq 47%

basics

~20 s

Kyverno substitutes double-brace expressions with JMESPath, which reads every dot as a step into a nested field. A key like topology.kubernetes.io/zone is parsed as three nested lookups that do not exist, so quote the whole key inside the path.

open as a page

In a Kyverno autogen rule, what replaces spec.containers for a Deployment and for a CronJob?

level: middleimportance: should knowfreq 55%

basics

~20 s

For a Deployment, StatefulSet, DaemonSet or Job the path becomes spec.template.spec.containers. For a CronJob it becomes spec.jobTemplate.spec.template.spec.containers, because the Pod template sits under a Job template. That extra depth is why CronJob gets its own generated rule.

open as a page

In a Kyverno pattern over spec.containers, how many containers must match the sub-pattern?

level: middleimportance: should knowfreq 45%

basics

~20 s

All of them. A Kyverno list pattern is applied to every element of the resource array, so one container without a memory limit fails the rule. The existence anchor on the array key relaxes it to at least one element.

open as a page

Kyverno background scanning is churning PolicyReport objects and loading etcd. How do you cut the cost?

level: seniorimportance: should knowfreq 38%

basics

~20 s

A background scan re-evaluates every stored object against every matching rule and writes results into PolicyReport objects that live in etcd. Cut the cost by narrowing what matches, scanning less often, and disabling background evaluation for admission-only rules.

open as a page

Deleting a Kyverno ClusterPolicy removed every NetworkPolicy it generated. Why, and how do you avoid that?

level: seniorimportance: should knowfreq 42%

basics

~20 s

Synchronized downstream resources are owned by the policy that generated them, so removing the policy garbage-collects the copies. Set generate.orphanDownstreamOnPolicyDelete to true before decommissioning, or turn synchronization off first, so the objects are left behind.

open as a page

How does a Kyverno mutate rule patch PersistentVolumeClaims that already exist in the cluster?

level: seniorimportance: should knowfreq 41%

basics

~20 s

By declaring mutate.targets, which points the patch at objects other than the one being admitted. Kyverno applies it from its background controller rather than in the admission request, so the change is asynchronous and needs RBAC on the target kind.

open as a page

Why does a Kyverno deny condition break when the JMESPath field it reads is absent?

level: seniorimportance: should knowfreq 42%

basics

~20 s

Kyverno substitutes the JMESPath expression into the condition before evaluating it. An absent field does not resolve to false; it fails substitution, so the rule errors instead of denying. Supply a default with the JMESPath or-operator, as in field || ''.

open as a page

A Kyverno rule stopped blocking after its ConfigMap was renamed - how would you have caught it?

level: seniorimportance: should knowfreq 43%

basics

~20 s

Alert on rule results, not on violations. A failed context lookup produces an error result and a skipped precondition produces a skip - both show up as zero violations, so watch error and skip counts and run a deliberately non-compliant canary against the cluster.

open as a page

A Kyverno Pod rule blocks Deployments, but the same violation in a CronJob is admitted. Why?

level: seniorimportance: should knowfreq 47%

basics

~20 s

Most likely the policy carries a pod-policies.kyverno.io/autogen-controllers annotation that narrows generation to a subset of kinds, so no CronJob variant was ever derived. Read the policy back from the cluster: if no autogen-cronjob- rule exists, CronJobs are outside the control.

open as a page

Your Kyverno verifyImages rule rewrote a tag to a digest and GitOps now reports permanent drift — why?

level: seniorimportance: should knowfreq 38%

basics

~10 s

verifyImages mutates the admitted object by default, appending the resolved digest to the tagged reference. Git still says only the tag, so a GitOps controller diffing desired against live reports permanent drift.

open as a page

How do you prove a Kyverno validate.pattern actually blocks, given that a bad anchor fails open?

level: seniorimportance: should knowfreq 38%

basics

~20 s

Test with resources that must be rejected, not only ones that must pass. A bad anchor makes a Kyverno pattern assert nothing and report no violation, so green runs prove nothing until a non-compliant fixture is rejected.

open as a page

showing 1–30 of 41