-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Problem: We want to have a telemetry job that periodically reports product telemetry every 24h. For now, telemetry data is empty and report is sent to the debug log. Solution: - Refactor leader election to use controller-runtime manager capabilities. This simplifies the existing code and make it easier to add a telemetry Job. - Add a telemetry Job that periodically reports empty telemetry to the debug log. - Make the period configurable at build time via TELEMETRY_REPORT_PERIOD Makefile variable. Note: leader elector refactoring changes behavior of NGF process when leadership gets lost: Before: the Manager would shutdown waiting for the runnables to exit. After: the Manager doesn't wait. It similar to NGF process panicing. This should be OK, as NGF container will restart and recover any potentially broken state (update not fully populated statuses, restore correct NGINX configuration). Testing: - Unit tests - Manual testing: - Ensure leader election works as expected - both leader and non-pods run successfully. - Ensure NGF container exits when stop being leader. - Ensure an upgrade from Release 1.1.0 is successful for leader election - the leader gets elected among the new pods. - Ensure the telemetry Job reports telemetry multiple times, using a small value of ELEMETRY_REPORT_PERIOD CLOSES #1382 Co-authored-by: Saylor Berman <[email protected]>
- Loading branch information
Showing
18 changed files
with
479 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/* | ||
Package runnables provides helper types for creating runnables for the controller-runtime manager when | ||
leader election is enabled. | ||
*/ | ||
package runnables |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package runnables | ||
|
||
import ( | ||
"context" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/manager" | ||
) | ||
|
||
// Leader is a Runnable that needs to be run only when the current instance is the leader. | ||
type Leader struct { | ||
manager.Runnable | ||
} | ||
|
||
var ( | ||
_ manager.LeaderElectionRunnable = &Leader{} | ||
_ manager.Runnable = &Leader{} | ||
) | ||
|
||
func (r *Leader) NeedLeaderElection() bool { | ||
return true | ||
} | ||
|
||
// LeaderOrNonLeader is a Runnable that needs to be run regardless of whether the current instance is the leader. | ||
type LeaderOrNonLeader struct { | ||
manager.Runnable | ||
} | ||
|
||
var ( | ||
_ manager.LeaderElectionRunnable = &LeaderOrNonLeader{} | ||
_ manager.Runnable = &LeaderOrNonLeader{} | ||
) | ||
|
||
func (r *LeaderOrNonLeader) NeedLeaderElection() bool { | ||
return false | ||
} | ||
|
||
// EnableAfterBecameLeader is a Runnable that will call the enable function when the current instance becomes | ||
// the leader. | ||
type EnableAfterBecameLeader struct { | ||
enable func(context.Context) | ||
} | ||
|
||
var ( | ||
_ manager.LeaderElectionRunnable = &EnableAfterBecameLeader{} | ||
_ manager.Runnable = &EnableAfterBecameLeader{} | ||
) | ||
|
||
// NewEnableAfterBecameLeader creates a new EnableAfterBecameLeader Runnable. | ||
func NewEnableAfterBecameLeader(enable func(context.Context)) *EnableAfterBecameLeader { | ||
return &EnableAfterBecameLeader{ | ||
enable: enable, | ||
} | ||
} | ||
|
||
func (j *EnableAfterBecameLeader) Start(ctx context.Context) error { | ||
j.enable(ctx) | ||
return nil | ||
} | ||
|
||
func (j *EnableAfterBecameLeader) NeedLeaderElection() bool { | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package runnables | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func TestLeader(t *testing.T) { | ||
leader := &Leader{} | ||
|
||
g := NewWithT(t) | ||
g.Expect(leader.NeedLeaderElection()).To(BeTrue()) | ||
} | ||
|
||
func TestLeaderOrNonLeader(t *testing.T) { | ||
leaderOrNonLeader := &LeaderOrNonLeader{} | ||
|
||
g := NewWithT(t) | ||
g.Expect(leaderOrNonLeader.NeedLeaderElection()).To(BeFalse()) | ||
} | ||
|
||
func TestEnableAfterBecameLeader(t *testing.T) { | ||
enabled := false | ||
enableAfterBecameLeader := NewEnableAfterBecameLeader(func(_ context.Context) { | ||
enabled = true | ||
}) | ||
|
||
g := NewWithT(t) | ||
g.Expect(enableAfterBecameLeader.NeedLeaderElection()).To(BeTrue()) | ||
g.Expect(enabled).To(BeFalse()) | ||
|
||
err := enableAfterBecameLeader.Start(context.Background()) | ||
g.Expect(err).To(BeNil()) | ||
|
||
g.Expect(enabled).To(BeTrue()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* | ||
Package telemetry is responsible for collecting and sending product telemetry data. | ||
*/ | ||
package telemetry |
Oops, something went wrong.