Skip to content

Commit

Permalink
Default the isEqual flag to true in alertmanager
Browse files Browse the repository at this point in the history
Solves #2602 - the new version of alertmanager introduces an isEqual
flag to support != matchers in alertmanager silences. Clients set this
to true in the JSON blob sent to the api to indicate a foo=bar matcher,
and to false to indicate foo!=bar. Due to gos unmarshalling, anything
that _doesn't_ include this flag will default to false (i.e. a !=
matcher), so any clients that aren't aware of this flag (such as amtools
before negative matchers and the new api) will not send it, and when you
think you are making a foo=bar matcher, alertmanager will interpret that
as a not equals.

This commit changes the Unmarshaling of the v1matcher struct to default
the IsEqual flag to true, to keep the old behaviour for clients not
setting the flag

Signed-off-by: sinkingpoint <colin@quirl.co.nz>
  • Loading branch information
sinkingpoint committed May 27, 2021
1 parent 66cfee4 commit 8f425b3
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/labels/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ func (m Matcher) MarshalJSON() ([]byte, error) {
}

func (m *Matcher) UnmarshalJSON(data []byte) error {
var v1m apiV1Matcher
v1m := apiV1Matcher{
IsEqual: true,
}

if err := json.Unmarshal(data, &v1m); err != nil {
return err
}
Expand Down
55 changes: 55 additions & 0 deletions pkg/labels/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,59 @@ func TestMatcherJSON(t *testing.T) {
t.Errorf("Doing Marshal and Unmarshal seems to be losing data; before %#v, after %#v", m, m2)
}
}

// Unmarshaling tests - testing that unmarshaling JSON gives expected matchers
tests = []struct {
name string
op MatchType
value string
want string
}{
{
name: "foo",
op: MatchEqual,
value: "bar",
want: `{"name":"foo","value":"bar","isRegex":false}`,
},
{
name: `foo`,
op: MatchEqual,
value: `bar`,
want: `{"name":"foo","value":"bar","isRegex":false,"isEqual":true}`,
},
{
name: `foo`,
op: MatchNotEqual,
value: `bar`,
want: `{"name":"foo","value":"bar","isRegex":false,"isEqual":false}`,
},
{
name: `foo`,
op: MatchRegexp,
value: `bar`,
want: `{"name":"foo","value":"bar","isRegex":true,"isEqual":true}`,
},
{
name: `foo`,
op: MatchNotRegexp,
value: `bar`,
want: `{"name":"foo","value":"bar","isRegex":true,"isEqual":false}`,
},
}

for _, test := range tests {
var m Matcher
if err := json.Unmarshal([]byte(test.want), &m); err != nil {
t.Fatal(err)
}

m2, err := NewMatcher(test.op, test.name, test.value)
if err != nil {
t.Fatal(err)
}

if !cmp(m, *m2) {
t.Errorf("Unmarshaling seems to be producing unexpected matchers; got %#v, expected %#v", m, m2)
}
}
}

0 comments on commit 8f425b3

Please sign in to comment.