Labels and Annotations

Dealing with Labels and Annotations

TL;DR

  • Set Labels for all Resources declared within a Project with commonLabels
  • Set Annotations for all Resources declared within a Project with commonAnnotations

Setting Labels and Annotations

Motivation

Users may want to define a common set of labels or annotations for all the Resource in a project.

  • Identify the Resources within a project by querying their labels.
  • Set metadata for all Resources within a project (e.g. environment=test).
  • Copy or Fork an existing Project and add or change labels and annotations.

Setting Labels

Example: Add the labels declared in commonLabels to all Resources in the project.

Important: Once set, commonLabels should not be changed so as not to change the Selectors for Services or Workloads.

Input: The kustomization.yaml and deployment.yaml files

  1. # kustomization.yaml
  2. apiVersion: kustomize.config.k8s.io/v1beta1
  3. kind: Kustomization
  4. commonLabels:
  5. app: foo
  6. environment: test
  7. resources:
  8. - deployment.yaml
  1. # deployment.yaml
  2. apiVersion: apps/v1
  3. kind: Deployment
  4. metadata:
  5. name: nginx-deployment
  6. labels:
  7. app: nginx
  8. bar: baz
  9. spec:
  10. selector:
  11. matchLabels:
  12. app: nginx
  13. bar: baz
  14. template:
  15. metadata:
  16. labels:
  17. app: nginx
  18. bar: baz
  19. spec:
  20. containers:
  21. - name: nginx
  22. image: nginx

Applied: The Resource that is Applied to the cluster

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. labels:
  5. app: foo # Label was changed
  6. environment: test # Label was added
  7. bar: baz # Label was ignored
  8. name: nginx-deployment
  9. spec:
  10. selector:
  11. matchLabels:
  12. app: foo # Selector was changed
  13. environment: test # Selector was added
  14. bar: baz # Selector was ignored
  15. template:
  16. metadata:
  17. labels:
  18. app: foo # Label was changed
  19. environment: test # Label was added
  20. bar: baz # Label was ignored
  21. spec:
  22. containers:
  23. - image: nginx
  24. name: nginx

Command / Examples

Check out the reference for commands and examples for setting labels

Propagating Labels to Selectors

In addition to updating the labels for each Resource, any selectors will also be updated to target the labels. e.g. the selectors for Services in the project will be updated to include the commonLabels in addition to the other labels.

Note: Once set, commonLabels should not be changed so as not to change the Selectors for Services or Workloads.

Common Labels

The k8s.io documentation defines a set of Common Labeling Conventions that may be applied to Applications.

Note: commonLabels should only be set for immutable labels, since they will be applied to Selectors.

Labeling Workload Resources makes it simpler to query Pods - e.g. for the purpose of getting their logs.

Setting Annotations

Setting Annotations is very similar to setting labels as seen above. Check out the reference for commands and examples.

Propagating Annotations

In addition to updating the annotations for each Resource, any fields that contain ObjectMeta (e.g. PodTemplate) will also have the annotations added.