Skip to content

Commit 7ee7e20

Browse files
authored
✨ util: Warning handler that discards messages that match a regular expression (#11179)
* ✨ util: Warning handler that discards messages that match a regular expression * fixup! ✨ util: Warning handler that discards messages that match a regular expression Remove deduplication. As a side-effect, simplify the implementation. * fixup! ✨ util: Warning handler that discards messages that match a regular expression Fix the test of an uninitialized handler, so that the logger is exercised. * fixup! ✨ util: Warning handler that discards messages that match a regular expression Split conflated test case into two independent test cases
1 parent 7e2ff01 commit 7ee7e20

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed

util/apiwarnings/doc.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package apiwarnings defines warning handlers used with API clients.
18+
package apiwarnings

util/apiwarnings/handler.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package apiwarnings
18+
19+
import (
20+
"regexp"
21+
22+
"github.com/go-logr/logr"
23+
)
24+
25+
// DiscardMatchingHandler is a handler that discards API server warnings
26+
// whose message matches any user-defined regular expressions, but otherwise
27+
// logs warnings to the user-defined logger.
28+
type DiscardMatchingHandler struct {
29+
// Logger is used to log responses with the warning header.
30+
Logger logr.Logger
31+
32+
// Expressions is a slice of regular expressions used to discard warnings.
33+
// If the warning message matches any expression, it is not logged.
34+
Expressions []regexp.Regexp
35+
}
36+
37+
// HandleWarningHeader handles logging for responses from API server that are
38+
// warnings with code being 299 and uses a logr.Logger for its logging purposes.
39+
func (h *DiscardMatchingHandler) HandleWarningHeader(code int, _, message string) {
40+
if code != 299 || message == "" {
41+
return
42+
}
43+
44+
for _, exp := range h.Expressions {
45+
if exp.MatchString(message) {
46+
return
47+
}
48+
}
49+
50+
h.Logger.Info(message)
51+
}

util/apiwarnings/handler_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
Copyright 2024 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package apiwarnings
18+
19+
import (
20+
"regexp"
21+
"testing"
22+
23+
"github.com/go-logr/logr/funcr"
24+
. "github.com/onsi/gomega"
25+
)
26+
27+
func TestDiscardMatchingHandler(t *testing.T) {
28+
tests := []struct {
29+
name string
30+
code int
31+
message string
32+
expressions []regexp.Regexp
33+
wantLogged bool
34+
}{
35+
{
36+
name: "log, if no expressions are defined",
37+
code: 299,
38+
message: "non-matching warning",
39+
wantLogged: true,
40+
},
41+
{
42+
name: "log, if warning does not match any expression",
43+
code: 299,
44+
message: "non-matching warning",
45+
expressions: []regexp.Regexp{},
46+
wantLogged: true,
47+
},
48+
{
49+
name: "do not log, if warning matches at least one expression",
50+
code: 299,
51+
message: "matching warning",
52+
expressions: []regexp.Regexp{
53+
*regexp.MustCompile("^matching.*"),
54+
},
55+
wantLogged: false,
56+
},
57+
{
58+
name: "do not log, if code is not 299",
59+
code: 0,
60+
message: "warning",
61+
expressions: []regexp.Regexp{},
62+
wantLogged: false,
63+
},
64+
{
65+
name: "do not log, if message is empty",
66+
code: 299,
67+
message: "",
68+
expressions: []regexp.Regexp{},
69+
wantLogged: false,
70+
},
71+
}
72+
for _, tt := range tests {
73+
t.Run(tt.name, func(t *testing.T) {
74+
g := NewWithT(t)
75+
logged := false
76+
h := DiscardMatchingHandler{
77+
Logger: funcr.New(func(_, _ string) {
78+
logged = true
79+
},
80+
funcr.Options{},
81+
),
82+
Expressions: tt.expressions,
83+
}
84+
h.HandleWarningHeader(tt.code, "", tt.message)
85+
g.Expect(logged).To(Equal(tt.wantLogged))
86+
})
87+
}
88+
}
89+
90+
func TestDiscardMatchingHandler_uninitialized(t *testing.T) {
91+
g := NewWithT(t)
92+
h := DiscardMatchingHandler{}
93+
g.Expect(func() {
94+
// Together, the code and message value ensure that the handler logs the message.
95+
h.HandleWarningHeader(299, "", "example")
96+
}).ToNot(Panic())
97+
}

0 commit comments

Comments
 (0)