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: psmdb.percona.com/v1
kind: PerconaServerMongoDB
metadata:
  name: my-cluster-name
  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 my-cluster-name-rs0-0 -o jsonpath='{.metadata.annotations}'

Using labels and annotations with objects created by the Operator

You can assign labels and annotations to various objects created by the Operator (e.g. Services used to expose components of the cluster, Persistent Volume Claims, etc.) with labels and annotations options in the appropriate subsections of the Custom Resource, as seen in the Custom Resource options reference and the deploy/cr.yaml configuration file .

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 expose subsections, the Operator does nothing if a new label or annotation is added to the object.

If the Service per Pod mode is not used, the Operator won’t remove any annotations and labels from any Services related to this expose subsection. Though, it is still possible to add annotations and labels via the Custom Resource in this case. Use the appropriate expose.serviceAnnotations and expose.serviceLabels fields.

Else, if the Service per Pod mode is active, the Operator removes unknown annotations and labels from Services created by the Operator for Pods. Yet it is still possible to specify which annotations and labels should be preserved (not wiped out) by the Operator. List them in the spec.ignoreAnnotations or spec.ignoreLabels fields of the deploy/cr.yaml, as follows:

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

The Operator will keep any Service annotation or label, key of which starts with the specified string. For example, the following annotations and labels will be not removed after applying the above cr.yaml fragment:

kind: Service
apiVersion: v1
metadata:
  name: my-cluster-name-cfg
  ...
  labels:
    some.custom.cloud.label/smth: somethinghere
    ...
  annotations:
    some.custom.cloud.annotation/smth: somethinghere
    ...

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-22