<p>I need to use <a href="https://github.com/Azure/azure-sdk-for-python" rel="nofollow noreferrer">Azure Python SDK</a> and <a href="https://github.com/kubernetes-client/python" rel="nofollow noreferrer">Kubernetes Python Client</a> to list the Pods CPU limits for a cluster running in AKS.</p>
<p>Although its straight forward using CLI/PowerShell but I need to use Python exclusively.
Must not use <a href="https://stackoverflow.com/questions/53535855/how-to-get-kubectl-configuration-from-azure-aks-with-python">subprocess calls</a>.</p>
<p>Here is snippet that gets <code>KubeConfig</code> object after authentication with Azure:</p>
<pre class="lang-py prettyprint-override"><code>from azure.identity import DefaultAzureCredential
from azure.mgmt.containerservice import ContainerServiceClient

credential = DefaultAzureCredential(exclude_cli_credential=True)
subscription_id = &quot;XXX&quot;
resource_group_name= 'MY-SUB'
cluster_name = &quot;my-aks-clustername&quot; 
container_service_client = ContainerServiceClient(credential, subscription_id)

kubeconfig = container_service_client.managed_clusters. \
list_cluster_user_credentials(resource_group_name, cluster_name). \
kubeconfigs[0]
</code></pre>
<p>But I am unsure how to put this to be used by K8s Python client:</p>
<pre class="lang-py prettyprint-override"><code>from kubernetes import client, config
config.load_kube_config() ## How to pass? 

v1 = client.CoreV1Api()
print(&quot;Listing pods with their IPs:&quot;)
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
    print(&quot;%s\t%s\t%s&quot; % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
</code></pre>

Rajesh Swarnkar
<p>I tried a repro and was able to achieve as below -</p>
<ol>
<li><p>Getting kubeconfig in correct decoded format from kubeconfigs[0] which is <a href="https://learn.microsoft.com/en-us/python/api/azure-mgmt-hybridkubernetes/azure.mgmt.hybridkubernetes.models.credentialresults?view=azure-python" rel="nofollow noreferrer">CredentialResults</a>.</p>
</li>
<li><p>Writing the generated kubeconfig in a file.</p>
</li>
<li><p>Loading the file in config module.</p>
<pre><code>from azure.identity import DefaultAzureCredential
from azure.mgmt.containerservice import ContainerServiceClient
import os
from kubernetes import client, config

credential = DefaultAzureCredential(exclude_cli_credential=True)
subscription_id = &quot;XXX&quot;
resource_group_name= 'MY-SUB'
cluster_name = &quot;my-aks-clustername&quot; 
container_service_client = ContainerServiceClient(credential, subscription_id)

# getting kubeconfig in a decoded format from CredentialResult

kubeconfig = container_service_client.managed_clusters. \
list_cluster_user_credentials(resource_group_name, cluster_name). \
kubeconfigs[0].value.decode(encoding='UTF-8')

# writing generated kubeconfig in a file
f=open(&quot;kubeconfig&quot;,&quot;w&quot;)
f.write(kubeconfig)
f.close()

# loading the config file
config.load_kube_config('kubeconfig')

# deleting the kubeconfig file
os.remove('kubeconfig')

v1 = client.CoreV1Api()
print(&quot;Listing containers with their CPU limits:&quot;)
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
  for container in i.spec.containers:
    if container.resources.limits:
      if 'cpu' in container.resources.limits:
         print( container.name, container.resources.limits['cpu'])
</code></pre>
</li>
</ol>
