yq 添加带有名称和子字段的数组元素

yq add array element with name and subfields

我知道了 test.yaml:

volumes:
  - name: var-log
    hostPath:
      path: /var/log
  - name: dev-log
    hostPath:
      path: /dev/log

并希望使用 Go yq - https://github.com/mikefarah/yq/

实现以下目标
volumes:
  - name: var-log
    hostPath:
      path: /var/log
  - name: dev-log
    hostPath:
      path: /dev/log
  - name: sys
      mountPath: /sys

我真的无法理解它 - 有:

yq eval '.volumes |= (. + {"name":{"name":"sys"}})' test.yaml我差不多明白了,但是第3个字段名不见了(应该是name: sys但它只是name:

volumes:
  - name: var-log
    hostPath:
      path: /var/log
  - name: dev-log
    hostPath:
      path: /dev/log
  - name:
      mountPath: /sys

我现在摆弄了一段时间,但无法正常工作。

您可以让 namemountPath 出现在同一层:

yq '.volumes += [{"name": "sys", "mountPath": "/sys"}]' test.yaml
volumes:
  - name: var-log
    hostPath:
      path: /var/log
  - name: dev-log
    hostPath:
      path: /dev/log
  - name: sys
    mountPath: /sys

或者您可以将它们嵌套,缺少“连接”值:

yq '.volumes += [{"name": {"mountPath": "/sys"}}]' test.yaml
volumes:
  - name: var-log
    hostPath:
      path: /var/log
  - name: dev-log
    hostPath:
      path: /dev/log
  - name:
      mountPath: /sys