Skip to content

Commit 766687c

Browse files
author
indusridhar
committed
Updates to rollback cli to have no args for versions
1 parent 19a6f6d commit 766687c

File tree

3 files changed

+57
-200
lines changed

3 files changed

+57
-200
lines changed

src/aks-preview/azext_aks_preview/_help.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2466,17 +2466,12 @@
24662466

24672467
helps['aks nodepool rollback'] = """
24682468
type: command
2469-
short-summary: Rollback an agent pool to a previously used configuration.
2469+
short-summary: Rollback an agent pool to the most recently used configuration (N-1).
24702470
long-summary: |
2471-
Rollback an agent pool to a previously used Kubernetes version or node image version.
2472-
At least one of --kubernetes-version or --node-image-version must be specified.
2471+
Rollback an agent pool to the most recently used version based on rollback history.
2472+
This will rollback both the Kubernetes version and node image version to their most recent previous state.
2473+
For downgrades to older versions (N-2 or earlier), use a separate downgrade operation.
24732474
parameters:
2474-
- name: --kubernetes-version -k
2475-
type: string
2476-
short-summary: Target Kubernetes version for rollback.
2477-
- name: --node-image-version
2478-
type: string
2479-
short-summary: Target node image version for rollback.
24802475
- name: --aks-custom-headers
24812476
type: string
24822477
short-summary: Send custom headers. When specified, format should be Key1=Value1,Key2=Value2.
@@ -2487,14 +2482,9 @@
24872482
type: string
24882483
short-summary: Set to '*' to allow a new resource to be created, but to prevent updating an existing resource.
24892484
examples:
2490-
- name: Rollback a nodepool to a specific Kubernetes version.
2491-
text: az aks nodepool rollback --resource-group MyResourceGroup --cluster-name MyManagedCluster --nodepool-name MyNodePool --kubernetes-version 1.28.5
2492-
crafted: true
2493-
- name: Rollback a nodepool to specific Kubernetes and node image versions.
2494-
text: az aks nodepool rollback --resource-group MyResourceGroup --cluster-name MyManagedCluster --nodepool-name MyNodePool --kubernetes-version 1.28.5 --node-image-version AKSUbuntu-1804gen2containerd-2024.01.15
2485+
- name: Rollback a nodepool to the most recently used version.
2486+
text: az aks nodepool rollback --resource-group MyResourceGroup --cluster-name MyManagedCluster --nodepool-name MyNodePool
24952487
crafted: true
2496-
- name: Rollback a nodepool to a specific node image version.
2497-
text: az aks nodepool rollback --resource-group MyResourceGroup --cluster-name MyManagedCluster --nodepool-name MyNodePool --node-image-version AKSUbuntu-1804gen2containerd-2024.01.15
24982488
crafted: true
24992489
"""
25002490

src/aks-preview/azext_aks_preview/custom.py

Lines changed: 40 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import threading
1515
import time
1616
import webbrowser
17-
import subprocess
1817

