Reputation: 25
I am trying to output the value for .metadata.name followed by the student's name in .spec.template.spec.containers[].students[] array using the regex in JsonPath for a Kubectl query.
I had actually asked a similar question linked here for this in jq.
How do I print a specific value of an array given a condition in jq if there is no key specified
The solution worked but I am wondering if there is an alternative solution for it using JsonPath or go-template perhaps (without the need for using jq).
For example, if I check the students[] array if it contains the word "Jeff", I would like the output to display as below:
student-deployment: Jefferson
What I've tried:
For JsonPath, I've tried the query below:
kubectl get deployment -o=jsonpath="{range .items[?(@.spec.template.spec.containers[*].students[*])]}{'\n'}{.metadata.name}{':\t'}{range .spec.template.spec.containers[*]}{.students[?(@=="Jefferson")]}{end}{end}"
But this only works to evaluate for matching words. Would it be possible to use JsonPath to query for regex as I've read here that JsonPath regex =~ doesn't work? I did try to use | grep and findstr but it still returned all values inside the array back to me. Other than jq, is there another way to retrieve the regex output?
https://github.com/kubernetes/kubernetes/issues/61406
The deployment template below is in json and I shortened it to only the relevant parts.
{
"apiVersion": "v1",
"items": [
{
"apiVersion": "apps/v1",
"kind": "Deployment",
"metadata": {
"name": "student-deployment",
"namespace": "default"
},
"spec": {
"template": {
"spec": {
"containers": [
{
"students": [
"Alice",
"Bob",
"Peter",
"Sally",
"Jefferson"
]
}
]
}
}
}
}
]
}
Upvotes: 2
Views: 6182
Reputation: 2280
The documentation for JSONPath Support clearly describes that it is not possible with JSONPath and you can use jq.
https://kubernetes.io/docs/reference/kubectl/jsonpath/
Upvotes: 4