Labels and annotations

Resources in Kubernetes are organized in a flat structure, with no hierarchicalinformation or relationship between them. However, such resources and objectscan be linked together and put in relationship through labels and annotations .

Note

For more information, please refer to the Kubernetes documentation on Labels and annotations and Labels and annotations .

In short:

You can select one or more labels and/or annotations you will usein your CloudNativePG deployments. Then you need to configure the operatorso that when you define these labels and/or annotations in a cluster’s metadata,they are automatically inherited by all resources created by it (including pods).

Note

Label and annotation inheritance is the technique adopted by CloudNativePG in lieu of alternative approaches such as pod templates.

Pre-requisites

By default, no label or annotation defined in the cluster’s metadata isinherited by the associated resources.In order to enable label/annotation inheritance, you need to follow theinstructions provided in the Operator configuration section.

Below we will continue on that example and limit it to the following:

  • annotations: categories

  • labels: app , environment , and workload

Note

Feel free to select the names that most suit your context for both annotations and labels. Remember that you can also use wildcards in naming and adopt strategies like mycompany/* for all labels or annotations starting with mycompany/ to be inherited.

Defining cluster’s metadata

When defining the cluster, before any resource is deployed, you canproperly set the metadata as follows:

apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
  name: cluster-example
  annotations:
    categories: database
  labels:
    environment: production
    workload: database
    app: sso
spec:
     # ... <snip>

Once the cluster is deployed, you can verify, for example, that the labelshave been correctly set in the pods with:

kubectl get pods --show-labels

Current limitations

Currently, CloudNativePG does not automatically propagate labels orannotations deletions. Therefore, when an annotation or label is removed froma Cluster, which was previously propagated to the underlying pods, the operatorwill not automatically remove it on the associated resources.