1

One way to get the resource quota values in kubernetes is to use the following command

>kubectl describe resourcequotas
Name:                   default-quota
Namespace:              my-namespace
Resource                Used     Hard
--------                ----     ----
configmaps              19       100
limits.cpu              13810m   18
limits.memory           25890Mi  36Gi

But issue is this display all the values in text file format. Anyone knows how I can get in json format!

Of course, I can parse the output and get the individual entry and construct the json.

kubectl describe quota  | grep limits.cpu | awk '{print $2}'
13810m

But I am looking for something inbuilt or some quick way of doing it. Thanks for your help.

nagendra547
  • 5,672
  • 3
  • 29
  • 43
  • 1
    Possible duplicate of [How to format the output of kubectl describe to JSON](https://stackoverflow.com/questions/37464518/how-to-format-the-output-of-kubectl-describe-to-json) – Vasili Angapov May 22 '19 at 06:30
  • Yes, I saw this. But this is not what I am looking for. thanks for pointing it. – nagendra547 May 22 '19 at 06:32
  • 1
    You can use 'kubectl get resourcequotas -o json', I assume it is not sufficient for your use case - please explain what you want to achieve and why that does not work for you – Thomas May 22 '19 at 07:22

2 Answers2

5

Thanks for your messages. Let me answer my own question, I have found one.

jq has solved my problem.

To get the Max limit of resources in json format

kubectl get quota -ojson | jq -r .items[].status.hard

To get the Current usage of resources in json format

kubectl get quota -ojson | jq -r .items[].status.used
nagendra547
  • 5,672
  • 3
  • 29
  • 43
1

kubectl itself provides a mechanism to provide jsonpath using -o jsonpath option. One of the main issues that I initially faced was with respect to having dot(.) in key. eg., limits.cpu. This issue can be solved by using the expression "limits\.cpu" (escaping dot)

kubectl get resourcequota -o jsonpath={.items[*].spec.hard."limits\.cpu"}
dinup24
  • 1,652
  • 3
  • 16
  • 26