Skip to content

Commit 63f0e35

Browse files
author
Xuewei Zhang
committed
Implement dynamic problemdaemon registration and initialization.
Added package problemdaemon. All future problem daemons should be registered by calling problemdaemon.register(). CLI interfaces will be automatically generated for all registered problem daemons in the form of "--config.DAEMON_NAME"
1 parent 5814195 commit 63f0e35

File tree

4 files changed

+194
-7
lines changed

4 files changed

+194
-7
lines changed

cmd/options/options.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,15 @@ import (
2424
"net/url"
2525

2626
"github.com/spf13/pflag"
27+
28+
"k8s.io/node-problem-detector/pkg/problemdaemon"
29+
"k8s.io/node-problem-detector/pkg/types"
2730
)
2831

2932
// NodeProblemDetectorOptions contains node problem detector command line and application options.
3033
type NodeProblemDetectorOptions struct {
3134
// command line options
3235

33-
// SystemLogMonitorConfigPaths specifies the list of paths to system log monitor configuration
34-
// files.
35-
SystemLogMonitorConfigPaths []string
36-
// CustomPluginMonitorConfigPaths specifies the list of paths to custom plugin monitor configuration
37-
// files.
38-
CustomPluginMonitorConfigPaths []string
3936
// PrintVersion is the flag determining whether version information is printed.
4037
PrintVersion bool
4138
// HostnameOverride specifies custom node name used to override hostname.
@@ -53,14 +50,25 @@ type NodeProblemDetectorOptions struct {
5350
// ApiServerOverride is the custom URI used to connect to Kubernetes ApiServer.
5451
ApiServerOverride string
5552

53+
// problem daemon options
54+
55+
// SystemLogMonitorConfigPaths specifies the list of paths to system log monitor configuration
56+
// files.
57+
SystemLogMonitorConfigPaths []string
58+
// CustomPluginMonitorConfigPaths specifies the list of paths to custom plugin monitor configuration
59+
// files.
60+
CustomPluginMonitorConfigPaths []string
61+
// MonitorConfigPaths specifies the list of paths to configuration files for each monitor.
62+
MonitorConfigPaths types.ProblemDaemonConfigPathMap
63+
5664
// application options
5765

5866
// NodeName is the node name used to communicate with Kubernetes ApiServer.
5967
NodeName string
6068
}
6169

6270
func NewNodeProblemDetectorOptions() *NodeProblemDetectorOptions {
63-
return &NodeProblemDetectorOptions{}
71+
return &NodeProblemDetectorOptions{MonitorConfigPaths: types.ProblemDaemonConfigPathMap{}}
6472
}
6573

6674
// AddFlags adds node problem detector command line options to pflag.
@@ -79,6 +87,17 @@ func (npdo *NodeProblemDetectorOptions) AddFlags(fs *pflag.FlagSet) {
7987
20256, "The port to bind the node problem detector server. Use 0 to disable.")
8088
fs.StringVar(&npdo.ServerAddress, "address",
8189
"127.0.0.1", "The address to bind the node problem detector server.")
90+
91+
for _, problemDaemonName := range problemdaemon.GetProblemDaemonNames() {
92+
npdo.MonitorConfigPaths[problemDaemonName] = &[]string{}
93+
fs.StringSliceVar(
94+
npdo.MonitorConfigPaths[problemDaemonName],
95+
"config."+string(problemDaemonName),
96+
[]string{},
97+
fmt.Sprintf("Comma separated configurations for %v monitor. %v",
98+
problemDaemonName,
99+
problemdaemon.GetProblemDaemonHandlerOrDie(problemDaemonName).CmdOptionDescription))
100+
}
82101
}
83102

84103
// ValidOrDie validates node problem detector command line options.
@@ -90,6 +109,12 @@ func (npdo *NodeProblemDetectorOptions) ValidOrDie() {
90109
if len(npdo.SystemLogMonitorConfigPaths) == 0 && len(npdo.CustomPluginMonitorConfigPaths) == 0 {
91110
panic(fmt.Sprintf("Either --system-log-monitors or --custom-plugin-monitors is required"))
92111
}
112+
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))
116+
}
117+
}
93118
}
94119

95120
// SetNodeNameOrDie sets `NodeName` field with valid value.

