Skip to content

Commit 2c3795e

Browse files
committed
pkg/leader: require the podname to be set explicitly
We can't trust that the hostname is always the pod name.
1 parent 5c50126 commit 2c3795e

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

pkg/leader/doc.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ The lock record in this case is a ConfigMap whose OwnerReference is set to the
4141
Pod that is the leader. When the leader is destroyed, the ConfigMap gets
4242
garbage-collected, enabling a different candidate Pod to become the leader.
4343
44-
Leader for Life requires that all candidate Pods be in the same Namespace.
44+
Leader for Life requires that all candidate Pods be in the same Namespace. It
45+
uses the downwards API to determine the pod name, as hostname is not reliable.
46+
You should run it configured with:
47+
48+
env:
49+
- name: POD_NAME
50+
valueFrom:
51+
fieldRef:
52+
fieldPath: metadata.name
4553
*/
4654
package leader

pkg/leader/leader.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package leader
1717
import (
1818
"context"
1919
"errors"
20+
"fmt"
2021
"io/ioutil"
2122
"os"
2223
"strings"
@@ -39,6 +40,8 @@ var errNoNS = errors.New("namespace not found for current environment")
3940
// attempts to become the leader.
4041
const maxBackoffInterval = time.Second * 16
4142

43+
const PodNameEnv = "POD_NAME"
44+
4245
// Become ensures that the current pod is the leader within its namespace. If
4346
// run outside a cluster, it will skip leader election and return nil. It
4447
// continuously tries to create a ConfigMap with the provided name and the
@@ -156,12 +159,14 @@ func myNS() (string, error) {
156159

157160
// myOwnerRef returns an OwnerReference that corresponds to the pod in which
158161
// this code is currently running.
162+
// It expects the environment variable POD_NAME to be set by the downwards API
159163
func myOwnerRef(ctx context.Context, client crclient.Client, ns string) (*metav1.OwnerReference, error) {
160-
hostname, err := os.Hostname()
161-
if err != nil {
162-
return nil, err
164+
podName := os.Getenv(PodNameEnv)
165+
if podName == "" {
166+
return nil, fmt.Errorf("required env %s not set, please configure downward API", PodNameEnv)
163167
}
164-
logrus.Debugf("found hostname: %s", hostname)
168+
169+
logrus.Debugf("found podname: %s", podName)
165170

166171
myPod := &corev1.Pod{
167172
TypeMeta: metav1.TypeMeta{
@@ -170,8 +175,8 @@ func myOwnerRef(ctx context.Context, client crclient.Client, ns string) (*metav1
170175
},
171176
}
172177

173-
key := crclient.ObjectKey{Namespace: ns, Name: hostname}
174-
err = client.Get(ctx, key, myPod)
178+
key := crclient.ObjectKey{Namespace: ns, Name: podName}
179+
err := client.Get(ctx, key, myPod)
175180
if err != nil {
176181
logrus.Errorf("failed to get pod: %v", err)
177182
return nil, err

pkg/scaffold/operator.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ spec:
6262
valueFrom:
6363
fieldRef:
6464
fieldPath: metadata.namespace
65+
- name: POD_NAME
66+
valueFrom:
67+
fieldRef:
68+
fieldPath: metadata.name
6569
- name: OPERATOR_NAME
6670
value: "{{.ProjectName}}"
6771
`

pkg/scaffold/operator_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ spec:
6161
valueFrom:
6262
fieldRef:
6363
fieldPath: metadata.namespace
64+
- name: POD_NAME
65+
valueFrom:
66+
fieldRef:
67+
fieldPath: metadata.name
6468
- name: OPERATOR_NAME
6569
value: "app-operator"
6670
`

0 commit comments

Comments
 (0)