Skip to content

Commit a7b9f54

Browse files
committed
v0.5.1
1 parent f8a0f8a commit a7b9f54

File tree

7 files changed

+118
-82
lines changed

7 files changed

+118
-82
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
# Changelog
22

33
- [Changelog](#changelog)
4+
- [v0.5.2](#v052)
45
- [v0.5.1](#v051)
56
- [v0.5.0](#v050)
67
- [v0.4.1](#v041)
78
- [v0.4.0](#v040)
89

910
---
1011

12+
## [v0.5.2](https://github.com/chicken231/helmizer/releases/tag/v0.5.1)
13+
14+
- Fix relative/absolute path issues when executing the script from a directory outside of what contains the `kustomization` or `helmizer.yaml`.
15+
1116
## [v0.5.1](https://github.com/chicken231/helmizer/releases/tag/v0.5.1)
1217

1318
- Fixed README

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ I began transitioning my `helm` charts to local templates via [helm template](ht
3232
The recommended way of using **Helmizer** is via a [YAML config file](./examples/resources/helmizer.yaml). But it can be run entirely from the CLI at parity with config. Don't combine them at the same time, though (e.g. `resources` defined in `helmizer.yaml` and CLI at the same time).
3333

3434
```
35-
usage: helmizer [-h] [--debug] [--dry-run] [--helmizer-config-path HELMIZER_CONFIG_PATH] [--quiet] [--version]
35+
usage: helmizer [-h] [--debug] [--dry-run] [--helmizer-config HELMIZER_CONFIG] [--kustomization-directory KUSTOMIZATION_DIR] [--quiet] [--version]
3636
3737
Helmizer
3838
@@ -41,10 +41,12 @@ optional arguments:
4141
4242
--debug Enable debug logging (default: False)
4343
--dry-run Do not write to a file system. (default: False)
44-
--helmizer-config-path HELMIZER_CONFIG_PATH
45-
Override helmizer file path. Default = '$KUSTOMIZATION_PATH/helmizer.yaml' (default: Present Working Directory)
46-
--quiet, -q Quiet output (TODO subcommand output) (default: False)
47-
--version show program's version number and exit
44+
--helmizer-config HELMIZER_CONFIG
45+
Override helmizer file path (default: None)
46+
--kustomization-directory KUSTOMIZATION_DIR
47+
Set path containing kustomization (default: None)
48+
--quiet, -q Quiet output from subprocesses (default: False)
49+
--version show program's version number and exit```
4850
```
4951

5052
## Configuration
@@ -146,7 +148,7 @@ pip3 install -r ./src/requirements.txt
146148
#### Build Locally (Optional)
147149

148150
```bash
149-
docker build -t helmizer:v0.5.1 .
151+
docker build -t helmizer:v0.5.2 .
150152
```
151153

152154
### Run
@@ -186,7 +188,7 @@ In this example (*Nix OS), we're redirecting program output to the (e.g. `kustom
186188
docker run --name helmizer \
187189
--rm \
188190
-v "$PWD"/examples:/tmp/helmizer -w /tmp/helmizer \
189-
docker.pkg.github.com/chicken231/helmizer/helmizer:v0.5.1 /usr/src/app/helmizer.py \
191+
docker.pkg.github.com/chicken231/helmizer/helmizer:v0.5.2 /usr/src/app/helmizer.py \
190192
-n sealed-secrets \
191193
--resource-paths ./resources/sealed-secrets/templates/ \
192194
--kustomization-directory ./resources/ > ./examples/resources/kustomization.yaml

examples/commonAnnotations/helmizer.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ helmizer:
1616
dry-run: false
1717
kustomization-directory: .
1818
kustomization-file-name: kustomization.yaml
19-
resource-absolute-paths: []
19+
resource-absolute-paths: false
2020
sort-keys: false
2121
version: '0.1.0'
2222
kustomize:

examples/commonLabels/helmizer.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ helmizer:
1616
dry-run: false
1717
kustomization-directory: .
1818
kustomization-file-name: kustomization.yaml
19-
resource-absolute-paths: []
19+
resource-absolute-paths: false
2020
sort-keys: true
2121
version: '0.1.0'
2222
kustomize:
@@ -25,7 +25,6 @@ kustomize:
2525
- sealed-secrets/templates/
2626
patchesStrategicMerge:
2727
- extra/
28-
commonAnnotations: {}
2928
commonLabels:
3029
app.kubernetes.io/helmizer: 'true'
3130
app.kubernetes.io/yes: 'false'

examples/patchesStrategicMerge/helmizer.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ helmizer:
1616
dry-run: false
1717
kustomization-directory: .
1818
kustomization-file-name: kustomization.yaml
19-
resource-absolute-paths: []
19+
resource-absolute-paths: false
2020
sort-keys: true
2121
version: '0.1.0'
2222
kustomize:
@@ -25,5 +25,3 @@ kustomize:
2525
- sealed-secrets/templates/
2626
patchesStrategicMerge:
2727
- extra/
28-
commonAnnotations: {}
29-
commonLabels: {}

examples/resources/helmizer.yaml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ helmizer:
1616
dry-run: false
1717
kustomization-directory: .
1818
kustomization-file-name: kustomization.yaml
19-
resource-absolute-paths: []
19+
resource-absolute-paths: false
2020
sort-keys: true
2121
version: '0.1.0'
2222
kustomize:
2323
namespace: sealed-secrets
2424
resources:
2525
- sealed-secrets/templates/
26-
commonAnnotations: {}
27-
commonLabels: {}

src/helmizer.py

Lines changed: 100 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ def __init__(self, helmizer_config, arguments):
4646
self.yaml['commonLabels'] = dict_get_common_labels
4747

4848
# patchesStrategicMerge
49-
list_patches_strategic_merge = self.get_files('patchesStrategicMerge')
49+
list_patches_strategic_merge = self.get_files(arguments, 'patchesStrategicMerge')
5050
if list_patches_strategic_merge:
5151
self.yaml['patchesStrategicMerge'] = list_patches_strategic_merge
5252

5353
# resources
54-
list_resources = self.get_files('resources')
54+
list_resources = self.get_files(arguments, 'resources')
5555
if list_resources:
5656
self.yaml['resources'] = list_resources
5757

@@ -64,6 +64,7 @@ def sort_keys(self):
6464
except KeyError:
6565
pass
6666

67+
6768
def print_kustomization(self):
6869
try:
6970
print(yaml.dump(self.yaml, sort_keys=False))
@@ -73,13 +74,18 @@ def print_kustomization(self):
7374

7475
def write_kustomization(self):
7576
# 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()
8186

8287
# identify kustomization file name
88+
# TODO allow kustomization of file name
8389
str_kustomization_file_name = str()
8490
try:
8591
str_kustomization_file_name = self.helmizer_config['helmizer']['kustomization-file-name'].get(str)
@@ -88,7 +94,7 @@ def write_kustomization(self):
8894

8995
# write to file
9096
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}')
9298
with open(kustomization_file_path, 'w') as file:
9399
file.write(yaml.dump(self.yaml))
94100
logging.debug(f'Successfully wrote to file: {kustomization_file_path}')
@@ -103,6 +109,7 @@ def render_template(self):
103109
self.print_kustomization()
104110
self.write_kustomization()
105111

112+
106113
def get_api_version(self):
107114
str_api_version = str()
108115
try:
@@ -137,6 +144,7 @@ def get_common_annotations(self):
137144
finally:
138145
return dict_common_annotations
139146

147+
140148
def get_common_labels(self):
141149
dict_common_labels = dict()
142150
try:
@@ -148,55 +156,85 @@ def get_common_labels(self):
148156
finally:
149157
return dict_common_labels
150158

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+
152165
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)
154168

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))
158177

159178
# 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))
172183

173184
# 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)
181187

182188
# 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))
185195

186196
return list_final_target_paths
197+
187198
except NotFoundError:
199+
logging.debug(f'key not found: {key}')
188200
pass
189201
except TypeError:
190202
pass
191203

192204

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
200238

201239

202240
def init_arg_parser():
@@ -206,12 +244,13 @@ def init_arg_parser():
206244
optionals = parser.add_argument_group()
207245
optionals.add_argument('--debug', dest='debug', action='store_true', help='Enable debug logging', default=False)
208246
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',
213252
default=False)
214-
optionals.add_argument('--version', action='version', version='v0.5.1')
253+
optionals.add_argument('--version', action='version', version='v0.5.2')
215254
arguments = parser.parse_args()
216255

217256
if arguments.quiet:
@@ -236,17 +275,24 @@ def validate_helmizer_config_version(helmizer_config_version):
236275

237276

238277
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+
239284
config = confuse.Configuration('helmizer', __name__)
240285
try:
241286
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')
245290
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}')
250296
except confuse.exceptions.ConfigReadError:
251297
# no config file found. Give up
252298
return dict()
@@ -256,26 +302,14 @@ def init_helmizer_config(arguments):
256302
except KeyError:
257303
logging.debug('Unable to validate version')
258304

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-
272305
return config
273306

274307

275308
def main():
276309
arguments = init_arg_parser()
277310
helmizer_config = init_helmizer_config(arguments)
278311
kustomization = Kustomization(helmizer_config, arguments)
312+
kustomization.run_subprocess(arguments)
279313
kustomization.render_template()
280314

281315

0 commit comments

Comments
 (0)