Skip to content

Commit

Permalink
fix: update migrator to fail softly when invalid YAMLs are found (#154)
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Cervello authored Jun 21, 2021
1 parent 93a8c9f commit 507302e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion slo_generator/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def api(ctx, config, signature_type, target):
@click.option('--glob',
type=str,
required=False,
default='**/slo_*.yaml',
default='**/*.yaml',
help='Glob expression to seek SLO configs in subpaths')
@click.option('--version',
type=str,
Expand Down
18 changes: 18 additions & 0 deletions slo_generator/migrations/migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ def do_migrate(source,
slo_config_str = source_path.open().read()
slo_config, ind, blc = yaml.util.load_yaml_guess_indent(slo_config_str)
curver = get_config_version(slo_config)
if not curver:
continue

# Source path info
click.secho("-" * 50)
Expand All @@ -114,6 +116,8 @@ def do_migrate(source,
# Run vx to vy migrator method
func = getattr(sys.modules[__name__], f"slo_config_{curver}to{version}")
slo_config_v2 = func(slo_config, shared_config, quiet=quiet)
if not slo_config_v2:
continue

# Write resulting config to target path
extra = '(replaced)' if target_path_str == source_path_str else ''
Expand Down Expand Up @@ -213,6 +217,15 @@ def slo_config_v1tov2(slo_config, shared_config={}, quiet=False, verbose=0):
slo_config_v2 = OrderedDict(copy.deepcopy(SLO_CONFIG_SCHEMA))
slo_config_v2['apiVersion'] = 'sre.google.com/v2'
slo_config_v2['kind'] = 'ServiceLevelObjective'
missing_keys = [
key for key in ['service_name', 'feature_name', 'slo_name', 'backend']
if key not in slo_config
]
if missing_keys:
click.secho(
f'Invalid configuration: missing required key(s) {missing_keys}.',
fg='red')
return None

# Get fields from old config
slo_metadata_name = '{service_name}-{feature_name}-{slo_name}'.format(
Expand Down Expand Up @@ -405,6 +418,11 @@ def get_config_version(config):
Returns:
str: SLO config version.
"""
if not isinstance(config, dict):
click.secho(
'Config does not correspond to any known SLO config versions.',
fg='red')
return None
api_version = config.get('apiVersion', '')
kind = config.get('kind', '')
if not kind: # old v1 format
Expand Down

0 comments on commit 507302e

Please sign in to comment.