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

Add cli flag for leader-election-resource-lock #8107

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 7 additions & 0 deletions charts/ingress-nginx/templates/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ rules:
verbs:
- list
- watch
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- list
- watch
{{- if and .Values.controller.scope.enabled .Values.controller.scope.namespace }}
- apiGroups:
- ""
Expand Down
15 changes: 15 additions & 0 deletions charts/ingress-nginx/templates/controller-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,21 @@ rules:
- configmaps
verbs:
- create
- apiGroups:
- coordination.k8s.io
resources:
- leases
resourceNames:
- {{ .Values.controller.electionID }}
verbs:
- get
- update
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- create
- apiGroups:
- ""
resources:
Expand Down
5 changes: 5 additions & 0 deletions cmd/nginx/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/spf13/pflag"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/ingress-nginx/internal/ingress/annotations/parser"
"k8s.io/ingress-nginx/internal/ingress/controller"
ngx_config "k8s.io/ingress-nginx/internal/ingress/controller/config"
Expand Down Expand Up @@ -127,6 +128,9 @@ Requires setting the publish-service parameter to a valid Service reference.`)
electionID = flags.String("election-id", "ingress-controller-leader",
`Election id to use for Ingress status updates.`)

leaderElectionResourceLock = flags.String("leader-election-resource-lock", resourcelock.ConfigMapsResourceLock,
`Resourcelock to use for ingress-controller leader-election. Supported values are "configmaps", "configmapsleases", "leases".`)

updateStatusOnShutdown = flags.Bool("update-status-on-shutdown", true,
`Update the load-balancer status of Ingress objects when the controller shuts down.
Requires the update-status parameter.`)
Expand Down Expand Up @@ -309,6 +313,7 @@ https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-g
KubeConfigFile: *kubeConfigFile,
UpdateStatus: *updateStatus,
ElectionID: *electionID,
LeaderElectionResourceLock: *leaderElectionResourceLock,
EnableProfiling: *profiling,
EnableMetrics: *enableMetrics,
MetricsPerHost: *metricsPerHost,
Expand Down
4 changes: 4 additions & 0 deletions docs/deploy/rbac.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ permissions are granted to the Role named `ingress-nginx`

Furthermore to support leader-election, the ingress-nginx-controller needs to
have access to a `configmap` using the resourceName `ingress-controller-leader-nginx`
and also a `lease` using the resourceName `ingress-controller-leader-nginx`
if `configmapsleases` or `leases` is used as default leader-election resource-lock.

> Note that resourceNames can NOT be used to limit requests using the “create”
> verb because authorizers only have access to information that can be obtained
Expand All @@ -55,6 +57,8 @@ have access to a `configmap` using the resourceName `ingress-controller-leader-n

* `configmaps`: get, update (for resourceName `ingress-controller-leader-nginx`)
* `configmaps`: create
* `leases`: get, update (for resourceName `ingress-controller-leader-nginx`)
* `leases`: create

This resourceName is the concatenation of the `election-id` and the
`ingress-class` as defined by the ingress-controller, which defaults to:
Expand Down
9 changes: 5 additions & 4 deletions internal/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ type Configuration struct {
PublishService string
PublishStatusAddress string

UpdateStatus bool
UseNodeInternalIP bool
ElectionID string
UpdateStatusOnShutdown bool
UpdateStatus bool
UseNodeInternalIP bool
ElectionID string
LeaderElectionResourceLock string
UpdateStatusOnShutdown bool

HealthCheckHost string
ListenPorts *ngx_config.ListenPorts
Expand Down
6 changes: 4 additions & 2 deletions internal/ingress/controller/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,10 @@ func (n *NGINXController) Start() {
electionID := n.cfg.ElectionID

setupLeaderElection(&leaderElectionConfig{
Client: n.cfg.Client,
ElectionID: electionID,
Client: n.cfg.Client,
ElectionID: electionID,
LeaderElectionResourceLock: n.cfg.LeaderElectionResourceLock,

OnStartedLeading: func(stopCh chan struct{}) {
if n.syncStatus != nil {
go n.syncStatus.Run(stopCh)
Expand Down
21 changes: 13 additions & 8 deletions internal/ingress/controller/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"k8s.io/klog/v2"

apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clientset "k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/leaderelection"
Expand All @@ -36,7 +35,8 @@ import (
type leaderElectionConfig struct {
Client clientset.Interface

ElectionID string
ElectionID string
LeaderElectionResourceLock string

OnStartedLeading func(chan struct{})
OnStoppedLeading func()
Expand Down Expand Up @@ -93,19 +93,24 @@ func setupLeaderElection(config *leaderElectionConfig) {
Host: hostname,
})

lock := resourcelock.ConfigMapLock{
ConfigMapMeta: metav1.ObjectMeta{Namespace: k8s.IngressPodDetails.Namespace, Name: config.ElectionID},
Client: config.Client.CoreV1(),
LockConfig: resourcelock.ResourceLockConfig{
lock, err := resourcelock.New(config.LeaderElectionResourceLock,
k8s.IngressPodDetails.Namespace,
config.ElectionID,
config.Client.CoreV1(),
config.Client.CoordinationV1(),
resourcelock.ResourceLockConfig{
Identity: k8s.IngressPodDetails.Name,
EventRecorder: recorder,
},
)
if err != nil {
klog.Fatalf("unexpected error creating resource lock: %v", err)
}

ttl := 30 * time.Second

elector, err := leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
Lock: &lock,
elector, err = leaderelection.NewLeaderElector(leaderelection.LeaderElectionConfig{
Lock: lock,
LeaseDuration: ttl,
RenewDeadline: ttl / 2,
RetryPeriod: ttl / 4,
Expand Down