@@ -615,7 +615,7 @@ def flatten_config_data(config_data, format_, content_type, prefix_to_add="", de
615615 depth = depth ,
616616 separator = separator ,
617617 )
618-
618+
619619 return flattened_data
620620
621621# App Service <-> List of KeyValue object
@@ -771,47 +771,46 @@ def __read_kv_from_kubernetes_configmap(
771771 Returns:
772772 list: List of KeyValue objects
773773 """
774- try :
775- key_values = []
776- from azure .cli .command_modules .acs .custom import aks_runcommand
777- from azure .cli .command_modules .acs ._client_factory import cf_managed_clusters
778-
779- # Get the AKS client from the factory
780- cmd .cli_ctx .data ['subscription_id' ] = aks_cluster ["subscription" ]
781- cmd .cli_ctx .data ['safe_params' ] = None
782- aks_client = cf_managed_clusters (cmd .cli_ctx )
774+ # try:
775+ key_values = []
776+ from azure .cli .command_modules .acs .custom import aks_runcommand
777+ from azure .cli .command_modules .acs ._client_factory import cf_managed_clusters
783778
784- # Command to get the ConfigMap and output it as JSON
785- command = f"kubectl get configmap { configmap_name } -n { namespace } -o json"
786-
787- # Execute the command on the cluster
788- result = aks_runcommand (cmd , aks_client , aks_cluster ["resource_group" ], aks_cluster ["name" ], command_string = command )
789-
790- if hasattr (result , 'logs' ) and result .logs :
791- if not hasattr (result , 'exit_code' ) or result .exit_code != 0 :
792- raise AzureResponseError (f"{ result .logs .strip ()} " )
793-
794- try :
795- import json
796- configmap_data = json .loads (result .logs )
797-
798- # Extract the data section which contains the key-value pairs
799- kvs = __extract_kv_from_configmap_data (
800- configmap_data , content_type , prefix_to_add , format_ , depth , separator )
801-
802- key_values .extend (kvs )
803- except json .JSONDecodeError :
804- raise ValidationError (
805- f"The result from ConfigMap { configmap_name } could not be parsed as valid JSON."
806- )
807- else :
808- raise AzureResponseError ("Unable to get the ConfigMap." )
779+ # Get the AKS client from the factory
780+ cmd .cli_ctx .data ['subscription_id' ] = aks_cluster ["subscription" ]
781+ cmd .cli_ctx .data ['safe_params' ] = None
782+ aks_client = cf_managed_clusters (cmd .cli_ctx )
809783
810- return key_values
811- except Exception as exception :
812- raise AzureInternalError (
813- f"Failed to read key-values from ConfigMap '{ configmap_name } ' in namespace '{ namespace } '.\n { str (exception )} "
814- )
784+ # Command to get the ConfigMap and output it as JSON
785+ command = f"kubectl get configmap { configmap_name } -n { namespace } -o json"
786+
787+ # Execute the command on the cluster
788+ result = aks_runcommand (cmd , aks_client , aks_cluster ["resource_group" ], aks_cluster ["name" ], command_string = command )
789+
790+ if hasattr (result , 'logs' ) and result .logs :
791+ if not hasattr (result , 'exit_code' ) or result .exit_code != 0 :
792+ raise AzureResponseError (f"{ result .logs .strip ()} " )
793+
794+ try :
795+ configmap_data = json .loads (result .logs )
796+
797+ # Extract the data section which contains the key-value pairs
798+ kvs = __extract_kv_from_configmap_data (
799+ configmap_data , content_type , prefix_to_add , format_ , depth , separator )
800+
801+ key_values .extend (kvs )
802+ except json .JSONDecodeError :
803+ raise ValidationError (
804+ f"The result from ConfigMap { configmap_name } could not be parsed as valid JSON."
805+ )
806+ else :
807+ raise AzureResponseError ("Unable to get the ConfigMap." )
808+
809+ return key_values
810+ # except Exception as exception:
811+ # raise AzureInternalError(
812+ # f"Failed to read key-values from ConfigMap '{configmap_name}' in namespace '{namespace}'.\n{str(exception)}"
813+ # )
815814
816815
817816def __extract_kv_from_configmap_data (configmap_data , content_type , prefix_to_add = "" , format_ = None , depth = None , separator = None ):
@@ -830,39 +829,40 @@ def __extract_kv_from_configmap_data(configmap_data, content_type, prefix_to_add
830829 list: List of KeyValue objects
831830 """
832831 key_values = []
833- import json
834-
832+
835833 if 'data' not in configmap_data :
836834 logger .warning ("ConfigMap exists but has no data" )
837835 return key_values
838-
836+
839837 for key , value in configmap_data ['data' ].items ():
840838 if format_ in ("json" , "yaml" , "properties" ):
841839 if format_ == "json" :
842840 try :
843841 value = json .loads (value )
844842 except json .JSONDecodeError :
845843 logger .warning (
846- f'Value "{ value } " for key "{ key } " is not a well formatted JSON data.'
844+ 'Value "%s" for key "%s" is not a well formatted JSON data.' ,
845+ value , key
847846 )
848847 continue
849848 elif format_ == "yaml" :
850849 try :
851850 value = yaml .safe_load (value )
852851 except yaml .YAMLError :
853852 logger .warning (
854- f'Value "{ value } " for key "{ key } " is not a well formatted YAML data.'
853+ 'Value "%s" for key "%s" is not a well formatted YAML data.' ,
854+ value , key
855855 )
856856 continue
857857 else :
858858 try :
859- import io
860859 value = javaproperties .load (io .StringIO (value ))
861- except Exception :
860+ except javaproperties . InvalidUEscapeError :
862861 logger .warning (
863- f'Value "{ value } " for key "{ key } " is not a well formatted properties data.'
862+ 'Value "%s" for key "%s" is not a well formatted properties data.' ,
863+ value , key
864864 )
865- continue
865+ continue
866866
867867 flattened_data = flatten_config_data (
868868 config_data = value ,
@@ -878,21 +878,22 @@ def __extract_kv_from_configmap_data(configmap_data, content_type, prefix_to_add
878878 if validate_import_key (key = k ):
879879 key_values .append (KeyValue (key = k , value = v ))
880880 return key_values
881-
882- elif validate_import_key (key ):
881+
882+ if validate_import_key (key ):
883883 # If content_type is JSON, validate the value
884884 if content_type and is_json_content_type (content_type ):
885885 try :
886886 json .loads (value )
887887 except json .JSONDecodeError :
888888 logger .warning (
889- f'Value "{ value } " for key "{ key } " is not a valid JSON object, which conflicts with the provided content type "{ content_type } ".'
889+ 'Value "{value}" for key "{key}" is not a valid JSON object, which conflicts with the provided content type "{content_type}".' ,
890+ value , key , content_type
890891 )
891892 continue
892-
893+
893894 kv = KeyValue (key = prefix_to_add + key , value = value )
894895 key_values .append (kv )
895-
896+
896897 return key_values
897898
898899
0 commit comments