Skip to content

Commit f0ed07a

Browse files
authored
Merge pull request #72 from andyxning/enrich_info_about_nodename
detail how node-problem-detector get node name in README
2 parents f457672 + 7302c70 commit f0ed07a

File tree

4 files changed

+40
-22
lines changed

4 files changed

+40
-22
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ For example, to run without auth, use the following config:
6262
http://APISERVER_IP:APISERVER_PORT?inClusterConfig=false
6363
```
6464
Refer [heapster docs](https://github.com/kubernetes/heapster/blob/1e40b0f4b5eeb3f02e11ee22c2b6fda36b6e6ea1/docs/source-configuration.md#kubernetes) for a complete list of available options.
65+
* `-hostname-override`: A customized node name used for node-problem-detector to update conditions and emit events. node-problem-detector gets node name first from `hostname-override`, then `NODE_NAME` environment variable and finally fall back to `os.Hostname`.
6566

6667
## Build Image
6768
Run `make` in the top directory. It will:

cmd/node_problem_detector.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ import (
2626
"k8s.io/node-problem-detector/pkg/version"
2727

2828
"github.com/golang/glog"
29+
"fmt"
2930
)
3031

3132
// TODO: Move flags to options directory.
3233
var (
3334
kernelMonitorConfigPath = flag.String("kernel-monitor", "/config/kernel-monitor.json", "The path to the kernel monitor config file")
3435
apiServerOverride = flag.String("apiserver-override", "", "Custom URI used to connect to Kubernetes ApiServer")
3536
printVersion = flag.Bool("version", false, "Print version information and quit")
37+
hostnameOverride = flag.String("hostname-override", "", "Custom node name used to override hostname")
3638
)
3739

3840
func validateCmdParams() {
@@ -41,6 +43,36 @@ func validateCmdParams() {
4143
}
4244
}
4345

46+
func getNodeNameOrDie() string {
47+
var nodeName string
48+
49+
// Check hostname override first for customized node name.
50+
if *hostnameOverride != "" {
51+
return *hostnameOverride
52+
}
53+
54+
// Get node name from environment variable NODE_NAME
55+
// By default, assume that the NODE_NAME env should have been set with
56+
// downward api or user defined exported environment variable. We prefer it because sometimes
57+
// the hostname returned by os.Hostname is not right because:
58+
// 1. User may override the hostname.
59+
// 2. For some cloud providers, os.Hostname is different from the real hostname.
60+
nodeName = os.Getenv("NODE_NAME")
61+
if nodeName != "" {
62+
return nodeName
63+
}
64+
65+
// For backward compatibility. If the env is not set, get the hostname
66+
// from os.Hostname(). This may not work for all configurations and
67+
// environments.
68+
nodeName, err := os.Hostname()
69+
if err != nil {
70+
panic(fmt.Sprintf("Failed to get host name: %v", err))
71+
}
72+
73+
return nodeName
74+
}
75+
4476
func main() {
4577
flag.Parse()
4678
validateCmdParams()
@@ -50,7 +82,9 @@ func main() {
5082
os.Exit(0)
5183
}
5284

85+
nodeName := getNodeNameOrDie()
86+
5387
k := kernelmonitor.NewKernelMonitorOrDie(*kernelMonitorConfigPath)
54-
p := problemdetector.NewProblemDetector(k, *apiServerOverride)
88+
p := problemdetector.NewProblemDetector(k, *apiServerOverride, nodeName)
5589
p.Run()
5690
}

pkg/problemclient/problem_client.go

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"encoding/json"
2121
"fmt"
2222
"net/url"
23-
"os"
2423

2524
"k8s.io/kubernetes/pkg/api"
2625
"k8s.io/kubernetes/pkg/api/unversioned"
@@ -51,7 +50,7 @@ type nodeProblemClient struct {
5150
}
5251

5352
// NewClientOrDie creates a new problem client, panics if error occurs.
54-
func NewClientOrDie(apiServerOverride string) Client {
53+
func NewClientOrDie(apiServerOverride, nodeName string) Client {
5554
c := &nodeProblemClient{clock: clock.RealClock{}}
5655

5756
// we have checked it is a valid URI after command line argument is parsed.:)
@@ -64,23 +63,7 @@ func NewClientOrDie(apiServerOverride string) Client {
6463

6564
// TODO(random-liu): Set QPS Limit
6665
c.client = client.NewOrDie(cfg)
67-
// Get node name from environment variable NODE_NAME
68-
// By default, assume that the NODE_NAME env should have been set with
69-
// downward api. We prefer it because sometimes the hostname returned
70-
// by os.Hostname is not right because:
71-
// 1. User may override the hostname.
72-
// 2. For some cloud providers, os.Hostname is different from the real hostname.
73-
c.nodeName = os.Getenv("NODE_NAME")
74-
if c.nodeName == "" {
75-
// For backward compatibility. If the env is not set, get the hostname
76-
// from os.Hostname(). This may not work for all configurations and
77-
// environments.
78-
var err error
79-
c.nodeName, err = os.Hostname()
80-
if err != nil {
81-
panic("empty node name")
82-
}
83-
}
66+
c.nodeName = nodeName
8467
c.nodeRef = getNodeRef(c.nodeName)
8568
c.recorders = make(map[string]record.EventRecorder)
8669
return c

pkg/problemdetector/problem_detector.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ type problemDetector struct {
4141

4242
// NewProblemDetector creates the problem detector. Currently we just directly passed in the problem daemons, but
4343
// in the future we may want to let the problem daemons register themselves.
44-
func NewProblemDetector(monitor kernelmonitor.KernelMonitor, apiServerOverride string) ProblemDetector {
45-
client := problemclient.NewClientOrDie(apiServerOverride)
44+
func NewProblemDetector(monitor kernelmonitor.KernelMonitor, apiServerOverride, nodeName string) ProblemDetector {
45+
client := problemclient.NewClientOrDie(apiServerOverride, nodeName)
4646
return &problemDetector{
4747
client: client,
4848
conditionManager: condition.NewConditionManager(client, clock.RealClock{}),

0 commit comments

Comments
 (0)