You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We have a sample for how to assign annotations to Pods based on the /binding subresource but not labels. The Kubernetes API doesn't allow assigning labels to bindings and having those translate to the Pods, so a mutate existing rule has to be used.
Solution Description
apiVersion: kyverno.io/v2beta1kind: ClusterPolicymetadata:
name: add-node-labels-podannotations:
pod-policies.kyverno.io/autogen-controllers: nonepolicies.kyverno.io/title: Add scheduled Node's labels to a Podpolicies.kyverno.io/category: Otherpolicies.kyverno.io/subject: Podkyverno.io/kyverno-version: 1.10.0policies.kyverno.io/minversion: 1.10.0kyverno.io/kubernetes-version: "1.26"policies.kyverno.io/description: >- Containers running in Pods may sometimes need access to node-specific information on which the Pod has been scheduled. A common use case is node topology labels to ensure pods are spread across failure zones in racks or in the cloud. The mutate-pod-binding policy already does this for annotations, but it does not handle labels. A useful use case is for passing metric label information to ServiceMonitors and then into Prometheus. This policy watches for Pod binding events when the pod is scheduled and then asynchronously mutates the existing Pod to add the labels. This policy requires the following changes to common default configurations: - The kyverno resourceFilter should not filter pod/Binding resources. - The kyverno backgroundController service account requires Update permission on pods. It is recommended to use https://kubernetes.io/docs/reference/access-authn-authz/rbac/#aggregated-clusterroles spec:
rules:
- name: add-topology-labelsmatch:
any:
- resources:
kinds:
- Pod/bindingcontext:
- name: nodevariable:
jmesPath: request.object.target.namedefault: ''
- name: ZoneLabelapiCall:
urlPath: "/api/v1/nodes/{{node}}"jmesPath: "metadata.labels.\"topology.kubernetes.io/zone\" || 'empty'"mutate:
targets:
- apiVersion: v1kind: Podname: "{{ request.object.metadata.name }}"namespace: "{{ request.object.metadata.namespace }}"patchStrategicMerge:
metadata:
labels:
# https://kubernetes.io/docs/reference/labels-annotations-taints/#topologykubernetesiozonetopology.kubernetes.io/zone: "{{ ZoneLabel }}"
Problem Statement
We have a sample for how to assign annotations to Pods based on the
/binding
subresource but not labels. The Kubernetes API doesn't allow assigning labels to bindings and having those translate to the Pods, so a mutate existing rule has to be used.Solution Description
Example "Good" Resource
No response
Example "Bad" Resource
No response
Other Comments
No response
Slack discussion
https://kubernetes.slack.com/archives/CLGR9BJU9/p1704971258092579?thread_ts=1704276295.075759&cid=CLGR9BJU9
Troubleshooting
The text was updated successfully, but these errors were encountered: