yamlyaml-anchors

YAML anchors and override


I have a workflow with the following templates:

  templates:
  - name: main
    dag:
      tasks:
        - name: task1
          template: step1
        - name: task2
          template: step2

  - name: step1
    container:
      image: some-image
      imagePullPolicy: IfNotPresent
      command: [ bash ]
      args:
        - exit 0

  - name: step2
    container:
      image: some-image
      imagePullPolicy: IfNotPresent
      command: [ bash ]
      args:
        - exit 0

As shown, step1 and step2 are the same with a difference in their names. I believe this duplication can be avoided by defining a single template and then using YAML anchors to reuse it.

  - name: $step
    container:
      image: some-image
      imagePullPolicy: IfNotPresent
      command: [ bash ]
      args:
        - exit 0

  - name: *step

My question is now regarding the name. How can the names be overriden?


Solution

  • I ended up using an anchor for the containers:

      - name: step1
        container: &container
          image: some-image
          imagePullPolicy: IfNotPresent
          command: [ bash ]
          args:
            - exit 0
    
      - name: step2
        container: *container