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 authored and hongzhen-ma committed Sep 26, 2022
1 parent 44cee1d commit 5f23adc
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 @@ -884,14 +884,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 @@ -984,6 +976,12 @@ rules:
- create
- patch
- update
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- "*"
- apiGroups:
- "k8s.cni.cncf.io"
resources:
Expand Down Expand Up @@ -1377,13 +1375,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 @@ -1465,6 +1456,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 @@ -39,7 +40,7 @@ func (c *Controller) isLeader() bool {
func (c *Controller) leaderElection() {
config := &leaderElectionConfig{
Client: c.config.KubeClient,
ElectionID: "ovn-config",
ElectionID: "kube-ovn-controller",
PodName: c.config.PodName,
PodNamespace: c.config.PodNamespace,
}
Expand Down Expand Up @@ -96,16 +97,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 @@ -113,6 +104,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 @@ -116,6 +107,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 @@ -127,6 +127,12 @@ rules:
- create
- patch
- update
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- "*"
- apiGroups:
- "k8s.cni.cncf.io"
resources:
Expand Down

0 comments on commit 5f23adc

Please sign in to comment.