Skip to content

Commit

Permalink
use leases for leader election (#1529)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangzujian committed Aug 3, 2022
1 parent f02df1a commit 29f3d6e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 39 deletions.
27 changes: 12 additions & 15 deletions dist/images/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1360,14 +1360,6 @@ EOF

if $DPDK; then
cat <<EOF > ovn.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: ovn-config
namespace: kube-system
data:
defaultNetworkType: '$NETWORK_TYPE'
---
apiVersion: v1
kind: ServiceAccount
Expand Down Expand Up @@ -1470,6 +1462,12 @@ rules:
- create
- patch
- update
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- "*"
- apiGroups:
- "k8s.cni.cncf.io"
resources:
Expand Down Expand Up @@ -1858,13 +1856,6 @@ EOF

else
cat <<EOF > ovn.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: ovn-config
namespace: kube-system
data:
defaultNetworkType: '$NETWORK_TYPE'
---
apiVersion: v1
kind: ServiceAccount
Expand Down Expand Up @@ -1956,6 +1947,12 @@ rules:
- create
- patch
- update
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- "*"
- apiGroups:
- "k8s.cni.cncf.io"
resources:
Expand Down
13 changes: 7 additions & 6 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 All @@ -38,7 +39,7 @@ func (c *Controller) isLeader() bool {
func (c *Controller) leaderElection() {
elector := setupLeaderElection(&leaderElectionConfig{
Client: c.config.KubeClient,
ElectionID: "ovn-config",
ElectionID: "kube-ovn-controller",
PodName: c.config.PodName,
PodNamespace: c.config.PodNamespace,
})
Expand Down Expand Up @@ -86,16 +87,16 @@ func setupLeaderElection(config *leaderElectionConfig) *leaderelection.LeaderEle
Component: ovnLeaderElector,
Host: hostname,
})
lock := resourcelock.ConfigMapLock{
ConfigMapMeta: metav1.ObjectMeta{Namespace: config.PodNamespace, Name: config.ElectionID},
Client: config.Client.CoreV1(),
lock := &resourcelock.LeaseLock{
LeaseMeta: metav1.ObjectMeta{Namespace: config.PodNamespace, Name: config.ElectionID},
Client: config.Client.CoordinationV1(),
LockConfig: resourcelock.ResourceLockConfig{
Identity: config.PodName,
EventRecorder: recorder,
},
}
elector, err := leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
Lock: &lock,
Lock: lock,
LeaseDuration: 15 * time.Second,
RenewDeadline: 10 * time.Second,
RetryPeriod: 2 * time.Second,
Expand Down
15 changes: 6 additions & 9 deletions yamls/ovn-dpdk.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ spec:
rule: 'RunAsAny'

---

apiVersion: v1
kind: ConfigMap
metadata:
name: ovn-config
namespace: kube-system
data:
defaultNetworkType: geneve
---
apiVersion: v1
kind: ServiceAccount
metadata:
Expand Down Expand Up @@ -123,6 +114,12 @@ rules:
- create
- patch
- update
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- "*"
- apiGroups:
- "k8s.cni.cncf.io"
resources:
Expand Down
15 changes: 6 additions & 9 deletions yamls/ovn-ha.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ spec:
rule: 'RunAsAny'

---

apiVersion: v1
kind: ConfigMap
metadata:
name: ovn-config
namespace: kube-system
data:
defaultNetworkType: geneve
---
apiVersion: v1
kind: ServiceAccount
metadata:
Expand Down Expand Up @@ -126,6 +117,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 29f3d6e

Please sign in to comment.