Andy K
Andy K

Reputation: 5044

kubectl output formatted : looking for a specific column

I'm looking to have a specific column, through my initial kubectl command

kubectl --context oam-ska-yo_jonnylee -n ebzz get -o json ingressroute zz-oamtoto

This give me the json below

{
    "apiVersion":"traefik.containo.us/v1alpha1",
    "kind":"IngressRoute",
    "metadata":{
       "annotations":{
          "kubectl.kubernetes.io/last-applied-configuration":"{"sum-up"}\n"
       },
       "creationTimestamp":"2021-02-23T11:17:56Z",
       "generation":1,
       "labels":{
          "instance":"webzz"
       },
       "managedFields":[
          {
             "apiVersion":"traefik.containo.us/v1alpha1",
             "fieldsType":"FieldsV1",
             "fieldsV1":{
                "f:metadata":{
                   "f:annotations":{
                      ".":{
                         
                      },
                      "f:kubectl.kubernetes.io/last-applied-configuration":{
                         
                      }
                   },
                   "f:labels":{
                      ".":{
                         
                      },
                      "f:instance":{
                         
                      }
                   }
                },
                "f:spec":{
                   ".":{
                      
                   },
                   "f:entryPoints":{
                      
                   },
                   "f:routes":{
                      
                   }
                }
             },
             "manager":"kubectl",
             "operation":"Update",
             "time":"2021-02-23T11:17:56Z"
          }
       ],
       "name":"zz-oamtoto",
       "namespace":"ebzz",
       "resourceVersion":"61112315",
       "selfLink":"/apis/traefik.containo.us/v1alpha1/namespaces/ebzz/ingressroutes/zz-oamtoto",
       "uid":"42727XXX-dd9e-45e4-9c7d-1225aea125"
    },
    "spec":{
       "entryPoints":[
          "http"
       ],
       "routes":[
          {
             "kind":"Rule",
             "match":"Host(`ebzz.acme.com`)",
             "middlewares":[
                {
                   "name":"ebzz-ebzz-basicauth"
                }
             ],
             "services":[
                {
                   "kind":"Service",
                   "name":"zz-oamtoto",
                   "port":1234
                }
             ]
          }
       ]
    }
 }

What I'm looking at is to find a specific column, the routes one and most of all, one of its specific sub-column

"match":"Host('ebzz.acme.com')"

I tried the command below

kubectl --context oam-ska-yo_jonnylee -n ebzz get -o=custom-columns=svc:.spec.routes.-kind ingressroute zz-oamtoto

Which gives me this:

 [map[kind:Rule match:Host(`ebzz.acme.com`) middlewares:[map[name:ebzz-ebzz-basicauth]] services:[map[kind:Service name:zz-oamtoto port:1234]]]]

What i've tried so far

kubectl --context oam-ska-yo_jonnylee -n ebzz get -o=custom-columns=svc:.spec.routes[kind] ingressroute zz-oamtoto

It gives me this

svc
error: invalid array index kind

I've tried this as well

kubectl --context oam-ska-yo_jonnylee -n ebzz get -o=custom-columns=svc:.spec.routes["kind"] ingressroute zz-oamtoto

But it gives me this

svc
error: invalid array index kind

Upvotes: 0

Views: 1304

Answers (2)

Andy K
Andy K

Reputation: 5044

After many trials, this one works

kubectl --context oam-ska-yo_jonnylee -n ebz get -o=custom-columns=.spec.routes[0].match ingressroute zz-oamtoto
  • The option custom-columns should give you the tuning if you are looking for a column.

  • For the first column, you are aiming to, you need to put a .spec followed by a dot and the column name, like this .spec.column_name

  • Once there, the routes column part, in this example, is an array. Therefore you need to put the [], followed by the number and then followed by the subname, here match. it gives you .spec.routes[0].match

Note: I use a svc in my initial kubectly query. It was a mistake, there is no use for that.

Upvotes: 1

Vit
Vit

Reputation: 8411

If I understood you correctly, you need

kubectl --context oam-ska-yo_jonnylee -n ebz get ingressroute zz-oamtoto --output=jsonpath={.spec.routes.match}

Here you can find more examples: kubectl Cheat Sheet

Upvotes: 1

Related Questions