My jq Cheatsheet
kubectl with jq is so powerful
The dozens of types and thousands of instances in hundreds of namespaces make it super hard to fetch things I want. For instance, who are owners of certain resources?
kubectl
’s options and flags, such as label-selector
, go-templates
, jsonpath
, etc., can improve query efficiency by filtering and selection. However, these options usually are only supportive of regular operations and not applicable to all situations. Moreover, different options have different grammatical rules, making it difficult to understand and master them all.
But kubectl
provides a method exporting the resource configuration (YAML) directly into JSON, -o json
. Then, we can turn our “imagination” into practice with CLI tools likejq
, awk
, grep
, etc. Among them, jq
maybe the best fit to reading, parsing, and mutating Kubernetes objects results from kubectl
.
It seems as easy as pie to understand jq
in the beginning, and the most common usages are read
and write
.
shell# read kind$ kubectl get pod xxx -n {namespace} -ojson | jq ‘.kind’$ “Pod”# edit the name$ kubectl get pod xxx -n {namespace} -ojson | jq ‘.metadata.name=”test”’