fix(webhook): resolve matcher order entirely in Terraform - #5214
Conversation
|
Would make more sense to move the whole sorting to webhook.tf? Something like this The config is saved in ssm and loaded by lambda. @Brend-Smits @guicaulada What you folks think? |
|
Agreed, your key gives the same order. One addition while editing that line: I'd pad wider than |
Ordering was split across two places: webhook.tf sorted by priority, then the dispatcher re-sorted the result to put exact and bidirectional matchers first. Fold both into the Terraform sort key so the order is decided once, and have the dispatcher consume the list as-is. That also removes an in-place sort of the config held on the ConfigLoader singleton, which outlives a single invocation, so one event could reorder what the next one saw on a warm container. Widen the priority padding while moving it. The pad has to be at least as wide as the value or lexicographic sorting stops matching numeric sorting: with %03d, priority 1000 sorted ahead of the 999 default because '1' < '9'. That was supposed to be prevented by a range validation, but the validation dereferenced a map as though it were an object, so try() swallowed the error and it has never rejected anything since it was added. Widening the pad makes the value range a non-issue, so drop the validation rather than repair it. Ordering is unchanged for any config in the previously documented 0-999 range. Ordering coverage moves to a terraform test, since the dispatcher no longer decides it.
ffb4858 to
9006fc9
Compare
| # is generous on purpose: a value wider than the padding sorts by its leading | ||
| # digit instead of its magnitude, which would put 1000 ahead of 999. | ||
| runner_matcher_config = { for k, v in var.runner_matcher_config : | ||
| format("%d-%010d-%s", |
There was a problem hiding this comment.
No good reason, honestly. 10 was just "comfortably wider than any priority anyone would realistically set" and I never went back to justify it. That slipped by me.
The only thing the width actually needs to guarantee is that string sorting matches numeric sorting, so it has to be at least as wide as the largest allowed priority. Happy to tie it to a real bound (there's an old validation on priority that was meant to cap it), or just narrow it if you'd prefer.
Reworked per @edersonbrilhante's suggestion: the ordering now happens once, in
webhook.tf.What changed
Ordering was split across two places.
webhook.tfsorted by priority, then the dispatcher re-sorted to put exact and bidirectional matchers first. Both are folded into the Terraform sort key, and the dispatcher consumes the list as-is:This also removes an in-place sort of the config held on the
ConfigLoadersingleton. That object outlives a single invocation, so one event's dispatch could reorder what the next event saw on a warm container. Idempotent in practice, but it made order depend on invocation history.On the padding width
The pad has to be at least as wide as the value, or lexicographic sorting stops matching numeric sorting. With
%03d, priority1000sorted ahead of the999default, because'1' < '9'.A range validation was supposed to prevent that, but it dereferences a map as though it were an object:
runner_matcher_configis amap(object(...)), so.matcherConfiglooks up a key of that name, never finds one, andtry()falls back to999. The condition has always evaluated999 >= 0 && 999 < 1000. It has never rejected anything since it was added in #3591.Widening the pad makes the range a non-issue, so this drops the validation rather than repairing it. Ordering is unchanged for any config within the previously documented 0-999 range — verified, in-range values sort identically under both widths.
Incidentally, the original design in #3590 specified
%05d; the implementation shipped%03d.Tests
Ordering coverage moves to
modules/webhook/tests/matcher_order.tftest.hcl, since the dispatcher no longer decides it. Two cases: strict-before-loose-then-priority, and a priority of 1000 sorting after the 999 default. Both fail if the strict component is dropped from the key or the pad is narrowed back to%03d.The three dispatcher tests that fed an unsorted config now use the order Terraform actually emits.
Closes #5133.