-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
🐛 prevent leader election when shutting down a non-elected manager #2724
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -378,6 +378,85 @@ var _ = Describe("manger.Manager", func() { | |
|
||
Expect(cm.gracefulShutdownTimeout.Nanoseconds()).To(Equal(int64(0))) | ||
}) | ||
|
||
It("should prevent leader election when shutting down a non-elected manager", func() { | ||
var rl resourcelock.Interface | ||
m1, err := New(cfg, Options{ | ||
LeaderElection: true, | ||
LeaderElectionNamespace: "default", | ||
LeaderElectionID: "test-leader-election-id", | ||
newResourceLock: func(config *rest.Config, recorderProvider recorder.Provider, options leaderelection.Options) (resourcelock.Interface, error) { | ||
var err error | ||
rl, err = leaderelection.NewResourceLock(config, recorderProvider, options) | ||
return rl, err | ||
}, | ||
HealthProbeBindAddress: "0", | ||
Metrics: metricsserver.Options{BindAddress: "0"}, | ||
PprofBindAddress: "0", | ||
}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(m1).ToNot(BeNil()) | ||
Expect(rl.Describe()).To(Equal("default/test-leader-election-id")) | ||
|
||
m1cm, ok := m1.(*controllerManager) | ||
Expect(ok).To(BeTrue()) | ||
m1cm.onStoppedLeading = func() {} | ||
|
||
m2, err := New(cfg, Options{ | ||
LeaderElection: true, | ||
LeaderElectionNamespace: "default", | ||
LeaderElectionID: "test-leader-election-id", | ||
newResourceLock: func(config *rest.Config, recorderProvider recorder.Provider, options leaderelection.Options) (resourcelock.Interface, error) { | ||
var err error | ||
rl, err = leaderelection.NewResourceLock(config, recorderProvider, options) | ||
return rl, err | ||
}, | ||
HealthProbeBindAddress: "0", | ||
Metrics: metricsserver.Options{BindAddress: "0"}, | ||
PprofBindAddress: "0", | ||
}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(m2).ToNot(BeNil()) | ||
Expect(rl.Describe()).To(Equal("default/test-leader-election-id")) | ||
|
||
m1done := make(chan struct{}) | ||
Expect(m1.Add(RunnableFunc(func(ctx context.Context) error { | ||
defer GinkgoRecover() | ||
close(m1done) | ||
return nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you simulate a Runnable that blocks until the context is canceled? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. & make sure it is stopped before manager.Start returns? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Edit] Done |
||
}))).To(Succeed()) | ||
|
||
ctx1, cancel1 := context.WithCancel(context.Background()) | ||
defer cancel1() | ||
go func() { | ||
defer GinkgoRecover() | ||
Expect(m1.Elected()).ShouldNot(BeClosed()) | ||
Expect(m1.Start(ctx1)).NotTo(HaveOccurred()) | ||
}() | ||
<-m1.Elected() | ||
<-m1done | ||
|
||
electionRunnable := &needElection{make(chan struct{})} | ||
|
||
Expect(m2.Add(electionRunnable)).To(Succeed()) | ||
|
||
ctx2, cancel2 := context.WithCancel(context.Background()) | ||
m2done := make(chan struct{}) | ||
go func() { | ||
defer GinkgoRecover() | ||
Expect(m2.Start(ctx2)).NotTo(HaveOccurred()) | ||
close(m2done) | ||
}() | ||
Consistently(m2.Elected()).ShouldNot(Receive()) | ||
|
||
go func() { | ||
defer GinkgoRecover() | ||
Consistently(electionRunnable.ch).ShouldNot(Receive()) | ||
}() | ||
cancel2() | ||
<-m2done | ||
}) | ||
|
||
It("should default ID to controller-runtime if ID is not set", func() { | ||
var rl resourcelock.Interface | ||
m1, err := New(cfg, Options{ | ||
|
@@ -1929,3 +2008,16 @@ func (f *fakeDeferredLoader) InjectScheme(scheme *runtime.Scheme) error { | |
type metricsDefaultServer interface { | ||
GetBindAddr() string | ||
} | ||
|
||
type needElection struct { | ||
ch chan struct{} | ||
} | ||
|
||
func (n *needElection) Start(_ context.Context) error { | ||
n.ch <- struct{}{} | ||
return nil | ||
} | ||
|
||
func (n *needElection) NeedLeaderElection() bool { | ||
return true | ||
} |
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 add a test, please?
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.
Added the test in 06d575d.