Kyverno Policy Configuration Use Cases
This document provides core policy configuration use cases based on Kyverno. It helps you implement automatic resource mutation, unified configuration, and automated injection of security templates and baseline environments based on Namespaces or Projects in your Kubernetes cluster.
TOC
1. Resource Mutation and Unified Configuration (Mutate)1.1 Injecting Unified Labels into Pods1.2 Enforcing Uniform RestartPolicy for Pods2. Automated Template Configuration Based on Namespace/Project (Generate)2.1 Automatically Injecting Default Isolated NetworkPolicy2.2 Automatically Initializing Project Configurations Based on Templates (DBS/Security Quotas)Summary1. Resource Mutation and Unified Configuration (Mutate)
Kyverno's Mutate rules can automatically modify submitted resources during the admission control phase. The following cases demonstrate how to inject unified labels into all Pods under a namespace and enforce a uniform restartPolicy.
1.1 Injecting Unified Labels into Pods
This policy will automatically append preset labels to all newly created Pods in the cluster (or within a specific namespace). This is commonly used for unified project management and billing scheduling.
1.2 Enforcing Uniform RestartPolicy for Pods
This policy enforces that the default restartPolicy for all newly created Pods is Always. This is critical to ensure that business containers are automatically restarted if they exit unexpectedly.
2. Automated Template Configuration Based on Namespace/Project (Generate)
When a new namespace or project is created, Kyverno's Generate rules can detect this event and automatically generate related Kubernetes resources (such as NetworkPolicy, ConfigMap, Secret, RoleBinding, etc.). This acts as an out-of-the-box security and unified configuration template.
2.1 Automatically Injecting Default Isolated NetworkPolicy
This policy automatically generates a default NetworkPolicy when a new namespace is created. This policy denies all inbound (Ingress) requests by default, thereby overriding default network connectivity and achieving network isolation between namespaces.
2.2 Automatically Initializing Project Configurations Based on Templates (DBS/Security Quotas)
This example demonstrates how to automatically prepare a series of underlying environments based on project attributes (e.g., labels applied when creating a Namespace). For instance, issuing DBS connection templates (for CLI or applications to read) and default security quotas (LimitRange).
Summary
- Mutate Capabilities: Non-intrusively fixes and supplements YAML submitted by developers, easily achieving label and state control at the resource level (such as
RestartPolicy). - Generate Capabilities: Acts as a declarative project generator. Once a Namespace creation event occurs, Kyverno automatically populates security policies (NetworkPolicy) and dependency templates (ConfigMap, Secret, LimitRange) in the background, providing a highly standardized and unified isolated environment for CLI tools and upper-level applications.