-
Notifications
You must be signed in to change notification settings - Fork 1.8k
pkg/leader: allow setting the pod name explicitly. #617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@mhrivnak PTAL, thanks! |
Assuming the hostname is super dangerous. I'm not sure this should ever be allowed. There can be two pods on the same node at the same time for any operator, and it's very easy to turn on host network without realizing it. We should be encouraging people to use the downward API from within their deployment to pass pod name as an env var / config flag all the time to prevent them accidentally having two leaders running. |
I would recommend changing the SDK to require a unique name and to instruct the user how to do it safely on Kubernetes. Selecting a random name is not safe either. |
@smarterclayton thanks! Sounds like we should just require the environment variable and never use the pod's hostname for pod name detection. @squeed want to make that change in this PR, and have it stop using the hostname at all? It would also help to add a quick blurb at the end of the docs in I'll be adding dev docs for this in the near future anyway and turning it on by default for new operators, so I'm happy to handle adding those docs at that time. |
@mhrivnak PR updated, PTAL. |
Test failure looks like a flake. |
@squeed Yeah seemed like an issue with the test cluster. I've reset the job and it passed. defer to @mhrivnak Also we should add the But maybe @mhrivnak can add that in when we turn on leader election by default. |
pkg/leader/leader.go
Outdated
return nil, err | ||
podName := os.Getenv(PodNameEnv) | ||
if podName == "" { | ||
return nil, errors.New(fmt.Sprintf("required env %s not set, please configure downward API", PodNameEnv)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also errors.New(fmt.Sprintf
==> fmt.Errorf(
@hasbro17 good point; updated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! This looks great. If you'll just fix the tab vs space issue in the scaffolds, this will be ready to merge.
pkg/leader/leader.go
Outdated
@@ -154,14 +156,31 @@ func myNS() (string, error) { | |||
return ns, nil | |||
} | |||
|
|||
// myName returns the name of the pod in which this code is currently running |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/myName/myPodName/
pkg/scaffold/operator.go
Outdated
@@ -61,6 +61,10 @@ spec: | |||
valueFrom: | |||
fieldRef: | |||
fieldPath: metadata.namespace | |||
- name: POD_NAME |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you just fix the indentation here? It appears there are tabs here vs spaces everywhere else, which explains the strange way it looks.
pkg/scaffold/operator_test.go
Outdated
@@ -63,6 +63,10 @@ spec: | |||
valueFrom: | |||
fieldRef: | |||
fieldPath: metadata.namespace | |||
- name: POD_NAME |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
Fixed the spacing issues (and rebased), PTAL. |
/ok-to-test |
pkg/leader/doc.go
Outdated
- name: POD_NAME | ||
valueFrom: | ||
fieldRef: | ||
fieldPath: metadata.name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, looks like there is one more case here of rogue tabs. Mind fixing that up?
We can't trust that the hostname is always the pod name.
@mhrivnak do you have any idea why this PR is not merging? |
Last I looked, I was just waiting to see the CI pass. Looks like that happened, so here we go! |
This is useful for pods running in hostNetwork, where the hostname is not the pod name.