Kubernetes API: list pods with a label

Viewed 4794

I have namespace with few deployments. One of the deployments has a specific label (my-label=yes). I want to get all the pods with this label.

This is how it done with kubectl:

kdev get pods -l my-label=yes

it's working.

Now I want to do it with Kubernetes API. This is the closest point I get:

curl https://kubernetes.default.svc/api/v1/namespaces/XXX/pods --silent --header "Authorization: Bearer $TOKEN" --insecure

This command get all the pods in the namespace. I want to filter the results to all the pods with this requested label. How to do it?

Even more wide question: Is this possible to "translate" kubectl command into REST API call?

2 Answers

Is this possible to "translate" kubectl command into REST API call?

When you execute any command using kubectl it internally gets translated into a REST call with json payload before sending it to Kubernetes API Server. An easy way to inspect that is to run the command with verbosity increased

kubectl get pods -n kube-system -l=tier=control-plane --v=8
I0730 15:21:01.907211    5320 loader.go:375] Config loaded from file:  /Users/arghyasadhu/.kube/config
I0730 15:21:01.912119    5320 round_trippers.go:420] GET https://xx.xx.xxx.xxx:6443/api/v1/namespaces/kube-system/pods?labelSelector=tier%3Dcontrol-plane&limit=500
I0730 15:21:01.912135    5320 round_trippers.go:427] Request Headers:
I0730 15:21:01.912139    5320 round_trippers.go:431]     Accept: application/json;as=Table;v=v1;g=meta.k8s.io,application/json;as=Table;v=v1beta1;g=meta.k8s.io,application/json
I0730 15:21:01.912143    5320 round_trippers.go:431]     User-Agent: kubectl/v1.18.0 (darwin/amd64) kubernetes/9e99141
I0730 15:21:02.071778    5320 round_trippers.go:446] Response Status: 200 OK in 159 milliseconds
I0730 15:21:02.071842    5320 round_trippers.go:449] Response Headers:
I0730 15:21:02.071858    5320 round_trippers.go:452]     Cache-Control: no-cache, private
I0730 15:21:02.071865    5320 round_trippers.go:452]     Content-Type: application/json
I0730 15:21:02.071870    5320 round_trippers.go:452]     Date: Thu, 30 Jul 2020 09:51:02 GMT
I0730 15:21:02.114281    5320 request.go:1068] Response Body: {"kind":"Table","apiVersion":"meta.k8s.io/v1","metadata":{"selfLink":"/api/v1/namespaces/kube-system/pods","resourceVersion":"1150005"},"columnDefinitions":[{"name":"Name","type":"string","format":"name","description":"Name must be unique within a namespace. Is required when creating resources, although some resources may allow a client to request the generation of an appropriate name automatically. Name is primarily intended for creation idempotence and configuration definition. Cannot be updated. More info: http://kubernetes.io/docs/user-guide/identifiers#names","priority":0},{"name":"Ready","type":"string","format":"","description":"The aggregate readiness state of this pod for accepting traffic.","priority":0},{"name":"Status","type":"string","format":"","description":"The aggregate status of the containers in this pod.","priority":0},{"name":"Restarts","type":"integer","format":"","description":"The number of times the containers in this pod have been restarted.","priority":0},{"name":"Age","type":"strin [truncated 16503 chars]

Found it.

curl https://kubernetes.default.svc/api/v1/namespaces/XXX/pods?labelSelector=my-label%3Dyes --silent --header "Authorization: Bearer $TOKEN" --insecure
Related