Skip to content

Commit a071760

Browse files
author
Xuewei Zhang
committed
Add existing monitors into the problem daemon registration hook.
1 parent 63f0e35 commit a071760

File tree

9 files changed

+435
-32
lines changed

9 files changed

+435
-32
lines changed

cmd/node_problem_detector.go

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ import (
2323
"github.com/spf13/pflag"
2424

2525
"k8s.io/node-problem-detector/cmd/options"
26-
"k8s.io/node-problem-detector/pkg/custompluginmonitor"
2726
"k8s.io/node-problem-detector/pkg/exporters/k8sexporter"
27+
"k8s.io/node-problem-detector/pkg/problemdaemon"
2828
"k8s.io/node-problem-detector/pkg/problemdetector"
29-
"k8s.io/node-problem-detector/pkg/systemlogmonitor"
3029
"k8s.io/node-problem-detector/pkg/types"
3130
"k8s.io/node-problem-detector/pkg/version"
3231
)
@@ -43,26 +42,13 @@ func main() {
4342
}
4443

4544
npdo.SetNodeNameOrDie()
46-
45+
npdo.SetConfigFromDeprecatedOptionsOrDie()
4746
npdo.ValidOrDie()
4847

49-
monitors := make(map[string]types.Monitor)
50-
for _, config := range npdo.SystemLogMonitorConfigPaths {
51-
if _, ok := monitors[config]; ok {
52-
// Skip the config if it's duplicated.
53-
glog.Warningf("Duplicated monitor configuration %q", config)
54-
continue
55-
}
56-
monitors[config] = systemlogmonitor.NewLogMonitorOrDie(config)
57-
}
58-
59-
for _, config := range npdo.CustomPluginMonitorConfigPaths {
60-
if _, ok := monitors[config]; ok {
61-
// Skip the config if it's duplicated.
62-
glog.Warningf("Duplicated monitor configuration %q", config)
63-
continue
64-
}
65-
monitors[config] = custompluginmonitor.NewCustomPluginMonitorOrDie(config)
48+
// Initialize problem daemons.
49+
problemDaemons := problemdaemon.NewProblemDaemons(npdo.MonitorConfigPaths)
50+
if len(problemDaemons) == 0 {
51+
glog.Fatalf("No problem daemon is configured")
6652
}
6753

6854
// Initialize exporters.
@@ -76,7 +62,7 @@ func main() {
7662
}
7763

7864
// Initialize NPD core.
79-
p := problemdetector.NewProblemDetector(monitors, exporters)
65+
p := problemdetector.NewProblemDetector(problemDaemons, exporters)
8066
if err := p.Run(); err != nil {
8167
glog.Fatalf("Problem detector failed with error: %v", err)
8268
}

cmd/options/options.go

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import (
2525

2626
"github.com/spf13/pflag"
2727

28+
"k8s.io/node-problem-detector/pkg/custompluginmonitor"
2829
"k8s.io/node-problem-detector/pkg/problemdaemon"
30+
"k8s.io/node-problem-detector/pkg/systemlogmonitor"
2931
"k8s.io/node-problem-detector/pkg/types"
3032
)
3133

@@ -54,9 +56,13 @@ type NodeProblemDetectorOptions struct {
5456

5557
// SystemLogMonitorConfigPaths specifies the list of paths to system log monitor configuration
5658
// files.
59+
// SystemLogMonitorConfigPaths is used by the deprecated option --system-log-monitors. The new
60+
// option --config.system-log-monitor will stored the config file paths in MonitorConfigPaths.
5761
SystemLogMonitorConfigPaths []string
5862
// CustomPluginMonitorConfigPaths specifies the list of paths to custom plugin monitor configuration
5963
// files.
64+
// CustomPluginMonitorConfigPaths is used by the deprecated option --custom-plugin-monitors. The
65+
// new option --config.custom-plugin-monitor will stored the config file paths in MonitorConfigPaths.
6066
CustomPluginMonitorConfigPaths []string
6167
// MonitorConfigPaths specifies the list of paths to configuration files for each monitor.
6268
MonitorConfigPaths types.ProblemDaemonConfigPathMap
@@ -75,8 +81,10 @@ func NewNodeProblemDetectorOptions() *NodeProblemDetectorOptions {
7581
func (npdo *NodeProblemDetectorOptions) AddFlags(fs *pflag.FlagSet) {
7682
fs.StringSliceVar(&npdo.SystemLogMonitorConfigPaths, "system-log-monitors",
7783
[]string{}, "List of paths to system log monitor config files, comma separated.")
84+
fs.MarkDeprecated("system-log-monitors", "replaced by --config.system-log-monitor. NPD will panic if both --system-log-monitors and --config.system-log-monitor are set.")
7885
fs.StringSliceVar(&npdo.CustomPluginMonitorConfigPaths, "custom-plugin-monitors",
7986
[]string{}, "List of paths to custom plugin monitor config files, comma separated.")
87+
fs.MarkDeprecated("custom-plugin-monitors", "replaced by --config.custom-plugin-monitor. NPD will panic if both --custom-plugin-monitors and --config.custom-plugin-monitor are set.")
8088
fs.BoolVar(&npdo.EnableK8sExporter, "enable-k8s-exporter", true, "Enables reporting to Kubernetes API server.")
8189
fs.StringVar(&npdo.ApiServerOverride, "apiserver-override",
8290
"", "Custom URI used to connect to Kubernetes ApiServer. This is ignored if --enable-k8s-exporter is false.")
@@ -106,14 +114,53 @@ func (npdo *NodeProblemDetectorOptions) ValidOrDie() {
106114
panic(fmt.Sprintf("apiserver-override %q is not a valid HTTP URI: %v",
107115
npdo.ApiServerOverride, err))
108116
}
109-
if len(npdo.SystemLogMonitorConfigPaths) == 0 && len(npdo.CustomPluginMonitorConfigPaths) == 0 {
110-
panic(fmt.Sprintf("Either --system-log-monitors or --custom-plugin-monitors is required"))
117+
118+
if len(npdo.SystemLogMonitorConfigPaths) != 0 {
119+
panic("SystemLogMonitorConfigPaths is deprecated. It should have been reassigned to MonitorConfigPaths. This should not happen.")
120+
}
121+
if len(npdo.CustomPluginMonitorConfigPaths) != 0 {
122+
panic("CustomPluginMonitorConfigPaths is deprecated. It should have been reassigned to MonitorConfigPaths. This should not happen.")
123+
}
124+
125+
configCount := 0
126+
for _, problemDaemonConfigPaths := range npdo.MonitorConfigPaths {
127+
configCount += len(*problemDaemonConfigPaths)
111128
}
129+
if configCount == 0 {
130+
panic("No configuration option for any problem daemon is specified.")
131+
}
132+
}
133+
134+
// SetConfigFromDeprecatedOptionsOrDie sets NPD option using deprecated options.
135+
func (npdo *NodeProblemDetectorOptions) SetConfigFromDeprecatedOptionsOrDie() {
136+
if len(npdo.SystemLogMonitorConfigPaths) != 0 {
137+
if npdo.MonitorConfigPaths[systemlogmonitor.SystemLogMonitorName] == nil {
138+
npdo.MonitorConfigPaths[systemlogmonitor.SystemLogMonitorName] = &[]string{}
139+
}
112140

113-
for problemDaemonName, configs := range npdo.MonitorConfigPaths {
114-
if configs == nil {
115-
panic(fmt.Sprintf("nil config for problem daemon %q. This should never happen, might indicates bug in pflag.", problemDaemonName))
141+
if len(*npdo.MonitorConfigPaths[systemlogmonitor.SystemLogMonitorName]) != 0 {
142+
panic("Option --system-log-monitors is deprecated in favor of --config.system-log-monitor. They cannot be set at the same time.")
116143
}
144+
145+
*npdo.MonitorConfigPaths[systemlogmonitor.SystemLogMonitorName] = append(
146+
*npdo.MonitorConfigPaths[systemlogmonitor.SystemLogMonitorName],
147+
npdo.SystemLogMonitorConfigPaths...)
148+
npdo.SystemLogMonitorConfigPaths = []string{}
149+
}
150+
151+
if len(npdo.CustomPluginMonitorConfigPaths) != 0 {
152+
if npdo.MonitorConfigPaths[custompluginmonitor.CustomPluginMonitorName] == nil {
153+
npdo.MonitorConfigPaths[custompluginmonitor.CustomPluginMonitorName] = &[]string{}
154+
}
155+
156+
if len(*npdo.MonitorConfigPaths[custompluginmonitor.CustomPluginMonitorName]) != 0 {
157+
panic("Option --custom-plugin-monitors is deprecated in favor of --config.custom-plugin-monitor. They cannot be set at the same time.")
158+
}
159+
160+
*npdo.MonitorConfigPaths[custompluginmonitor.CustomPluginMonitorName] = append(
161+
*npdo.MonitorConfigPaths[custompluginmonitor.CustomPluginMonitorName],
162+
npdo.CustomPluginMonitorConfigPaths...)
163+
npdo.CustomPluginMonitorConfigPaths = []string{}
117164
}
118165
}
119166

0 commit comments

Comments
 (0)