yq to manage your K8s yaml

On the current project, I had the need to extract the cpu limits from the deployment descriptor, sum them to check resource usage and print a report.

I need to do it on a lot of microservices, and also it can change in respect of the target environment.

I had a big helm template file to work with, so I need to filter the correct yaml descriptor, and work on it.

I ended up using a tool called yq which enable to do query like the jq utility (which is for json file). After expanding helm template in a file called k8s-deployment-descriptor.yaml, I was able to get the sum for all the pods with a sub optimal command like:

yq 'select(.kind == "Deployment").spec.template.spec.containers[].resources.limits.cpu | sub("m","")  ' \
 k8s-deployment-descriptor.yaml | awk '{sum+=$0} END{print sum}'

I was unable to find a way to sum them inside yq, so I was forced to use awk.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.