JQ vs Python for Kubernetes Object Parsing
Using JQ or Python for same results.
These days it’s hard to work with Kubernetes without touching JSON + JQ. However, I always struggle with its language. Some concepts are very hard to cover and I usually end up looking it up at Google, Stack Overflow. I generally struggle with easy tasks as printing output in one line etc.
See one real life example:
kubectl get deploy -A -o json | jq -c '.items[] | select((.metadata.name | contains("editor")) or (.metadata.name | contains("tool")) or (.metadata.name | contains("world"))) | {name: .metadata.name, chart: .metadata.labels.chart, appVersion: .metadata.labels.appVersion}'
As you can see, this lists all deployments in your Kubernetes cluster as JSON objects and if existing deployment matches some sub-strings it will print different labels in one line. I tried to implement the same thing in Python and it came out great. It was surely a more pleasant experience than using?JQ.
from kubernetes import client, config
config.load_kube_config()
v1 = client.AppsV1Api()
deployments=v1.list_deployment_for_all_namespaces(pretty=True)
for i in deployments.items:
if any (sub in i.metadata.name for sub in ['-tool', 'editor', 'xml', 'world']):
print("name = {name}, chart={chart}, appVersion={appVersion}".format(name = i.metadata.name, chart=i.metadata.labels['chart'], appVersion=i.metadata.labels['appVersion']))
Which method do you prefer?
Cloud Architect, {Developer,Director} at NetBSD
1 年Usually just jq or json2tsv ( https://codemadness.org/json2tsv.html ) and AWK if it's a bit more complicated. In my experience pretty complex jq programs are a bit like regular expressions: can be easy to write but hard to read a couple of hours^Wdays later! :) In that particular case though... I would have probably lied by using custom column output of `kubectl` and just some AWK: :) kubectl get deployment -A -o custom-columns="NAME:.metadata.name,CHART:.metadata.labels.chart,APP-VERSION:.metadata.labels.appVersion" | awk '$1 ~ /editor|tool|world/'
Software Engineer ? DevOps ? Open Source
1 年jq is great query tool in everyone's arsenal, if your goal is not only simple query then pick better tool .
? Cloud Architect
1 年jq for me personally :) ... this free course https://kodekloud.com/courses/json-path-quiz/ helped me to practice it during studing for CKA :) ... but it is really for beginners, although, I have had a fun during practicing, just learning by doing :) ... python would be big overhead for my needs ...
DevSecOps engineer
1 年personally I would not whip up python code for a one time json parsing and would use jq. however I agree with the fact that I mostly Google the syntax :)