Labels and Annotations

Dealing with Labels and Annotations

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

# kustomization.yaml
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
commonLabels:
  app: foo
  environment: test
resources:
- deployment.yaml
# deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
    bar: baz
spec:
  selector:
    matchLabels:
      app: nginx
      bar: baz
  template:
    metadata:
      labels:
        app: nginx
        bar: baz
    spec:
      containers:
      - name: nginx
        image: nginx

Applied: The Resource that is Applied to the cluster

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: foo # Label was changed
    environment: test # Label was added
    bar: baz # Label was ignored
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: foo # Selector was changed
      environment: test # Selector was added
      bar: baz # Selector was ignored
  template:
    metadata:
      labels:
        app: foo # Label was changed
        environment: test # Label was added
        bar: baz # Label was ignored
    spec:
      containers:
      - image: nginx
        name: nginx

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.

Setting Annotations

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


Last modified October 31, 2020: docs render fix (cd35140)