Skip to content

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

Merged
merged 1 commit into from
Oct 23, 2018

Conversation

squeed
Copy link
Contributor

@squeed squeed commented Oct 15, 2018

This is useful for pods running in hostNetwork, where the hostname is not the pod name.

@openshift-ci-robot openshift-ci-robot added size/S Denotes a PR that changes 10-29 lines, ignoring generated files. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 15, 2018
@squeed
Copy link
Contributor Author

squeed commented Oct 15, 2018

@mhrivnak PTAL, thanks!

@smarterclayton
Copy link

smarterclayton commented Oct 16, 2018

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.

@smarterclayton
Copy link

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.

@mhrivnak
Copy link
Member

@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 doc.go that using the downward API for this is a requirement.

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.

@squeed
Copy link
Contributor Author

squeed commented Oct 17, 2018

@mhrivnak PR updated, PTAL.

@squeed
Copy link
Contributor Author

squeed commented Oct 17, 2018

Test failure looks like a flake.

@hasbro17
Copy link
Contributor

@squeed Yeah seemed like an issue with the test cluster. I've reset the job and it passed.
LGTM

defer to @mhrivnak

Also we should add the POD_NAME env to the generated Deployment manifest so it's set by default via the downward API.
In the template:
https://github.com/operator-framework/operator-sdk/blob/master/pkg/scaffold/operator.go#L59
And the unit test:
https://github.com/operator-framework/operator-sdk/blob/master/pkg/scaffold/operator_test.go#L61

But maybe @mhrivnak can add that in when we turn on leader election by default.

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))
Copy link
Contributor

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(

@squeed
Copy link
Contributor Author

squeed commented Oct 17, 2018

@hasbro17 good point; updated.

@openshift-ci-robot openshift-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Oct 17, 2018
Copy link
Member

@mhrivnak mhrivnak left a 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.

@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/myName/myPodName/

@@ -61,6 +61,10 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: POD_NAME
Copy link
Member

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.

@@ -63,6 +63,10 @@ spec:
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: POD_NAME
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

@squeed
Copy link
Contributor Author

squeed commented Oct 22, 2018

Fixed the spacing issues (and rebased), PTAL.

@lilic
Copy link
Member

lilic commented Oct 22, 2018

/ok-to-test

- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
Copy link
Member

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?

@openshift-ci-robot openshift-ci-robot removed the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Oct 22, 2018
We can't trust that the hostname is always the pod name.
@knobunc
Copy link

knobunc commented Oct 23, 2018

@mhrivnak do you have any idea why this PR is not merging?

@mhrivnak
Copy link
Member

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants