kubectl get pods JSONPATH failed to do a pattern match

12/7/2021

I'm trying to retrieve pods using a JSONPATH query which matches the name with a certain pattern matching as specified below and I get the error as shown. Any reason what would be the reason for the failure.

kubectl get po -n sdfd -o jsonpath='{.items[?(@.metadata.generateName =~ /abc.*?/i)].status.podIP}'
error: error parsing jsonpath {.items[?(@.metadata.generateName =~ /abc.*?/i)].status.podIP}, unrecognized character in action: U+007E '~'

Please find the kubectl cli version as shown below :-

kubectl version                                                  
Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.0", GitCommit:"e19964183377d0ec2052d1f1fa930c4d7575bd50", GitTreeState:"clean", BuildDate:"2020-08-26T21:54:15Z", GoVersion:"go1.15", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.9", GitCommit:"454b5b515582f8ac8419435dc9c230fc97fb844b", GitTreeState:"clean", BuildDate:"2021-11-01T19:59:05Z", GoVersion:"go1.15.14", Compiler:"gc", Platform:"linux/amd64"}
-- Avi
jsonpath
kubectl
kubernetes

2 Answers

12/7/2021

Regular expressions are not supported with jsonpath. Here's the github issue for this matter. You can use jq to process the output with regex.

-- gohm'c
Source: StackOverflow

12/7/2021

According to the documentation:

JSONPath regular expressions are not supported. If you want to match using regular expressions, you can use a tool such as jq.

Run the following command to achieve the desired result:

kubectl get pods -o json | jq -r '.items[] | select(.metadata.name | test("test-")).spec.containers[].image'

Refer to this Github issue and stackpost for more information.

-- Fariya Rahmat
Source: StackOverflow