Skip to content

Commit 483d63d

Browse files
committed
✨ util: Warning handler that discards a default set of messages
1 parent ba608cb commit 483d63d

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

util/apiwarnings/default.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
"fmt"
21+
"regexp"
22+
23+
"github.com/go-logr/logr"
24+
25+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
26+
)
27+
28+
// DomainQualifiedFinalizerWarning is a regular expression that matches a
29+
// warning that the API server returns when a finalizer is not domain-qualified.
30+
func DomainQualifiedFinalizerWarning(domain string) *regexp.Regexp {
31+
return regexp.MustCompile(
32+
fmt.Sprintf("^metadata.finalizers:.*%s.*prefer a domain-qualified finalizer name to avoid accidental conflicts with other finalizer writers$", domain),
33+
)
34+
}
35+
36+
// DefaultHandler is a handler that discards warnings that are the result of
37+
// decisions made by the Cluster API project, but cannot be immediately
38+
// addressed, and are therefore not helpful to the user.
39+
func DefaultHandler(l logr.Logger) *DiscardMatchingHandler {
40+
return NewDiscardMatchingHandler(
41+
l,
42+
DiscardMatchingHandlerOptions{
43+
Deduplicate: true,
44+
Expressions: []regexp.Regexp{
45+
*DomainQualifiedFinalizerWarning(clusterv1.GroupVersion.Group),
46+
},
47+
},
48+
)
49+
}

util/apiwarnings/default_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
"testing"
21+
22+
"github.com/go-logr/logr/funcr"
23+
. "github.com/onsi/gomega"
24+
)
25+
26+
func TestDefaultHandler(t *testing.T) {
27+
tests := []struct {
28+
name string
29+
code int
30+
message string
31+
wantLogged bool
32+
}{
33+
{
34+
name: "log, if warning does not match any expression",
35+
code: 299,
36+
message: `metadata.finalizers: "foo.example.com": prefer a domain-qualified finalizer name to avoid accidental conflicts with other finalizer writers`,
37+
wantLogged: true,
38+
},
39+
{
40+
name: "do not log, if warning matches at least one expression",
41+
code: 299,
42+
message: `metadata.finalizers: "dockermachine.infrastructure.cluster.x-k8s.io": prefer a domain-qualified finalizer name to avoid accidental conflicts with other finalizer writers`,
43+
wantLogged: false,
44+
},
45+
}
46+
for _, tt := range tests {
47+
t.Run(tt.name, func(t *testing.T) {
48+
g := NewWithT(t)
49+
logged := false
50+
h := DefaultHandler(
51+
funcr.New(func(_, _ string) {
52+
logged = true
53+
},
54+
funcr.Options{},
55+
),
56+
)
57+
h.HandleWarningHeader(tt.code, "", tt.message)
58+
g.Expect(logged).To(Equal(tt.wantLogged))
59+
})
60+
}
61+
}

0 commit comments

Comments
 (0)