Skip to content

Commit

Permalink
Use leader-with-lease based election in the operator
Browse files Browse the repository at this point in the history
...instead of leader-for-life. The latter was implemented in the
operator SDK early on before controller-runtime supported
leader-with-lease. However leader-with-lease is the preferred
method and leader-for-life is being proposed for deprecation
(see operator-framework/operator-lib#117).

Signed-off-by: Tom Pantelis <[email protected]>
  • Loading branch information
tpantelis committed Dec 13, 2024
1 parent e5d27f3 commit 5fbba06
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 24 deletions.
11 changes: 1 addition & 10 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"runtime"

configv1 "github.com/openshift/api/config/v1"
"github.com/operator-framework/operator-lib/leader"
"github.com/submariner-io/admiral/pkg/log/kzerolog"
"github.com/submariner-io/admiral/pkg/names"
admversion "github.com/submariner-io/admiral/pkg/version"
Expand Down Expand Up @@ -84,8 +83,7 @@ func main() {
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
flag.StringVar(&pprofAddr, "pprof-bind-address", ":8082", "The address the profiling endpoint binds to.")
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
"Enable leader election for the controller manager to ensure there is only one active instance.")

kzerolog.AddFlags(nil)
flag.Parse()
Expand Down Expand Up @@ -121,13 +119,6 @@ func main() {

ctx := ctrl.SetupSignalHandler()

// Become the leader before proceeding
err = leader.Become(ctx, "submariner-operator-lock")
if err != nil {
log.Error(err, "")
os.Exit(1)
}

// Set up the CRDs we need
crdUpdater, err := crd.UpdaterFromRestConfig(cfg)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ spec:
image: controller:0.0.0
command:
- submariner-operator
# args:
# - --enable-leader-election
args:
- --leader-elect
imagePullPolicy: Always
env:
- name: WATCH_NAMESPACE
Expand Down
11 changes: 11 additions & 0 deletions config/rbac/submariner-operator/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,14 @@ rules:
- servicediscoveries/finalizers
verbs:
- update
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- get
- list
- watch
- create
- update
- delete
35 changes: 23 additions & 12 deletions pkg/embeddedyamls/yamls.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.1
controller-gen.kubebuilder.io/version: v0.16.5
name: brokers.submariner.io
spec:
group: submariner.io
Expand Down Expand Up @@ -105,7 +105,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.1
controller-gen.kubebuilder.io/version: v0.16.5
name: submariners.submariner.io
spec:
group: submariner.io
Expand Down Expand Up @@ -1118,7 +1118,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.1
controller-gen.kubebuilder.io/version: v0.16.5
name: servicediscoveries.submariner.io
spec:
group: submariner.io
Expand Down Expand Up @@ -1277,7 +1277,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.1
controller-gen.kubebuilder.io/version: v0.16.5
name: clusters.submariner.io
spec:
group: submariner.io
Expand Down Expand Up @@ -1348,7 +1348,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.1
controller-gen.kubebuilder.io/version: v0.16.5
name: endpoints.submariner.io
spec:
group: submariner.io
Expand Down Expand Up @@ -1429,7 +1429,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.1
controller-gen.kubebuilder.io/version: v0.16.5
name: gateways.submariner.io
spec:
group: submariner.io
Expand Down Expand Up @@ -1602,7 +1602,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.1
controller-gen.kubebuilder.io/version: v0.16.5
name: clusterglobalegressips.submariner.io
spec:
group: submariner.io
Expand Down Expand Up @@ -1728,7 +1728,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.1
controller-gen.kubebuilder.io/version: v0.16.5
name: globalegressips.submariner.io
spec:
group: submariner.io
Expand Down Expand Up @@ -1903,7 +1903,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.1
controller-gen.kubebuilder.io/version: v0.16.5
name: globalingressips.submariner.io
spec:
group: submariner.io
Expand Down Expand Up @@ -2056,7 +2056,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.1
controller-gen.kubebuilder.io/version: v0.16.5
name: gatewayroutes.submariner.io
spec:
group: submariner.io
Expand Down Expand Up @@ -2117,7 +2117,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.1
controller-gen.kubebuilder.io/version: v0.16.5
name: nongatewayroutes.submariner.io
spec:
group: submariner.io
Expand Down Expand Up @@ -2178,7 +2178,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.16.1
controller-gen.kubebuilder.io/version: v0.16.5
name: routeagents.submariner.io
spec:
group: submariner.io
Expand Down Expand Up @@ -2808,6 +2808,17 @@ rules:
- servicediscoveries/finalizers
verbs:
- update
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- get
- list
- watch
- create
- update
- delete
`
Config_rbac_submariner_operator_role_binding_yaml = `---
kind: RoleBinding
Expand Down

0 comments on commit 5fbba06

Please sign in to comment.