Used to retrieve the allocatable resources of a Kubernetes cluster.
Assumes that this is being executed within the K8s cluster.
Tested using python 2.7 and requires the installation of two pip libraries:
pip install pint
pip install kubernetes
It provides the same values as kubectl describe nodes
, for example:
Allocated resources:
(Total limits may be over 100 percent, i.e., overcommitted.)
Resource Requests Limits
-------- -------- ------
cpu 660m (34%) 500m (25%)
memory 622Mi (11%) 938Mi (16%)
ephemeral-storage 0 (0%) 0 (0%)
The percent computation are indeed unit based however pint takes care of remembering the units. So, yes, before using the percent values (i.e. cpu_req_per, cpu_lmt_per, etc) you would need to do a
.to('dimensionless')
, e.g.,stats["cpu_lmt_per"].to('dimensionless')
.However, it seems that pint does this automagically for you:
So the short answer is: you don't have to explicitly convert percent values, pint takes care of this for you.