prashant
prashant

Reputation: 3328

How to access key in a map returned by kubectl

I want to access limits.memory variable returned by get command in k8s

kubectl get resourcequota default -n 103000-p4-dev -o custom-columns=USED:.status.used
USED
map[limits.memory:0 requests.cpu:0 requests.memory:0]

I tried many ways but couldn't succeed

[root@iaasn00126847 ~]# k get resourcequota default -n 103000-p4-dev -o custom-columns=USED:.status.used.limits.memory

returns nothing

Is there a delimiter to fetch the same

Upvotes: 8

Views: 11554

Answers (4)

Marcello DeSales
Marcello DeSales

Reputation: 22329

For values with /, you don't need to escape them, but just the dots using brackets.

$ kubectl -n istio-system get service http2-service-ingress \
-o jsonpath="{.metadata.annotations['service\.beta\.kubernetes\.io/aws-load-balancer-type']}"

Upvotes: 7

Grigoris Thanasoulas
Grigoris Thanasoulas

Reputation: 76

There is no need to use jsonpath. You can still use the custom-columns output, but you need to put the key in (single or double) quotes, and escape all the dots, like this:

k get resourcequota default -n 103000-p4-dev -o custom-columns=USED:.status.used."limits\.memory"

I am currently using this with kubectl v1.17, to list nodes, as follows:

kubectl get nodes -o custom-columns=NAME:.metadata.name,ZONE:.metadata.labels.'topology\.kubernetes\.io/region'
kubectl get nodes -o custom-columns=NAME:.metadata.name,ZONE:.metadata.labels."topology\.kubernetes\.io/region"

Upvotes: 2

mchawre
mchawre

Reputation: 12268

Try with jsonpath

kubectl get resourcequota default -n 103000-p4-dev  -o jsonpath="{.status.used.limits\.memory}"

This is what I tried

$ kubectl apply -f https://k8s.io/examples/admin/resource/quota-mem-cpu.yaml
resourcequota/mem-cpu-demo created
$ kubectl get resourcequota
NAME           CREATED AT
mem-cpu-demo   2019-10-09T06:38:39Z
$ 
$ kubectl get resourcequota mem-cpu-demo -o json
{
    "apiVersion": "v1",
    "kind": "ResourceQuota",
    "metadata": {
        "annotations": {
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"ResourceQuota\",\"metadata\":{\"annotations\":{},\"name\":\"mem-cpu-demo\",\"namespace\":\"default\"},\"spec\":{\"hard\":{\"limits.cpu\":\"2\",\"limits.memory\":\"2Gi\",\"requests.cpu\":\"1\",\"requests.memory\":\"1Gi\"}}}\n"
        },
        "creationTimestamp": "2019-10-09T06:38:39Z",
        "name": "mem-cpu-demo",
        "namespace": "default",
        "resourceVersion": "975",
        "selfLink": "/api/v1/namespaces/default/resourcequotas/mem-cpu-demo",
        "uid": "0d74d782-b717-4845-a0da-424776c05d45"
    },
    "spec": {
        "hard": {
            "limits.cpu": "2",
            "limits.memory": "2Gi",
            "requests.cpu": "1",
            "requests.memory": "1Gi"
        }
    },
    "status": {
        "hard": {
            "limits.cpu": "2",
            "limits.memory": "2Gi",
            "requests.cpu": "1",
            "requests.memory": "1Gi"
        },
        "used": {
            "limits.cpu": "0",
            "limits.memory": "0",
            "requests.cpu": "0",
            "requests.memory": "0"
        }
    }
}
$
$ kubectl get resourcequota mem-cpu-demo -o jsonpath="{.status.used}"
map[limits.cpu:0 limits.memory:0 requests.cpu:0 requests.memory:0]$
$
$ kubectl get resourcequota mem-cpu-demo -o jsonpath="{.status.used.limits\.memory}"
0
$
$ kubectl get resourcequota mem-cpu-demo -o jsonpath="{.status.hard.limits\.memory}"
2Gi
$

Upvotes: 22

Feiyu Zhou
Feiyu Zhou

Reputation: 4544

Since you key (limits.memory) contains dot, maybe you should try like this:

[root@iaasn00126847 ~]# k get resourcequota default -n 103000-p4-dev -o custom-columns=USED:.status.used.'limits\.memory'

Upvotes: 4

Related Questions