echo "apiVersion: v1
kind: Node
metadata:
name: host-cluster-control-plane-64j47
labels:
beta.kubernetes.io/arch: amd64
" | yq -o p
Result:
apiVersion = v1
kind = Node
metadata.name = host-cluster-control-plane-64j47
metadata.labels.beta.kubernetes.io/arch = amd64
That's almost what I want. I am looking for the key to get values.
I could use metadata.name
like this:
echo "apiVersion: v1
kind: Node
metadata:
name: host-cluster-control-plane-64j47
labels:
beta.kubernetes.io/arch: amd64
" | yq '.metadata.name'
But the -o p
option of yq
does not quote the key, if needed.
I can't use metadata.labels.beta.kubernetes.io/arch
as key, since the correct syntax is metadata.labels["beta.kubernetes.io/arch"]
.
Is there an automated way to get the keys of a yaml file so that I can use the keys in yq
(or jq
)?
The desired output would be something like this:
apiVersion = v1
kind = Node
metadata.name = host-cluster-control-plane-64j47
metadata.labels["beta.kubernetes.io/arch"] = amd64
I am looking for the valid key, because I want to create a second command line to select these values.
For example:
❯ k get nodes -o yaml | yq '.items[].metadata.labels["beta.kubernetes.io/arch"]'
amd64
amd64
amd64
You can get close by doing something like:
yq '(.. | key | select(test("\."))) |= ("[\"" + . + "\"]")' file.yaml -op
apiVersion = v1
kind = Node
metadata.name = host-cluster-control-plane-64j47
metadata.labels.["beta.kubernetes.io/arch"] = amd64
Or you could do:
yq '(.. | key | select(test("\."))) |= sub("\.", "\.")' file.yaml -op
apiVersion = v1
kind = Node
metadata.name = host-cluster-control-plane-64j47
metadata.labels.beta\\.kubernetes\\.io/arch = amd64
BTW - I'm not sure how it's supposed be escaped in property files, I'd be willing to update yq to do it natively someone raises a bug with details on github...
Disclaimer: I wrote yq