executing-plans
executing-plans
Use when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available
kyverno-label-enforcementEnforces required labels on Pods and Namespaces using Kyverno validate rules with pattern matching and CEL expressions for compliance tracking.
Independent GitHub snapshot · Automated classification · Original documentation governs
name: kyverno-label-enforcement description: Enforces required labels on Pods and Namespaces using Kyverno validate rules with pattern matching and CEL expressions for compliance tracking. license: MIT compatibility: opencode archetypes:
Enforces required labels on Pods and Namespaces using Kyverno validate rules with pattern matching and CEL expressions. When loaded, this skill makes the model design validation policies that ensure every workload and namespace carries the labels required for cost tracking, compliance, and operational standards — rejecting resources that lack required label keys or invalid value formats.
pattern with "?*" for non-empty value validation on label keysapp.kubernetes.io/name, team)matches() functionkube-system, kyverno) from enforcementfail violationskyverno-mutate-patterns to auto-inject missing labelsapp.kubernetes.io standardsenv: production, env: staging) for governanceDefine the required label set — Identify the exact label keys and value constraints that must be present on target resources. Determine which label values are valid (using regex patterns) and which are mandatory (non-empty checks). Checkpoint: Create a documented list of required labels with their expected key format, value constraints, and validation rules.
Create the validate rule with pattern matching — Build a ClusterPolicy with a validate rule using pattern matching to require specific label keys with non-empty values. For advanced validation, use CEL expressions with the matches() function to enforce value format constraints. Checkpoint: Verify the pattern uses "?*" for non-empty string validation, CEL matches() for regex-based value checks, and that the resource kind selector targets the right workload types.
Deploy and monitor violations — Apply the policy and verify resources without required labels are rejected. Check PolicyReport for fail results and track violations over time. Consider pairing with a mutation policy to auto-inject missing labels. Checkpoint: Confirm PolicyReport shows pass for compliant resources and fail with descriptive messages for non-compliant ones.
Deploy a ClusterPolicy that enforces three required labels on all Pods: app.kubernetes.io/name, app.kubernetes.io/instance, and app.kubernetes.io/version. Uses pattern matching with "?*" to require non-empty string values.
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-app-labels
annotations:
policies.kyverno.io/title: Require App Labels on Pods
policies.kyverno.io/category: Operational Standards
policies.kyverno.io/description: >-
Enforces the use of app.kubernetes.io labels on all Pods
to support service discovery and compliance tracking.
spec:
validationFailureAction: Enforce
rules:
- name: require-app-kubernetes-labels
match:
any:
- resources:
kinds:
- Pod
preconditions:
all:
- key: "{{ request.namespace }}"
operator: NotIn
value:
- kube-system
- kyverno
- istio-system
validate:
message: "Labels app.kubernetes.io/name, app.kubernetes.io/instance, and app.kubernetes.io/version are required."
pattern:
metadata:
labels:
app.kubernetes.io/name: "?*"
app.kubernetes.io/instance: "?*"
app.kubernetes.io/version: "?*"
PolicyReport Example for Label Validation:
apiVersion: wgpolicyk8s.io/v1alpha2
kind: PolicyReport
metadata:
name: require-app-labels-report
namespace: default
results:
- policy: require-app-labels
rule: require-app-kubernetes-labels
result: pass
severity: medium
message: "Resource passed policy check"
scored: true
source: kyverno
timestamp:
seconds: 1733501530
- policy: require-app-labels
rule: require-app-kubernetes-labels
result: fail
severity: medium
message: "Labels app.kubernetes.io/name, app.kubernetes.io/instance, and app.kubernetes.io/version are required."
scored: true
source: kyverno
resources:
- name: my-pod
kind: Pod
summary:
error: 0
fail: 1
pass: 1
skip: 0
warn: 0
Implement a comprehensive multi-label policy inspired by Kubecost's cost tracking requirements. This policy enforces five required labels (owner, team, department, app, env) with CEL-based regex validation on label values to ensure they follow naming conventions.
# ❌ BAD — No value format validation; accepts any non-empty value
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: bad-label-enforcement
spec:
rules:
- name: require-cost-labels
validate:
message: "Required labels missing"
pattern:
metadata:
labels:
owner: "?*"
team: "?*"
# ✅ GOOD — Multi-label with CEL regex validation on values
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-cost-allocation-labels
annotations:
policies.kyverno.io/title: Require Cost Allocation Labels
policies.kyverno.io/category: FinOps
policies.kyverno.io/description: >-
Enforces Kubecost-compatible cost allocation labels on all Pods
with regex validation for naming conventions.
spec:
validationFailureAction: Enforce
background: false
rules:
- name: enforce-cost-labels
match:
any:
- resources:
kinds:
- Pod
preconditions:
all:
- key: "{{ request.namespace }}"
operator: NotIn
value:
- kube-system
- kyverno
validate:
message: >-
Required cost allocation labels (owner, team, department, app, env)
must be present and follow the format: lowercase alphanumeric with hyphens.
pattern:
metadata:
labels:
owner: "?*"
team: "?*"
department: "?*"
app: "?*"
env: "?*"
any:
- rule: enforce-owner-format
match:
any:
- resources:
kinds:
- Pod
validate:
message: "Label 'owner' must match format: lowercase alphanumeric with hyphens (e.g., 'john-doe', 'team-alpha')."
deny:
conditions:
any:
- key: "{{ request.object.metadata.labels.owner }}"
operator: NotMatches
value: "[a-z0-9][a-z0-9-]{0,62}"
- rule: enforce-env-format
match:
any:
- resources:
kinds:
- Pod
validate:
message: "Label 'env' must be one of: production, staging, development, testing."
deny:
conditions:
any:
- key: "{{ request.object.metadata.labels.env }}"
operator: NotIn
value:
- production
- staging
- development
- testing
Enforce required labels on Namespaces to support resource governance and cost tracking at the namespace level. This pattern uses the Namespace kind in the match conditions and validates a separate set of governance labels.
apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
name: require-namespace-labels
annotations:
policies.kyverno.io/title: Require Namespace Labels
policies.kyverno.io/category: Resource Governance
policies.kyverno.io/description: >-
Enforces governance labels on all Namespaces including owner, team,
and compliance tags for resource attribution and audit trails.
spec:
validationFailureAction: Enforce
rules:
- name: require-governance-labels
match:
any:
- resources:
kinds:
- Namespace
preconditions:
all:
- key: "{{ request.namespace }}"
operator: NotIn
value:
- kube-system
- kube-public
- kube-node-lease
validate:
message: "Namespaces require governance labels: owner, team, and compliance-tier."
pattern:
metadata:
labels:
owner: "?*"
team: "?*"
compliance-tier: "?*"
"?*" pattern value to validate non-empty label values — this rejects empty strings, null, and missing keysapp.kubernetes.io/name) rather than generic patternsmatches() regex for format enforcement (e.g., "[a-z0-9][a-z0-9-]{0,62}")kube-system, kube-public, kyverno, istio-system) from enforcement to prevent cluster disruptionkyverno-mutate-patterns) to auto-inject missing labels on create eventsvalidationFailureAction: Enforce for blocking non-compliant resources, Audit for reporting-only mode during rolloutkube-system or other core Kubernetes namespaces — this will break cluster operationsresources.kinds in the match block| Skill | Purpose |
|---|---|
kyverno-mutate-patterns | Auto-inject missing labels to complement enforcement |
kyverno-resource-quota-validation | Validate resource quotas alongside label requirements |
Authoritative documentation links for Kyverno label enforcement.