Skip to content

Commit 310fed3

Browse files
authored
Merge pull request #91 from andyxning/add_option_pkg
add options pkg
2 parents 25c5fee + d0e0a8c commit 310fed3

File tree

4 files changed

+134
-64
lines changed

4 files changed

+134
-64
lines changed

cmd/node_problem_detector.go

Lines changed: 18 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -17,69 +17,23 @@ limitations under the License.
1717
package main
1818

1919
import (
20-
"flag"
2120
"net"
2221
"net/http"
2322
_ "net/http/pprof"
24-
"net/url"
2523
"os"
2624
"strconv"
2725

2826
"github.com/golang/glog"
2927
"github.com/spf13/pflag"
3028

3129
"k8s.io/node-problem-detector/pkg/kernelmonitor"
30+
"k8s.io/node-problem-detector/pkg/options"
31+
"k8s.io/node-problem-detector/pkg/problemclient"
3232
"k8s.io/node-problem-detector/pkg/problemdetector"
3333
"k8s.io/node-problem-detector/pkg/version"
3434
)
3535

36-
// TODO: Move flags to options directory.
37-
var (
38-
kernelMonitorConfigPath = pflag.String("kernel-monitor", "/config/kernel-monitor.json", "The path to the kernel monitor config file")
39-
apiServerOverride = pflag.String("apiserver-override", "", "Custom URI used to connect to Kubernetes ApiServer")
40-
printVersion = pflag.Bool("version", false, "Print version information and quit")
41-
hostnameOverride = pflag.String("hostname-override", "", "Custom node name used to override hostname")
42-
serverPort = pflag.Int("port", 10256, "The port to bind the node problem detector server. Use 0 to disable.")
43-
serverAddress = pflag.String("address", "127.0.0.1", "The address to bind the node problem detector server.")
44-
)
45-
46-
func validateCmdParams() {
47-
if _, err := url.Parse(*apiServerOverride); err != nil {
48-
glog.Fatalf("apiserver-override %q is not a valid HTTP URI: %v", *apiServerOverride, err)
49-
}
50-
}
51-
52-
func getNodeNameOrDie() string {
53-
var nodeName string
54-
55-
// Check hostname override first for customized node name.
56-
if *hostnameOverride != "" {
57-
return *hostnameOverride
58-
}
59-
60-
// Get node name from environment variable NODE_NAME
61-
// By default, assume that the NODE_NAME env should have been set with
62-
// downward api or user defined exported environment variable. We prefer it because sometimes
63-
// the hostname returned by os.Hostname is not right because:
64-
// 1. User may override the hostname.
65-
// 2. For some cloud providers, os.Hostname is different from the real hostname.
66-
nodeName = os.Getenv("NODE_NAME")
67-
if nodeName != "" {
68-
return nodeName
69-
}
70-
71-
// For backward compatibility. If the env is not set, get the hostname
72-
// from os.Hostname(). This may not work for all configurations and
73-
// environments.
74-
nodeName, err := os.Hostname()
75-
if err != nil {
76-
glog.Fatalf("Failed to get host name: %v", err)
77-
}
78-
79-
return nodeName
80-
}
81-
82-
func startHTTPServer(p problemdetector.ProblemDetector) {
36+
func startHTTPServer(p problemdetector.ProblemDetector, npdo *options.NodeProblemDetectorOptions) {
8337
// Add healthz http request handler. Always return ok now, add more health check
8438
// logic in the future.
8539
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
@@ -88,31 +42,36 @@ func startHTTPServer(p problemdetector.ProblemDetector) {
8842
})
8943
// Add the http handlers in problem detector.
9044
p.RegisterHTTPHandlers()
91-
err := http.ListenAndServe(net.JoinHostPort(*serverAddress, strconv.Itoa(*serverPort)), nil)
45+
46+
addr := net.JoinHostPort(npdo.ServerAddress, strconv.Itoa(npdo.ServerPort))
47+
err := http.ListenAndServe(addr, nil)
9248
if err != nil {
9349
glog.Fatalf("Failed to start server: %v", err)
9450
}
9551
}
9652

