Skip to content
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

add leader election mechanism #1285

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

basit9958
Copy link

@basit9958 basit9958 commented Aug 6, 2023

What this PR does / why we need it:

Added Leader Election Mechanism

OnStartedLeading: This callback is executed when the current instance becomes the leader. It starts running the controller logic by calling the Run function.
OnStoppedLeading: This callback is executed when the current instance is no longer the leader. It logs a message and may exit gracefully if needed.
OnNewLeader: This callback is executed when a new leader is elected. It logs a message indicating the new leader's identity.

Which issue(s) this PR fixes:

Fixes #838

Does this PR introduce a user-facing change?


Additional Notes for your reviewer:

Review Checklist:
  • Follows the developer guidelines
  • Relevant tests are added or updated
  • Relevant docs in this repo added or updated
  • Relevant carvel.dev docs added or updated in a separate PR and there's
    a link to that PR
  • Code is at least as readable and maintainable as it was before this
    change

Additional documentation e.g., Proposal, usage docs, etc.:


client *clientset.Clientset
)

func getNewLock(lockname, podname, namespace string) *resourcelock.LeaseLock {
Copy link
Contributor

Choose a reason for hiding this comment

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

Early Feedback: Can we move all the leader election code to a different file / module? Main ought to be as simple as possible (just start the app)

Copy link
Author

Choose a reason for hiding this comment

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

okay will look into this

@neil-hickey
Copy link
Contributor

Hi @basit9958 , thanks for this!

Is this PR intended to be for demonstration / Proof of Concept purposes - or the intended feature complete? If it's intended to be complete, looks like it doesn't compile right now.

+ go build -trimpath -mod=vendor -o controller ./cmd/controller/...
# github.com/vmware-tanzu/carvel-kapp-controller/cmd/controller
Error: cmd/controller/main.go:113:20: cannot use lock (variable of type *resourcelock.LeaseLock) as "context".Context value in argument to runLeaderElection: *resourcelock.LeaseLock does not implement "context".Context (missing method Deadline)
Error: cmd/controller/main.go:113:26: cannot use ctx (variable of type "context".Context) as *resourcelock.LeaseLock value in argument to runLeaderElection
Error: Process completed with exit code 1.

@basit9958
Copy link
Author

Hi @basit9958 , thanks for this!

Is this PR intended to be for demonstration / Proof of Concept purposes - or the intended feature complete? If it's intended to be complete, looks like it doesn't compile right now.

+ go build -trimpath -mod=vendor -o controller ./cmd/controller/...
# github.com/vmware-tanzu/carvel-kapp-controller/cmd/controller
Error: cmd/controller/main.go:113:20: cannot use lock (variable of type *resourcelock.LeaseLock) as "context".Context value in argument to runLeaderElection: *resourcelock.LeaseLock does not implement "context".Context (missing method Deadline)
Error: cmd/controller/main.go:113:26: cannot use ctx (variable of type "context".Context) as *resourcelock.LeaseLock value in argument to runLeaderElection
Error: Process completed with exit code 1.

It is meant to be complete, I will fix these errors

Signed-off-by: Basit Hasan <hasanbasit13@gmail.com>
Signed-off-by: Basit Hasan <hasanbasit13@gmail.com>
Signed-off-by: Basit Hasan <hasanbasit13@gmail.com>
Signed-off-by: Basit Hasan <hasanbasit13@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

Successfully merging this pull request may close these issues.

kapp-controller leader election should provide safety during update operations
2 participants