Skip to content

Commit fb49856

Browse files
authored
Merge pull request #815 from hakman/klogv2
Move glog/klog logging to klog/v2
2 parents 76bf7b7 + 5210373 commit fb49856

File tree

70 files changed

+293
-4390
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+293
-4390
lines changed

cmd/healthchecker/health_checker.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,24 @@ import (
2323

2424
"github.com/spf13/pflag"
2525

26+
"k8s.io/klog/v2"
2627
"k8s.io/node-problem-detector/cmd/healthchecker/options"
2728
"k8s.io/node-problem-detector/pkg/custompluginmonitor/types"
2829
"k8s.io/node-problem-detector/pkg/healthchecker"
2930
)
3031

3132
func main() {
32-
// Set glog flag so that it does not log to files.
33-
if err := flag.Set("logtostderr", "true"); err != nil {
34-
fmt.Printf("Failed to set logtostderr=true: %v", err)
35-
os.Exit(int(types.Unknown))
36-
}
33+
klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
34+
klog.InitFlags(klogFlags)
35+
klogFlags.VisitAll(func(f *flag.Flag) {
36+
switch f.Name {
37+
case "v", "vmodule", "logtostderr":
38+
flag.CommandLine.Var(f.Value, f.Name, f.Usage)
39+
}
40+
})
41+
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
42+
pflag.CommandLine.MarkHidden("vmodule")
43+
pflag.CommandLine.MarkHidden("logtostderr")
3744

3845
hco := options.NewHealthCheckerOptions()
3946
hco.AddFlags(pflag.CommandLine)

cmd/logcounter/log_counter.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,24 @@ import (
2626

2727
"github.com/spf13/pflag"
2828

29+
"k8s.io/klog/v2"
2930
"k8s.io/node-problem-detector/cmd/logcounter/options"
3031
"k8s.io/node-problem-detector/pkg/custompluginmonitor/types"
3132
"k8s.io/node-problem-detector/pkg/logcounter"
3233
)
3334

3435
func main() {
35-
// Set glog flag so that it does not log to files.
36-
if err := flag.Set("logtostderr", "true"); err != nil {
37-
fmt.Printf("Failed to set logtostderr=true: %v", err)
38-
os.Exit(int(types.Unknown))
39-
}
36+
klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
37+
klog.InitFlags(klogFlags)
38+
klogFlags.VisitAll(func(f *flag.Flag) {
39+
switch f.Name {
40+
case "v", "vmodule", "logtostderr":
41+
flag.CommandLine.Var(f.Value, f.Name, f.Usage)
42+
}
43+
})
44+
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
45+
pflag.CommandLine.MarkHidden("vmodule")
46+
pflag.CommandLine.MarkHidden("logtostderr")
4047

4148
fedo := options.NewLogCounterOptions()
4249
fedo.AddFlags(pflag.CommandLine)

cmd/nodeproblemdetector/node_problem_detector.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package main
1919
import (
2020
"context"
2121

22-
"github.com/golang/glog"
22+
"k8s.io/klog/v2"
2323

2424
_ "k8s.io/node-problem-detector/cmd/nodeproblemdetector/exporterplugins"
2525
_ "k8s.io/node-problem-detector/cmd/nodeproblemdetector/problemdaemonplugins"
@@ -46,18 +46,18 @@ func npdMain(ctx context.Context, npdo *options.NodeProblemDetectorOptions) erro
4646
// Initialize problem daemons.
4747
problemDaemons := problemdaemon.NewProblemDaemons(npdo.MonitorConfigPaths)
4848
if len(problemDaemons) == 0 {
49-
glog.Fatalf("No problem daemon is configured")
49+
klog.Fatalf("No problem daemon is configured")
5050
}
5151

5252
// Initialize exporters.
5353
defaultExporters := []types.Exporter{}
5454
if ke := k8sexporter.NewExporterOrDie(ctx, npdo); ke != nil {
5555
defaultExporters = append(defaultExporters, ke)
56-
glog.Info("K8s exporter started.")
56+
klog.Info("K8s exporter started.")
5757
}
5858
if pe := prometheusexporter.NewExporterOrDie(npdo); pe != nil {
5959
defaultExporters = append(defaultExporters, pe)
60-
glog.Info("Prometheus exporter started.")
60+
klog.Info("Prometheus exporter started.")
6161
}
6262

6363
plugableExporters := exporters.NewExporters()
@@ -67,7 +67,7 @@ func npdMain(ctx context.Context, npdo *options.NodeProblemDetectorOptions) erro
6767
npdExporters = append(npdExporters, plugableExporters...)
6868

6969
if len(npdExporters) == 0 {
70-
glog.Fatalf("No exporter is successfully setup")
70+
klog.Fatalf("No exporter is successfully setup")
7171
}
7272

7373
// Initialize NPD core.

cmd/nodeproblemdetector/node_problem_detector_linux.go renamed to cmd/nodeproblemdetector/node_problem_detector_unix.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
//go:build unix
2+
13
/*
24
Copyright 2021 The Kubernetes Authors All rights reserved.
35
@@ -18,18 +20,31 @@ package main
1820

1921
import (
2022
"context"
23+
"flag"
2124

22-
"github.com/golang/glog"
2325
"github.com/spf13/pflag"
26+
"k8s.io/klog/v2"
2427
"k8s.io/node-problem-detector/cmd/options"
2528
)
2629

2730
func main() {
31+
klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
32+
klog.InitFlags(klogFlags)
33+
klogFlags.VisitAll(func(f *flag.Flag) {
34+
switch f.Name {
35+
case "v", "vmodule", "logtostderr":
36+
flag.CommandLine.Var(f.Value, f.Name, f.Usage)
37+
}
38+
})
39+
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
40+
pflag.CommandLine.MarkHidden("vmodule")
41+
pflag.CommandLine.MarkHidden("logtostderr")
42+
2843
npdo := options.NewNodeProblemDetectorOptions()
2944
npdo.AddFlags(pflag.CommandLine)
3045

3146
pflag.Parse()
3247
if err := npdMain(context.Background(), npdo); err != nil {
33-
glog.Fatalf("Problem detector failed with error: %v", err)
48+
klog.Fatalf("Problem detector failed with error: %v", err)
3449
}
3550
}

cmd/nodeproblemdetector/node_problem_detector_windows.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ package main
1818

1919
import (
2020
"context"
21+
"flag"
2122
"fmt"
2223
"sync"
2324
"time"
2425

25-
"github.com/golang/glog"
2626
"github.com/spf13/pflag"
2727
"golang.org/x/sys/windows/svc"
2828
"golang.org/x/sys/windows/svc/debug"
2929
"golang.org/x/sys/windows/svc/eventlog"
30+
"k8s.io/klog/v2"
3031
"k8s.io/node-problem-detector/cmd/options"
3132
)
3233

@@ -43,6 +44,18 @@ var (
4344
)
4445

4546
func main() {
47+
klogFlags := flag.NewFlagSet("klog", flag.ExitOnError)
48+
klog.InitFlags(klogFlags)
49+
klogFlags.VisitAll(func(f *flag.Flag) {
50+
switch f.Name {
51+
case "v", "vmodule", "logtostderr":
52+
flag.CommandLine.Var(f.Value, f.Name, f.Usage)
53+
}
54+
})
55+
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
56+
pflag.CommandLine.MarkHidden("vmodule")
57+
pflag.CommandLine.MarkHidden("logtostderr")
58+
4659
npdo := options.NewNodeProblemDetectorOptions()
4760
npdo.AddFlags(pflag.CommandLine)
4861

@@ -62,7 +75,7 @@ func main() {
6275
func isRunningAsWindowsService() bool {
6376
runningAsService, err := svc.IsWindowsService()
6477
if err != nil {
65-
glog.Errorf("cannot determine if running as Windows Service assuming standalone, %v", err)
78+
klog.Errorf("cannot determine if running as Windows Service assuming standalone, %v", err)
6679
return false
6780
}
6881
return runningAsService

cmd/nodeproblemdetector/node_problem_detector_windows_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ limitations under the License.
2020
package main
2121

2222
import (
23-
"context"
2423
"testing"
2524

2625
"golang.org/x/sys/windows/svc"

go.mod

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ require (
1111
github.com/avast/retry-go v3.0.0+incompatible
1212
github.com/coreos/go-systemd v0.0.0-20191104093116-d3cd4ed1dbcf
1313
github.com/euank/go-kmsg-parser v2.0.0+incompatible
14-
github.com/golang/glog v1.1.2
1514
github.com/hpcloud/tail v1.0.0
1615
github.com/prometheus/client_model v0.4.0
1716
github.com/prometheus/common v0.44.0
@@ -25,7 +24,7 @@ require (
2524
k8s.io/api v0.28.2
2625
k8s.io/apimachinery v0.28.2
2726
k8s.io/client-go v0.28.2
28-
k8s.io/klog v1.0.0
27+
k8s.io/klog/v2 v2.100.1
2928
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
3029
)
3130

@@ -96,7 +95,6 @@ require (
9695
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
9796
gopkg.in/yaml.v2 v2.4.0 // indirect
9897
gopkg.in/yaml.v3 v3.0.1 // indirect
99-
k8s.io/klog/v2 v2.100.1 // indirect
10098
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
10199
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
102100
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect

go.sum

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,8 +524,6 @@ github.com/golang-jwt/jwt/v4 v4.0.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzw
524524
github.com/golang-jwt/jwt/v4 v4.2.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
525525
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
526526
github.com/golang/glog v1.0.0/go.mod h1:EWib/APOK0SL3dFbYqvxE3UYd8E6s1ouQ7iEp/0LWV4=
527-
github.com/golang/glog v1.1.2 h1:DVjP2PbBOzHyzA+dn3WhHIq4NdVu3Q+pvivFICf/7fo=
528-
github.com/golang/glog v1.1.2/go.mod h1:zR+okUeTbrL6EL3xHUDxZuEtGv04p5shwip1+mL/rLQ=
529527
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
530528
github.com/golang/groupcache v0.0.0-20190129154638-5b532d6fd5ef/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
531529
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
@@ -1871,7 +1869,6 @@ k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8
18711869
k8s.io/gengo v0.0.0-20200428234225-8167cfdcfc14/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0=
18721870
k8s.io/gengo v0.0.0-20201113003025-83324d819ded/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
18731871
k8s.io/gengo v0.0.0-20210813121822-485abfe95c7c/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E=
1874-
k8s.io/klog v1.0.0 h1:Pt+yjF5aB1xDSVbau4VsWe+dQNzA0qv1LlXdC2dF6Q8=
18751872
k8s.io/klog v1.0.0/go.mod h1:4Bi6QPql/J/LkTDqv7R/cd3hPo4k2DG6Ptcz060Ez5I=
18761873
k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE=
18771874
k8s.io/klog/v2 v2.2.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y=

pkg/custompluginmonitor/custom_plugin_monitor.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"os"
2222
"time"
2323

24-
"github.com/golang/glog"
24+
"k8s.io/klog/v2"
2525

2626
"k8s.io/node-problem-detector/pkg/custompluginmonitor/plugin"
2727
cpmtypes "k8s.io/node-problem-detector/pkg/custompluginmonitor/types"
@@ -59,25 +59,25 @@ func NewCustomPluginMonitorOrDie(configPath string) types.Monitor {
5959
}
6060
f, err := os.ReadFile(configPath)
6161
if err != nil {
62-
glog.Fatalf("Failed to read configuration file %q: %v", configPath, err)
62+
klog.Fatalf("Failed to read configuration file %q: %v", configPath, err)
6363
}
6464
err = json.Unmarshal(f, &c.config)
6565
if err != nil {
66-
glog.Fatalf("Failed to unmarshal configuration file %q: %v", configPath, err)
66+
klog.Fatalf("Failed to unmarshal configuration file %q: %v", configPath, err)
6767
}
6868
// Apply configurations
6969
err = (&c.config).ApplyConfiguration()
7070
if err != nil {
71-
glog.Fatalf("Failed to apply configuration for %q: %v", configPath, err)
71+
klog.Fatalf("Failed to apply configuration for %q: %v", configPath, err)
7272
}
7373

7474
// Validate configurations
7575
err = c.config.Validate()
7676
if err != nil {
77-
glog.Fatalf("Failed to validate custom plugin config %+v: %v", c.config, err)
77+
klog.Fatalf("Failed to validate custom plugin config %+v: %v", c.config, err)
7878
}
7979

80-
glog.Infof("Finish parsing custom plugin monitor config file %s: %+v", c.configPath, c.config)
80+
klog.Infof("Finish parsing custom plugin monitor config file %s: %+v", c.configPath, c.config)
8181

8282
c.plugin = plugin.NewPlugin(c.config)
8383
// A 1000 size channel should be big enough.
@@ -96,26 +96,26 @@ func initializeProblemMetricsOrDie(rules []*cpmtypes.CustomRule) {
9696
if rule.Type == types.Perm {
9797
err := problemmetrics.GlobalProblemMetricsManager.SetProblemGauge(rule.Condition, rule.Reason, false)
9898
if err != nil {
99-
glog.Fatalf("Failed to initialize problem gauge metrics for problem %q, reason %q: %v",
99+
klog.Fatalf("Failed to initialize problem gauge metrics for problem %q, reason %q: %v",
100100
rule.Condition, rule.Reason, err)
101101
}
102102
}
103103
err := problemmetrics.GlobalProblemMetricsManager.IncrementProblemCounter(rule.Reason, 0)
104104
if err != nil {
105-
glog.Fatalf("Failed to initialize problem counter metrics for %q: %v", rule.Reason, err)
105+
klog.Fatalf("Failed to initialize problem counter metrics for %q: %v", rule.Reason, err)
106106
}
107107
}
108108
}
109109

110110
func (c *customPluginMonitor) Start() (<-chan *types.Status, error) {
111-
glog.Infof("Start custom plugin monitor %s", c.configPath)
111+
klog.Infof("Start custom plugin monitor %s", c.configPath)
112112
go c.plugin.Run()
113113
go c.monitorLoop()
114114
return c.statusChan, nil
115115
}
116116

117117
func (c *customPluginMonitor) Stop() {
118-
glog.Infof("Stop custom plugin monitor %s", c.configPath)
118+
klog.Infof("Stop custom plugin monitor %s", c.configPath)
119119
c.tomb.Stop()
120120
}
121121

@@ -133,16 +133,16 @@ func (c *customPluginMonitor) monitorLoop() {
133133
select {
134134
case result, ok := <-resultChan:
135135
if !ok {
136-
glog.Errorf("Result channel closed: %s", c.configPath)
136+
klog.Errorf("Result channel closed: %s", c.configPath)
137137
return
138138
}
139-
glog.V(3).Infof("Receive new plugin result for %s: %+v", c.configPath, result)
139+
klog.V(3).Infof("Receive new plugin result for %s: %+v", c.configPath, result)
140140
status := c.generateStatus(result)
141-
glog.V(3).Infof("New status generated: %+v", status)
141+
klog.V(3).Infof("New status generated: %+v", status)
142142
c.statusChan <- status
143143
case <-c.tomb.Stopping():
144144
c.plugin.Stop()
145-
glog.Infof("Custom plugin monitor stopped: %s", c.configPath)
145+
klog.Infof("Custom plugin monitor stopped: %s", c.configPath)
146146
c.tomb.Done()
147147
return
148148
}
@@ -256,15 +256,15 @@ func (c *customPluginMonitor) generateStatus(result cpmtypes.Result) *types.Stat
256256
err := problemmetrics.GlobalProblemMetricsManager.IncrementProblemCounter(
257257
event.Reason, 1)
258258
if err != nil {
259-
glog.Errorf("Failed to update problem counter metrics for %q: %v",
259+
klog.Errorf("Failed to update problem counter metrics for %q: %v",
260260
event.Reason, err)
261261
}
262262
}
263263
for _, condition := range c.conditions {
264264
err := problemmetrics.GlobalProblemMetricsManager.SetProblemGauge(
265265
condition.Type, condition.Reason, condition.Status == types.True)
266266
if err != nil {
267-
glog.Errorf("Failed to update problem gauge metrics for problem %q, reason %q: %v",
267+
klog.Errorf("Failed to update problem gauge metrics for problem %q, reason %q: %v",
268268
condition.Type, condition.Reason, err)
269269
}
270270
}
@@ -277,7 +277,7 @@ func (c *customPluginMonitor) generateStatus(result cpmtypes.Result) *types.Stat
277277
}
278278
// Log only if condition has changed
279279
if len(activeProblemEvents) != 0 || len(inactiveProblemEvents) != 0 {
280-
glog.V(0).Infof("New status generated: %+v", status)
280+
klog.V(0).Infof("New status generated: %+v", status)
281281
}
282282
return status
283283
}
@@ -297,7 +297,7 @@ func toConditionStatus(s cpmtypes.Status) types.ConditionStatus {
297297
func (c *customPluginMonitor) initializeStatus() {
298298
// Initialize the default node conditions
299299
c.conditions = initialConditions(c.config.DefaultConditions)
300-
glog.Infof("Initialize condition generated: %+v", c.conditions)
300+
klog.Infof("Initialize condition generated: %+v", c.conditions)
301301
// Update the initial status
302302
c.statusChan <- &types.Status{
303303
Source: c.config.Source,

0 commit comments

Comments
 (0)