9753
func main() {
98-
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
54+
npdo := options.NewNodeProblemDetectorOptions()
55+
npdo.AddFlags(pflag.CommandLine)
56+
9957
pflag.Parse()
10058

101-
validateCmdParams()
59+
npdo.SetNodeNameOrDie()
10260

103-
if *printVersion {
61+
npdo.ValidOrDie()
62+
63+
if npdo.PrintVersion {
10464
version.PrintVersion()
10565
os.Exit(0)
10666
}
10767

108-
nodeName := getNodeNameOrDie()
109-
110-
k := kernelmonitor.NewKernelMonitorOrDie(*kernelMonitorConfigPath)
111-
p := problemdetector.NewProblemDetector(k, *apiServerOverride, nodeName)
68+
k := kernelmonitor.NewKernelMonitorOrDie(npdo.KernelMonitorConfigPath)
69+
c := problemclient.NewClientOrDie(npdo)
70+
p := problemdetector.NewProblemDetector(k, c)
11271

11372
// Start http server.
114-
if *serverPort > 0 {
115-
startHTTPServer(p)
73+
if npdo.ServerPort > 0 {
74+
startHTTPServer(p, npdo)
11675
}
11776

11877
if err := p.Run(); err != nil {

pkg/options/options.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
Copyright 2017 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 options
18+
19+
import (
20+
"flag"
21+
"fmt"
22+
"os"
23+
24+
"net/url"
25+
26+
"github.com/spf13/pflag"
27+
)
28+
29+
// NodeProblemDetectorOptions contains node problem detector command line and application options.
30+
type NodeProblemDetectorOptions struct {
31+
// command line options
32+
33+
// KernelMonitorConfigPath specifies the path to kernel monitor configuration file.
34+
KernelMonitorConfigPath string
35+
// ApiServerOverride is the custom URI used to connect to Kubernetes ApiServer.
36+
ApiServerOverride string
37+
// PrintVersion is the flag determining whether version information is printed.
38+
PrintVersion bool
39+
// HostnameOverride specifies custom node name used to override hostname.
40+
HostnameOverride string
41+
// ServerPort is the port to bind the node problem detector server. Use 0 to disable.
42+
ServerPort int
43+
// ServerAddress is the address to bind the node problem detector server.
44+
ServerAddress string
45+
46+
// application options
47+
48+
// NodeName is the node name used to communicate with Kubernetes ApiServer.
49+
NodeName string
50+
}
51+
52+
func NewNodeProblemDetectorOptions() *NodeProblemDetectorOptions {
53+
return &NodeProblemDetectorOptions{}
54+
}
55+
56+
// AddFlags adds node problem detector command line options to pflag.
57+
func (npdo *NodeProblemDetectorOptions) AddFlags(fs *pflag.FlagSet) {
58+
fs.StringVar(&npdo.KernelMonitorConfigPath, "kernel-monitor",
59+
"/config/kernel-monitor.json", "The path to the kernel monitor config file")
60+
fs.StringVar(&npdo.ApiServerOverride, "apiserver-override",
61+
"", "Custom URI used to connect to Kubernetes ApiServer")
62+
fs.BoolVar(&npdo.PrintVersion, "version", false, "Print version information and quit")
63+
fs.StringVar(&npdo.HostnameOverride, "hostname-override",
64+
"", "Custom node name used to override hostname")
65+
fs.IntVar(&npdo.ServerPort, "port",
66+
10256, "The port to bind the node problem detector server. Use 0 to disable.")
67+
fs.StringVar(&npdo.ServerAddress, "address",
68+
"127.0.0.1", "The address to bind the node problem detector server.")
69+
}
70+
71+
// ValidOrDie validates node problem detector command line options.
72+
func (npdo *NodeProblemDetectorOptions) ValidOrDie() {
73+
if _, err := url.Parse(npdo.ApiServerOverride); err != nil {
74+
panic(fmt.Sprintf("apiserver-override %q is not a valid HTTP URI: %v",
75+
npdo.ApiServerOverride, err))
76+
}
77+
}
78+
79+
// SetNodeNameOrDie sets `NodeName` field with valid value.
80+
func (npdo *NodeProblemDetectorOptions) SetNodeNameOrDie() {
81+
// Check hostname override first for customized node name.
82+
if npdo.HostnameOverride != "" {
83+
npdo.NodeName = npdo.HostnameOverride
84+
return
85+
}
86+
87+
// Get node name from environment variable NODE_NAME
88+
// By default, assume that the NODE_NAME env should have been set with
89+
// downward api or user defined exported environment variable. We prefer it because sometimes
90+
// the hostname returned by os.Hostname is not right because:
91+
// 1. User may override the hostname.
92+
// 2. For some cloud providers, os.Hostname is different from the real hostname.
93+
npdo.NodeName = os.Getenv("NODE_NAME")
94+
if npdo.NodeName != "" {
95+
return
96+
}
97+
98+
// For backward compatibility. If the env is not set, get the hostname
99+
// from os.Hostname(). This may not work for all configurations and
100+
// environments.
101+
nodeName, err := os.Hostname()
102+
if err != nil {
103+
panic(fmt.Sprintf("Failed to get host name: %v", err))
104+
}
105+
106+
npdo.NodeName = nodeName
107+
}
108+
109+
func init() {
110+
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
111+
}

pkg/problemclient/problem_client.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"k8s.io/kubernetes/pkg/util/clock"
3030

3131
"k8s.io/heapster/common/kubernetes"
32+
"k8s.io/node-problem-detector/pkg/options"
3233
)
3334

3435
// Client is the interface of problem client
@@ -50,11 +51,11 @@ type nodeProblemClient struct {
5051
}
5152

5253
// NewClientOrDie creates a new problem client, panics if error occurs.
53-
func NewClientOrDie(apiServerOverride, nodeName string) Client {
54+
func NewClientOrDie(npdo *options.NodeProblemDetectorOptions) Client {
5455
c := &nodeProblemClient{clock: clock.RealClock{}}
5556

5657
// we have checked it is a valid URI after command line argument is parsed.:)
57-
uri, _ := url.Parse(apiServerOverride)
58+
uri, _ := url.Parse(npdo.ApiServerOverride)
5859

5960
cfg, err := kubernetes.GetKubeClientConfig(uri)
6061
if err != nil {
@@ -63,7 +64,7 @@ func NewClientOrDie(apiServerOverride, nodeName string) Client {
6364

6465
// TODO(random-liu): Set QPS Limit
6566
c.client = client.NewOrDie(cfg)
66-
c.nodeName = nodeName
67+
c.nodeName = npdo.NodeName
6768
c.nodeRef = getNodeRef(c.nodeName)
6869
c.recorders = make(map[string]record.EventRecorder)
6970
return c

pkg/problemdetector/problem_detector.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ type problemDetector struct {
4444

4545
// NewProblemDetector creates the problem detector. Currently we just directly passed in the problem daemons, but
4646
// in the future we may want to let the problem daemons register themselves.
47-
func NewProblemDetector(monitor kernelmonitor.KernelMonitor, apiServerOverride, nodeName string) ProblemDetector {
48-
client := problemclient.NewClientOrDie(apiServerOverride, nodeName)
47+
func NewProblemDetector(monitor kernelmonitor.KernelMonitor, client problemclient.Client) ProblemDetector {
4948
return &problemDetector{
5049
client: client,
5150
conditionManager: condition.NewConditionManager(client, clock.RealClock{}),

0 commit comments

Comments
 (0)