Home > Back-end >  kubectl get pods JSONPATH failed to do a pattern match
kubectl get pods JSONPATH failed to do a pattern match

Time:12-07

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"}

CodePudding user response:

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.

CodePudding user response:

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.

  • Related