Search code examples
kuberneteskubectl

kubectl - get names of pods with specific label


I am trying to get the podname from the pod json using the command which is returning the error

kgp -o jsonpath="{.items[*].metadata[?(@.labels.module=='ddvv-script')].name}"

Error

 is not array or slice and cannot be filtered. Printing more information for debugging the template:
        template was:
                {.items[*].metadata[?(@.labels.module=='ddvv-script')].name}
        object given to jsonpath engine was:

Sample file

{
    "apiVersion": "v1",
    "items": [
        {
            "apiVersion": "v1",
            "kind": "Pod",
            "metadata": {
                "creationTimestamp": "2020-09-18T17:42:50Z",
                "generateName": "ddvv-script-6b784db6bd-",
                "labels": {
                    "app": "my-configs",
                    "lf.module": "ddvv-script",
                    "module": "ddvv-script",
                    "pod-template-hash": "6b784db6bd",
                    "release": "config"
                },
                "name": "ddvv-script-6b784db6bd-rjtgh",

What is wrong with this command


Solution

  • You can use below command. It gets podname of pods which has label module=ddvv-script

    kubectl get pods --selector=module=ddvv-script --output=jsonpath={.items..metadata.name}