Skip to content

Commit

Permalink
Add support for leader election
Browse files Browse the repository at this point in the history
By default, the leader election is
enabled in the deployment yaml,
but controller's replica count is
set to 1.

This is to prevent IPAM controller
misbehaviour in case if user will
decide to scale deployment.

Signed-off-by: Yury Kulazhenkov <ykulazhenkov@nvidia.com>
  • Loading branch information
ykulazhenkov committed May 9, 2023
1 parent a1e2c92 commit a140114
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
1 change: 1 addition & 0 deletions cmd/ipam-controller/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func RunController(ctx context.Context, config *rest.Config, opts *options.Optio
Port: 9443,
HealthProbeBindAddress: opts.ProbeAddr,
LeaderElection: opts.EnableLeaderElection,
LeaderElectionNamespace: opts.LeaderElectionNamespace,
LeaderElectionID: "dd1643cf.nvidia.com",
LeaderElectionReleaseOnCancel: true,
})
Expand Down
27 changes: 16 additions & 11 deletions cmd/ipam-controller/app/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,25 @@ import (
// New initialize and return new Options object
func New() *Options {
return &Options{
Options: *cmdoptions.New(),
MetricsAddr: ":8080",
ProbeAddr: ":8081",
EnableLeaderElection: false,
ConfigMapName: "nvidia-k8s-ipam-config",
ConfigMapNamespace: "kube-system",
Options: *cmdoptions.New(),
MetricsAddr: ":8080",
ProbeAddr: ":8081",
EnableLeaderElection: false,
LeaderElectionNamespace: "kube-system",
ConfigMapName: "nvidia-k8s-ipam-config",
ConfigMapNamespace: "kube-system",
}
}

// Options holds command line options for controller
type Options struct {
cmdoptions.Options
MetricsAddr string
ProbeAddr string
EnableLeaderElection bool
ConfigMapName string
ConfigMapNamespace string
MetricsAddr string
ProbeAddr string
EnableLeaderElection bool
LeaderElectionNamespace string
ConfigMapName string
ConfigMapNamespace string
}

// AddNamedFlagSets register flags for common options in NamedFlagSets
Expand All @@ -61,6 +63,9 @@ func (o *Options) AddNamedFlagSets(sharedFS *cliflag.NamedFlagSets) {
controllerFS.BoolVar(&o.EnableLeaderElection, "leader-elect", o.EnableLeaderElection,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
controllerFS.StringVar(&o.LeaderElectionNamespace, "leader-elect-namespace", o.LeaderElectionNamespace,
"Determines the namespace in which the leader "+
"election resource will be created.")
controllerFS.StringVar(&o.ConfigMapName, "config-name",
o.ConfigMapName, "The name of the ConfigMap which holds controller configuration")
controllerFS.StringVar(&o.ConfigMapNamespace, "config-namespace",
Expand Down
30 changes: 30 additions & 0 deletions deploy/nv-ipam.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,25 @@ rules:
- get
- list
- watch
- apiGroups:
- coordination.k8s.io
resources:
- leases
verbs:
- get
- list
- watch
- create
- update
- patch
- delete
- apiGroups:
- ""
resources:
- events
verbs:
- create
- patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
Expand Down Expand Up @@ -175,6 +194,15 @@ spec:
priorityClassName: system-cluster-critical
serviceAccountName: nv-ipam-controller
affinity:
podAntiAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
- labelSelector:
matchExpressions:
- key: name
operator: In
values:
- nv-ipam-controller
topologyKey: "kubernetes.io/hostname"
nodeAffinity:
preferredDuringSchedulingIgnoredDuringExecution:
- weight: 1
Expand Down Expand Up @@ -209,6 +237,8 @@ spec:
args:
- --config-name=nvidia-k8s-ipam-config
- --config-namespace=$(POD_NAMESPACE)
- --leader-elect=true
- --leader-elect-namespace=$(POD_NAMESPACE)
env:
- name: POD_NAMESPACE
valueFrom:
Expand Down

0 comments on commit a140114

Please sign in to comment.