[AKS Preview] Add backup enablement support via dataprotection extension#9615
[AKS Preview] Add backup enablement support via dataprotection extension#9615anshulahuja98 wants to merge 1 commit intoAzure:mainfrom
Conversation
|
Validation for Breaking Change Starting...
Thanks for your contribution! |
|
Hi @anshulahuja98, |
|
Thank you for your contribution! We will review the pull request and get back to you soon. |
|
The git hooks are available for azure-cli and azure-cli-extensions repos. They could help you run required checks before creating the PR. Please sync the latest code with latest dev branch (for azure-cli) or main branch (for azure-cli-extensions). pip install azdev --upgrade
azdev setup -c <your azure-cli repo path> -r <your azure-cli-extensions repo path>
|
CodeGen Tools Feedback CollectionThank you for using our CodeGen tool. We value your feedback, and we would like to know how we can improve our product. Please take a few minutes to fill our codegen survey |
There was a problem hiding this comment.
Pull request overview
This PR adds support for enabling AKS cluster backups via the dataprotection extension during cluster updates. The feature allows users to configure backup settings through the az aks update command with options to specify backup strategies and configuration files.
Changes:
- Added
set_up_backupmethod to handle backup enablement during cluster updates - Introduced three new parameters:
enable_backup,backup_strategy, andbackup_configuration_file - Imported backup strategy constants from the dataprotection extension to support argument validation
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
| src/aks-preview/azext_aks_preview/managed_cluster_decorator.py | Added set_up_backup method to orchestrate backup enablement and added json import |
| src/aks-preview/azext_aks_preview/custom.py | Added three backup-related parameters to the aks_update function signature |
| src/aks-preview/azext_aks_preview/_params.py | Defined backup parameter arguments and imported 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 | ||
|
|
There was a problem hiding this comment.
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.
| 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 |
| 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. |
There was a problem hiding this comment.
Spelling error: 'emtpy' should be 'empty'.
| # In update scenario, use emtpy str as default. | |
| # In update scenario, use empty str as default. |
| 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) | ||
| return mc |
There was a problem hiding this comment.
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.
| # pylint: disable=too-many-lines | ||
| import copy | ||
| import datetime | ||
| import json |
There was a problem hiding this comment.
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.
| import json |
| f"/providers/Microsoft.ContainerService/managedClusters/{self.context.get_name()}" | ||
| ) | ||
|
|
||
| dataprotection_enable_backup_helper(self.cmd, str(cluster_resource_id), backup_strategy, backup_configuration_file) |
There was a problem hiding this comment.
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.
| 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) |
| 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. | ||
| c.argument('ssh_access', arg_type=get_enum_type(ssh_accesses), is_preview=True) |
There was a problem hiding this comment.
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.
| # Backup | ||
| enable_backup=False, | ||
| backup_strategy=None, | ||
| backup_configuration_parameters=None, |
There was a problem hiding this comment.
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'.
| backup_configuration_parameters=None, | |
| backup_configuration_file=None, |
This checklist is used to make sure that common guidelines for a pull request are followed.
Related command
General Guidelines
azdev style <YOUR_EXT>locally? (pip install azdevrequired)python scripts/ci/test_index.py -qlocally? (pip install wheel==0.30.0required)For new extensions:
About Extension Publish
There is a pipeline to automatically build, upload and publish extension wheels.
Once your pull request is merged into main branch, a new pull request will be created to update
src/index.jsonautomatically.You only need to update the version information in file setup.py and historical information in file HISTORY.rst in your PR but do not modify
src/index.json.