### Match Services by Name Pattern or User Source: https://kyverno.io/docs/policy-types/cluster-policy/match-exclude This example demonstrates matching Services starting with 'prod-' or named 'staging', OR any Service created by the user 'dave'. Wildcards are supported in resource names for flexible matching. ```yaml match: any: - resources: names: - 'prod-*' - 'staging' kinds: - Service operations: - CREATE - resources: kinds: - Service operations: - CREATE subjects: - kind: User name: dave ``` -------------------------------- ### Kyverno Resource Manifests for Global Value Example Source: https://kyverno.io/docs/subprojects/kyverno-cli Example Pod manifests to demonstrate global value precedence. ```yaml apiVersion: v1 kind: Pod metadata: name: test-global-prod spec: containers: - name: nginx image: nginx:latest --- apiVersion: v1 kind: Pod metadata: name: test-global-dev spec: containers: - name: nginx image: nginx:1.12 ``` -------------------------------- ### Kyverno Resource Manifest Example Source: https://kyverno.io/docs/subprojects/kyverno-cli An example of a Namespace resource manifest. ```yaml kind: Namespace apiVersion: v1 metadata: name: devtest ``` -------------------------------- ### Install and Verify Kyverno CLI via Krew Source: https://kyverno.io/docs/subprojects/kyverno-cli Installs the Kyverno CLI using the kubectl krew plugin manager and verifies the installation by checking the version. ```bash # Install Kyverno CLI using kubectl krew plugin manager kubectl krew install kyverno # test the Kyverno CLI kubectl kyverno version ``` -------------------------------- ### Install Kyverno using YAML Manifest Source: https://kyverno.io/docs/installation/installation Install Kyverno using a single YAML installation manifest from a tagged release. This method is an alternative to Helm, though Helm is preferred for production. ```bash kubectl create -f https://github.com/kyverno/kyverno/releases/download/v1.16.2/install.yaml ``` -------------------------------- ### Install Pre-Release Kyverno Versions with Helm Source: https://kyverno.io/docs/installation/installation Install pre-release versions (alpha, beta, rc) of Kyverno using Helm by adding the `--devel` flag to the installation command. ```bash helm install kyverno kyverno/kyverno -n kyverno --create-namespace --devel ``` -------------------------------- ### Install Kyverno and Prometheus Stack Source: https://kyverno.io/docs/guides/monitoring Install the Kyverno Helm chart and the kube-prometheus-stack chart. Ensure the namespaces 'kyverno' and 'monitoring' are created. ```bash helm install kyverno kyverno/kyverno --namespace kyverno --create-namespace helm install monitoring prometheus-community/kube-prometheus-stack -n monitoring --create-namespace ``` -------------------------------- ### Kubectl Describe Deployment Example Source: https://kyverno.io/docs/policy-types/cluster-policy/mutate This output shows an example of Kubernetes events for a deployment, indicating that a Kyverno policy was applied successfully. It is useful for verifying policy application. ```bash $ kubectl describe deploy foobar ... Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal PolicyApplied 29s (x2 over 31s) kyverno-mutate policy add-sec/add-sec-rule applied ``` -------------------------------- ### Install Kyverno CLI via Homebrew Source: https://kyverno.io/docs/subprojects/kyverno-cli Installs the Kyverno CLI using the Homebrew package manager. ```bash brew install kyverno ``` -------------------------------- ### Install Kyverno CLI Binary Manually Source: https://kyverno.io/docs/subprojects/kyverno-cli Manually installs a specific version of the Kyverno CLI by downloading the binary, extracting it, and copying it to the system's PATH. ```bash curl -LO https://github.com/kyverno/kyverno/releases/download/v1.12.0/kyverno-cli_v1.12.0_linux_x86_64.tar.gz tar -xvf kyverno-cli_v1.12.0_linux_x86_64.tar.gz sudo cp kyverno /usr/local/bin/ ``` -------------------------------- ### Install Kyverno via Helm (Non-Production) Source: https://kyverno.io/docs/installation/installation Use these Helm commands to install Kyverno in a non-production environment. Ensure you add and update the Kyverno Helm repository before installation. ```bash helm repo add kyverno https://kyverno.github.io/kyverno/ helm repo update helm install kyverno kyverno/kyverno -n kyverno --create-namespace ``` -------------------------------- ### Example: Test Subresource Matching Source: https://kyverno.io/docs/subprojects/kyverno-cli An example demonstrating how to specify subresource details within the `values` file for testing purposes. ```yaml apiVersion: cli.kyverno.io/v1alpha1 kind: Values metadata: name: values subresources: - subresource: name: 'deployments/scale' kind: 'Scale' group: 'autoscaling' version: 'v1' parentResource: name: 'deployments' kind: 'Deployment' group: 'apps' version: 'v1' ``` -------------------------------- ### Install Prometheus Source: https://kyverno.io/docs/guides/monitoring Apply the Prometheus configuration using kubectl to install it in the cluster. This is a prerequisite for viewing metrics. ```bash kubectl apply -k github.com/kyverno/grafana-dashboard/examples/prometheus ``` -------------------------------- ### ValidatingWebhookConfiguration Example Source: https://kyverno.io/docs/guides/admission-controllers This example shows an abbreviated ValidatingWebhookConfiguration. It defines rules for sending creation requests for Deployments to a specific service within the cluster. Ensure the clientConfig correctly points to your service. ```yaml apiVersion: admissionregistration.k8s.io/v1 kind: ValidatingWebhookConfiguration metadata: name: kyverno-resource-validating-webhook-cfg webhooks: - name: validate.kyverno.svc-fail ## The name of this webhook rules: ## What resources should be sent - apiGroups: - apps apiVersions: - v1 operations: - CREATE resources: - deployments clientConfig: ## Where the resources should be sent caBundle: LS0t0tLS0K service: name: kyverno-svc namespace: kyverno path: /validate/fail port: 443 timeoutSeconds: 10 ## How long should the API server wait failurePolicy: Fail ## What should happen after the wait is over ``` -------------------------------- ### Example Deployment for PolicyException Source: https://kyverno.io/docs/guides/exceptions This Deployment is used as an example to demonstrate how a PolicyException can allow a resource that would otherwise violate a policy rule. ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: important-tool namespace: delta labels: app: busybox spec: replicas: 1 selector: matchLabels: app: busybox template: metadata: labels: app: busybox spec: hostIPC: true containers: - image: busybox:1.35 name: busybox command: ['sleep', '1d'] ``` -------------------------------- ### Complete ArgoCD Application Example for Kyverno Source: https://kyverno.io/docs/installation/platform-notes This example demonstrates a complete ArgoCD Application manifest for deploying Kyverno. It includes annotations for ServerSideDiff, destination and source configurations, and sync policy settings. ```yaml apiVersion: argoproj.io/v1alpha1 kind: Application metadata: name: kyverno namespace: argocd annotations: argocd.argoproj.io/compare-options: ServerSideDiff=true,IncludeMutationWebhook=true spec: destination: namespace: kyverno server: https://kubernetes.default.svc project: default source: chart: kyverno repoURL: https://kyverno.github.io/kyverno targetRevision: helm: values: | webhookLabels: app.kubernetes.io/managed-by: argocd syncPolicy: automated: prune: true selfHeal: true syncOptions: - CreateNamespace=true - ServerSideApply=true ``` -------------------------------- ### Install Kyverno CLI via AUR (Arch Linux) Source: https://kyverno.io/docs/subprojects/kyverno-cli Installs the Kyverno CLI on Arch Linux using an AUR helper like yay. ```bash yay -S kyverno-git ``` -------------------------------- ### Example of Denied Deployment with Bad Annotation Source: https://kyverno.io/docs/policy-types/cel-libraries This example demonstrates the output when a deployment is created with an annotation that violates the 'check-goodboi' Kyverno policy. ```bash / # cat < Object{ metadata: Object.metadata{ labels: Object.metadata.labels{ environment: "production", managed: "true" } } } ``` -------------------------------- ### CEL-based ValidatingPolicy Structure Example Source: https://kyverno.io/docs/guides/migration-to-cel Example of a CEL-based ValidatingPolicy, focused on a single action (validation) with a CEL expression. ```yaml apiVersion: policies.kyverno.io/v1 kind: ValidatingPolicy metadata: name: require-app-version-labels spec: matchConstraints: resourceRules: - apiGroups: - '' apiVersions: - v1 operations: - CREATE - UPDATE resources: - pods validationActions: - Deny validations: - expression: > ['app', 'version'].all(label, object.metadata.?labels[label].orValue('') != '' ) message: 'Required labels missing' ``` -------------------------------- ### Kyverno Create Exception Command Options Source: https://kyverno.io/docs/kyverno-cli/reference/kyverno_create_exception Lists the available options for the 'kyverno create exception' command, including filters for resources, namespace, output path, and policy rule specifications. ```bash --all stringArray List of resource filters --any stringArray List of resource filters -b, --background Set to false when policy shouldn't be considered in background scans (default true) -h, --help help for exception --namespace string Policy exception namespace -o, --output string Output path (uses standard console output if not set) --policy-rules --policy-rules=policy,rule-1,rule-2 Policy name, followed by rule names (--policy-rules=policy,rule-1,rule-2,...) ``` -------------------------------- ### Install Pod Security Standards Policies via Helm Source: https://kyverno.io/docs/guides/pod-security Installs the Kyverno Pod Security Standards (PSS) policies using the kyverno/kyverno-policies Helm chart. The policies are installed into the 'kyverno-policies' namespace, which will be created if it doesn't exist. Adjust the namespace as needed. ```bash helm install kyverno-pss kyverno/kyverno-policies \ --namespace kyverno-policies --create-namespace \ --set policyGroups=pod-security ``` -------------------------------- ### Example Pod Resource Source: https://kyverno.io/docs/policy-types/cluster-policy/jmespath This is a sample Kubernetes Pod resource definition used to demonstrate array flattening. ```yaml apiVersion: v1 kind: Pod metadata: name: mypod spec: initContainers: - name: redis image: redis containers: - name: busybox image: busybox - name: nginx image: nginx ``` -------------------------------- ### Find Deployment Resource API Source: https://kyverno.io/docs/policy-types/cluster-policy/external-data-sources This example demonstrates how to find the API group and version for 'Deployment' resources by combining `kubectl api-resources` and `kubectl api-versions`. First, it filters for 'deploy' using `grep` to identify the resource, then it filters for 'apps' to find the corresponding API version. ```bash kubectl api-resources | grep deploy ``` ```bash kubectl api-versions | grep apps ``` -------------------------------- ### Generate bash completion script for installation Source: https://kyverno.io/docs/kyverno-cli/reference/kyverno_completion Generates the bash completion script and directs it to the appropriate directory for installation on Linux systems. ```bash kyverno completion bash > /etc/bash_completion.d/kyverno ``` -------------------------------- ### Kyverno Test Output Summary Source: https://kyverno.io/docs/subprojects/kyverno-cli Example output from the 'kyverno test' command, showing test results and a summary. ```bash Loading test ( kyverno-test.yaml ) ... Loading values/variables ... Loading policies ... Loading resources ... Loading exceptions ... Applying 1 policy to 2 resources with 0 exceptions ... Checking results ... │────│────────────────────│──────│─────────────────────────│────────│────────│ │ ID │ POLICY │ RULE │ RESOURCE │ RESULT │ REASON │ │────│────────────────────│──────│─────────────────────────│────────│────────│ │ 1 │ disallow-host-path │ │ v1/Pod/default/bad-pod │ Pass │ Ok │ │ 2 │ disallow-host-path │ │ v1/Pod/default/good-pod │ Pass │ Ok │ │────│────────────────────│──────│─────────────────────────│────────│────────│ Test Summary: 2 tests passed and 0 tests failed ``` -------------------------------- ### Example Kubernetes Event for Resource Deletion Source: https://kyverno.io/docs/policy-types/deleting-policy This is an example of a Kubernetes Event object emitted by Kyverno when a DeletingPolicy successfully cleans up a resource. ```yaml apiVersion: v1 kind: Event metadata: name: cleanup-old-test-pods.184c935c5c7c52c0 namespace: default creationTimestamp: '2025-06-26T11:13:00Z' resourceVersion: '3894' uid: 064e08ef-4547-43a3-b199-d2bbadd93b65 action: Resource Cleaned Up reason: PolicyApplied message: successfully deleted the target resource Pod/default/example involvedObject: apiVersion: policies.kyverno.io/v1 kind: DeletingPolicy name: deleting-pod uid: cc44fb71-9413-4bbf-bc37-036a10f02c7c related: apiVersion: v1 kind: Pod name: example namespace: default reportingComponent: kyverno-cleanup reportingInstance: kyverno-cleanup-kyverno-cleanup-controller-76c8b69df6-89mjj type: Normal ``` -------------------------------- ### Create a Compliant Pod and Inspect PolicyReport Source: https://kyverno.io/docs/guides/reports This section shows how to create a Pod that adheres to the policy and then inspect the generated PolicyReport to confirm a 'PASS' status. It includes commands to create the Pod, list Pods, and get PolicyReports. ```bash $ kubectl run busybox --image busybox:1.28 -- sleep 9999 pod/busybox created $ kubectl get po NAME READY STATUS RESTARTS AGE busybox 1/1 Running 0 66s $ kubectl get polr -o wide NAME KIND NAME PASS FAIL WARN ERROR SKIP AGE 89044d72-8a1e-4af0-877b-9be727dc3ec4 Pod busybox 1 0 0 0 0 15s ``` ```yaml --- results: - message: validation rule 'secrets-not-from-env-vars' passed. policy: secrets-not-from-env-vars result: pass rule: secrets-not-from-env-vars scored: true source: kyverno timestamp: nanos: 0 seconds: 1666097147 summary: error: 0 fail: 0 pass: 1 skip: 0 warn: 0 ``` -------------------------------- ### ClusterAdmissionReport Example Source: https://kyverno.io/docs/guides/reports An example of a ClusterAdmissionReport resource, which is an intermediary resource used by Kyverno to build final policy reports for admission events. ```yaml apiVersion: kyverno.io/v1alpha2 kind: ClusterAdmissionReport metadata: creationTimestamp: '2022-10-18T13:15:09Z' generation: 1 labels: app.kubernetes.io/managed-by: kyverno audit.kyverno.io/resource.hash: a7ec5160f220c5b83c26b5c8f7dc35b6 audit.kyverno.io/resource.uid: 61946422-14ba-4aa2-94b4-229d38446381 cpol.kyverno.io/require-ns-labels: '4773' name: c0cc7337-9bcd-4d53-abb2-93f7f5555216 resourceVersion: '4986' uid: 10babc6c-9e6e-4386-abed-c13f50091523 spec: owner: apiVersion: v1 kind: Namespace name: testing uid: 61946422-14ba-4aa2-94b4-229d38446381 results: - message: 'validation error: The label `thisshouldntexist` is required. rule check-for-labels-on-namespace failed at path /metadata/labels/thisshouldntexist/' policy: require-ns-labels result: fail rule: check-for-labels-on-namespace scored: true source: kyverno timestamp: nanos: 0 seconds: 1666098909 summary: error: 0 fail: 1 pass: 0 skip: 0 warn: 0 ``` -------------------------------- ### Incoming Pod Manifest Source: https://kyverno.io/docs/policy-types/cluster-policy/jmespath A sample Pod manifest before mutation by a Kyverno policy. This shows the initial state of the resource. ```yaml apiVersion: v1 kind: Pod metadata: name: mypod spec: containers: - name: busybox image: busybox ``` -------------------------------- ### Generate zsh completion script for installation Source: https://kyverno.io/docs/kyverno-cli/reference/kyverno_completion Generates the zsh completion script and directs it to the correct location within the zsh fpath for installation. ```zsh kyverno completion zsh > "${fpath[1]}/_kyverno" ``` -------------------------------- ### Verify Kyverno Kubernetes Install Manifest Signature Source: https://kyverno.io/docs/guides/security Verify the signature of the Kubernetes install manifest for Kyverno. This ensures the integrity of the deployment artifacts. ```bash cosign verify ghcr.io/kyverno/manifests/kyverno: \ --certificate-identity-regexp="https://github.com/kyverno/kyverno/.github/workflows/release.yaml@refs/tags/*" \ --certificate-oidc-issuer="https://token.actions.githubusercontent.com" | jq ``` -------------------------------- ### Kyverno Create Command Source: https://kyverno.io/docs/kyverno-cli/reference/kyverno Assists in the creation of various Kyverno resources. ```bash kyverno create -h ``` -------------------------------- ### Create a Policy Exception File Source: https://kyverno.io/docs/kyverno-cli/reference/kyverno_create_exception Example of creating a Kyverno policy exception file. It specifies the exception name, namespace, and the policy rules and resource filters to apply. ```bash kyverno create exception my-exception --namespace my-ns --policy-rules "policy,rule-1,rule-2" --any "kind=Pod,kind=Deployment,name=test-*" ```