Matrix
Matrix

Reputation: 2639

Azure-sdk-for-python AKS- How to get available latest version for Kubernetes on AKS

I'm trying to get available versions of Kubernetes on AKS via python.sdk and upgrade the AKS with the latest version afterwards.

Here is what I've tried considering this doc https://learn.microsoft.com/en-us/rest/api/aks/managed-clusters/get-upgrade-profile

for resource in list(resource_list):
    if resource.type== "Microsoft.ContainerService/managedClusters":
       print("ResourceName:", resource.name, "ResourceType:", resource.type)
       containerservice_client = ContainerServiceClient(credential, sub.subscription_id)
       get_aks = containerservice_client.managed_clusters.get(group.name, resource.name)
       aks_get_upgrade = containerservice_client.managed_clusters.get_upgrade_profile(group.name, resource.name)
       print("AKSGetUpdate", aks_get_upgrade)

However it doesn't return kubernetesVersion.

I would appreciate if someone can help with this.

Upvotes: 0

Views: 1064

Answers (1)

Ansuman Bal
Ansuman Bal

Reputation: 11421

You can try using the below code , which I used to test in my environment:

from azure.identity import AzureCliCredential
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.containerservice import ContainerServiceClient
from azure.mgmt.containerservice.models import ManagedClusterPoolUpgradeProfileUpgradesItem

credential = AzureCliCredential()
subscription_id = "SUBID"
resource_group= 'RG'
resouce_client=ResourceManagementClient(credential,subscription_id)
container_client=ContainerServiceClient(credential,subscription_id)
resouce_list=resouce_client.resources.list_by_resource_group(resource_group)
for resource in list (resouce_list) :
    if resource.type == 'Microsoft.ContainerService/managedClusters':
       print("ResourceName:", resource.name, "ResourceType:", resource.type)
       get_aks = container_client.managed_clusters.get(resource_group, resource.name)
       aks_get_details = container_client.managed_clusters.get_upgrade_profile(resource_group, resource.name)
       aks_get_upgrade = aks_get_details.control_plane_profile
       upgrades = aks_get_upgrade.upgrades
       print("AKS_current_controlPlane", aks_get_upgrade.kubernetes_version)
       for i in upgrades:
           print("AKSGetUpdate_upgrade_controlPlane", i.kubernetes_version,i.is_preview)

Output:

enter image description here

Upvotes: 1

Related Questions