Skip to content

Labels

adeploy provides functions and mechanisms to create default and custom labels following best practises in a single place of your default or namespace/release configuration.

Default Labels

JinjaHelm

You can use the Jinja function create_labels() to create set of default labels plus additional labels that you can specify in your defaults.yml or namespace/release configuration as follows:

defaults.yml
labels:
  nginx: {{ create_labels(name='nginx') }}

In your Jinja deployment template, you can use the labels variable as follows:

deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ name }}-{{ release }}
  namespace: {{ namespace }}
  labels: {{ labels.nginx }}
spec:
  selector:
    matchLabels: {{ labels.nginx }}
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels: {{ labels.nginx }}

See create_labels() for usage and parameters.

Nested Labels

You can use create_labels() to define global labels i.e. in your defaults.yml:

defaults.yml
labels: {{ create_labels(component="my-deployment") }}

And use create_labels() another time to add object related labels i.e. in your template files:

templates/deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ name }}-{{ release }}
  namespace: {{ namespace }}
  labels: {{ create_labels(name="nginx", labels=labels) }}
spec:
  selector:
    matchLabels: {{ create_labels(name="nginx", labels=labels) }}
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels: {{ create_labels(name="nginx", labels=labels) }}

This will result in the following labels:

build/jinja/.../prod/deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: 006-nested-labels-prod
  namespace: playground
  labels: 
    app.kubernetes.io/name: "nginx"
    app.kubernetes.io/instance: "prod"
    app.kubernetes.io/component: "my-deployment"
    app.kubernetes.io/part-of: "006-nested-labels"
    app.kubernetes.io/managed-by: "adeploy"

See create_labels() for usage and parameters.

Global Label Configuration Jinja

Similar to probes or resource-limits, you can specify a global labels configuration in defaults.yml or in the namespace/release configuration as follows:

defaults.yml
_labels:
  app.kubernetes.io/component: nginx
  app.kubernetes.io/instance: {{ name }}-{{ release }}
  app.kubernetes.io/part-of: {{ name }}
  my.custom.labels/test: my-label

These labels will be added to all appropriate metadata, matchLabels and selector properties.

Tip

Note that you can use Jinja variables in the labels, too.