简体   繁体   中英

Merge / Apend two YAML files

Let's assume that you want to inject an extra container to all the Pods submitted to the cluster.

You could save the YAML configuration for the extra container as a YAML file called file:

fileA:

apiVersion: v1
kind: Pod
metadata:
  name: envoy-pod
spec:
  containers:
  - name: proxy-container
    image: envoyproxy/envoy:v1.12.2
    ports:
      - containerPort: 80

and you have fileB

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
  - name: test-container
    image: k8s.gcr.io/busybox
    env:
    - name: DB_URL
      value: postgres://db_url:5432

The output should be

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
  - name: test-container
    image: k8s.gcr.io/busybox
    env:
    - name: DB_URL
      value: postgres://db_url:5432
  - name: proxy-container
    image: envoyproxy/envoy:v1.12.2
    ports:
      - containerPort: 80

This was possible in the older version of yq by

yq m -a append fileA.yaml fileB.yaml

However, this appears not possible in v4 - any suggestions?

You can now use the merge/append operator *+ :

yq '. *+ load("fileB.yaml")' fileA.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
    - name: proxy-container
      image: envoyproxy/envoy:v1.12.2
      ports:
        - containerPort: 80
    - name: test-container
      image: k8s.gcr.io/busybox
      env:
        - name: DB_URL
          value: postgres://db_url:5432

gojq won't retain the original ordering of keys, but if that is not a concern you could go with:

gojq --yaml-output --yaml-input '.spec.containers += input.spec.containers'  y2.yaml y1.yaml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM