Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/aks-preview/azext_aks_preview/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@
validate_nat_gateway_idle_timeout,
validate_nat_gateway_managed_outbound_ip_count,
)

# Import backup strategy constants from dataprotection extension
from azure.cli.core.extension.operations import add_extension_to_path
add_extension_to_path("dataprotection")
from azext_dataprotection.manual._consts import (
CONST_AKS_BACKUP_STRATEGIES,
CONST_BACKUP_STRATEGY_WEEK,
CONST_BACKUP_STRATEGY_MONTH,
CONST_BACKUP_STRATEGY_IMMUTABLE,
CONST_BACKUP_STRATEGY_DISASTER_RECOVERY,
CONST_BACKUP_STRATEGY_CUSTOM,
)

backup_presets = CONST_AKS_BACKUP_STRATEGIES

Comment on lines +28 to +40
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dataprotection extension is imported at module load time (lines 28-37), which means the extension must be installed even when users are not using the backup functionality. This will cause import errors for users who don't have the dataprotection extension installed. Consider wrapping these imports in a try-except block similar to how it's done in managed_cluster_decorator.py (lines 7387-7395), or move the imports into a function that's only called when backup functionality is needed. See _helpers.py:414-427 for the pattern used with k8s-extension.

Suggested change
from azure.cli.core.extension.operations import add_extension_to_path
add_extension_to_path("dataprotection")
from azext_dataprotection.manual._consts import (
CONST_AKS_BACKUP_STRATEGIES,
CONST_BACKUP_STRATEGY_WEEK,
CONST_BACKUP_STRATEGY_MONTH,
CONST_BACKUP_STRATEGY_IMMUTABLE,
CONST_BACKUP_STRATEGY_DISASTER_RECOVERY,
CONST_BACKUP_STRATEGY_CUSTOM,
)
backup_presets = CONST_AKS_BACKUP_STRATEGIES
try:
from azure.cli.core.extension.operations import add_extension_to_path
add_extension_to_path("dataprotection")
from azext_dataprotection.manual._consts import (
CONST_AKS_BACKUP_STRATEGIES,
CONST_BACKUP_STRATEGY_WEEK,
CONST_BACKUP_STRATEGY_MONTH,
CONST_BACKUP_STRATEGY_IMMUTABLE,
CONST_BACKUP_STRATEGY_DISASTER_RECOVERY,
CONST_BACKUP_STRATEGY_CUSTOM,
)
backup_presets = CONST_AKS_BACKUP_STRATEGIES
except Exception: # pylint: disable=broad-except
# Fallback definitions when the dataprotection extension is not installed.
CONST_AKS_BACKUP_STRATEGIES = None
CONST_BACKUP_STRATEGY_WEEK = None
CONST_BACKUP_STRATEGY_MONTH = None
CONST_BACKUP_STRATEGY_IMMUTABLE = None
CONST_BACKUP_STRATEGY_DISASTER_RECOVERY = None
CONST_BACKUP_STRATEGY_CUSTOM = None
backup_presets = CONST_AKS_BACKUP_STRATEGIES

Copilot uses AI. Check for mistakes.
from azure.cli.core.commands.parameters import (
edge_zone_type,
file_type,
Expand Down Expand Up @@ -163,6 +178,7 @@
CONST_UPGRADE_STRATEGY_ROLLING,
CONST_UPGRADE_STRATEGY_BLUE_GREEN
)
from azure.cli.core.commands.validators import validate_file_or_dict

from azext_aks_preview._validators import (
validate_acr,
Expand Down Expand Up @@ -1740,6 +1756,13 @@ def load_arguments(self, _):
'by that action.'
)
)
c.argument("enable_backup", help="Enable backup for the cluster", is_preview=True, action="store_true")
c.argument("backup_strategy", arg_type=get_enum_type(backup_presets), help="Backup strategy for the cluster. Defaults to Week.", is_preview=True)
c.argument("backup_configuration_file", type=validate_file_or_dict,
options_list=['--backup-configuration-file', '-f'],
help="Path to backup configuration file (JSON) or inline JSON string.", is_preview=True)
# In update scenario, use emtpy str as default.
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling error: 'emtpy' should be 'empty'.

Suggested change
# In update scenario, use emtpy str as default.
# In update scenario, use empty str as default.

Copilot uses AI. Check for mistakes.
c.argument('ssh_access', arg_type=get_enum_type(ssh_accesses), is_preview=True)
Comment on lines +1759 to +1765
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Poor code organization: The backup-related parameters (lines 1759-1763) are inserted between unrelated parameters, with a comment on line 1764 that applies to the ssh_access parameter on line 1765, not to the backup parameters. This makes the code harder to read and maintain. Consider grouping related parameters together and ensuring comments are adjacent to the parameters they describe.

Copilot uses AI. Check for mistakes.
c.argument('enable_static_egress_gateway', is_preview=True, action='store_true')
c.argument('disable_static_egress_gateway', is_preview=True, action='store_true')
c.argument("enable_imds_restriction", action="store_true", is_preview=True)
Expand Down
4 changes: 4 additions & 0 deletions src/aks-preview/azext_aks_preview/custom.py
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,10 @@ def aks_update(
# IMDS restriction
enable_imds_restriction=False,
disable_imds_restriction=False,
# Backup
enable_backup=False,
backup_strategy=None,
backup_configuration_parameters=None,
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parameter name mismatch: The function parameter is named 'backup_configuration_parameters' but the code uses 'backup_configuration_file' when accessing it via context.raw_param.get(). This inconsistency will cause the parameter value to not be retrieved correctly. Either rename the parameter in custom.py to 'backup_configuration_file' to match the _params.py definition, or update the context.raw_param.get() call to use 'backup_configuration_parameters'.

Suggested change
backup_configuration_parameters=None,
backup_configuration_file=None,

Copilot uses AI. Check for mistakes.
migrate_vmas_to_vms=False,
enable_upstream_kubescheduler_user_configuration=False,
disable_upstream_kubescheduler_user_configuration=False,
Expand Down
31 changes: 31 additions & 0 deletions src/aks-preview/azext_aks_preview/managed_cluster_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# pylint: disable=too-many-lines
import copy
import datetime
import json
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused import: The json module is imported but not used anywhere in the file. Remove this import unless it's needed for functionality not visible in this diff.

Suggested change
import json

Copilot uses AI. Check for mistakes.
import os
from types import SimpleNamespace
from typing import Any, Dict, List, Optional, Tuple, TypeVar, Union
Expand Down Expand Up @@ -7373,9 +7374,39 @@ def update_mc_profile_preview(self) -> ManagedCluster:
mc = self.update_upstream_kubescheduler_user_configuration(mc)
# update ManagedSystem pools, must at end
mc = self.update_managed_system_pools(mc)
# set up backup
mc = self.set_up_backup(mc)

return mc

def set_up_backup(self, mc: ManagedCluster) -> ManagedCluster:

enable_backup = self.context.raw_param.get("enable_backup")
if enable_backup:
# Validate that dataprotection extension is installed
try:
from azure.cli.core.extension.operations import add_extension_to_path
add_extension_to_path("dataprotection")
from azext_dataprotection.manual.aks.aks_helper import dataprotection_enable_backup_helper
except (ImportError, ModuleNotFoundError):
raise CLIError(
"The 'dataprotection' extension is required for AKS backup functionality.\n"
"Please install it using: az extension add --name dataprotection"
)

backup_strategy = self.context.raw_param.get("backup_strategy")
backup_configuration_file = self.context.raw_param.get("backup_configuration_file")

# Build the cluster resource ID
cluster_resource_id = (
f"/subscriptions/{self.context.get_subscription_id()}"
f"/resourceGroups/{self.context.get_resource_group_name()}"
f"/providers/Microsoft.ContainerService/managedClusters/{self.context.get_name()}"
)

dataprotection_enable_backup_helper(self.cmd, str(cluster_resource_id), backup_strategy, backup_configuration_file)
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary type conversion: The cluster_resource_id is already a string (f-string concatenation), so wrapping it with str() on line 7407 is redundant. Remove the str() call for cleaner code.

Suggested change
dataprotection_enable_backup_helper(self.cmd, str(cluster_resource_id), backup_strategy, backup_configuration_file)
dataprotection_enable_backup_helper(self.cmd, cluster_resource_id, backup_strategy, backup_configuration_file)

Copilot uses AI. Check for mistakes.
return mc
Comment on lines +7382 to +7408
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing test coverage: The new set_up_backup method lacks test coverage. The codebase has comprehensive tests for similar set_up methods in managed_cluster_decorator.py (see test_managed_cluster_decorator.py). Add unit tests that verify the method's behavior including successful backup enablement, handling missing dataprotection extension, and correct parameter passing.

Copilot uses AI. Check for mistakes.

def check_is_postprocessing_required(self, mc: ManagedCluster) -> bool:
"""Helper function to check if postprocessing is required after sending a PUT request to create the cluster.

Expand Down
Loading