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 May 19, 2022
1 parent ae56306 commit 4f1e812
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
Expand Up @@ -1358,14 +1358,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 @@ -1468,6 +1460,12 @@ rules:
- create
- patch
- update
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- "*"
- apiGroups:
- "k8s.cni.cncf.io"
resources:
Expand Down Expand Up @@ -1856,13 +1854,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 @@ -1955,6 +1946,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
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
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
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
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 4f1e812

Please sign in to comment.