YDD9
YDD9

Reputation: 135

How to use kubectl get pods -o=jsonpath={}

I'm following https://www.katacoda.com/courses/kubernetes/liveness-readiness-healthchecks

I saw an interesting command to parse json object use flag -o=jsonpath in kubectl as below:

# pod=$(kubectl get pods --selector="name=bad-frontend" --output=jsonpath={.items..metadata.name})

# kubectl describe pod $pod

How map[string]interface {}, []interface {}{map[string]interface {}, map[string]interface {}{} is used in the raw json?

How can I easily know {.itme..metadata.name} is correct, not using something {..item.metadata..name}?

Raw file to check (if you pass wrongly {}, it will return this to you):

map[string]interface {}{"kind":"List", "apiVersion":"v1", "metadata":map[string]interface {}{}, "items":[]interface {}{map[string]interface {}{"status":map[string]interface {}{"phase":"Running", "conditions":[]interface {}{map[string]interface {}{"type":"Ready", "status":"False", "lastProbeTime":interface {}(nil), "lastTransitionTime":"2018-01-03T14:41:26Z", "reason":"ContainersNotReady", "message":"containers with unready status: [bad-frontend]"}}, "hostIP":"127.0.0.1", "podIP":"172.18.0.3", "startTime":"2018-01-03T14:41:26Z", "containerStatuses":[]interface {}{map[string]interface {}{"ready":false, "restartCount":6, "image":"katacoda/docker-http-server:unhealthy", "imageID":"docker://sha256:dc680e51481ae0256b5483e0d3c0bd188215a67b0926d4ed07e8a9fe55e16154", "containerID":"docker://3a067a667e8e73784c761d75c3614d3aa138a6d498b22b48e1c23aa1d8158170", "name":"bad-frontend", "state":map[string]interface {}{"waiting":map[string]interface {}{"reason":"CrashLoopBackOff", "message":"Back-off 2m40s restarting failed container=bad-frontend pod=bad-frontend-zbgrw_default(28bdddce-f094-11e7-9f7b-0242ac110036)"}}, "lastState":map[string]interface {}{"terminated":map[string]interface {}{"containerID":"docker://3a067a667e8e73784c761d75c3614d3aa138a6d498b22b48e1c23aa1d8158170", "exitCode":2, "reason":"Error", "startedAt":"2018-01-03T14:44:57Z", "finishedAt":"2018-01-03T14:45:06Z"}}}}}, "kind":"Pod", "apiVersion":"v1", "metadata":map[string]interface {}{"name":"bad-frontend-zbgrw", "generateName":"bad-frontend-", "uid":"28bdddce-f094-11e7-9f7b-0242ac110036", "labels":map[string]interface {}{"name":"bad-frontend"}, "annotations":map[string]interface {}{"kubernetes.io/created-by":"{\"kind\":\"SerializedReference\",\"apiVersion\":\"v1\",\"reference\":{\"kind\":\"ReplicationController\",\"namespace\":\"default\",\"name\":\"bad-frontend\",\"uid\":\"24a58c4a-f094-11e7-9f7b-0242ac110036\",\"apiVersion\":\"v1\",\"resourceVersion\":\"15\"}}\n"}, "namespace":"default", "selfLink":"/api/v1/namespaces/default/pods/bad-frontend-zbgrw", "resourceVersion":"300", "creationTimestamp":"2018-01-03T14:41:18Z"}, "spec":map[string]interface {}{"restartPolicy":"Always", "terminationGracePeriodSeconds":30, "dnsPolicy":"ClusterFirst", "nodeName":"127.0.0.1", "securityContext":map[string]interface {}{}, "containers":[]interface {}{map[string]interface {}{"terminationMessagePath":"/dev/termination-log", "imagePullPolicy":"IfNotPresent", "name":"bad-frontend", "image":"katacoda/docker-http-server:unhealthy", "resources":map[string]interface {}{}, "livenessProbe":map[string]interface{}{"timeoutSeconds":1, "periodSeconds":10, "successThreshold":1, "failureThreshold":3, "httpGet":map[string]interface {}{"path":"/", "port":80, "scheme":"HTTP"}, "initialDelaySeconds":1}, "readinessProbe":map[string]interface {}{"httpGet":map[string]interface {}{"path":"/", "port":80, "scheme":"HTTP"}, "initialDelaySeconds":1, "timeoutSeconds":1, "periodSeconds":10, "successThreshold":1, "failureThreshold":3}}}}}}}

Upvotes: 1

Views: 10242

Answers (1)

YDD9
YDD9

Reputation: 135

I'm happy that I found a good solution explained in details here: https://gist.github.com/so0k/42313dbb3b547a0f51a547bb968696ba

Basically you have to install jq or jiq, then type kubectl get no -o json | jid -q | pbcopy. The fun part begins: you type . and item, etc will be proposed for you to choose, you can easily construct your filter syntax.

Upvotes: 1

Related Questions