Skip to content

Commit

Permalink
use configmapsleases for leader election
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzujian committed May 13, 2022
1 parent 673138f commit 0c25d8e
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
12 changes: 12 additions & 0 deletions dist/images/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1468,6 +1468,12 @@ rules:
- create
- patch
- update
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- "*"
- apiGroups:
- "k8s.cni.cncf.io"
resources:
Expand Down Expand Up @@ -1955,6 +1961,12 @@ rules:
- create
- patch
- update
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- "*"
- apiGroups:
- "k8s.cni.cncf.io"
resources:
Expand Down
21 changes: 16 additions & 5 deletions pkg/controller/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"time"

"github.com/kubeovn/kube-ovn/pkg/util"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
Expand All @@ -14,6 +13,8 @@ import (
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/client-go/tools/record"
"k8s.io/klog/v2"

"github.com/kubeovn/kube-ovn/pkg/util"
)

const ovnLeaderElector = "ovn-controller-leader-elector"
Expand Down Expand Up @@ -107,18 +108,28 @@ func setupLeaderElection(config *leaderElectionConfig) *leaderelection.LeaderEle
Host: hostname,
})

lock := resourcelock.ConfigMapLock{
leaseLock := &resourcelock.LeaseLock{
LeaseMeta: metav1.ObjectMeta{Namespace: config.PodNamespace, Name: config.ElectionID},
Client: config.Client.CoordinationV1(),
LockConfig: resourcelock.ResourceLockConfig{
Identity: config.PodName,
EventRecorder: recorder,
},
}
cmLock := &resourcelock.ConfigMapLock{
ConfigMapMeta: metav1.ObjectMeta{Namespace: config.PodNamespace, Name: config.ElectionID},
Client: config.Client.CoreV1(),
LockConfig: resourcelock.ResourceLockConfig{
Identity: config.PodName,
EventRecorder: recorder,
},
}
lock := resourcelock.MultiLock{
Primary: leaseLock,
Secondary: cmLock,
}

var err error

elector, err = leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
elector, err := leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
Lock: &lock,
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
Expand Down
6 changes: 6 additions & 0 deletions yamls/ovn-dpdk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ rules:
- create
- patch
- update
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- "*"
- apiGroups:
- "k8s.cni.cncf.io"
resources:
Expand Down
6 changes: 6 additions & 0 deletions yamls/ovn-ha.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ rules:
- create
- patch
- update
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- "*"
- apiGroups:
- "k8s.cni.cncf.io"
resources:
Expand Down
6 changes: 6 additions & 0 deletions yamls/ovn.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ rules:
- create
- patch
- update
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- "*"
- apiGroups:
- "k8s.cni.cncf.io"
resources:
Expand Down

0 comments on commit 0c25d8e

Please sign in to comment.