How to obtain the specified part of the content from the output content in k8s?

11/17/2021

When using kubectl get -o yaml/json to obtain resource information, the output content is too detailed, how to obtain the specified part of the content?

[root@ops-harbor ~]# kubectl get -n monitoring prometheus-prome-prometheus-operator-prometheus-0 -ojson

{
    "apiVersion": "v1",
    "kind": "Pod",
    "metadata": {
        "creationTimestamp": "2021-11-02T08:58:33Z",
        "generateName": "prometheus-prome-prometheus-operator-prometheus-",
        "labels": {
            "app": "prometheus",
            "controller-revision-hash": "prometheus-prome-prometheus-operator-prometheus-c56894959",
            "prometheus": "prome-prometheus-operator-prometheus",
            "statefulset.kubernetes.io/pod-name": "prometheus-prome-prometheus-operator-prometheus-0"
        },
        "name": "prometheus-prome-prometheus-operator-prometheus-0",
        "namespace": "monitoring",
        "ownerReferences": [
            {
                "apiVersion": "apps/v1",
                "blockOwnerDeletion": true,
                "controller": true,
                "kind": "StatefulSet",
                "name": "prometheus-prome-prometheus-operator-prometheus",
                "uid": "3c02e78b-610c-4e9c-9171-cc47b00274a3"
            }
        ],
        "resourceVersion": "2640925",
        "selfLink": "/api/v1/namespaces/monitoring/pods/prometheus-prome-prometheus-operator-prometheus-0",
        "uid": "e728914c-2a3c-4d6a-8a18-5ebec0e0cebd"
    },

# ...long long content

For example, I only want to get the following 2 sections of information.

    "apiVersion": "v1",
    "kind": "Pod",

        "ownerReferences": [
            {
                "apiVersion": "apps/v1",
                "blockOwnerDeletion": true,
                "controller": true,
                "kind": "StatefulSet",
                "name": "prometheus-prome-prometheus-operator-prometheus",
                "uid": "3c02e78b-610c-4e9c-9171-cc47b00274a3"
            }
-- Lee Ning
kubectl
kubernetes

1 Answer

11/17/2021
kubectl get pod -n monitoring prometheus-prome-prometheus-operator-prometheus-0 -o json | jq .metadata.ownerReferences

or

kubectl get pod -n monitoring prometheus-prome-prometheus-operator-prometheus-0 -o jsonpath={.metadata.ownerReferences} | jq
-- gohm'c
Source: StackOverflow