Skip to content

Commit

Permalink
testenv: Allow klog and glog to coexist
Browse files Browse the repository at this point in the history
This change allows test environments with glog to work with testenv.

Signed-off-by: Sunny <darkowlzz@protonmail.com>
  • Loading branch information
darkowlzz committed Sep 16, 2021
1 parent d726dea commit 2576e9e
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion runtime/testenv/testenv.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ package testenv

import (
"context"
"flag"
"fmt"
"sync"
"time"
Expand Down Expand Up @@ -53,7 +54,19 @@ var (
)

func init() {
klog.InitFlags(nil)
// NOTE: The following enables glog and klog to coexist. If a test depends
// on a package that uses glog, klog flag initialization here would result
// in redeclaration of some flags. The following creates a new FlagSet and
// uses it to initialize klog.
// Refer: https://github.com/kubernetes/klog/blob/f8e668dbaa5f6f0e6a5c24ffd7667263840d79ae/examples/coexist_glog/coexist_glog.go
// The klog flags set belog are not available to the klogr logger below
// because they aren't set on the global flagset. To prevent conflicting
// with any flags registered later during the test setup, the flags are left
// empty. In the tests, a custom logger can be created with all the
// necessary flags and assigned to the logger explicitly.
klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
klog.InitFlags(klogFlags)

logger := klogr.New()
log.SetLogger(logger)
ctrl.SetLogger(logger)
Expand Down

0 comments on commit 2576e9e

Please sign in to comment.