skip to Main Content

I need to use Azure Python SDK and Kubernetes Python Client to list the Pods CPU limits for a cluster running in AKS.

Although its straight forward using CLI/PowerShell but I need to use Python exclusively.
Must not use subprocess calls.

Here is snippet that gets KubeConfig object after authentication with Azure:

from azure.identity import DefaultAzureCredential
from azure.mgmt.containerservice import ContainerServiceClient

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

kubeconfig = container_service_client.managed_clusters. 
list_cluster_user_credentials(resource_group_name, cluster_name). 
kubeconfigs[0]

But I am unsure how to put this to be used by K8s Python client:

from kubernetes import client, config
config.load_kube_config() ## How to pass? 

v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
    print("%st%st%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))

3

Answers


  1. from kubernetes import client, config
    config.load_kube_config() ## How to pass? 
    
    v1 = client.CoreV1Api()
    print("Listing pods with their CPU limits:")
    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.resources.limits['cpu'])
    
    Login or Signup to reply.
  2. You can use the config.load_kube_config method and pass in the kubeconfig object you obtained earlier as a parameter. The method accepts a config_file parameter, which can be a file object, a file-like object, or a string file path.

    Since kubeconfig is a string, you can pass it as a string file path, like so:

    from kubernetes import client, config
    
    # Pass the kubeconfig string as a file path
    config.load_kube_config(config_file=kubeconfig)
    
    v1 = client.CoreV1Api()
    print("Listing pods with their IPs:")
    ret = v1.list_pod_for_all_namespaces(watch=False)
    for i in ret.items:
        print("%st%st%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
    
    Login or Signup to reply.
  3. I tried a repro and was able to achieve as below –

    1. Getting kubeconfig in correct decoded format from kubeconfigs[0] which is CredentialResults.

    2. Writing the generated kubeconfig in a file.

    3. Loading the file in config module.

      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 = "XXX"
      resource_group_name= 'MY-SUB'
      cluster_name = "my-aks-clustername" 
      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("kubeconfig","w")
      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("Listing containers with their CPU limits:")
      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'])
      
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search