Yq: not working when yaml file contains `---`

Created on 11 Sep 2019  路  8Comments  路  Source: mikefarah/yq

Example file test.yaml:

apiVersion: v1
kind: Service
metadata:
  labels:
    qcloud-app: my-app
  name: my-app
spec:
  ports:
  - name: tcp-3000-3000
    port: 80
    protocol: TCP
    targetPort: 3000
  selector:
    qcloud-app: my-app
  type: ClusterIP

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    qcloud-app: my-app
  name: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      qcloud-app: my-app
  template:
    metadata:
      labels:
        qcloud-app: my-app
    spec:
      containers:
        image: my-app:latest
        imagePullPolicy: Always
        name: my-app
      restartPolicy: Always
yq r test.yaml  # only gets the first part above ---

yq r -d "*" wx-collection-service.yaml  # this will get all data of test.yaml

Most helpful comment

Hi @mikefarah , in my case, YQ strips off the --- , also double quotes, also another thing I noticed was the yaml indentation for the first level:

---
version: "0.17.0"
java_opts:
  - "-Djavax.net.ssl.1=2"
  - "-Djavax.net.ssl.test='3'"
java_tool_options:
  - "-Xmx1g"
  - "-Xms1g"
  - "-javaagent:{{ home_dir }}/app/lib/cinnamon-agent.jar"

Becomes:

java_opts:
- -Djavax.net.ssl.1=2
- -Djavax.net.ssl.test='3'
java_tool_options:
- -Xmx1g
- -Xms1g
- -javaagent:{{ home_dir }}/app/lib/cinnamon-agent.jar
version: 0.17.0

All 8 comments

Worked for me, yq version 2.4.0

yq r -d"*" test.yaml kind

- Service
- Deployment

What didn't work?

Hi @mikefarah , in my case, YQ strips off the --- , also double quotes, also another thing I noticed was the yaml indentation for the first level:

---
version: "0.17.0"
java_opts:
  - "-Djavax.net.ssl.1=2"
  - "-Djavax.net.ssl.test='3'"
java_tool_options:
  - "-Xmx1g"
  - "-Xms1g"
  - "-javaagent:{{ home_dir }}/app/lib/cinnamon-agent.jar"

Becomes:

java_opts:
- -Djavax.net.ssl.1=2
- -Djavax.net.ssl.test='3'
java_tool_options:
- -Xmx1g
- -Xms1g
- -javaagent:{{ home_dir }}/app/lib/cinnamon-agent.jar
version: 0.17.0

As @fabianotessarolo mentioned, I also have the problem that yc is removing the leading --- line and also indenting the first level wrong.

@eryx12o45 the indentation is not wrong however, both formats yield the same object structure

Indentation is now controllable via flags - mind you @cebolan is right, the indentation wasn't wrong before (though I agree the formatting is odd).

The underlying yaml parser https://godoc.org/gopkg.in/yaml.v3#Node does not expose whether the yaml document has a separator or not - yq could re-parse the file to check, but that seems a little excessive given it's just a cosmetic issue (or is this causing failures somewhere?)

Seems like only the first part was readed.

root@shp-bank-app-k8s-node16:~/yaml# yq r activity.yaml 'apiVersion'
v1
root@shp-bank-app-k8s-node16:~/yaml# yq r activity.yaml 'spec'
ports:
- name: tcp-10420-10420
  port: 80
  protocol: TCP
  targetPort: 10420
selector:
  qcloud-app: activity
type: ClusterIP
root@shp-bank-app-k8s-node16:~/yaml# yq r activity.yaml 'spec.template'
root@shp-bank-app-k8s-node16:~/yaml#

Works this way, thanks @v-avdeev

root@shp-bank-app-k8s-node16:~/yaml# yq r -d "*" test.yaml "spec.template.spec.containers.image"
my-app:latest
yq r --help
Flags:
  -d, --doc string   process document index number (0 based, * for all documents) (default "0")
Was this page helpful?
0 / 5 - 0 ratings

Related issues

yfried picture yfried  路  3Comments

OnkelTem picture OnkelTem  路  8Comments

curiositycasualty picture curiositycasualty  路  5Comments

jmreicha picture jmreicha  路  5Comments

mdubinko picture mdubinko  路  8Comments