1918
from azext_aks_preview._client_factory import (
2019
CUSTOM_MGMT_AKS_PREVIEW,
@@ -73,8 +72,6 @@
7372
get_all_extensions_in_allow_list,
7473
raise_validation_error_if_extension_type_not_in_allow_list,
7574
get_extension_in_allow_list,
76-
uses_kubelogin_devicecode,
77-
which,
7875
)
7976
from azext_aks_preview._podidentity import (
8077
_ensure_managed_identity_operator_permission,
@@ -120,7 +117,6 @@
120117
)
121118
from azext_aks_preview.machine import (
122119
add_machine,
123-
update_machine,
124120
)
125121
from azext_aks_preview.jwtauthenticator import (
126122
aks_jwtauthenticator_add_internal,
@@ -1092,7 +1088,6 @@ def aks_create(
10921088
acns_advanced_networkpolicies=None,
10931089
acns_transit_encryption_type=None,
10941090
enable_retina_flow_logs=None,
1095-
enable_container_network_logs=None,
10961091
acns_datapath_acceleration_mode=None,
10971092
# nodepool
10981093
crg_id=None,
@@ -1156,9 +1151,6 @@ def aks_create(
11561151
# managed system pool
11571152
enable_managed_system_pool=False,
11581153
enable_upstream_kubescheduler_user_configuration=False,
1159-
# managed gateway installation
1160-
enable_gateway_api=False,
1161-
enable_hosted_system=False
11621154
):
11631155
# DO NOT MOVE: get all the original parameters and save them as a dictionary
11641156
raw_parameters = locals()
@@ -1362,8 +1354,6 @@ def aks_update(
13621354
acns_transit_encryption_type=None,
13631355
enable_retina_flow_logs=None,
13641356
disable_retina_flow_logs=None,
1365-
enable_container_network_logs=None,
1366-
disable_container_network_logs=None,
13671357
acns_datapath_acceleration_mode=None,
13681358
# metrics profile
13691359
enable_cost_analysis=False,
@@ -1396,9 +1386,6 @@ def aks_update(
13961386
migrate_vmas_to_vms=False,
13971387
enable_upstream_kubescheduler_user_configuration=False,
13981388
disable_upstream_kubescheduler_user_configuration=False,
1399-
# managed gateway installation
1400-
enable_gateway_api=False,
1401-
disable_gateway_api=False,
14021389
):
14031390
# DO NOT MOVE: get all the original parameters and save them as a dictionary
14041391
raw_parameters = locals()
@@ -1533,29 +1520,6 @@ def aks_get_credentials(
15331520
encoding='UTF-8')
15341521
print_or_merge_credentials(
15351522
path, kubeconfig, overwrite_existing, context_name)
1536-
# Check if kubeconfig requires kubelogin with devicecode and convert it
1537-
if uses_kubelogin_devicecode(kubeconfig):
1538-
if which("kubelogin"):
1539-
try:
1540-
# Run kubelogin convert-kubeconfig -l azurecli
1541-
subprocess.run(
1542-
["kubelogin", "convert-kubeconfig", "-l", "azurecli"],
1543-
cwd=os.path.dirname(path),
1544-
check=True,
1545-
)
1546-
logger.warning("Converted kubeconfig to use Azure CLI authentication.")
1547-
except subprocess.CalledProcessError as e:
1548-
logger.warning("Failed to convert kubeconfig with kubelogin: %s", str(e))
1549-
except Exception as e: # pylint: disable=broad-except
1550-
logger.warning("Error running kubelogin: %s", str(e))
1551-
else:
1552-
logger.warning(
1553-
"The kubeconfig uses devicecode authentication which requires kubelogin. "
1554-
"Please install kubelogin from https://github.com/Azure/kubelogin or run "
1555-
"'az aks install-cli' to install both kubectl and kubelogin. "
1556-
"If devicecode login fails, try running "
1557-
"'kubelogin convert-kubeconfig -l azurecli' to unblock yourself."
1558-
)
15591523
except (IndexError, ValueError) as exc:
15601524
raise CLIError("Fail to find kubeconfig file.") from exc
15611525

@@ -1578,8 +1542,8 @@ def aks_scale(cmd, # pylint: disable=unused-argument
15781542
"Please specify nodepool name or use az aks nodepool command to scale node pool"
15791543
)
15801544

1581-
for agent_profile in (instance.agent_pool_profiles or []):
1582-
if agent_profile.name == nodepool_name or (nodepool_name == "" and instance.agent_pool_profiles and len(instance.agent_pool_profiles) == 1):
1545+
for agent_profile in instance.agent_pool_profiles:
1546+
if agent_profile.name == nodepool_name or (nodepool_name == "" and len(instance.agent_pool_profiles) == 1):
15831547
if agent_profile.enable_auto_scaling:
15841548
raise CLIError(
15851549
"Cannot scale cluster autoscaler enabled node pool.")
@@ -1629,7 +1593,7 @@ def aks_upgrade(cmd,
16291593
_fill_defaults_for_pod_identity_profile(instance.pod_identity_profile)
16301594

16311595
vmas_cluster = False
1632-
for agent_profile in (instance.agent_pool_profiles or []):
1596+
for agent_profile in instance.agent_pool_profiles:
16331597
if agent_profile.type.lower() == "availabilityset":
16341598
vmas_cluster = True
16351599
break
@@ -1646,7 +1610,7 @@ def aks_upgrade(cmd,
16461610

16471611
# This only provide convenience for customer at client side so they can run az aks upgrade to upgrade all
16481612
# nodepools of a cluster. The SDK only support upgrade single nodepool at a time.
1649-
for agent_pool_profile in (instance.agent_pool_profiles or []):
1613+
for agent_pool_profile in instance.agent_pool_profiles:
16501614
if vmas_cluster:
16511615
raise CLIError('This cluster is not using VirtualMachineScaleSets. Node image upgrade only operation '
16521616
'can only be applied on VirtualMachineScaleSets and VirtualMachines(Preview) cluster.')
@@ -1715,7 +1679,7 @@ def aks_upgrade(cmd,
17151679
return None
17161680

17171681
if upgrade_all:
1718-
for agent_profile in (instance.agent_pool_profiles or []):
1682+
for agent_profile in instance.agent_pool_profiles:
17191683
agent_profile.orchestrator_version = kubernetes_version
17201684
agent_profile.creation_data = None
17211685

@@ -2230,29 +2194,46 @@ def aks_agentpool_rollback(cmd, # pylint: disable=unused-argument
22302194
resource_group_name,
22312195
cluster_name,
22322196
nodepool_name,
2233-
kubernetes_version=None,
2234-
node_image_version=None,
22352197
aks_custom_headers=None,
22362198
if_match=None,
22372199
if_none_match=None,
22382200
no_wait=False):
2239-
"""Rollback a nodepool to a previously used configuration."""
2201+
"""Rollback a nodepool to the most recent previous version configuration."""
22402202

2241-
# Require at least one version to be specified
2242-
if not kubernetes_version and not node_image_version:
2243-
raise RequiredArgumentMissingError(
2244-
"Please specify at least one of --kubernetes-version or --node-image-version. "
2245-
"Use 'az aks nodepool get-rollback-versions' to see available rollback versions."
2203+
logger.info("Fetching the most recent rollback version...")
2204+
2205+
# Get upgrade profile to retrieve recently used versions
2206+
upgrade_profile = client.get_upgrade_profile(resource_group_name, cluster_name, nodepool_name)
2207+
2208+
if not upgrade_profile.recently_used_versions or len(upgrade_profile.recently_used_versions) == 0:
2209+
raise CLIError(
2210+
"No rollback versions available. The nodepool must have been upgraded at least once "
2211+
"to have rollback history available."
22462212
)
2213+
2214+
# Sort by timestamp (most recent first) and get the most recent version
2215+
sorted_versions = sorted(
2216+
upgrade_profile.recently_used_versions,
2217+
key=lambda v: v.timestamp if v.timestamp else datetime.datetime.min,
2218+
reverse=True
2219+
)
2220+
most_recent = sorted_versions[0]
2221+
2222+
kubernetes_version = most_recent.orchestrator_version
2223+
node_image_version = most_recent.node_image_version
2224+
2225+
logger.info(
2226+
"Rolling back to the most recent version: "
2227+
"Kubernetes version: %s, Node image version: %s (timestamp: %s)",
2228+
kubernetes_version, node_image_version, most_recent.timestamp
2229+
)
22472230

22482231
# Get the current agent pool
22492232
current_agentpool = client.get(resource_group_name, cluster_name, nodepool_name)
22502233

22512234
# Update the agent pool configuration with rollback versions
2252-
if kubernetes_version:
2253-
current_agentpool.orchestrator_version = kubernetes_version
2254-
if node_image_version:
2255-
current_agentpool.node_image_version = node_image_version
2235+
current_agentpool.orchestrator_version = kubernetes_version
2236+
current_agentpool.node_image_version = node_image_version
22562237

22572238
# Set custom headers if provided
22582239
headers = get_aks_custom_headers(aks_custom_headers)
@@ -2671,33 +2652,6 @@ def aks_machine_add(
26712652
return add_machine(cmd, client, raw_parameters, no_wait)
26722653

26732654

2674-
# pylint: disable=unused-argument
2675-
def aks_machine_update(
2676-
cmd,
2677-
client,
2678-
resource_group_name,
2679-
cluster_name,
2680-
nodepool_name,
2681-
machine_name=None,
2682-
tags=None,
2683-
node_taints=None,
2684-
labels=None,
2685-
no_wait=False,
2686-
):
2687-
existedMachine = None
2688-
try:
2689-
existedMachine = client.get(resource_group_name, cluster_name, nodepool_name, machine_name)
2690-
except ResourceNotFoundError:
2691-
raise ClientRequestError(
2692-
f"Machine '{machine_name}' does not exist. Please use 'az aks machine list' to get current list of machines."
2693-
)
2694-
2695-
if existedMachine:
2696-
# DO NOT MOVE: get all the original parameters and save them as a dictionary
2697-
raw_parameters = locals()
2698-
return update_machine(client, raw_parameters, existedMachine, no_wait)
2699-
2700-
27012655
def aks_addon_list_available():
27022656
available_addons = []
27032657
for k, v in ADDONS.items():
@@ -3103,13 +3057,12 @@ def aks_enable_addons(
31033057
if enable_virtual_node:
31043058
# All agent pool will reside in the same vnet, we will grant vnet level Contributor role
31053059
# in later function, so using a random agent pool here is OK
3106-
if result.agent_pool_profiles and len(result.agent_pool_profiles) > 0:
3107-
random_agent_pool = result.agent_pool_profiles[0]
3108-
if random_agent_pool.vnet_subnet_id != "":
3109-
add_virtual_node_role_assignment(
3110-
cmd, result, random_agent_pool.vnet_subnet_id)
3111-
# Else, the cluster is not using custom VNet, the permission is already granted in AKS RP,
3112-
# we don't need to handle it in client side in this case.
3060+
random_agent_pool = result.agent_pool_profiles[0]
3061+
if random_agent_pool.vnet_subnet_id != "":
3062+
add_virtual_node_role_assignment(
3063+
cmd, result, random_agent_pool.vnet_subnet_id)
3064+
# Else, the cluster is not using custom VNet, the permission is already granted in AKS RP,
3065+
# we don't need to handle it in client side in this case.
31133066

31143067
else:
31153068
result = sdk_no_wait(no_wait, client.begin_create_or_update,
@@ -4063,38 +4016,6 @@ def aks_mesh_upgrade_rollback(
40634016
mesh_upgrade_command=CONST_AZURE_SERVICE_MESH_UPGRADE_COMMAND_ROLLBACK)
40644017

40654018

4066-
def aks_mesh_enable_istio_cni(
4067-
cmd,
4068-
client,
4069-
resource_group_name,
4070-
name,
4071-
):
4072-
"""Enable Istio CNI chaining for the Azure Service Mesh proxy redirection mechanism."""
4073-
return _aks_mesh_update(
4074-
cmd,
4075-
client,
4076-
resource_group_name,
4077-
name,
4078-
enable_istio_cni=True,
4079-
)
4080-
4081-
4082-
def aks_mesh_disable_istio_cni(
4083-
cmd,
4084-
client,
4085-
resource_group_name,
4086-
name,
4087-
):
4088-
"""Disable Istio CNI chaining for the Azure Service Mesh proxy redirection mechanism."""
4089-
return _aks_mesh_update(
4090-
cmd,
4091-
client,
4092-
resource_group_name,
4093-
name,
4094-
disable_istio_cni=True,
4095-
)
4096-
4097-
40984019
def _aks_mesh_get_supported_revisions(
40994020
cmd,
41004021
client,
@@ -4129,8 +4050,6 @@ def _aks_mesh_update(
41294050
revision=None,
41304051
yes=False,
41314052
mesh_upgrade_command=None,
4132-
enable_istio_cni=None,
4133-
disable_istio_cni=None,
41344053
):
41354054
raw_parameters = locals()
41364055

@@ -4530,9 +4449,6 @@ def aks_check_network_outbound(
45304449
if not cluster:
45314450
raise ValidationError("Can not get cluster information!")
45324451

4533-
if not cluster.agent_pool_profiles or len(cluster.agent_pool_profiles) == 0:
4534-
raise ValidationError("No agent pool profiles found in the cluster!")
4535-
45364452
vm_set_type = cluster.agent_pool_profiles[0].type
45374453
if not vm_set_type:
45384454
raise ValidationError("Can not get VM set type of the cluster!")

0 commit comments

Comments
 (0)