Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Update migrator to fail softly when invalid YAMLs are found #154

Merged
merged 1 commit into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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