Skip to content

Commit 92a3c35

Browse files
authored
Merge pull request kubernetes#30 from Random-Liu/get-node-name-from-downward-api
NPD: Get node name from the downward api.
2 parents 09af299 + 9054dab commit 92a3c35

File tree

4 files changed

+21
-21
lines changed

4 files changed

+21
-21
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
all: push
22

3-
# See pod.yaml for the version currently running-- bump this ahead before rebuilding!
3+
# See node-problem-detector.yaml for the version currently running-- bump this ahead before rebuilding!
44
TAG = v0.2
55

66
PROJ = google_containers

README.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,10 @@ spec:
7373
securityContext:
7474
privileged: true
7575
env:
76-
- name: POD_NAME
76+
- name: NODE_NAME
7777
valueFrom:
7878
fieldRef:
79-
fieldPath: metadata.name
80-
- name: POD_NAMESPACE
81-
valueFrom:
82-
fieldRef:
83-
fieldPath: metadata.namespace
79+
fieldPath: spec.nodeName
8480
volumeMounts:
8581
- name: log
8682
mountPath: /log

node-problem-detector.yaml

+2-6
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,10 @@ spec:
1818
securityContext:
1919
privileged: true
2020
env:
21-
- name: POD_NAME
21+
- name: NODE_NAME
2222
valueFrom:
2323
fieldRef:
24-
fieldPath: metadata.name
25-
- name: POD_NAMESPACE
26-
valueFrom:
27-
fieldRef:
28-
fieldPath: metadata.namespace
24+
fieldPath: spec.nodeName
2925
volumeMounts:
3026
- name: log
3127
mountPath: /log

pkg/problemclient/problem_client.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,23 @@ func NewClientOrDie() Client {
5757
}
5858
// TODO(random-liu): Set QPS Limit
5959
c.client = client.NewOrDie(cfg)
60-
// Get node name from the current pod.
61-
pod, err := c.client.Pods(os.Getenv("POD_NAMESPACE")).Get(os.Getenv("POD_NAME"))
62-
if err != nil {
63-
panic(err)
64-
}
65-
if pod.Spec.NodeName == "" {
66-
panic("empty node name")
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. We prefer it because sometimes the hostname returned
63+
// 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+
c.nodeName = os.Getenv("NODE_NAME")
67+
if c.nodeName == "" {
68+
// For backward compatibility. If the env is not set, get the hostname
69+
// from os.Hostname(). This may not work for all configurations and
70+
// environments.
71+
var err error
72+
c.nodeName, err = os.Hostname()
73+
if err != nil {
74+
panic("empty node name")
75+
}
6776
}
68-
c.nodeName = pod.Spec.NodeName
6977
c.nodeRef = getNodeRef(c.nodeName)
7078
c.recorders = make(map[string]record.EventRecorder)
7179
return c

0 commit comments

Comments
 (0)