Skip to content

Commit

Permalink
use leases AND configmaps for leader election (kubevirt#2140)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Henriksen <mhenriks@redhat.com>
  • Loading branch information
mhenriks authored Feb 4, 2022
1 parent 013cb6b commit 73ac262
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
9 changes: 8 additions & 1 deletion cmd/cdi-controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,14 @@ func start(ctx context.Context, cfg *rest.Config) {
klog.Fatalf("Error building extClient: %s", err.Error())
}

mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{})
opts := manager.Options{
LeaderElection: true,
LeaderElectionNamespace: namespace,
LeaderElectionID: "cdi-controller-leader-election-helper",
LeaderElectionResourceLock: "leases",
}

mgr, err := manager.New(config.GetConfigOrDie(), opts)
if err != nil {
klog.Errorf("Unable to setup controller manager: %v", err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion cmd/cdi-operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func main() {
LeaderElection: true,
LeaderElectionNamespace: namespace,
LeaderElectionID: "cdi-operator-leader-election-helper",
LeaderElectionResourceLock: "configmaps",
LeaderElectionResourceLock: "configmapsleases",
}

// Create a new Manager to provide shared dependencies and start components
Expand Down
11 changes: 11 additions & 0 deletions pkg/operator/resources/namespaced/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,17 @@ func createControllerRole() *rbacv1.Role {
"delete",
},
},
{
APIGroups: []string{
"coordination.k8s.io",
},
Resources: []string{
"leases",
},
Verbs: []string{
"*",
},
},
}
return utils.ResourceBuilder.CreateRole(controllerResourceName, rules)
}
Expand Down
11 changes: 11 additions & 0 deletions pkg/operator/resources/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,17 @@ func getNamespacedPolicyRules() []rbacv1.PolicyRule {
"patch",
},
},
{
APIGroups: []string{
"coordination.k8s.io",
},
Resources: []string{
"leases",
},
Verbs: []string{
"*",
},
},
}
return rules
}
Expand Down

0 comments on commit 73ac262

Please sign in to comment.