为什么 - image 没有缩进,而是与 containers 对齐?

为什么 - image 没有缩进,而是与 containers 对齐?

apiVersion: v1
kind: Pod
metadata:
  name: busy-pod
  labels: 
    owner: chrono
    env: prod
    region: north
    tier: back
    selfdefined: important
spec: 
  containers:
  - image: docker.nastool.de/library/busybox:latest
    name: busy
    imagePullPolicy: IfNotPresent
    env: 
      - name: os
        value: "ubuntu"
      - name: debug
        value: "on"
    command:
      - /bin/echo
    args: 
      - "$(os), $(debug)"

参考 Indenting YAML files

The example below shows a combination of sequences and mappings using two spaces of indentation. Note how the sequence assigned to the mapping is indented.

labels:
  - name: "bug"
    color: "ee0701"

  - name: "enhancement"
    color: "0e8a16"

repository:
  allow_merge_commit: true
  allow_rebase_merge: false
  default_branch: "main"

The example below shows a combination of sequences and mappings using two spaces of indentation. Note how the sequence assigned to the mapping does not use indentation.

labels:
- name: "bug"
  color: "ee0701"

- name: "enhancement"
  color: "0e8a16"

repository:
  allow_merge_commit: true
  allow_rebase_merge: false
  default_branch: "main"

According to the YAML specification, the above examples are equivalent.

也就是说使用两个空格的缩进和不使用缩进是等价的。

spec: 
  containers:
  - image: docker.nastool.de/library/busybox:latest

spec: 
  containers:
    - image: docker.nastool.de/library/busybox:latest

注意这里不仅要将 - image 缩进两个空格,属于同一 block 的 name、imagePullPolicy、env 等同样要缩进两个空格。

apiVersion: v1
kind: Pod
metadata:
  name: busy-pod
  labels: 
    owner: chrono
    env: prod
    region: north
    tier: back
    selfdefined: important
spec: 
  containers:
    - image: docker.nastool.de/library/busybox:latest
      name: busy
      imagePullPolicy: IfNotPresent
      env: 
        - name: os
          value: "ubuntu"
        - name: debug
          value: "on"
      command:
        - /bin/echo
      args: 
        - "$(os), $(debug)"

使用 - image 有两空格缩进的 busy-pod.yml 文件来启动 pod,从下面的输出中可知,启动成功。

$ kubectl apply -f busy-pod.yml 
pod/busy-pod created

$ kubectl get pods
NAME       READY   STATUS             RESTARTS      AGE
busy-pod   0/1     CrashLoopBackOff   2 (22s ago)   39s

$ kubectl logs busy-pod
ubuntu, on


posted @ 2024-11-24 11:19  甲兵匪已  阅读(9)  评论(0)    收藏  举报