Skip to content

Commit

Permalink
Merge pull request #210 from werf/suppress-klog
Browse files Browse the repository at this point in the history
[cmd] Suppress klog as in werf
  • Loading branch information
distorhead authored Jun 15, 2021
2 parents 255fd6e + f2a5815 commit 8ebddce
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
63 changes: 58 additions & 5 deletions cmd/kubedog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import (
"strconv"
"time"

"github.com/spf13/cobra"
"k8s.io/klog"
klog_v2 "k8s.io/klog/v2"

"github.com/spf13/cobra"
"github.com/werf/logboek"

"github.com/werf/kubedog"
Expand All @@ -27,9 +28,6 @@ func main() {
// set flag.Parsed() for glog
flag.CommandLine.Parse([]string{})

klog.SetOutputBySeverity("INFO", ioutil.Discard)
klog.SetOutputBySeverity("WARNING", ioutil.Discard)

var namespace string
var timeoutSeconds int
var statusProgressPeriodSeconds int64
Expand Down Expand Up @@ -69,6 +67,16 @@ func main() {
}

init := func() {
if err := SilenceKlog(context.Background()); err != nil {
fmt.Fprintf(os.Stderr, "Unable to initialize klog: %s\n", err)
os.Exit(1)
}

if err := SilenceKlogV2(context.Background()); err != nil {
fmt.Fprintf(os.Stderr, "Unable to initialize klog_v2: %s\n", err)
os.Exit(1)
}

err := kube.Init(kube.InitOptions{kube.KubeConfigOptions{Context: kubeContext, ConfigPath: kubeConfig}})
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to initialize kube: %s\n", err)
Expand Down Expand Up @@ -99,7 +107,6 @@ func main() {

logboek.Context(context.Background()).Streams().SetWidth(terminalWidth)
}

}

rootCmd := &cobra.Command{Use: "kubedog"}
Expand Down Expand Up @@ -317,3 +324,49 @@ func main() {
os.Exit(1)
}
}

func SilenceKlogV2(ctx context.Context) error {
fs := flag.NewFlagSet("klog", flag.PanicOnError)
klog_v2.InitFlags(fs)

if err := fs.Set("logtostderr", "false"); err != nil {
return err
}
if err := fs.Set("alsologtostderr", "false"); err != nil {
return err
}
if err := fs.Set("stderrthreshold", "5"); err != nil {
return err
}

// Suppress info and warnings from client-go reflector
klog_v2.SetOutputBySeverity("INFO", ioutil.Discard)
klog_v2.SetOutputBySeverity("WARNING", ioutil.Discard)
klog_v2.SetOutputBySeverity("ERROR", ioutil.Discard)
klog_v2.SetOutputBySeverity("FATAL", logboek.Context(ctx).ErrStream())

return nil
}

func SilenceKlog(ctx context.Context) error {
fs := flag.NewFlagSet("klog", flag.PanicOnError)
klog.InitFlags(fs)

if err := fs.Set("logtostderr", "false"); err != nil {
return err
}
if err := fs.Set("alsologtostderr", "false"); err != nil {
return err
}
if err := fs.Set("stderrthreshold", "5"); err != nil {
return err
}

// Suppress info and warnings from client-go reflector
klog.SetOutputBySeverity("INFO", ioutil.Discard)
klog.SetOutputBySeverity("WARNING", ioutil.Discard)
klog.SetOutputBySeverity("ERROR", ioutil.Discard)
klog.SetOutputBySeverity("FATAL", logboek.Context(ctx).ErrStream())

return nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
k8s.io/cli-runtime v0.20.2
k8s.io/client-go v0.20.2
k8s.io/klog v1.0.0
k8s.io/klog/v2 v2.4.0 // indirect
sigs.k8s.io/structured-merge-diff/v3 v3.0.0 // indirect
)

Expand Down

0 comments on commit 8ebddce

Please sign in to comment.