@@ -46,12 +46,12 @@ def __init__(self, helmizer_config, arguments):
46
46
self .yaml ['commonLabels' ] = dict_get_common_labels
47
47
48
48
# patchesStrategicMerge
49
- list_patches_strategic_merge = self .get_files ('patchesStrategicMerge' )
49
+ list_patches_strategic_merge = self .get_files (arguments , 'patchesStrategicMerge' )
50
50
if list_patches_strategic_merge :
51
51
self .yaml ['patchesStrategicMerge' ] = list_patches_strategic_merge
52
52
53
53
# resources
54
- list_resources = self .get_files ('resources' )
54
+ list_resources = self .get_files (arguments , 'resources' )
55
55
if list_resources :
56
56
self .yaml ['resources' ] = list_resources
57
57
@@ -64,6 +64,7 @@ def sort_keys(self):
64
64
except KeyError :
65
65
pass
66
66
67
+
67
68
def print_kustomization (self ):
68
69
try :
69
70
print (yaml .dump (self .yaml , sort_keys = False ))
@@ -73,13 +74,18 @@ def print_kustomization(self):
73
74
74
75
def write_kustomization (self ):
75
76
# identify kustomization file's parent directory
76
- str_kustomization_directory = str ()
77
- try :
78
- str_kustomization_directory = self .helmizer_config ['helmizer' ]['kustomization-directory' ].get (str )
79
- except KeyError :
80
- str_kustomization_directory = '.'
77
+ str_kustomization_dir = str ()
78
+
79
+ if self .arguments .kustomization_dir :
80
+ str_kustomization_dir = self .arguments .kustomization_dir
81
+ else :
82
+ try :
83
+ str_kustomization_dir = self .helmizer_config ['helmizer' ]['kustomization-directory' ].get (str )
84
+ except KeyError :
85
+ str_kustomization_dir = getcwd ()
81
86
82
87
# identify kustomization file name
88
+ # TODO allow kustomization of file name
83
89
str_kustomization_file_name = str ()
84
90
try :
85
91
str_kustomization_file_name = self .helmizer_config ['helmizer' ]['kustomization-file-name' ].get (str )
@@ -88,7 +94,7 @@ def write_kustomization(self):
88
94
89
95
# write to file
90
96
try :
91
- kustomization_file_path = path .normpath (f'{ str_kustomization_directory } /{ str_kustomization_file_name } ' )
97
+ kustomization_file_path = path .normpath (f'{ str_kustomization_dir } /{ str_kustomization_file_name } ' )
92
98
with open (kustomization_file_path , 'w' ) as file :
93
99
file .write (yaml .dump (self .yaml ))
94
100
logging .debug (f'Successfully wrote to file: { kustomization_file_path } ' )
@@ -103,6 +109,7 @@ def render_template(self):
103
109
self .print_kustomization ()
104
110
self .write_kustomization ()
105
111
112
+
106
113
def get_api_version (self ):
107
114
str_api_version = str ()
108
115
try :
@@ -137,6 +144,7 @@ def get_common_annotations(self):
137
144
finally :
138
145
return dict_common_annotations
139
146
147
+
140
148
def get_common_labels (self ):
141
149
dict_common_labels = dict ()
142
150
try :
@@ -148,55 +156,85 @@ def get_common_labels(self):
148
156
finally :
149
157
return dict_common_labels
150
158
151
- def get_files (self , key ):
159
+
160
+ def get_files (self , arguments , key ):
161
+ list_target_paths = list ()
162
+ list_final_target_paths = list ()
163
+ str_kustomization_path = str ()
164
+
152
165
try :
153
- paths = self .helmizer_config ['kustomize' ][key ].get (list )
166
+ # test if the key to configure is even defined in input helmizer config
167
+ list_kustomization_children = self .helmizer_config ['kustomize' ][key ].get (list )
154
168
155
- if len (paths ) > 0 :
156
- list_final_target_paths = list ()
157
- for target_path in paths :
169
+ if arguments .kustomization_dir :
170
+ str_kustomization_path = path .abspath (arguments .kustomization_dir )
171
+ else :
172
+ str_kustomization_path = path .abspath (self .helmizer_config ['helmizer' ]['kustomization-directory' ].get (str ))
173
+
174
+ if len (list_kustomization_children ) > 0 :
175
+ for target_path in list_kustomization_children :
176
+ str_child_path = path .abspath (path .join (str_kustomization_path , target_path ))
158
177
159
178
# walk directory
160
- if path .isdir (target_path ):
161
- for (dirpath , _ , filenames ) in walk (target_path ):
162
- for file in filenames :
163
- absolute_path = path .normpath (f'{ dirpath } /{ file } ' )
164
- # TODO fix this
165
- if len (self .helmizer_config ['helmizer' ]['resource-absolute-paths' ].get (list )) > 0 :
166
- if len (self .helmizer_config ['helmizer' ]['resource-absolute-paths' ].get (list )) > 0 :
167
- list_final_target_paths .append (absolute_path )
168
- else :
169
- if self .helmizer_config ['helmizer' ]['kustomization-directory' ]:
170
- str_relative_path = path .relpath (absolute_path , self .helmizer_config ['helmizer' ]['kustomization-directory' ].get (str ))
171
- list_final_target_paths .append (str_relative_path )
179
+ if path .isdir (str_child_path ):
180
+ for (dirpath , _ , filenames ) in walk (str_child_path ):
181
+ for filename in filenames :
182
+ list_target_paths .append (path .join (dirpath , filename ))
172
183
173
184
# file
174
- elif path .isfile (target_path ):
175
- absolute_path = path .abspath (target_path )
176
- if self .helmizer_config ['helmizer' ]['resource-absolute-paths' ].get (bool ):
177
- list_final_target_paths .append (absolute_path )
178
- else :
179
- str_relative_path = path .relpath (absolute_path , getcwd ())
180
- list_final_target_paths .append (str_relative_path )
185
+ elif path .isfile (str_child_path ):
186
+ list_target_paths .append (str_child_path )
181
187
182
188
# url
183
- elif validate_url (target_path ):
184
- list_final_target_paths .append (target_path )
189
+ elif validate_url (str_child_path ):
190
+ list_target_paths .append (str_child_path )
191
+
192
+ # convert absolute paths into paths relative to the kustomization directory
193
+ for final_target_path in list_target_paths :
194
+ list_final_target_paths .append (path .relpath (final_target_path , str_kustomization_path ))
185
195
186
196
return list_final_target_paths
197
+
187
198
except NotFoundError :
199
+ logging .debug (f'key not found: { key } ' )
188
200
pass
189
201
except TypeError :
190
202
pass
191
203
192
204
193
- def run_subprocess (arguments , command_string ):
194
- logging .debug (f"creating subprocess: \' { command_string } \' " )
195
- # TODO make it respect this arg
196
- if arguments .quiet :
197
- subprocess .run (f'{ command_string } ' , shell = True , check = True , stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL , text = False )
198
- else :
199
- subprocess .run (f'{ command_string } ' , shell = True , check = True , text = True )
205
+ def run_subprocess (self , arguments ):
206
+ subprocess_working_directory = str ()
207
+ if arguments .kustomization_dir :
208
+ subprocess_working_directory = path .abspath (path .normpath (arguments .kustomization_dir ))
209
+ else :
210
+ subprocess_working_directory = path .abspath (path .normpath (self .helmizer_config ['helmizer' ]['kustomization-directory' ].get (str )))
211
+ logging .debug (f'Subprocess working directory: { subprocess_working_directory } ' )
212
+
213
+
214
+ list_command_string = list ()
215
+ for config_command in self .helmizer_config ['helmizer' ]['commandSequence' ]:
216
+ try :
217
+
218
+ # construct command(s)
219
+ if config_command ['command' ]:
220
+ command = config_command ['command' ].get (str )
221
+ if config_command ['args' ]:
222
+ args = ' ' .join (config_command ['args' ].get (list )) # combine list elements into space-delimited
223
+ list_command_string .append (f'{ command } { args } ' )
224
+ else :
225
+ list_command_string .append (f'{ command } ' )
226
+
227
+ # execute
228
+ for command in list_command_string :
229
+ logging .debug (f"creating subprocess: \' { command } \' " )
230
+ if arguments .quiet :
231
+ subprocess .run (f'{ command } ' , shell = True , check = True , stdout = subprocess .DEVNULL ,
232
+ stderr = subprocess .DEVNULL , text = True , cwd = subprocess_working_directory )
233
+ else :
234
+ subprocess .run (f'{ command } ' , shell = True , check = True , text = True , cwd = subprocess_working_directory )
235
+
236
+ except NotFoundError as e :
237
+ pass
200
238
201
239
202
240
def init_arg_parser ():
@@ -206,12 +244,13 @@ def init_arg_parser():
206
244
optionals = parser .add_argument_group ()
207
245
optionals .add_argument ('--debug' , dest = 'debug' , action = 'store_true' , help = 'Enable debug logging' , default = False )
208
246
optionals .add_argument ('--dry-run' , dest = 'dry_run' , action = 'store_true' , help = 'Do not write to a file system.' , default = False )
209
- optionals .add_argument ('--helmizer-config-path' , dest = 'helmizer_config_path' , action = 'store' , type = str ,
210
- help = 'Override helmizer file path. Default = \' $KUSTOMIZATION_PATH/helmizer.yaml\' ' ,
211
- default = getcwd ())
212
- optionals .add_argument ('--quiet' , '-q' , dest = 'quiet' , action = 'store_true' , help = 'Quiet output from subcommands' ,
247
+ optionals .add_argument ('--helmizer-directory' , dest = 'helmizer_directory' , action = 'store' , type = str ,
248
+ help = 'Override helmizer file path' )
249
+ optionals .add_argument ('--kustomization-directory' , dest = 'kustomization_dir' , action = 'store' , type = str ,
250
+ help = 'Set path containing kustomization' )
251
+ optionals .add_argument ('--quiet' , '-q' , dest = 'quiet' , action = 'store_true' , help = 'Quiet output from subprocesses' ,
213
252
default = False )
214
- optionals .add_argument ('--version' , action = 'version' , version = 'v0.5.1 ' )
253
+ optionals .add_argument ('--version' , action = 'version' , version = 'v0.5.2 ' )
215
254
arguments = parser .parse_args ()
216
255
217
256
if arguments .quiet :
@@ -236,17 +275,24 @@ def validate_helmizer_config_version(helmizer_config_version):
236
275
237
276
238
277
def init_helmizer_config (arguments ):
278
+ str_helmizer_config_path = str ()
279
+ if arguments .helmizer_directory :
280
+ str_helmizer_config_path = arguments .helmizer_directory
281
+ else :
282
+ str_helmizer_config_path = getcwd ()
283
+
239
284
config = confuse .Configuration ('helmizer' , __name__ )
240
285
try :
241
286
try :
242
- logging . debug ( f'Trying helmizer config path from config: { arguments . helmizer_config_path } / helmizer.yaml' )
243
- config . set_file (f'{ arguments . helmizer_config_path } /helmizer.yaml' )
244
- logging . debug (f'parsed config: { config } ' )
287
+ # assume file name is helmizer.yaml
288
+ logging . debug (f'Trying helmizer config path from config: { str_helmizer_config_path } /helmizer.yaml' )
289
+ config . set_file (f'{ str_helmizer_config_path } /helmizer.yaml ' )
245
290
except KeyError :
246
- if arguments .helmizer_config_path :
247
- logging .debug (f'Trying helmizer config path from argument: { arguments .helmizer_config_path } ' )
248
- config .set_file (path .normpath (arguments .helmizer_config_path ))
249
- logging .debug (f'parsed config: { config } ' )
291
+ if str_helmizer_config_path :
292
+ logging .debug (f'Trying helmizer config path from argument: { str_helmizer_config_path } ' )
293
+ config .set_file (path .normpath (str_helmizer_config_path ))
294
+ finally :
295
+ logging .debug (f'parsed config: { config } ' )
250
296
except confuse .exceptions .ConfigReadError :
251
297
# no config file found. Give up
252
298
return dict ()
@@ -256,26 +302,14 @@ def init_helmizer_config(arguments):
256
302
except KeyError :
257
303
logging .debug ('Unable to validate version' )
258
304
259
- # TODO give it its own function
260
- for config_command in config ['helmizer' ]['commandSequence' ]:
261
- try :
262
- if config_command ['command' ]:
263
- command = config_command ['command' ].get (str )
264
- if config_command ['args' ]:
265
- args = ' ' .join (config_command ['args' ].get (list )) # combine list elements into space-delimited
266
- run_subprocess (arguments , f'{ command } { args } ' )
267
- else :
268
- run_subprocess (command )
269
- except NotFoundError as e :
270
- pass
271
-
272
305
return config
273
306
274
307
275
308
def main ():
276
309
arguments = init_arg_parser ()
277
310
helmizer_config = init_helmizer_config (arguments )
278
311
kustomization = Kustomization (helmizer_config , arguments )
312
+ kustomization .run_subprocess (arguments )
279
313
kustomization .render_template ()
280
314
281
315
0 commit comments