How to apply a jq filter to kubectl output in watch mode?

7/11/2018

I want to filter the output of a kubectl command running in --watch mode to keep an eye on changes to a certain k8s annotation. I'm using the following command:

kubectl get pod my-pod --watch -o json | jq '.metadata.annotations["my-annotation"]'

Problem is - nothing is printed until I stop the command. I'd like the jq to process the input and print it as the changes to my-pod happen.

I tried using the --unbuffered flag but it doesn't fix it.

-- grdl
jq
kubectl
kubernetes

1 Answer

7/11/2018

So i've tested your command and it works perfectly. You are however missing a quote at the end of your command.

kubectl get pod nginx-5dfd5597bb-tp8h7 --watch -o json | jq '.metadata.name'

gives me

"nginx-5dfd5597bb-tp8h7"

if you can this command to work but not your own; it is probably related to the brackets and quotes or missing object keys.

-- Webber
Source: StackOverflow