region group plan gate: judge planned sizes, keep the applied baseline
The G0 preview was refused because the size rule read resource_drift: an operator Standby lowered min and desired (2 -> 1), which looked like a shrink while the plan raised them (1 -> 2). The rule now compares planned rows only, and also refuses a plan below the last applied size that the drift row records, so a config shrink cannot hide behind Standby drift. Drift rows still go through every other check. Opus review; P2 applied. Squashed from region-group-gate-drift into main.
2 files changed,
+75
−2
scripts/check-region-group-plan.py+13 −2scripts/check_region_group_plan_test.py+62 −0
CI passed — run details
diff --git a/scripts/check-region-group-plan.py b/scripts/check-region-group-plan.py| index a4ee8b437..9f24d8424 100755 |
| --- a/scripts/check-region-group-plan.py |
| +++ b/scripts/check-region-group-plan.py |
| @@ -466,6 +466,10 @@ def check(document, entries, manifest): |
| rows = changes(document) |
| groups = group_modules(document, rows) |
| parsed = [parse_resource(row, groups) for row in rows] |
| + drift_before = {} |
| + for address, kind, _, change in parsed[len(document['resource_changes']):]: |
| + if kind == 'aws_autoscaling_group' and change['before'] is not None: |
| + drift_before.setdefault(address, []).append(change['before']) |
| entries = allow_entries(entries) |
| disposable = cleanup_entries(manifest, rows, groups) |
| existing = [(address, kind, name, change['before']) |
| @@ -491,7 +495,7 @@ def check(document, entries, manifest): |
| for row, (address, kind, name, change) in zip(rows, parsed) |
| if row['mode'] == 'managed' and change['before'] is not None and change['after'] is not None] |
| identities = fixed_identities(existing + planned, groups) |
| - for row, (address, kind, name, change) in zip(rows, parsed): |
| + for index, (row, (address, kind, name, change)) in enumerate(zip(rows, parsed)): |
| if row['mode'] == 'data': |
| continue |
| before, after, actions = change['before'], change['after'], change['actions'] |
| @@ -543,8 +547,15 @@ def check(document, entries, manifest): |
| old, new = before.get(key), after.get(key) |
| if (type(old) is not int or type(new) is not int or old < 0 or new < 0): |
| raise Refused(UNKNOWN % address) |
| - if new < old: |
| + if index < len(document['resource_changes']) and new < old: |
| raise Refused(DECREASE % address) |
| + if index < len(document['resource_changes']): |
| + for baseline in drift_before.get(address, []): |
| + applied = baseline.get(key) |
| + if type(applied) is not int or applied < 0: |
| + raise Refused(UNKNOWN % address) |
| + if new < applied: |
| + raise Refused(DECREASE % address) |
| # Stale exceptions do not silently carry into a different plan. |
| for entry in entries: |
| matches = [change for address, _, _, change in parsed if address == entry['address']] |
diff --git a/scripts/check_region_group_plan_test.py b/scripts/check_region_group_plan_test.py| index bfff772b9..577c2c1f3 100644 |
| --- a/scripts/check_region_group_plan_test.py |
| +++ b/scripts/check_region_group_plan_test.py |
| @@ -319,6 +319,68 @@ class GateTests(unittest.TestCase): |
| self.gate(plan(resource('aws_autoscaling_group.group', 'aws_autoscaling_group', |
| 'group', baseline, dict(min_size=3, max_size=3, desired_capacity=3))), 0) |
| + def test_asg_planned_increase_after_standby_drift(self): |
| + baseline = dict(min_size=2, max_size=2, desired_capacity=2) |
| + standby = dict(baseline, min_size=1, desired_capacity=1) |
| + document = plan(resource('aws_autoscaling_group.group', 'aws_autoscaling_group', |
| + 'group', standby, baseline)) |
| + document['resource_drift'] = [resource('aws_autoscaling_group.group', |
| + 'aws_autoscaling_group', 'group', baseline, standby)] |
| + self.gate(document, 0) |
| + |
| + def test_asg_planned_sizes_against_last_applied_drift(self): |
| + for attr in ['min_size', 'max_size', 'desired_capacity']: |
| + for applied, live, planned, expected in [(3, 1, 2, 1), (3, 1, 1, 1), |
| + (2, 1, 2, 0), (3, 2, 1, 1)]: |
| + with self.subTest(attr=attr, applied=applied, live=live, planned=planned): |
| + baseline = dict(min_size=1, max_size=3, desired_capacity=1) |
| + before = dict(baseline, **{attr: live}) |
| + after = dict(baseline, **{attr: planned}) |
| + document = plan(resource('aws_autoscaling_group.group', |
| + 'aws_autoscaling_group', 'group', before, after, |
| + actions=['no-op'] if live == planned else ['update'])) |
| + document['resource_drift'] = [resource('aws_autoscaling_group.group', |
| + 'aws_autoscaling_group', 'group', |
| + dict(baseline, **{attr: applied}), before)] |
| + result = self.gate(document, expected) |
| + if expected: |
| + self.assertIn('would decrease', result.stderr) |
| + |
| + def test_asg_planned_decrease_from_refreshed_size(self): |
| + for old, new, drifted in [(2, 1, False), (3, 2, True)]: |
| + with self.subTest(drifted=drifted): |
| + before = dict(min_size=old, max_size=3, desired_capacity=old) |
| + after = dict(min_size=new, max_size=3, desired_capacity=new) |
| + document = plan(resource('aws_autoscaling_group.group', |
| + 'aws_autoscaling_group', 'group', before, after)) |
| + if drifted: |
| + document['resource_drift'] = [resource('aws_autoscaling_group.group', |
| + 'aws_autoscaling_group', 'group', after, before)] |
| + self.assertIn('would decrease', self.gate(document, 1).stderr) |
| + |
| + def test_asg_drift_keeps_delete_and_shape_refusals(self): |
| + baseline = dict(min_size=2, max_size=2, desired_capacity=2) |
| + for mutation in ['delete', 'replace', 'unknown', 'missing', 'boolean', 'negative']: |
| + with self.subTest(mutation=mutation): |
| + document = plan(resource('aws_autoscaling_group.group', |
| + 'aws_autoscaling_group', 'group', baseline, baseline, actions=['no-op'])) |
| + drift = resource('aws_autoscaling_group.group', 'aws_autoscaling_group', |
| + 'group', baseline, dict(baseline)) |
| + change = drift['change'] |
| + if mutation == 'delete': |
| + change.update(actions=['delete'], after=None) |
| + elif mutation == 'replace': |
| + change['actions'] = ['delete', 'create'] |
| + elif mutation == 'unknown': |
| + change['after_unknown'] = {'min_size': True} |
| + elif mutation == 'missing': |
| + del change['after']['min_size'] |
| + else: |
| + change['after']['min_size'] = True if mutation == 'boolean' else -1 |
| + document['resource_drift'] = [drift] |
| + message = 'would decrease' if mutation in ['delete', 'replace'] else 'cannot safely read' |
| + self.assertIn(message, self.gate(document, 1).stderr) |
| + |
| def test_fixed_eip_reassociation_and_ingress_protection(self): |
| self.gate(plan(resource('aws_eip.host', 'aws_eip', 'host', |
| {'instance': 'i-old'}, {'instance': 'i-new'})), 1) |