1

What is the meaning of value key being under the "name" list but with no dash before it? (How should I read and understand this)

apiVersion: v1
kind: Pod
metadata:
  name: postgres
  labels:
    tier: db-tier
spec:
  containers:
    - name: postgres
      image: postgres
      env:
        - name: POSTGRES_PASSWORD
          value: mysecretpassword

Doing this instead will not result in a valid K8s YAML. Why is that ?

apiVersion: v1
kind: Pod
metadata:
  name: postgres
  labels:
    tier: db-tier
spec:
  containers:
    - name: postgres
      image: postgres
      env:
        - name: POSTGRES_PASSWORD
        - value: mysecretpassword
zulu-700
  • 47
  • 5

2 Answers2

3

If we considered your suggestion on contrary to actual solution than kubernetes treat your format of

env:
    - name: POSTGRES_PASSWORD
    - value: mysecretpassword
    - name: variable
    - value: answer

as:-

env: [{name:POSTGRES_PASSWORD}, {value:mysecretpassword}, {name:variable}, {value:answer}]

In this way, K8s will not able to map actual object of key value pair.

So the actual format to define array in yaml, or we can say the syntax to define an array in yaml is below.

For example:-

env:
    - name: POSTGRES_PASSWORD
      value: mysecretpassword
    - name: POSTGRES_USER
      value: root

This will be treated as:-

env: [{name:POSTGRES_PASSWORD, value:mysecretpassword}, {name:POSTGRES_USER, value:root}]

For more help you can visit so link 1 and so link 2.

vishal
  • 1,368
  • 2
  • 15
  • 34
0

The env section of a pod definition YAML file accepts an array of key value pairs.

The dash express an element in the array.

I suggest to look at the YAML spec : https://www.commonwl.org/user_guide/yaml/#arrays

And also recommend the Kubernetes for absolute beginners course which is very great :

https://www.udemy.com/course/learn-kubernetes/

Christophe Willemsen
  • 19,399
  • 2
  • 29
  • 36