Do not import plugins unnecessarily.

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu
2019-06-13 17:57:53 -07:00
parent 975dc718a5
commit f2d17ee77b
2 changed files with 68 additions and 38 deletions
+29 -14
View File
@@ -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{}
}
+39 -24
View File
@@ -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{
MonitorConfigPaths: types.ProblemDaemonConfigPathMap{
systemlogmonitor.SystemLogMonitorName: &[]string{"config-b"},
},
},
},
{
name: "using deprecated & new options on CustomPluginMonitor",
orig: NodeProblemDetectorOptions{
CustomPluginMonitorConfigPaths: []string{"config-a"},
MonitorConfigPaths: types.ProblemDaemonConfigPathMap{
custompluginmonitor.CustomPluginMonitorName: &[]string{"config-b"},
customPluginMonitorName: &[]string{"config-b"},
},
},
expectPanic: true,
wanted: NodeProblemDetectorOptions{
},
{
name: "using deprecated options when SystemLogMonitor is not registered",
orig: NodeProblemDetectorOptions{
SystemLogMonitorConfigPaths: []string{"config-a"},
CustomPluginMonitorConfigPaths: []string{"config-b"},
MonitorConfigPaths: types.ProblemDaemonConfigPathMap{
custompluginmonitor.CustomPluginMonitorName: &[]string{"config-b"},
customPluginMonitorName: &[]string{},
},
},
expectPanic: true,
},
{
name: "using deprecated options when CustomPluginMonitor is not registered",
orig: NodeProblemDetectorOptions{
SystemLogMonitorConfigPaths: []string{"config-a"},
CustomPluginMonitorConfigPaths: []string{"config-b"},
MonitorConfigPaths: types.ProblemDaemonConfigPathMap{
systemLogMonitorName: &[]string{},
},
},
expectPanic: true,
},
}