Skip to content

Commit 919e064

Browse files
Merge pull request GoogleCloudPlatform#1241 from justinsb/filter_logs
tests: Filter log messages
2 parents f7113d8 + 5913a99 commit 919e064

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

config/tests/samples/create/harness.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"testing"
2525
"time"
2626

27+
"github.com/go-logr/logr"
2728
"github.com/google/go-cmp/cmp"
2829
transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport"
2930
"golang.org/x/oauth2"
@@ -33,6 +34,7 @@ import (
3334
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3435
"k8s.io/apimachinery/pkg/runtime/schema"
3536
"k8s.io/apimachinery/pkg/types"
37+
"k8s.io/apimachinery/pkg/util/sets"
3638
"k8s.io/apimachinery/pkg/util/wait"
3739
"k8s.io/client-go/rest"
3840
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -346,6 +348,8 @@ func NewHarness(ctx context.Context, t *testing.T) *Harness {
346348
ctrlManagerShutdown.Wait()
347349
t.Log("controller-runtime manager is shutdown")
348350
})
351+
kccConfig.ManagerOptions.Logger = filterLogs(log)
352+
349353
mgr, err := kccmanager.New(mgrContext, h.restConfig, kccConfig)
350354
if err != nil {
351355
t.Fatalf("error creating new manager: %v", err)
@@ -549,3 +553,53 @@ func (h *Harness) ReplaceString(from, to string) func(string) string {
549553
return strings.ReplaceAll(s, from, to)
550554
}
551555
}
556+
557+
func filterLogs(log logr.Logger) logr.Logger {
558+
f := &filterSink{sink: log.GetSink()}
559+
f.IgnoreMessages = sets.New[string]()
560+
f.IgnoreMessages.Insert("Registered controller")
561+
f.IgnoreMessages.Insert("Starting Controller")
562+
f.IgnoreMessages.Insert("Starting EventSource")
563+
f.IgnoreMessages.Insert("Starting workers")
564+
f.IgnoreMessages.Insert("Shutdown signal received, waiting for all workers to finish")
565+
f.IgnoreMessages.Insert("All workers finished")
566+
return log.WithSink(f)
567+
}
568+
569+
type filterSink struct {
570+
IgnoreMessages sets.Set[string]
571+
sink logr.LogSink
572+
}
573+
574+
// Init implements logr.LogSink
575+
func (s *filterSink) Init(info logr.RuntimeInfo) {
576+
s.sink.Init(info)
577+
}
578+
579+
// Enabled implements logr.LogSink
580+
func (s *filterSink) Enabled(level int) bool {
581+
return s.sink.Enabled(level)
582+
}
583+
584+
// Info implements logr.LogSink
585+
func (s *filterSink) Info(level int, msg string, args ...any) {
586+
if s.IgnoreMessages.Has(msg) {
587+
return
588+
}
589+
s.sink.Info(level, msg, args...)
590+
}
591+
592+
// WithValues implements logr.LogSink
593+
func (s *filterSink) WithValues(keysAndValues ...any) logr.LogSink {
594+
return &filterSink{IgnoreMessages: s.IgnoreMessages, sink: s.sink.WithValues(keysAndValues...)}
595+
}
596+
597+
// WithName implements logr.LogSink
598+
func (s *filterSink) WithName(name string) logr.LogSink {
599+
return &filterSink{IgnoreMessages: s.IgnoreMessages, sink: s.sink.WithName(name)}
600+
}
601+
602+
// Error implements logr.LogSink
603+
func (s *filterSink) Error(err error, msg string, args ...any) {
604+
s.sink.Error(err, msg, args...)
605+
}

0 commit comments

Comments
 (0)