Skip to content

Labels and annotations

Labels and annotations are used to attach additional metadata information to Kubernetes resources.

Labels and annotations are rather similar. The difference between them is that labels are used by Kubernetes to identify and select objects, while annotations are assigning additional non-identifying information to resources. Therefore, typical role of Annotations is facilitating integration with some external tools.

Setting labels and annotations in the Custom Resource

You can set labels and/or annotations as key/value string pairs in the Custom Resource metadata section of the deploy/cr.yaml as follows:

apiVersion: ps.percona.com/v1alpha1
kind: PerconaServerMySQL
metadata:
  name: cluster1
  annotations:
    percona.com/issue-vault-token: "true"
  labels:
    ...

The easiest way to check which labels are attached to a specific object with is using the additional --show-labels option of the kubectl get command. Checking the annotations is not much more difficult: it can be done as in the following example:

$ kubectl get pod cluster1-mysql-0 -o jsonpath='{.metadata.annotations}'

Specifying labels and annotations ignored by the Operator

Sometimes various Kubernetes flavors can add their own annotations to the objects managed by the Operator.

The Operator keeps track of all changes to its objects and can remove annotations that appeared without its participation.

If there are no annotations or labels in the Custom Resource, the Operator does nothing if new label or annotation added to the object.

If there is an annotation or a label specified in the Custom Resource, the Operator starts to manage annotations and labels. In this case it removes unknown annotations and labels.

Still, it is possible to specify which annotations and labels should be ignored by the Operator by listing them in the spec.ignoreAnnotations or spec.ignoreLabels keys of the deploy/cr.yaml, as follows:

spec:
  ignoreAnnotations:
    - some.custom.cloud.annotation/smth
  ignoreLabels:
    - some.custom.cloud.label/smth
...

The Operator will ignore any annotation and label which keys starts with the mentioned above examples. For example, the following annotations and labels will be ignored after applying the above cr.yaml fragment:

annotations:
  some.custom.cloud.annotation/smth: somethinghere
labels:
  some.custom.cloud.label/smth: somethinghere

The Operator will ignore specified annotations and labels for all objects: Pods, Services, etc.

Get expert help

If you need assistance, visit the community forum for comprehensive and free database knowledge, or contact our Percona Database Experts for professional support and services. Join K8S Squad to benefit from early access to features and “ask me anything” sessions with the Experts.


Last update: 2024-04-02