Skip to content

Fix revert commit for leader election #3136

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions internal/framework/runnables/runnables.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,34 @@ func (r *LeaderOrNonLeader) NeedLeaderElection() bool {
return false
}

// EnableAfterBecameLeader is a Runnable that will call the enable function when the current instance becomes
// CallFunctionsAfterBecameLeader is a Runnable that will call the given functions when the current instance becomes
// the leader.
type EnableAfterBecameLeader struct {
enable func(context.Context)
type CallFunctionsAfterBecameLeader struct {
enableFunctions []func(context.Context)
}

var (
_ manager.LeaderElectionRunnable = &EnableAfterBecameLeader{}
_ manager.Runnable = &EnableAfterBecameLeader{}
_ manager.LeaderElectionRunnable = &CallFunctionsAfterBecameLeader{}
_ manager.Runnable = &CallFunctionsAfterBecameLeader{}
)

// NewEnableAfterBecameLeader creates a new EnableAfterBecameLeader Runnable.
func NewEnableAfterBecameLeader(enable func(context.Context)) *EnableAfterBecameLeader {
return &EnableAfterBecameLeader{
enable: enable,
// NewCallFunctionsAfterBecameLeader creates a new CallFunctionsAfterBecameLeader Runnable.
func NewCallFunctionsAfterBecameLeader(
enableFunctions []func(context.Context),
) *CallFunctionsAfterBecameLeader {
return &CallFunctionsAfterBecameLeader{
enableFunctions: enableFunctions,
}
}

func (j *EnableAfterBecameLeader) Start(ctx context.Context) error {
j.enable(ctx)
func (j *CallFunctionsAfterBecameLeader) Start(ctx context.Context) error {
for _, f := range j.enableFunctions {
f(ctx)
}

return nil
}

func (j *EnableAfterBecameLeader) NeedLeaderElection() bool {
func (j *CallFunctionsAfterBecameLeader) NeedLeaderElection() bool {
return true
}
19 changes: 11 additions & 8 deletions internal/framework/runnables/runnables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@ func TestLeaderOrNonLeader(t *testing.T) {
g.Expect(leaderOrNonLeader.NeedLeaderElection()).To(BeFalse())
}

func TestEnableAfterBecameLeader(t *testing.T) {
func TestCallFunctionsAfterBecameLeader(t *testing.T) {
t.Parallel()
enabled := false
enableAfterBecameLeader := NewEnableAfterBecameLeader(func(_ context.Context) {
enabled = true
statusUpdaterEnabled := false
provisionerEnabled := false

callFunctionsAfterBecameLeader := NewCallFunctionsAfterBecameLeader([]func(ctx context.Context){
func(_ context.Context) { statusUpdaterEnabled = true },
func(_ context.Context) { provisionerEnabled = true },
})

g := NewWithT(t)
g.Expect(enableAfterBecameLeader.NeedLeaderElection()).To(BeTrue())
g.Expect(enabled).To(BeFalse())
g.Expect(callFunctionsAfterBecameLeader.NeedLeaderElection()).To(BeTrue())

err := enableAfterBecameLeader.Start(context.Background())
err := callFunctionsAfterBecameLeader.Start(context.Background())
g.Expect(err).ToNot(HaveOccurred())

g.Expect(enabled).To(BeTrue())
g.Expect(statusUpdaterEnabled).To(BeTrue())
g.Expect(provisionerEnabled).To(BeTrue())
}
7 changes: 7 additions & 0 deletions internal/mode/static/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,13 @@
return fmt.Errorf("cannot register event loop: %w", err)
}

if err = mgr.Add(runnables.NewCallFunctionsAfterBecameLeader([]func(context.Context){
groupStatusUpdater.Enable,
nginxProvisioner.Enable,
})); err != nil {
return fmt.Errorf("cannot register functions that get called after Pod becomes leader: %w", err)
}

Check warning on line 270 in internal/mode/static/manager.go

View check run for this annotation

Codecov / codecov/patch

internal/mode/static/manager.go#L265-L270

Added lines #L265 - L270 were not covered by tests

if cfg.ProductTelemetryConfig.Enabled {
dataCollector := telemetry.NewDataCollectorImpl(telemetry.DataCollectorConfig{
K8sClientReader: mgr.GetAPIReader(),
Expand Down
Loading