14

Does Kubernetes GET API actually support fieldSelector parameter to query values of array fields?

For example, I have a Pod like:

apiGroup: v1
kind: Pod
metadata:
  ownerReferences:
  - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: grpc-ping-r7f8r-deployment-54d688d777
    uid: 262bab1a-1c79-11ea-8e23-42010a800016

Can I do something like:

kubectl get pods --field-selector 'metadata.ownerReferences.uid=262bab1a-1c79-11ea-8e23-42010a800016'

This command fails (field label not supported: metadata.ownerReferences.uid). I suspect the reason is that ownerReferences is an array field itself. I've also tried, but didn't work:

  • metadata.ownerReferences[*].uid=
  • metadata.ownerReferences[].uid=

I might try client-go SDK for Kubernetes API, but I suspect it won't work for the same reason.

Is there a server-side way to query by this? Thanks much.

ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214

2 Answers2

32

The --field-selector only works with some limited fields.

Which contains:

"metadata.name",
"metadata.namespace",
"spec.nodeName",
"spec.restartPolicy",
"spec.schedulerName",
"spec.serviceAccountName",
"status.phase",
"status.podIP",
"status.podIPs",
"status.nominatedNodeName"

But you can perform the task by using jq. Here is a command that I used for listing all ready nodes. It demonstrates the use of array fields that you're looking for.

$ kubectl get nodes -o json | jq -r '.items[] | select(.status.conditions[] | select(.type=="Ready" and .status=="True")) | .metadata.name '

master-0
node-1
node-3
ahmet alp balkan
  • 42,679
  • 38
  • 138
  • 214
Kamol Hasan
  • 12,218
  • 1
  • 37
  • 46
  • Thanks, I wasn't able to find which fields are searchable through field selector. https://kubernetes.io/docs/concepts/overview/working-with-objects/field-selectors/ the docs don't have it. It seems like what I'm trying to do is not possible. – ahmet alp balkan Jan 07 '20 at 00:43
  • @AhmetB-Google always welcome, though I knew you didn't need this one :P but might help others. – Kamol Hasan Jan 07 '20 at 04:18
2

I think what you really want to do is a filter rather than a query. By using JSONPath you can filter out content using ?().

For example the following would work:

kubectl get pods -o jsonpath='{range .items[?(.metadata.ownerReferences.uid=262bab1a-1c79-11ea-8e23-42010a800016)]}{.metadata.name}{end}'
Kevin Languasco
  • 2,318
  • 1
  • 14
  • 20