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
metadata
and levellabels
are glued together (hereA.B
) from how levellabels
and levelbeta.kubernetes.io/arch
are (hereA["B"]
). Is it only the presence of dots (and/or maybe the slash) in "B"? What if these are present in "A" (on top-level)? Please define or include more samples. – Ballroomyq '.. | select(tag == "!!str") | path | . style="flow"'
oryq '.. | select(tag == "!!str") | [{"path": path, "value": .}]'
), and then operate on/with that instead. Embedding generated code is rarely a good idea. – Ballroomyq
output the path array and then you can later use the setpath path operation... – Bicentenary