Skip to content

allow leader election to accept namespace #1

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

Closed
wants to merge 2 commits into from

Conversation

jmrodri
Copy link
Member

@jmrodri jmrodri commented Jul 21, 2020

  • remove isRunLocal()
  • change errNoNamespace value, it now returns
    "Namespace is required when running outside the cluster"
  • update tests

Copy link
Member

@joelanford joelanford left a comment

Choose a reason for hiding this comment

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

If the POD_NAME environment variable is not set, I think we could use this code. That way, we would no longer require operators to set the POD_NAME environment variable using the downward API in their deployment (as long as they don't override hostname in their pod spec).

This behavior would need to be documented.

func Become(ctx context.Context, lockName string) error {
// leader. Upon termination of that pod, the garbage collector will delete
// the ConfigMap, enabling a different pod to become the leader.
func Become(ctx context.Context, lockName string, ns string) error {
Copy link
Member

Choose a reason for hiding this comment

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

WDYT of duplicating the leaderelection.Options struct from controller-runtime and using that as a parameter to leader.Become()

Suggested change
func Become(ctx context.Context, lockName string, ns string) error {
func Become(ctx context.Context, opts Options) error {

@jmrodri jmrodri changed the base branch from master to main July 21, 2020 22:02
@@ -210,10 +198,7 @@ func isPodEvicted(pod corev1.Pod) bool {

// getOperatorNamespace returns the namespace the operator should be running in.
func getOperatorNamespace() (string, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: have this take a path to make it testable (without resetting a global in tests)

Suggested change
func getOperatorNamespace() (string, error) {
func getOperatorNamespace(namespacePath string) (string, error) {

@mhrivnak
Copy link
Member

This approach to leader election depends on a "lease" resource, implemented with a ConfigMap, having an owner reference to the Pod that the operator is running in. If the Pod goes away, the lease automatically goes with it, and thus someone else can become the leader.

But given that, I don't see how this is going to work if the operator itself is either running outside the cluster, or running in a different namespace than where the lease is. Mind elaborating on the goal and how it's going to work?

@joelanford
Copy link
Member

joelanford commented Jul 22, 2020

@mhrivnak Yeah, that's totally right. I had lost track of that constraint. Given that, it doesn't make sense to accept an explicit namespace since this will only ever work in-cluster, and owner references don't work across namespace boundaries.

Do you think it's reasonable to use the pod hostname if POD_NAME isn't set (as long as we document that the pod spec hostname value should not be overridden)?

Or should we keep the hard requirement on POD_NAME?

@coveralls
Copy link

coveralls commented Jul 22, 2020

Pull Request Test Coverage Report for Build 178610974

  • 3 of 7 (42.86%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-57.6%) to 42.396%

Changes Missing Coverage Covered Lines Changed/Added Lines %
leader/leader.go 3 7 42.86%
Totals Coverage Status
Change from base Build 178609315: -57.6%
Covered Lines: 92
Relevant Lines: 217

💛 - Coveralls

jmrodri added 2 commits July 22, 2020 10:55
* remove isRunLocal()
* change errNoNamespace value, it now returns
  "Namespace is required when running outside the cluster"
* update tests
@mhrivnak
Copy link
Member

Do you think it's reasonable to use the pod hostname if POD_NAME isn't set (as long as we document that the pod spec hostname value should not be overridden)?

Or should we keep the hard requirement on POD_NAME?

My first implementation used the pod hostname, but that was changed after it was pointed out as not being a reliable way to identify the pod name. Based on the comments, it sounds like the hostname should be avoided.

@jmrodri
Copy link
Member Author

jmrodri commented Jul 22, 2020

Based on the comments above, it sounds like it would be a bad idea to accept a namespace parameter. Closing this PR.

@jmrodri jmrodri closed this Jul 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants