HiveBrain v1.2.0
Get Started
← Back to all entries
snippetkubernetesMinor

How to specify a filter for Kubectl -o flag=custom-column

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
columnflagcustomfilterforhowspecifykubectl

Problem

I want to use kubectl get -o=custom-columns to extract information with specific columns. The cropped yaml response is like this

{
    "apiVersion": "v1",
    "items": [
        {
            "apiVersion": "v1",
            "kind": "Pod",
            "metadata": {
                "creationTimestamp": "2018-11-27T14:37:46Z",
                "generateName": "kubernetes-bootcamp-5c69669756-",
                "labels": {
                    "pod-template-hash": "1725225312",
                    "run": "kubernetes-bootcamp"
                },
                "name": "kubernetes-bootcamp-5c69669756-cxhwf",
                "namespace": "default",
                "ownerReferences": [
                    {
                        "apiVersion": "extensions/v1beta1",
                        "blockOwnerDeletion": true,
                        "controller": true,
                        "kind": "ReplicaSet",
                        "name": "kubernetes-bootcamp-5c69669756",
                        "uid": "950aac8e-f24f-11e8-bf5d-065b6ba59aa8"
                    }
                ],
                "resourceVersion": "118035",
                "selfLink": "/api/v1/namespaces/default/pods/kubernetes-bootcamp-5c69669756-cxhwf",
                "uid": "0190c234-f252-11e8-bf5d-065b6ba59aa8"
            },


I want to extract the name from items[0].metadata.name, but I don't know the syntax.

My questions:

  • What is the query syntax used in this situation. Is it JSON path? I don't see it documented.



  • What is the specific query in my case?

Solution

Since the result is JSON, pipe the result to jq and go from there.

kubectl get | jq '.items[0].metadata.name'


yields

➜ cat abc.json | jq '.items[0].metadata.name'
"kubernetes-bootcamp-5c69669756-cxhwf"

Code Snippets

kubectl get | jq '.items[0].metadata.name'
➜ cat abc.json | jq '.items[0].metadata.name'
"kubernetes-bootcamp-5c69669756-cxhwf"

Context

StackExchange DevOps Q#5556, answer score: 2

Revisions (0)

No revisions yet.