@@ -90,7 +90,7 @@ def call(args, context):
9090 else cluster_config
9191 )
9292 if parsed .dry_run :
93- print (cluster_config )
93+ print (yaml . dump ( cluster_config ) )
9494 else :
9595 _dump_config (kubeconfig_path , cluster_config )
9696
@@ -146,27 +146,35 @@ def _dump_config(filepath, data):
146146 yaml .dump (data , file_descriptor )
147147
148148
149- # Merges the lists in the provided dicts. If non-list properties of the two
150- # dicts differ, uses the value from the first dict.
151149def _merge_dict (dict_1 , dict_2 ):
150+ """
151+ Merges two dicts:
152+ * Lists that are present in both dicts are merged together by their "name" key
153+ (preferring duplicate values in the first dict)
154+ * `None` or missing keys in the first dict are set to the second dict's value
155+ * Other values are preferred from the first dict
156+ """
152157 # Return a new dict to prevent any accidental mutations
153158 result = {}
154159
155- for key in dict_1 :
156- if not isinstance (dict_1 [key ], list ):
157- result [key ] = dict_1 [key ]
158- continue
159-
160- merge_map = {sub ["name" ]: sub for sub in dict_1 [key ]}
161-
162- for sub in dict_2 [key ]:
163- # If the name is already in the merge map, skip
164- if sub ["name" ] in merge_map :
165- continue
166-
167- merge_map [sub ["name" ]] = sub
168-
169- # Convert back to a list
170- result [key ] = list (merge_map .values ())
160+ for key , dict_1_value in dict_1 .items ():
161+ if dict_1_value is None and (dict_2_value := dict_2 .get (key )):
162+ # Replace null value in previous config
163+ result [key ] = dict_2_value
164+ elif isinstance (dict_1_value , list ) and (
165+ dict_2_value := dict_2 .get (key )
166+ ):
167+ merge_map = {sub ["name" ]: sub for sub in dict_1_value }
168+ for list_2_item in dict_2_value :
169+ if (list_2_name := list_2_item ["name" ]) not in merge_map :
170+ merge_map [list_2_name ] = list_2_item
171+ # Convert back to a list
172+ result [key ] = list (merge_map .values ())
173+ else :
174+ result [key ] = dict_1_value
175+
176+ # Process keys missing in dict_1
177+ for key in set (dict_2 .keys ()).difference (dict_1 .keys ()):
178+ result [key ] = dict_2 [key ]
171179
172180 return result
0 commit comments