diff --git a/Makefile b/Makefile index f28bbdaf5..ca3f65e5d 100644 --- a/Makefile +++ b/Makefile @@ -95,7 +95,7 @@ version: -o bin/node-problem-detector \ -ldflags '-X $(PKG)/pkg/version.version=$(VERSION)' \ $(BUILD_TAGS) \ - cmd/node_problem_detector.go + ./cmd/node-problem-detector Dockerfile: Dockerfile.in sed -e 's|@BASEIMAGE@|$(BASEIMAGE)|g' $< >$@ diff --git a/cmd/node_problem_detector.go b/cmd/node-problem-detector/node_problem_detector.go similarity index 100% rename from cmd/node_problem_detector.go rename to cmd/node-problem-detector/node_problem_detector.go diff --git a/cmd/plugins.go b/cmd/node-problem-detector/plugins.go similarity index 100% rename from cmd/plugins.go rename to cmd/node-problem-detector/plugins.go diff --git a/cmd/options/options.go b/cmd/options/options.go index 49062dc9e..c7786ce28 100644 --- a/cmd/options/options.go +++ b/cmd/options/options.go @@ -25,9 +25,7 @@ import ( "github.com/spf13/pflag" - "k8s.io/node-problem-detector/pkg/custompluginmonitor" "k8s.io/node-problem-detector/pkg/problemdaemon" - "k8s.io/node-problem-detector/pkg/systemlogmonitor" "k8s.io/node-problem-detector/pkg/types" ) @@ -80,7 +78,11 @@ type NodeProblemDetectorOptions struct { } func NewNodeProblemDetectorOptions() *NodeProblemDetectorOptions { - return &NodeProblemDetectorOptions{MonitorConfigPaths: types.ProblemDaemonConfigPathMap{}} + npdo := &NodeProblemDetectorOptions{MonitorConfigPaths: types.ProblemDaemonConfigPathMap{}} + for _, problemDaemonName := range problemdaemon.GetProblemDaemonNames() { + npdo.MonitorConfigPaths[problemDaemonName] = &[]string{} + } + return npdo } // AddFlags adds node problem detector command line options to pflag. @@ -108,7 +110,6 @@ func (npdo *NodeProblemDetectorOptions) AddFlags(fs *pflag.FlagSet) { "127.0.0.1", "The address to bind the Prometheus scrape endpoint.") for _, problemDaemonName := range problemdaemon.GetProblemDaemonNames() { - npdo.MonitorConfigPaths[problemDaemonName] = &[]string{} fs.StringSliceVar( npdo.MonitorConfigPaths[problemDaemonName], "config."+string(problemDaemonName), @@ -142,34 +143,48 @@ func (npdo *NodeProblemDetectorOptions) ValidOrDie() { } } +// Plugin names for custom plugin monitor and system log monitor. +// Hard code them here to: +// 1) Handle deprecated flags for --system-log-monitors and --custom-plugin-monitors. +// 2) Avoid direct dependencies to packages in those plugins, so that those plugins +// can be disabled at compile time. +const ( + customPluginMonitorName = "custom-plugin-monitor" + systemLogMonitorName = "system-log-monitor" +) + // SetConfigFromDeprecatedOptionsOrDie sets NPD option using deprecated options. func (npdo *NodeProblemDetectorOptions) SetConfigFromDeprecatedOptionsOrDie() { if len(npdo.SystemLogMonitorConfigPaths) != 0 { - if npdo.MonitorConfigPaths[systemlogmonitor.SystemLogMonitorName] == nil { - npdo.MonitorConfigPaths[systemlogmonitor.SystemLogMonitorName] = &[]string{} + if npdo.MonitorConfigPaths[systemLogMonitorName] == nil { + // As long as the problem daemon is registered, MonitorConfigPaths should + // not be nil. + panic("System log monitor is not supported") } - if len(*npdo.MonitorConfigPaths[systemlogmonitor.SystemLogMonitorName]) != 0 { + if len(*npdo.MonitorConfigPaths[systemLogMonitorName]) != 0 { panic("Option --system-log-monitors is deprecated in favor of --config.system-log-monitor. They cannot be set at the same time.") } - *npdo.MonitorConfigPaths[systemlogmonitor.SystemLogMonitorName] = append( - *npdo.MonitorConfigPaths[systemlogmonitor.SystemLogMonitorName], + *npdo.MonitorConfigPaths[systemLogMonitorName] = append( + *npdo.MonitorConfigPaths[systemLogMonitorName], npdo.SystemLogMonitorConfigPaths...) npdo.SystemLogMonitorConfigPaths = []string{} } if len(npdo.CustomPluginMonitorConfigPaths) != 0 { - if npdo.MonitorConfigPaths[custompluginmonitor.CustomPluginMonitorName] == nil { - npdo.MonitorConfigPaths[custompluginmonitor.CustomPluginMonitorName] = &[]string{} + if npdo.MonitorConfigPaths[customPluginMonitorName] == nil { + // As long as the problem daemon is registered, MonitorConfigPaths should + // not be nil. + panic("Custom plugin monitor is not supported") } - if len(*npdo.MonitorConfigPaths[custompluginmonitor.CustomPluginMonitorName]) != 0 { + if len(*npdo.MonitorConfigPaths[customPluginMonitorName]) != 0 { panic("Option --custom-plugin-monitors is deprecated in favor of --config.custom-plugin-monitor. They cannot be set at the same time.") } - *npdo.MonitorConfigPaths[custompluginmonitor.CustomPluginMonitorName] = append( - *npdo.MonitorConfigPaths[custompluginmonitor.CustomPluginMonitorName], + *npdo.MonitorConfigPaths[customPluginMonitorName] = append( + *npdo.MonitorConfigPaths[customPluginMonitorName], npdo.CustomPluginMonitorConfigPaths...) npdo.CustomPluginMonitorConfigPaths = []string{} } diff --git a/cmd/options/options_test.go b/cmd/options/options_test.go index 67178b94e..05902db7e 100644 --- a/cmd/options/options_test.go +++ b/cmd/options/options_test.go @@ -23,8 +23,6 @@ import ( "github.com/stretchr/testify/assert" - "k8s.io/node-problem-detector/pkg/custompluginmonitor" - "k8s.io/node-problem-detector/pkg/systemlogmonitor" "k8s.io/node-problem-detector/pkg/types" ) @@ -255,15 +253,15 @@ func TestSetConfigFromDeprecatedOptionsOrDie(t *testing.T) { name: "no deprecated options", orig: NodeProblemDetectorOptions{ MonitorConfigPaths: types.ProblemDaemonConfigPathMap{ - systemlogmonitor.SystemLogMonitorName: &[]string{"config-a", "config-b"}, - custompluginmonitor.CustomPluginMonitorName: &[]string{"config-c", "config-d"}, + systemLogMonitorName: &[]string{"config-a", "config-b"}, + customPluginMonitorName: &[]string{"config-c", "config-d"}, }, }, expectPanic: false, wanted: NodeProblemDetectorOptions{ MonitorConfigPaths: types.ProblemDaemonConfigPathMap{ - systemlogmonitor.SystemLogMonitorName: &[]string{"config-a", "config-b"}, - custompluginmonitor.CustomPluginMonitorName: &[]string{"config-c", "config-d"}, + systemLogMonitorName: &[]string{"config-a", "config-b"}, + customPluginMonitorName: &[]string{"config-c", "config-d"}, }, }, }, @@ -272,13 +270,16 @@ func TestSetConfigFromDeprecatedOptionsOrDie(t *testing.T) { orig: NodeProblemDetectorOptions{ SystemLogMonitorConfigPaths: []string{"config-a", "config-b"}, CustomPluginMonitorConfigPaths: []string{"config-c", "config-d"}, - MonitorConfigPaths: types.ProblemDaemonConfigPathMap{}, + MonitorConfigPaths: types.ProblemDaemonConfigPathMap{ + customPluginMonitorName: &[]string{}, + systemLogMonitorName: &[]string{}, + }, }, expectPanic: false, wanted: NodeProblemDetectorOptions{ MonitorConfigPaths: types.ProblemDaemonConfigPathMap{ - systemlogmonitor.SystemLogMonitorName: &[]string{"config-a", "config-b"}, - custompluginmonitor.CustomPluginMonitorName: &[]string{"config-c", "config-d"}, + systemLogMonitorName: &[]string{"config-a", "config-b"}, + customPluginMonitorName: &[]string{"config-c", "config-d"}, }, }, }, @@ -287,14 +288,15 @@ func TestSetConfigFromDeprecatedOptionsOrDie(t *testing.T) { orig: NodeProblemDetectorOptions{ SystemLogMonitorConfigPaths: []string{"config-a", "config-b"}, MonitorConfigPaths: types.ProblemDaemonConfigPathMap{ - custompluginmonitor.CustomPluginMonitorName: &[]string{"config-c", "config-d"}, + customPluginMonitorName: &[]string{"config-c", "config-d"}, + systemLogMonitorName: &[]string{}, }, }, expectPanic: false, wanted: NodeProblemDetectorOptions{ MonitorConfigPaths: types.ProblemDaemonConfigPathMap{ - systemlogmonitor.SystemLogMonitorName: &[]string{"config-a", "config-b"}, - custompluginmonitor.CustomPluginMonitorName: &[]string{"config-c", "config-d"}, + systemLogMonitorName: &[]string{"config-a", "config-b"}, + customPluginMonitorName: &[]string{"config-c", "config-d"}, }, }, }, @@ -303,14 +305,15 @@ func TestSetConfigFromDeprecatedOptionsOrDie(t *testing.T) { orig: NodeProblemDetectorOptions{ CustomPluginMonitorConfigPaths: []string{"config-a", "config-b"}, MonitorConfigPaths: types.ProblemDaemonConfigPathMap{ - systemlogmonitor.SystemLogMonitorName: &[]string{"config-c", "config-d"}, + customPluginMonitorName: &[]string{}, + systemLogMonitorName: &[]string{"config-c", "config-d"}, }, }, expectPanic: false, wanted: NodeProblemDetectorOptions{ MonitorConfigPaths: types.ProblemDaemonConfigPathMap{ - systemlogmonitor.SystemLogMonitorName: &[]string{"config-c", "config-d"}, - custompluginmonitor.CustomPluginMonitorName: &[]string{"config-a", "config-b"}, + systemLogMonitorName: &[]string{"config-c", "config-d"}, + customPluginMonitorName: &[]string{"config-a", "config-b"}, }, }, }, @@ -319,30 +322,42 @@ func TestSetConfigFromDeprecatedOptionsOrDie(t *testing.T) { orig: NodeProblemDetectorOptions{ SystemLogMonitorConfigPaths: []string{"config-a"}, MonitorConfigPaths: types.ProblemDaemonConfigPathMap{ - systemlogmonitor.SystemLogMonitorName: &[]string{"config-b"}, + systemLogMonitorName: &[]string{"config-b"}, }, }, expectPanic: true, - wanted: NodeProblemDetectorOptions{ + }, + { + name: "using deprecated & new options on CustomPluginMonitor", + orig: NodeProblemDetectorOptions{ + CustomPluginMonitorConfigPaths: []string{"config-a"}, MonitorConfigPaths: types.ProblemDaemonConfigPathMap{ - systemlogmonitor.SystemLogMonitorName: &[]string{"config-b"}, + customPluginMonitorName: &[]string{"config-b"}, }, }, + expectPanic: true, }, { - name: "using deprecated & new options on CustomPluginMonitor", + name: "using deprecated options when SystemLogMonitor is not registered", orig: NodeProblemDetectorOptions{ - CustomPluginMonitorConfigPaths: []string{"config-a"}, + SystemLogMonitorConfigPaths: []string{"config-a"}, + CustomPluginMonitorConfigPaths: []string{"config-b"}, MonitorConfigPaths: types.ProblemDaemonConfigPathMap{ - custompluginmonitor.CustomPluginMonitorName: &[]string{"config-b"}, + customPluginMonitorName: &[]string{}, }, }, expectPanic: true, - wanted: NodeProblemDetectorOptions{ + }, + { + name: "using deprecated options when CustomPluginMonitor is not registered", + orig: NodeProblemDetectorOptions{ + SystemLogMonitorConfigPaths: []string{"config-a"}, + CustomPluginMonitorConfigPaths: []string{"config-b"}, MonitorConfigPaths: types.ProblemDaemonConfigPathMap{ - custompluginmonitor.CustomPluginMonitorName: &[]string{"config-b"}, + systemLogMonitorName: &[]string{}, }, }, + expectPanic: true, }, }