Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ util: Warning handler that discards messages that match a regular expression #11179

Merged
merged 4 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions util/apiwarnings/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package apiwarnings defines warning handlers used with API clients.
package apiwarnings
51 changes: 51 additions & 0 deletions util/apiwarnings/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package apiwarnings

import (
"regexp"

"github.com/go-logr/logr"
)

// DiscardMatchingHandler is a handler that discards API server warnings
// whose message matches any user-defined regular expressions, but otherwise
// logs warnings to the user-defined logger.
type DiscardMatchingHandler struct {
// Logger is used to log responses with the warning header.
Logger logr.Logger

// Expressions is a slice of regular expressions used to discard warnings.
// If the warning message matches any expression, it is not logged.
Expressions []regexp.Regexp
}

// HandleWarningHeader handles logging for responses from API server that are
// warnings with code being 299 and uses a logr.Logger for its logging purposes.
func (h *DiscardMatchingHandler) HandleWarningHeader(code int, _, message string) {
if code != 299 || message == "" {
return
}

for _, exp := range h.Expressions {
if exp.MatchString(message) {
return
}
}

h.Logger.Info(message)
}
91 changes: 91 additions & 0 deletions util/apiwarnings/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
Copyright 2024 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package apiwarnings

import (
"regexp"
"testing"

"github.com/go-logr/logr/funcr"
. "github.com/onsi/gomega"
)

func TestDiscardMatchingHandler(t *testing.T) {
tests := []struct {
name string
code int
message string
expressions []regexp.Regexp
wantLogged bool
}{
{
name: "log, if no expressions are defined",
code: 299,
message: "non-matching warning",
wantLogged: true,
},
{
name: "log, if warning does not match any expression",
code: 299,
message: "non-matching warning",
expressions: []regexp.Regexp{},
wantLogged: true,
},
{
name: "do not log, if warning matches at least one expression",
code: 299,
message: "matching warning",
expressions: []regexp.Regexp{
*regexp.MustCompile("^matching.*"),
},
wantLogged: false,
},
{
name: "do not log, if code is not 299",
sbueringer marked this conversation as resolved.
Show resolved Hide resolved
code: 0,
message: "",
expressions: []regexp.Regexp{},
wantLogged: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
logged := false
h := DiscardMatchingHandler{
Logger: funcr.New(func(_, _ string) {
logged = true
},
funcr.Options{},
),
Expressions: tt.expressions,
}
h.HandleWarningHeader(tt.code, "", tt.message)
g.Expect(logged).To(Equal(tt.wantLogged))
})
}
}

func TestDiscardMatchingHandler_NilLogger(t *testing.T) {
g := NewWithT(t)
h := DiscardMatchingHandler{
// Logger is nil
}
g.Expect(func() {
h.HandleWarningHeader(0, "", "")
}).ToNot(Panic())
}