pkg/problemdaemon/problem_daemon.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package problemdaemon
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/golang/glog"
23+
24+
"k8s.io/node-problem-detector/pkg/types"
25+
)
26+
27+
var (
28+
handlers = make(map[types.ProblemDaemonType]types.ProblemDaemonHandler)
29+
)
30+
31+
// Register registers a problem daemon factory method, which will be used to create the problem daemon.
32+
func Register(problemDaemonType types.ProblemDaemonType, handler types.ProblemDaemonHandler) {
33+
handlers[problemDaemonType] = handler
34+
}
35+
36+
// GetProblemDaemonNames retrieves all available problem daemon types.
37+
func GetProblemDaemonNames() []types.ProblemDaemonType {
38+
problemDaemonTypes := []types.ProblemDaemonType{}
39+
for problemDaemonType := range handlers {
40+
problemDaemonTypes = append(problemDaemonTypes, problemDaemonType)
41+
}
42+
return problemDaemonTypes
43+
}
44+
45+
// GetProblemDaemonHandlerOrDie retrieves the ProblemDaemonHandler for a specific type of problem daemon, panic if error occurs..
46+
func GetProblemDaemonHandlerOrDie(problemDaemonType types.ProblemDaemonType) types.ProblemDaemonHandler {
47+
handler, ok := handlers[problemDaemonType]
48+
if !ok {
49+
panic(fmt.Sprintf("Problem daemon handler for %v does not exist", problemDaemonType))
50+
}
51+
return handler
52+
}
53+
54+
// NewProblemDaemons creates all problem daemons based on the configurations provided.
55+
func NewProblemDaemons(monitorConfigPaths types.ProblemDaemonConfigPathMap) []types.Monitor {
56+
problemDaemonMap := make(map[string]types.Monitor)
57+
for problemDaemonType, configs := range monitorConfigPaths {
58+
for _, config := range *configs {
59+
if _, ok := problemDaemonMap[config]; ok {
60+
// Skip the config if it's duplicated.
61+
glog.Warningf("Duplicated problem daemon configuration %q", config)
62+
continue
63+
}
64+
problemDaemonMap[config] = handlers[problemDaemonType].CreateProblemDaemonOrDie(config)
65+
}
66+
}
67+
68+
problemDaemons := []types.Monitor{}
69+
for _, problemDaemon := range problemDaemonMap {
70+
problemDaemons = append(problemDaemons, problemDaemon)
71+
}
72+
return problemDaemons
73+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package problemdaemon
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
24+
"k8s.io/node-problem-detector/pkg/types"
25+
)
26+
27+
func TestRegistration(t *testing.T) {
28+
fooMonitorFactory := func(configPath string) types.Monitor {
29+
return nil
30+
}
31+
fooMonitorHandler := types.ProblemDaemonHandler{
32+
CreateProblemDaemonOrDie: fooMonitorFactory,
33+
CmdOptionDescription: "foo option",
34+
}
35+
36+
barMonitorFactory := func(configPath string) types.Monitor {
37+
return nil
38+
}
39+
barMonitorHandler := types.ProblemDaemonHandler{
40+
CreateProblemDaemonOrDie: barMonitorFactory,
41+
CmdOptionDescription: "bar option",
42+
}
43+
44+
Register("foo", fooMonitorHandler)
45+
Register("bar", barMonitorHandler)
46+
47+
expectedProblemDaemonNames := []types.ProblemDaemonType{"foo", "bar"}
48+
problemDaemonNames := GetProblemDaemonNames()
49+
50+
assert.ElementsMatch(t, expectedProblemDaemonNames, problemDaemonNames)
51+
assert.Equal(t, "foo option", GetProblemDaemonHandlerOrDie("foo").CmdOptionDescription)
52+
assert.Equal(t, "bar option", GetProblemDaemonHandlerOrDie("bar").CmdOptionDescription)
53+
54+
handlers = make(map[types.ProblemDaemonType]types.ProblemDaemonHandler)
55+
}
56+
57+
func TestGetProblemDaemonHandlerOrDie(t *testing.T) {
58+
fooMonitorFactory := func(configPath string) types.Monitor {
59+
return nil
60+
}
61+
fooMonitorHandler := types.ProblemDaemonHandler{
62+
CreateProblemDaemonOrDie: fooMonitorFactory,
63+
CmdOptionDescription: "foo option",
64+
}
65+
66+
Register("foo", fooMonitorHandler)
67+
68+
assert.NotPanics(t, func() { GetProblemDaemonHandlerOrDie("foo") })
69+
assert.Panics(t, func() { GetProblemDaemonHandlerOrDie("bar") })
70+
71+
handlers = make(map[types.ProblemDaemonType]types.ProblemDaemonHandler)
72+
}

pkg/types/types.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,20 @@ type Exporter interface {
113113
// Export problems to the control plane.
114114
ExportProblems(*Status)
115115
}
116+
117+
// ProblemDaemonType is the type of the problem daemon.
118+
// One type of problem daemon may be used to initialize multiple problem daemon instances.
119+
type ProblemDaemonType string
120+
121+
// ProblemDaemonConfigPathMap represents configurations on all types of problem daemons:
122+
// 1) Each key represents a type of problem daemon.
123+
// 2) Each value represents the config file paths to that type of problem daemon.
124+
type ProblemDaemonConfigPathMap map[ProblemDaemonType]*[]string
125+
126+
// ProblemDaemonHandler represents the initialization handler for a type problem daemon.
127+
type ProblemDaemonHandler struct {
128+
// CreateProblemDaemonOrDie initializes a problem daemon, panic if error occurs.
129+
CreateProblemDaemonOrDie func(string) Monitor
130+
// CmdOptionDescription explains how to configure the problem daemon from command line arguments.
131+
CmdOptionDescription string
132+
}

0 commit comments

Comments
 (0)