Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 463
prashant Avatar asked Nov 30 '22 21:11

prashant


1 Answers

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
$
like image 98
mchawre Avatar answered Dec 04 '22 01:12

mchawre