@@ -2222,6 +2222,114 @@ def aks_agentpool_get_upgrade_profile(cmd, # pylint: disable=unused-argument
22222222 return client .get_upgrade_profile (resource_group_name , cluster_name , nodepool_name )
22232223
22242224
2225+ def aks_agentpool_get_rollback_versions (cmd , # pylint: disable=unused-argument
2226+ client ,
2227+ resource_group_name ,
2228+ cluster_name ,
2229+ nodepool_name ):
2230+ """Get rollback versions for a nodepool."""
2231+ upgrade_profile = client .get_upgrade_profile (resource_group_name , cluster_name , nodepool_name )
2232+ return upgrade_profile .recently_used_versions
2233+
2234+
2235+ def aks_agentpool_rollback (cmd , # pylint: disable=unused-argument
2236+ client ,
2237+ resource_group_name ,
2238+ cluster_name ,
2239+ nodepool_name ,
2240+ aks_custom_headers = None ,
2241+ if_match = None ,
2242+ if_none_match = None ,
2243+ no_wait = False ):
2244+ """Rollback a nodepool to the most recent previous version configuration."""
2245+ from azext_aks_preview ._client_factory import cf_managed_clusters
2246+
2247+ # Warn users when auto-upgrade is enabled
2248+ if cmd and getattr (cmd , "cli_ctx" , None ):
2249+ try :
2250+ managed_clusters_client = cf_managed_clusters (cmd .cli_ctx )
2251+ managed_cluster = managed_clusters_client .get (resource_group_name , cluster_name )
2252+ auto_upgrade_profile = getattr (managed_cluster , "auto_upgrade_profile" , None )
2253+
2254+ upgrade_channel = getattr (auto_upgrade_profile , "upgrade_channel" , None ) if auto_upgrade_profile else None
2255+ node_os_upgrade_channel = (
2256+ getattr (auto_upgrade_profile , "node_os_upgrade_channel" , None )
2257+ if auto_upgrade_profile
2258+ else None
2259+ )
2260+
2261+ upgrade_channel_enabled = upgrade_channel and str (upgrade_channel ).lower () != "none"
2262+ node_os_channel_enabled = node_os_upgrade_channel and str (node_os_upgrade_channel ).lower () not in [
2263+ "none" ,
2264+ "unmanaged" ,
2265+ ]
2266+
2267+ if upgrade_channel_enabled or node_os_channel_enabled :
2268+ logger .warning (
2269+ "Auto-upgrade is enabled on cluster '%s' (upgradeChannel=%s, nodeOSUpgradeChannel=%s). "
2270+ "Rollback will not succeed until auto-upgrade is disabled. Please disable auto-upgrade to roll back the node pool." ,
2271+ cluster_name ,
2272+ upgrade_channel or "none" ,
2273+ node_os_upgrade_channel or "Unmanaged" ,
2274+ )
2275+ except Exception as ex : # pylint: disable=broad-except
2276+ logger .debug ("Unable to retrieve auto-upgrade configuration before rollback: %s" , ex )
2277+
2278+ logger .info ("Fetching the most recent rollback version..." )
2279+
2280+ # Get upgrade profile to retrieve recently used versions
2281+ upgrade_profile = client .get_upgrade_profile (resource_group_name , cluster_name , nodepool_name )
2282+
2283+ if not upgrade_profile .recently_used_versions or len (upgrade_profile .recently_used_versions ) == 0 :
2284+ raise CLIError (
2285+ "No rollback versions available. The nodepool must have been upgraded at least once "
2286+ "to have rollback history available."
2287+ )
2288+
2289+ # Sort by timestamp (most recent first) and get the most recent version
2290+ sorted_versions = sorted (
2291+ upgrade_profile .recently_used_versions ,
2292+ key = lambda v : v .timestamp if v .timestamp else datetime .datetime .min ,
2293+ reverse = True
2294+ )
2295+ most_recent = sorted_versions [0 ]
2296+
2297+ kubernetes_version = most_recent .orchestrator_version
2298+ node_image_version = most_recent .node_image_version
2299+
2300+ logger .info (
2301+ "Rolling back to the most recent version: "
2302+ "Kubernetes version: %s, Node image version: %s (timestamp: %s)" ,
2303+ kubernetes_version , node_image_version , most_recent .timestamp
2304+ )
2305+
2306+ # Get the current agent pool
2307+ current_agentpool = client .get (resource_group_name , cluster_name , nodepool_name )
2308+
2309+ # Update the agent pool configuration with rollback versions
2310+ current_agentpool .orchestrator_version = kubernetes_version
2311+ current_agentpool .node_image_version = node_image_version
2312+
2313+ # Set custom headers if provided
2314+ headers = get_aks_custom_headers (aks_custom_headers )
2315+ if if_match :
2316+ headers ['If-Match' ] = if_match
2317+ if if_none_match :
2318+ headers ['If-None-Match' ] = if_none_match
2319+
2320+ # Perform the rollback by updating the agent pool
2321+ # Server-side will validate the versions
2322+ return sdk_no_wait (
2323+ no_wait ,
2324+ client .begin_create_or_update ,
2325+ resource_group_name ,
2326+ cluster_name ,
2327+ nodepool_name ,
2328+ current_agentpool ,
2329+ headers = headers if headers else None
2330+ )
2331+
2332+
22252333def aks_agentpool_stop (cmd , # pylint: disable=unused-argument
22262334 client ,
22272335 resource_group_name ,
0 commit comments