Skip to content

Commit 1252761

Browse files
authored
feat: use controller runtime leader election to allow multiple instances of the operator to run at the same time (#4)
1 parent 3d658a4 commit 1252761

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

cmd/manager/main.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323

2424
"github.com/operator-framework/operator-sdk/pkg/k8sutil"
2525
kubemetrics "github.com/operator-framework/operator-sdk/pkg/kube-metrics"
26-
"github.com/operator-framework/operator-sdk/pkg/leader"
2726
"github.com/operator-framework/operator-sdk/pkg/log/zap"
2827
"github.com/operator-framework/operator-sdk/pkg/metrics"
2928
sdkVersion "github.com/operator-framework/operator-sdk/version"
@@ -53,6 +52,13 @@ func init() {
5352
"t",
5453
map[string]string{},
5554
"Tags to add to the aws instances on which the cluster nodes run on")
55+
56+
pflag.StringVarP(
57+
&flags.LeaderElectionNamespace,
58+
"leader-election-namespace",
59+
"l",
60+
"",
61+
"The leader election namespace. Will auto-discover if not provided")
5662
}
5763

5864
func printVersion() {
@@ -106,17 +112,16 @@ func main() {
106112
os.Exit(1)
107113
}
108114

109-
ctx := context.TODO()
110-
// Become the leader before proceeding
111-
err = leader.Become(ctx, "node-tagger-lock")
112-
if err != nil {
113-
log.Error(err, "")
114-
os.Exit(1)
115-
}
116115

117116
managerOptions := manager.Options{
118117
MetricsBindAddress: fmt.Sprintf("%s:%d", metricsHost, metricsPort),
119118
HealthProbeBindAddress: fmt.Sprintf("%s:%d", healthProbeHost, healthProbePort),
119+
LeaderElection: true,
120+
LeaderElectionID: "node-tagger-lock",
121+
}
122+
123+
if flags.LeaderElectionNamespace != "" {
124+
managerOptions.LeaderElectionNamespace = flags.LeaderElectionNamespace
120125
}
121126

122127
// Create a new Cmd to provide shared dependencies and start components
@@ -155,7 +160,7 @@ func main() {
155160
if err != nil {
156161
log.Error(err, "Error getting service Monitor namespace")
157162
} else {
158-
addMetrics(ctx, cfg, serviceMonitorNamespace)
163+
addMetrics(context.TODO(), cfg, serviceMonitorNamespace)
159164
}
160165

161166
log.Info("Starting the Cmd.")

pkg/flags/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
package flags
22

33
var InstanceTags map[string]string
4+
var LeaderElectionNamespace string

0 commit comments

Comments
 (0)