Skip to content

Commit

Permalink
Allow problem daemon plugins to define their command line options
Browse files Browse the repository at this point in the history
  • Loading branch information
Xuewei Zhang committed Sep 13, 2019
1 parent 0f0e5ef commit f35bae4
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 405 deletions.
5 changes: 2 additions & 3 deletions cmd/nodeproblemdetector/node_problem_detector.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (
)

func main() {
npdo := options.NewNodeProblemDetectorOptions()
npdo := &options.NodeProblemDetectorOptions{}
npdo.AddFlags(pflag.CommandLine)

pflag.Parse()
Expand All @@ -46,11 +46,10 @@ func main() {
}

npdo.SetNodeNameOrDie()
npdo.SetConfigFromDeprecatedOptionsOrDie()
npdo.ValidOrDie()

// Initialize problem daemons.
problemDaemons := problemdaemon.NewProblemDaemons(npdo.MonitorConfigPaths)
problemDaemons := problemdaemon.NewProblemDaemons()
if len(problemDaemons) == 0 {
glog.Fatalf("No problem daemon is configured")
}
Expand Down
103 changes: 3 additions & 100 deletions cmd/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (

"k8s.io/node-problem-detector/pkg/exporters"
"k8s.io/node-problem-detector/pkg/problemdaemon"
"k8s.io/node-problem-detector/pkg/types"
)

// NodeProblemDetectorOptions contains node problem detector command line and application options.
Expand Down Expand Up @@ -64,44 +63,14 @@ type NodeProblemDetectorOptions struct {
// PrometheusServerAddress is the address to bind the Prometheus scrape endpoint.
PrometheusServerAddress string

// problem daemon options

// SystemLogMonitorConfigPaths specifies the list of paths to system log monitor configuration
// files.
// SystemLogMonitorConfigPaths is used by the deprecated option --system-log-monitors. The new
// option --config.system-log-monitor will stored the config file paths in MonitorConfigPaths.
SystemLogMonitorConfigPaths []string
// CustomPluginMonitorConfigPaths specifies the list of paths to custom plugin monitor configuration
// files.
// CustomPluginMonitorConfigPaths is used by the deprecated option --custom-plugin-monitors. The
// new option --config.custom-plugin-monitor will stored the config file paths in MonitorConfigPaths.
CustomPluginMonitorConfigPaths []string
// MonitorConfigPaths specifies the list of paths to configuration files for each monitor.
MonitorConfigPaths types.ProblemDaemonConfigPathMap

// application options

// NodeName is the node name used to communicate with Kubernetes ApiServer.
NodeName string
}

func NewNodeProblemDetectorOptions() *NodeProblemDetectorOptions {
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.
func (npdo *NodeProblemDetectorOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringSliceVar(&npdo.SystemLogMonitorConfigPaths, "system-log-monitors",
[]string{}, "List of paths to system log monitor config files, comma separated.")
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.")
fs.StringSliceVar(&npdo.CustomPluginMonitorConfigPaths, "custom-plugin-monitors",
[]string{}, "List of paths to custom plugin monitor config files, comma separated.")
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.")
fs.BoolVar(&npdo.EnableK8sExporter, "enable-k8s-exporter", true, "Enables reporting to Kubernetes API server.")
fs.StringVar(&npdo.ApiServerOverride, "apiserver-override",
"", "Custom URI used to connect to Kubernetes ApiServer. This is ignored if --enable-k8s-exporter is false.")
Expand All @@ -124,14 +93,10 @@ func (npdo *NodeProblemDetectorOptions) AddFlags(fs *pflag.FlagSet) {
exporterHandler := exporters.GetExporterHandlerOrDie(exporterName)
exporterHandler.Options.SetFlags(fs)
}

for _, problemDaemonName := range problemdaemon.GetProblemDaemonNames() {
fs.StringSliceVar(
npdo.MonitorConfigPaths[problemDaemonName],
"config."+string(problemDaemonName),
[]string{},
fmt.Sprintf("Comma separated configurations for %v monitor. %v",
problemDaemonName,
problemdaemon.GetProblemDaemonHandlerOrDie(problemDaemonName).CmdOptionDescription))
problemDaemonHandler := problemdaemon.GetProblemDaemonHandlerOrDie(problemDaemonName)
problemDaemonHandler.Options.SetFlags(fs)
}
}

Expand All @@ -141,68 +106,6 @@ func (npdo *NodeProblemDetectorOptions) ValidOrDie() {
panic(fmt.Sprintf("apiserver-override %q is not a valid HTTP URI: %v",
npdo.ApiServerOverride, err))
}

if len(npdo.SystemLogMonitorConfigPaths) != 0 {
panic("SystemLogMonitorConfigPaths is deprecated. It should have been reassigned to MonitorConfigPaths. This should not happen.")
}
if len(npdo.CustomPluginMonitorConfigPaths) != 0 {
panic("CustomPluginMonitorConfigPaths is deprecated. It should have been reassigned to MonitorConfigPaths. This should not happen.")
}

configCount := 0
for _, problemDaemonConfigPaths := range npdo.MonitorConfigPaths {
configCount += len(*problemDaemonConfigPaths)
}
if configCount == 0 {
panic("No configuration option for any problem daemon is specified.")
}
}

// 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[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[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[systemLogMonitorName] = append(
*npdo.MonitorConfigPaths[systemLogMonitorName],
npdo.SystemLogMonitorConfigPaths...)
npdo.SystemLogMonitorConfigPaths = []string{}
}

if len(npdo.CustomPluginMonitorConfigPaths) != 0 {
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[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[customPluginMonitorName] = append(
*npdo.MonitorConfigPaths[customPluginMonitorName],
npdo.CustomPluginMonitorConfigPaths...)
npdo.CustomPluginMonitorConfigPaths = []string{}
}
}

// SetNodeNameOrDie sets `NodeName` field with valid value.
Expand Down
Loading

0 comments on commit f35bae4

Please sign in to comment.