Search code examples
kubernetes-helm

What does & and * denote in a helm template?


What does & and * denote in a helm template?

https://github.com/SeleniumHQ/docker-selenium/blob/trunk/charts/selenium-grid/templates/chrome-node-deployment.yaml#L7

https://github.com/SeleniumHQ/docker-selenium/blob/trunk/charts/selenium-grid/templates/chrome-node-deployment.yaml#L25

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ template "seleniumGrid.chromeNode.fullname" . }}
  namespace: {{ .Release.Namespace }}
  labels: &chrome_node_labels //**& here**
    app: selenium-chrome-node
    app.kubernetes.io/name: selenium-chrome-node
    {{- include "seleniumGrid.commonLabels" . | nindent 4 }}
    {{- with .Values.chromeNode.labels }}
      {{- toYaml . | nindent 4 }}
    {{- end }}
    {{- with .Values.customLabels }}
      {{- toYaml . | nindent 4 }}
    {{- end }}
spec:
  replicas: {{ .Values.chromeNode.replicas }}
  selector:
    matchLabels:
      app: selenium-chrome-node
      app.kubernetes.io/instance: {{ .Release.Name }}
  template:
    metadata:
      labels: *chrome_node_labels // **star here **
      annotations:

Solution

  • This looks like the usage of YAML anchors, but I remember it would result in an error if used directly in the Helm templates. I guess they might be doing a second rendering here, where the YAML is parsed first.

    YAML anchors

    Anchors and aliases. There are 2 parts to this:

    • The anchor & which defines a chunk of configuration

    • The alias * used to refer to that chunk elsewhere

    So in the example below we use &build-test to define a step entity, which has several lines for reuse, and the alias *build-test to reuse it.

    definitions: 
      steps:
        - step: &build-test
            name: Build and test
            script:
              - mvn package
            artifacts:
              - target/**
    
    pipelines:
      branches:
        develop:
          - step: *build-test
        main:
          - step: *build-test
    

    anchors-and-aliases