Search code examples
pythonazurekubernetesazure-aksazure-sdk-python

Azure-sdk-for-python AKS: how to add a new nodepool using


I can't understand from the documentation of azure for python SDK how can you create a new node-pool in an existing Kubernetes cluster? it is easy to do it in the command line:

az aks nodepool add --cluster-name CLUSTER-NAME
    --resource-group RESOURCE-GROUP
    --name NODE-NAME
    --enable-cluster-autoscaler
    --kubernetes-version 1.15.7
    --min-count 1
    --max-count 4
    --node-count 1
    --node-vm-size Standard_NC6s_v2

How can I implement the exact same command using the python SDK? currently, I am able to connect to the client like that :

# pip install azure
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.containerservice import ContainerServiceClient
credentials = ServicePrincipalCredentials(CLIENT,KEY,tenant = TENANT_ID)
client = ContainerServiceClient(credentials, subscription_id)
credentialResults = client.managed_clusters.list_cluster_user_credentials(resource_group, aks_service_name)

# How can I continue from here to create or delete a new nodepool?

Gow can I continue from here to create or delete a new nodepool?


Solution

  • You can make use of the following code to create nodepool,

    def aks_scale(cmd, client, resource_group_name, name, node_count, nodepool_name="", no_wait=False):
        instance = client.get(resource_group_name, name)
        # TODO: change this approach when we support multiple agent pools.
        for agent_profile in instance.agent_pool_profiles:
            if agent_profile.name == nodepool_name or (nodepool_name == "" and len(instance.agent_pool_profiles) == 1):
                agent_profile.count = int(node_count)  # pylint: disable=no-member
                # null out the SP and AAD profile because otherwise validation complains
                instance.service_principal_profile = None
                instance.aad_profile = None
                return sdk_no_wait(no_wait, client.create_or_update, resource_group_name, name, instance)
        raise CLIError('The nodepool "{}" was not found.'.format(nodepool_name))
    

    To Add NodePool

    def aks_agentpool_add(cmd, client, resource_group_name, cluster_name, nodepool_name,
                          kubernetes_version=None,
                          zones=None,
                          enable_node_public_ip=False,
                          node_vm_size=None,
                          node_osdisk_size=0,
                          node_count=3,
                          vnet_subnet_id=None,
                          max_pods=0,
                          os_type="Linux",
                          min_count=None,
                          max_count=None,
                          enable_cluster_autoscaler=False,
                          node_taints=None,
                          tags=None,
                          labels=None,
                          mode="User",
                          no_wait=False):
        instances = client.list(resource_group_name, cluster_name)
        for agentpool_profile in instances:
            if agentpool_profile.name == nodepool_name:
                raise CLIError("Node pool {} already exists, please try a different name, "
                               "use 'aks nodepool list' to get current list of node pool".format(nodepool_name))
    
        taints_array = []
    
        if node_taints is not None:
            for taint in node_taints.split(','):
                try:
                    taint = taint.strip()
                    taints_array.append(taint)
                except ValueError:
                    raise CLIError('Taint does not match allowed values. Expect value such as "special=true:NoSchedule".')
    
        if node_vm_size is None:
            if os_type.lower() == "windows":
                node_vm_size = "Standard_D2s_v3"
            else:
                node_vm_size = "Standard_DS2_v2"
    
        agent_pool = AgentPool(
            name=nodepool_name,
            tags=tags,
            node_labels=labels,
            count=int(node_count),
            vm_size=node_vm_size,
            os_type=os_type,
            storage_profile=ContainerServiceStorageProfileTypes.managed_disks,
            vnet_subnet_id=vnet_subnet_id,
            agent_pool_type="VirtualMachineScaleSets",
            max_pods=int(max_pods) if max_pods else None,
            orchestrator_version=kubernetes_version,
            availability_zones=zones,
            enable_node_public_ip=enable_node_public_ip,
            node_taints=taints_array,
            mode=mode
        )
    
        _check_cluster_autoscaler_flag(enable_cluster_autoscaler, min_count, max_count, node_count, agent_pool)
    
        if node_osdisk_size:
            agent_pool.os_disk_size_gb = int(node_osdisk_size)
    
        return sdk_no_wait(no_wait, client.create_or_update, resource_group_name, cluster_name, nodepool_name, agent_pool)
    

    Github reference