DEV Community

Cover image for Simple trick to get specific elements from Kubernetes kubectl output
Karolis
Karolis

Posted on

2

Simple trick to get specific elements from Kubernetes kubectl output

Usually, most Kubernetes objects are quite big as they have namespace, name, labels, template, spec and a bunch of other fields. When you need to get only one or several fields, it can get tedious scrolling up and down in your terminal looking for it :) Luckily, kubectl supports formatting your output using JSON path that allows you to specify the format and values yourself.

In the following example I will show how to format a Custom Resource output from the operator that I have recently wrote so it's easy to retrieve data from the status field.

First, let's see what data is available:

$ kubectl get  webhookrelayforwards.forward.webhookrelay.com -o json
{
    "apiVersion": "v1",
    "items": [
        {
            "apiVersion": "forward.webhookrelay.com/v1",
            "kind": "WebhookRelayForward",
            "metadata": {
                "annotations": {
                    "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"forward.webhookrelay.com/v1\",\"kind\":\"WebhookRelayForward\",\"metadata\":{\"annotations\":{},\"name\":\"forward-to-jenkins\",\"namespace\":\"jenkins\"},\"spec\":{\"buckets\":[{\"inputs\":[{\"description\":\"Endpoint for GitHub\",\"name\":\"public-endpoint\",\"responseBody\":\"OK\",\"responseStatusCode\":200}],\"name\":\"jenkins-whr-operator\",\"outputs\":[{\"destination\":\"http://jenkins-operator-http-jenkins:8080/github-webhook/\",\"name\":\"jenkins\"}]}],\"image\":\"webhookrelay/webhookrelayd-ubi8:latest\"}}\n"
                },
                "creationTimestamp": "2020-07-05T12:52:11Z",
                "generation": 2,
                "name": "forward-to-jenkins",
                "namespace": "jenkins",
                "resourceVersion": "270615",
                "selfLink": "/apis/forward.webhookrelay.com/v1/namespaces/jenkins/webhookrelayforwards/forward-to-jenkins",
                "uid": "9d75809c-1795-45e2-a437-bb89b22bdac4"
            },
            "spec": {
                "buckets": [
                    {
                        "inputs": [
                            {
                                "description": "Endpoint for GitHub",
                                "name": "public-endpoint",
                                "responseBody": "OK",
                                "responseStatusCode": 200
                            }
                        ],
                        "name": "jenkins-whr-operator",
                        "outputs": [
                            {
                                "destination": "http://jenkins-operator-http-jenkins:8080/github-webhook/",
                                "name": "jenkins"
                            }
                        ]
                    }
                ],
                "image": "webhookrelay/webhookrelayd-ubi8:latest"
            },
            "status": {
                "agentStatus": "Running",
                "publicEndpoints": [
                    "https://k0yv9ip5sxxp55ncsu936k.hooks.webhookrelay.com"
                ],
                "ready": true,
                "routingStatus": "Configured"
            }
        }
    ],
    "kind": "List",
    "metadata": {
        "resourceVersion": "",
        "selfLink": ""
    }
}

Enter fullscreen mode Exit fullscreen mode

Here, if we want to grab just the value from publicEndpoints, we specify again the resource and additional jsonpath query:

$ kubectl get webhookrelayforwards.forward.webhookrelay.com forward-to-jenkins -o 'jsonpath={.status.publicEndpoints[0]}'
https://k0yv9ip5sxxp55ncsu936k.hooks.webhookrelay.com
Enter fullscreen mode Exit fullscreen mode

Go ahead and try it on your own Kubernetes cluster! :)

You can find more examples in the official docs here: https://kubernetes.io/docs/reference/kubectl/jsonpath/.

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay