Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use leases for leader election #1529

Merged
merged 1 commit into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions dist/images/install.sh
Original file line number Diff line number Diff line change
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
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