Skip to content

Commit

Permalink
chore: make leader election ID configurable (#1797)
Browse files Browse the repository at this point in the history
  • Loading branch information
saurav-agarwalla authored Nov 6, 2024
1 parent ab33542 commit fea57a1
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func NewOperator() (context.Context, *Operator) {
mgrOpts := ctrl.Options{
Logger: logging.IgnoreDebugEvents(logger),
LeaderElection: !options.FromContext(ctx).DisableLeaderElection,
LeaderElectionID: "karpenter-leader-election",
LeaderElectionID: options.FromContext(ctx).LeaderElectionName,
LeaderElectionNamespace: options.FromContext(ctx).LeaderElectionNamespace,
LeaderElectionResourceLock: resourcelock.LeasesResourceLock,
LeaderElectionReleaseOnCancel: true,
Expand Down
2 changes: 2 additions & 0 deletions pkg/operator/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Options struct {
KubeClientBurst int
EnableProfiling bool
DisableLeaderElection bool
LeaderElectionName string
LeaderElectionNamespace string
MemoryLimit int64
LogLevel string
Expand Down Expand Up @@ -88,6 +89,7 @@ func (o *Options) AddFlags(fs *FlagSet) {
fs.IntVar(&o.KubeClientBurst, "kube-client-burst", env.WithDefaultInt("KUBE_CLIENT_BURST", 300), "The maximum allowed burst of queries to the kube-apiserver")
fs.BoolVarWithEnv(&o.EnableProfiling, "enable-profiling", "ENABLE_PROFILING", false, "Enable the profiling on the metric endpoint")
fs.BoolVarWithEnv(&o.DisableLeaderElection, "disable-leader-election", "DISABLE_LEADER_ELECTION", false, "Disable the leader election client before executing the main loop. Disable when running replicated components for high availability is not desired.")
fs.StringVar(&o.LeaderElectionName, "leader-election-name", env.WithDefaultString("LEADER_ELECTION_NAME", "karpenter-leader-election"), "Leader election name to create and monitor the lease if running outside the cluster")
fs.StringVar(&o.LeaderElectionNamespace, "leader-election-namespace", env.WithDefaultString("LEADER_ELECTION_NAMESPACE", ""), "Leader election namespace to create and monitor the lease if running outside the cluster")
fs.Int64Var(&o.MemoryLimit, "memory-limit", env.WithDefaultInt64("MEMORY_LIMIT", -1), "Memory limit on the container running the controller. The GC soft memory limit is set to 90% of this value.")
fs.StringVar(&o.LogLevel, "log-level", env.WithDefaultString("LOG_LEVEL", "info"), "Log verbosity level. Can be one of 'debug', 'info', or 'error'")
Expand Down
6 changes: 6 additions & 0 deletions pkg/operator/options/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ var _ = Describe("Options", func() {
KubeClientBurst: lo.ToPtr(300),
EnableProfiling: lo.ToPtr(false),
DisableLeaderElection: lo.ToPtr(false),
LeaderElectionName: lo.ToPtr("karpenter-leader-election"),
LeaderElectionNamespace: lo.ToPtr(""),
MemoryLimit: lo.ToPtr[int64](-1),
LogLevel: lo.ToPtr("info"),
Expand All @@ -127,6 +128,7 @@ var _ = Describe("Options", func() {
"--kube-client-burst", "0",
"--enable-profiling",
"--disable-leader-election=true",
"--leader-election-name=karpenter-controller",
"--leader-election-namespace=karpenter",
"--memory-limit", "0",
"--log-level", "debug",
Expand All @@ -145,6 +147,7 @@ var _ = Describe("Options", func() {
KubeClientBurst: lo.ToPtr(0),
EnableProfiling: lo.ToPtr(true),
DisableLeaderElection: lo.ToPtr(true),
LeaderElectionName: lo.ToPtr("karpenter-controller"),
LeaderElectionNamespace: lo.ToPtr("karpenter"),
MemoryLimit: lo.ToPtr[int64](0),
LogLevel: lo.ToPtr("debug"),
Expand All @@ -166,6 +169,7 @@ var _ = Describe("Options", func() {
os.Setenv("KUBE_CLIENT_BURST", "0")
os.Setenv("ENABLE_PROFILING", "true")
os.Setenv("DISABLE_LEADER_ELECTION", "true")
os.Setenv("LEADER_ELECTION_NAME", "karpenter-controller")
os.Setenv("LEADER_ELECTION_NAMESPACE", "karpenter")
os.Setenv("MEMORY_LIMIT", "0")
os.Setenv("LOG_LEVEL", "debug")
Expand All @@ -188,6 +192,7 @@ var _ = Describe("Options", func() {
KubeClientBurst: lo.ToPtr(0),
EnableProfiling: lo.ToPtr(true),
DisableLeaderElection: lo.ToPtr(true),
LeaderElectionName: lo.ToPtr("karpenter-controller"),
LeaderElectionNamespace: lo.ToPtr("karpenter"),
MemoryLimit: lo.ToPtr[int64](0),
LogLevel: lo.ToPtr("debug"),
Expand Down Expand Up @@ -232,6 +237,7 @@ var _ = Describe("Options", func() {
KubeClientBurst: lo.ToPtr(0),
EnableProfiling: lo.ToPtr(true),
DisableLeaderElection: lo.ToPtr(true),
LeaderElectionName: lo.ToPtr("karpenter-leader-election"),
LeaderElectionNamespace: lo.ToPtr(""),
MemoryLimit: lo.ToPtr[int64](0),
LogLevel: lo.ToPtr("debug"),
Expand Down
1 change: 1 addition & 0 deletions pkg/test/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type OptionsFields struct {
KubeClientBurst *int
EnableProfiling *bool
DisableLeaderElection *bool
LeaderElectionName *string
LeaderElectionNamespace *string
MemoryLimit *int64
LogLevel *string
Expand Down

0 comments on commit fea57a1

Please sign in to comment.