Skip to content

Commit 59804ad

Browse files
committed
Fix up node name resolution from within Pod
At present the hostname resolution is handed back the Pod name rather than the node name when run within a Kubernetes deployment. We fix this up by specifying a NODE_NAME environment variable and passing it in - falling back to hostname lookup if it's unset, as in e.g. node-local testing outside of a deployment context.
1 parent 46d2bfb commit 59804ad

6 files changed

+29
-4
lines changed

Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
FROM golang as builder
22

33
ARG KUBECONFIG
4+
ARG NODE_NAME
45

56
ENV GO111MODULE=on
67

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ $ go get github.com/adaptant-labs/k8s-dt-node-labeller
5050
```
5151

5252
For Docker containers and Kubernetes deployment instructions, see below.
53+
5354
## Usage
5455

5556
General usage is as follows:

controller.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ type reconcileNodeLabels struct {
2121
var _ reconcile.Reconciler = &reconcileNodeLabels{}
2222

2323
func (r *reconcileNodeLabels) Reconcile(request reconcile.Request) (reconcile.Result, error) {
24-
// set up a convinient log object so we don't have to type request over and over again
24+
// set up a convenient log object so we don't have to type request over and over again
2525
log := r.log.WithValues("request", request)
2626

2727
node := &corev1.Node{}

k8s-dt-labeller-ds.yaml

+6-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ spec:
5050
nodeSelector:
5151
kubernetes.io/arch: arm64
5252
containers:
53-
- image: adaptant/k8s-dt-node-labeller:latest
53+
- env:
54+
- name: NODE_NAME
55+
valueFrom:
56+
fieldRef:
57+
fieldPath: spec.nodeName
58+
image: adaptant/k8s-dt-node-labeller:latest
5459
name: dt-labeller
5560
securityContext:
5661
# Needed for /sys/firmware access

k8s-dt-labeller-nfd.yaml

+6-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ spec:
6060
nodeSelector:
6161
kubernetes.io/arch: arm64
6262
initContainers:
63-
- image: adaptant/k8s-dt-node-labeller:latest
63+
- env:
64+
- name: NODE_NAME
65+
valueFrom:
66+
fieldRef:
67+
fieldPath: spec.nodeName
68+
image: adaptant/k8s-dt-node-labeller:latest
6469
name: dt-labeller
6570
args:
6671
- "-f"

main.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,19 @@ func appendNodesIfNotExist(nodes[] string, names ...string) []string {
157157
return nodes
158158
}
159159

160+
func getNodeName() (string, error) {
161+
// Within the Kubernetes Pod, the hostname provides the Pod name, rather than the node name, so we pass in the
162+
// node name via the NODE_NAME environment variable instead.
163+
nodeName := os.Getenv("NODE_NAME")
164+
if len(nodeName) > 0 {
165+
return nodeName, nil
166+
}
167+
168+
// If the NODE_NAME environment variable is unset, fall back on hostname matching (e.g. when running outside of
169+
// a Kubernetes deployment).
170+
return os.Hostname()
171+
}
172+
160173
func main() {
161174
var n string
162175
var d bool
@@ -245,7 +258,7 @@ func main() {
245258
// tagged as part of the node's metadata.
246259
pred := predicate.Funcs{
247260
CreateFunc: func(e event.CreateEvent) bool {
248-
hostname, err := os.Hostname()
261+
hostname, err := getNodeName()
249262
if err != nil {
250263
return false
251264
}

0 commit comments

Comments
 (0)