which API should i use so as to access the V2beta2PodsMetricStatus Model of kubernetes-client in Python?

9/7/2020

I am developing an application in python where i need to consume the current values of a metric for some pods(eg. cpu, memory). I can get this info through an API (/apis/metrics.k8s.io/v1beta1/pods) but i try to use the kubernetes client so as to access these metrics within my python app.

I see that the V2beta2PodsMetricStatus Model includes the information i need but i can not find the API endpoint i should use so as to reach this model.

Any help or alternative option would be very helpful since i am really stacked with this issue several days.

Thank you very much for you time.

-- efotopoulou
kubernetes
metrics
python

2 Answers

9/7/2020

You could use the call_api method of api_client as below to call an API which is not part of core kubernetes API.

ret_metrics = api_client.call_api('/apis/metrics.k8s.io/v1beta1/pods', 'GET', auth_settings = ['BearerToken'], response_type='json', _preload_content=False) 
response = ret_metrics[0].data.decode('utf-8')

There is an open issue to support it via V2beta2PodsMetricStatus model

-- Arghya Sadhu
Source: StackOverflow

9/7/2020

I finally get the metrics by executing directly the relevant kubectl command. I do not like the solution but it works. Hope to be able to use the kubernetes-client instead at the near future.

import subprocess
p = subprocess.getoutput('kubectl get --raw /apis/metrics.k8s.io/v1beta1/namespaces/default/pods')
ret_metrics = json.loads(p)
items = ret_metrics['items']
 for item in items:
   print(item['metadata']['name'])
-- efotopoulou
Source: StackOverflow