|
22 | 22 | from azure.cli.core.util import user_confirmation |
23 | 23 | from azure.cli.core.azclierror import ( |
24 | 24 | AzureInternalError, |
| 25 | + AzureResponseError, |
25 | 26 | FileOperationError, |
26 | 27 | InvalidArgumentValueError, |
27 | 28 | RequiredArgumentMissingError, |
@@ -767,47 +768,48 @@ def __read_kv_from_kubernetes_configmap( |
767 | 768 | Returns: |
768 | 769 | list: List of KeyValue objects |
769 | 770 | """ |
770 | | - key_values = [] |
771 | | - from azure.cli.command_modules.acs.custom import aks_runcommand |
772 | | - from azure.cli.command_modules.acs._client_factory import cf_managed_clusters |
773 | | - |
774 | | - # Get the AKS client from the factory |
775 | | - cmd.cli_ctx.data['subscription_id'] = aks_cluster["subscription"] |
776 | | - cmd.cli_ctx.data['safe_params'] = None |
777 | | - aks_client = cf_managed_clusters(cmd.cli_ctx) |
778 | | - |
779 | | - # Command to get the ConfigMap and output it as JSON |
780 | | - command = f"kubectl get configmap {configmap_name} -n {namespace} -o json" |
781 | | - |
782 | | - # Execute the command on the cluster |
783 | | - result = aks_runcommand(cmd, aks_client, aks_cluster["resource_group"], aks_cluster["name"], command_string=command) |
| 771 | + try: |
| 772 | + key_values = [] |
| 773 | + from azure.cli.command_modules.acs.custom import aks_runcommand |
| 774 | + from azure.cli.command_modules.acs._client_factory import cf_managed_clusters |
| 775 | + |
| 776 | + # Get the AKS client from the factory |
| 777 | + cmd.cli_ctx.data['subscription_id'] = aks_cluster["subscription"] |
| 778 | + cmd.cli_ctx.data['safe_params'] = None |
| 779 | + aks_client = cf_managed_clusters(cmd.cli_ctx) |
784 | 780 |
|
785 | | - if hasattr(result, 'logs') and result.logs: |
786 | | - if not hasattr(result, 'exit_code') or result.exit_code != 0: |
787 | | - raise CLIError( |
788 | | - f"Failed to get ConfigMap {configmap_name} in namespace {namespace}. {result.logs.strip()}" |
789 | | - ) |
| 781 | + # Command to get the ConfigMap and output it as JSON |
| 782 | + command = f"kubectl get configmap {configmap_name} -n {namespace} -o json" |
| 783 | + |
| 784 | + # Execute the command on the cluster |
| 785 | + result = aks_runcommand(cmd, aks_client, aks_cluster["resource_group"], aks_cluster["name"], command_string=command) |
| 786 | + |
| 787 | + if hasattr(result, 'logs') and result.logs: |
| 788 | + if not hasattr(result, 'exit_code') or result.exit_code != 0: |
| 789 | + raise AzureResponseError(f"{result.logs.strip()}") |
| 790 | + |
| 791 | + try: |
| 792 | + import json |
| 793 | + configmap_data = json.loads(result.logs) |
| 794 | + |
| 795 | + # Extract the data section which contains the key-value pairs |
| 796 | + kvs = __extract_kv_from_configmap_data( |
| 797 | + configmap_data, content_type, prefix_to_add, format_, depth, separator) |
| 798 | + |
| 799 | + key_values.extend(kvs) |
| 800 | + except json.JSONDecodeError: |
| 801 | + raise ValidationError( |
| 802 | + f"The result from ConfigMap {configmap_name} could not be parsed as valid JSON." |
| 803 | + ) |
| 804 | + else: |
| 805 | + raise AzureResponseError("Unable to get the ConfigMap.") |
790 | 806 |
|
791 | | - try: |
792 | | - import json |
793 | | - configmap_data = json.loads(result.logs) |
794 | | - |
795 | | - # Extract the data section which contains the key-value pairs |
796 | | - kvs = __extract_kv_from_configmap_data( |
797 | | - configmap_data, content_type, prefix_to_add, format_, depth, separator) |
798 | | - |
799 | | - key_values.extend(kvs) |
800 | | - except json.JSONDecodeError: |
801 | | - raise ValidationError( |
802 | | - f"The result from ConfigMap {configmap_name} could not be parsed as valid JSON." |
803 | | - ) |
804 | | - else: |
| 807 | + return key_values |
| 808 | + except Exception as exception: |
805 | 809 | raise CLIError( |
806 | | - f"Failed to get ConfigMap {configmap_name} in namespace {namespace}." |
| 810 | + f"Failed to read key-values from ConfigMap '{configmap_name}' in namespace '{namespace}'.\n{str(exception)}" |
807 | 811 | ) |
808 | 812 |
|
809 | | - return key_values |
810 | | - |
811 | 813 | def __extract_kv_from_configmap_data(configmap_data, content_type, prefix_to_add="", format_=None, depth=None, separator=None): |
812 | 814 | """ |
813 | 815 | Helper function to extract key-value pairs from ConfigMap data. |
|
0 commit comments