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 Lease for leader election #4276

Merged
merged 1 commit into from
Oct 24, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,79 +6,14 @@ metadata:
labels:
{{- include "nginx-ingress.labels" . | nindent 4 }}
rules:
{{- if .Values.controller.appprotect.enable }}
- apiGroups:
- appprotect.f5.com
resources:
- appolicies
- aplogconfs
- apusersigs
verbs:
- get
- watch
- list
{{- end }}
{{- if .Values.controller.appprotectdos.enable }}
- apiGroups:
- appprotectdos.f5.com
resources:
- apdospolicies
- apdoslogconfs
- dosprotectedresources
verbs:
- get
- watch
- list
{{- end }}
- apiGroups:
- discovery.k8s.io
resources:
- endpointslices
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- services
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- secrets
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- configmaps
verbs:
- get
- list
- watch
{{- if .Values.controller.reportIngressStatus.enableLeaderElection }}
- update
- create
{{- end }}
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- list
- watch
- update
- apiGroups:
- ""
resources:
- namespaces
- pods
- secrets
- services
verbs:
- get
- list
Expand All @@ -96,11 +31,16 @@ rules:
resources:
- leases
verbs:
- list
- watch
- apiGroups:
- discovery.k8s.io
resources:
- endpointslices
verbs:
- get
- list
- watch
- update
- create
- apiGroups:
- networking.k8s.io
resources:
Expand All @@ -123,6 +63,30 @@ rules:
verbs:
- update
{{- end }}
{{- if .Values.controller.appprotect.enable }}
- apiGroups:
- appprotect.f5.com
resources:
- appolicies
- aplogconfs
- apusersigs
verbs:
- get
- watch
- list
{{- end }}
{{- if .Values.controller.appprotectdos.enable }}
- apiGroups:
- appprotectdos.f5.com
resources:
- apdospolicies
- apdoslogconfs
- dosprotectedresources
verbs:
- get
- watch
- list
{{- end }}
{{- if .Values.controller.enableCustomResources }}
- apiGroups:
- k8s.nginx.org
Expand Down Expand Up @@ -188,19 +152,4 @@ rules:
verbs:
- update
{{- end }}
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: {{ include "nginx-ingress.fullname" . }}
labels:
{{- include "nginx-ingress.labels" . | nindent 4 }}
subjects:
- kind: ServiceAccount
name: {{ include "nginx-ingress.serviceAccountName" . }}
namespace: {{ .Release.Namespace }}
roleRef:
kind: ClusterRole
name: {{ include "nginx-ingress.fullname" . }}
apiGroup: rbac.authorization.k8s.io
{{- end }}
{{- end}}
16 changes: 16 additions & 0 deletions charts/nginx-ingress/templates/clusterrolebiding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{{- if .Values.rbac.create }}
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: {{ include "nginx-ingress.fullname" . }}
labels:
{{- include "nginx-ingress.labels" . | nindent 4 }}
subjects:
- kind: ServiceAccount
name: {{ include "nginx-ingress.serviceAccountName" . }}
namespace: {{ .Release.Namespace }}
roleRef:
kind: ClusterRole
name: {{ include "nginx-ingress.fullname" . }}
apiGroup: rbac.authorization.k8s.io
{{- end }}
56 changes: 56 additions & 0 deletions charts/nginx-ingress/templates/controller-role.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{{- if .Values.rbac.create }}
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: {{ include "nginx-ingress.fullname" . }}
labels:
{{- include "nginx-ingress.labels" . | nindent 4 }}
namespace: {{ .Release.Namespace }}
rules:
- apiGroups:
- ""
resources:
- configmaps
- pods
- secrets
- services
verbs:
- get
- list
- watch
- apiGroups:
- ""
resources:
- namespaces
verbs:
- get
- apiGroups:
- ""
resources:
- pods
verbs:
- update
- apiGroups:
- ""
resources:
- events
verbs:
- create
- patch
- list
- apiGroups:
- coordination.k8s.io
resources:
- leases
resourceNames:
- {{ .Values.controller.reportIngressStatus.leaderElectionLockName }}
verbs:
- get
- update
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- create
{{- end }}
17 changes: 17 additions & 0 deletions charts/nginx-ingress/templates/controller-rolebiding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{{- if .Values.rbac.create }}
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: {{ include "nginx-ingress.fullname" . }}
labels:
{{- include "nginx-ingress.labels" . | nindent 4 }}
namespace: {{ .Release.Namespace }}
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: {{ include "nginx-ingress.fullname" . }}
subjects:
- kind: ServiceAccount
name: {{ include "nginx-ingress.serviceAccountName" . }}
namespace: {{ .Release.Namespace }}
{{- end }}
5 changes: 2 additions & 3 deletions charts/nginx-ingress/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,8 @@ controller:
## Enable Leader election to avoid multiple replicas of the controller reporting the status of Ingress resources. controller.reportIngressStatus.enable must be set to true.
enableLeaderElection: true

## Specifies the name of the ConfigMap, within the same namespace as the controller, used as the lock for leader election. controller.reportIngressStatus.enableLeaderElection must be set to true.
## Autogenerated if not set or set to "".
# leaderElectionLockName: "nginx-ingress-leader-election"
## Specifies the name to be used as the lock for leader election. controller.reportIngressStatus.enableLeaderElection must be set to true.
leaderElectionLockName: "nginx-ingress-leader"

## The annotations of the leader election configmap.
annotations: {}
Expand Down
35 changes: 18 additions & 17 deletions internal/k8s/leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package k8s

import (
"context"
"fmt"
"os"
"time"

"github.com/golang/glog"

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/leaderelection"
Expand All @@ -31,25 +31,26 @@ func newLeaderElector(client kubernetes.Interface, callbacks leaderelection.Lead
EventRecorder: recorder,
}

lock, err := resourcelock.New(resourcelock.LeasesResourceLock,
namespace,
lockName,
client.CoreV1(),
client.CoordinationV1(),
lc,
)
if err != nil {
return nil, fmt.Errorf("error creating leader election lock: %w", err)
leaseMeta := metav1.ObjectMeta{
Namespace: namespace,
Name: lockName,
}

lock := &resourcelock.LeaseLock{
LeaseMeta: leaseMeta,
Client: client.CoordinationV1(),
LockConfig: lc,
}

ttl := 30 * time.Second
return leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
Lock: lock,
LeaseDuration: ttl,
RenewDeadline: ttl / 2,
RetryPeriod: ttl / 4,
Callbacks: callbacks,
})
return leaderelection.NewLeaderElector(
leaderelection.LeaderElectionConfig{
Lock: lock,
LeaseDuration: ttl,
RenewDeadline: ttl / 2,
RetryPeriod: ttl / 4,
Callbacks: callbacks,
})
}

// createLeaderHandler builds the handler funcs for leader handling
Expand Down