Skip to content

Commit 5b14236

Browse files
committed
main.go: support (but deprecated) OPERATOR_NAME for leader lock, add --leader-election-namespace
1 parent d262ebb commit 5b14236

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

main.go

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ var (
4040

4141
func main() {
4242
var (
43-
metricsAddr string
44-
enableLeaderElection bool
45-
leaderElectionID string
43+
metricsAddr string
44+
enableLeaderElection bool
45+
leaderElectionID string
46+
leaderElectionNamespace string
4647

4748
watchesFile string
4849
defaultMaxConcurrentReconciles int
@@ -55,21 +56,18 @@ func main() {
5556
pflag.StringVar(&metricsAddr, "metrics-addr", "0.0.0.0:8383", "The address the metric endpoint binds to.")
5657
pflag.BoolVar(&enableLeaderElection, "enable-leader-election", false,
5758
"Enable leader election for controller manager. Enabling this will ensure there is only one active controller manager.")
58-
pflag.StringVar(&leaderElectionID, "leader-election-id", "leader-lock",
59+
pflag.StringVar(&leaderElectionID, "leader-election-id", "",
5960
"Name of the configmap that is used for holding the leader lock.")
61+
pflag.StringVar(&leaderElectionNamespace, "leader-election-namespace", "",
62+
"Namespace in which to create the leader election configmap for holding the leader lock (required if running locally).")
6063

6164
pflag.StringVar(&watchesFile, "watches-file", "./watches.yaml", "Path to watches.yaml file.")
6265
pflag.DurationVar(&defaultReconcilePeriod, "reconcile-period", 0, "Default reconcile period for controllers (use 0 to disable periodic reconciliation)")
6366
pflag.IntVar(&defaultMaxConcurrentReconciles, "max-concurrent-reconciles", 1, "Default maximum number of concurrent reconciles for controllers.")
6467

65-
// Deprecated: --max-workers flag is currently supported, but it does not align well with the name of the option it
66-
// configures on the controller (MaxConcurrentReconciles). Flag `--max-concurrent-reconciles` should be used
67-
// instead.
68+
// Deprecated: --max-workers flag does not align well with the name of the option it configures on the controller
69+
// (MaxConcurrentReconciles). Flag `--max-concurrent-reconciles` should be used instead.
6870
pflag.IntVar(&defaultMaxWorkers, "max-workers", 1, "Default maximum number of concurrent reconciles for controllers.")
69-
if err := pflag.CommandLine.MarkDeprecated("max-workers", "use --default-max-concurrent-reconciles instead"); err != nil {
70-
setupLog.Error(err, "failed to mark --max-workers flag as deprecated")
71-
os.Exit(1)
72-
}
7371
if err := pflag.CommandLine.MarkHidden("max-workers"); err != nil {
7472
setupLog.Error(err, "failed to hide --max-workers flag")
7573
os.Exit(1)
@@ -86,18 +84,34 @@ func main() {
8684
zapl.StacktraceLevel(&sttLvl),
8785
))
8886

87+
// Deprecated: --max-workers flag does not align well with the name of the option it configures on the controller
88+
// (MaxConcurrentReconciles). Flag `--max-concurrent-reconciles` should be used instead.
8989
if pflag.Lookup("max-workers").Changed {
90-
if !pflag.Lookup("max-concurrent-reconciles").Changed {
90+
setupLog.Info("flag --max-workers has been deprecated, use --max-concurrent-reconciles instead")
91+
if pflag.Lookup("max-concurrent-reconciles").Changed {
92+
setupLog.Info("ignoring --max-workers since --max-concurrent-reconciles is set")
93+
} else {
9194
defaultMaxConcurrentReconciles = defaultMaxWorkers
9295
}
93-
setupLog.Info("ignoring --max-workers since --max-concurrent-reconciles is set")
96+
}
97+
98+
// Deprecated: OPERATOR_NAME environment variable is an artifact of the legacy operator-sdk project scaffolding.
99+
// Flag `--leader-election-id` should be used instead.
100+
if operatorName, found := os.LookupEnv("OPERATOR_NAME"); found {
101+
setupLog.Info("environment variable OPERATOR_NAME has been deprecated, use --leader-election-id instead.")
102+
if pflag.Lookup("leader-election-id").Changed {
103+
setupLog.Info("ignoring OPERATOR_NAME environment variable since --leader-election-id is set")
104+
} else {
105+
leaderElectionID = operatorName
106+
}
94107
}
95108

96109
options := ctrl.Options{
97-
MetricsBindAddress: "0.0.0.0:8383",
98-
LeaderElection: enableLeaderElection,
99-
LeaderElectionID: leaderElectionID,
100-
NewClient: manager.NewDelegatingClientFunc(),
110+
MetricsBindAddress: "0.0.0.0:8383",
111+
LeaderElection: enableLeaderElection,
112+
LeaderElectionID: leaderElectionID,
113+
LeaderElectionNamespace: leaderElectionNamespace,
114+
NewClient: manager.NewDelegatingClientFunc(),
101115
}
102116
manager.ConfigureWatchNamespaces(&options, setupLog)
103117
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), options)

0 commit comments

Comments
 (0)