Skip to content

Commit 73640ad

Browse files
Add options to customize leader election (#257)
* Add options to customize leader election
1 parent 268f4ae commit 73640ad

File tree

3 files changed

+54
-11
lines changed

3 files changed

+54
-11
lines changed

cloud/scope/load_balancer_reconciler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,12 @@ func (s *ClusterScope) GetLoadBalancers(ctx context.Context) (*loadbalancer.Load
273273
return nil, errors.New("cluster api tags have been modified out of context")
274274
}
275275
}
276-
var page *string;
276+
var page *string
277277
for {
278278
lbs, err := s.LoadBalancerClient.ListLoadBalancers(ctx, loadbalancer.ListLoadBalancersRequest{
279279
CompartmentId: common.String(s.GetCompartmentId()),
280280
DisplayName: common.String(s.GetControlPlaneLoadBalancerName()),
281-
Page: page,
281+
Page: page,
282282
})
283283
if err != nil {
284284
s.Logger.Error(err, "Failed to list lb by name")

cloud/scope/machine.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ func (m *MachineScope) getMachineFromOCID(ctx context.Context, instanceID *strin
323323
// GetMachineByDisplayName returns the machine from the compartment if there is a matching DisplayName,
324324
// and it was created by the cluster
325325
func (m *MachineScope) GetMachineByDisplayName(ctx context.Context, name string) (*core.Instance, error) {
326-
var page *string;
327-
for {
326+
var page *string
327+
for {
328328
req := core.ListInstancesRequest{DisplayName: common.String(name),
329329
CompartmentId: common.String(m.getCompartmentId()), Page: page}
330330
resp, err := m.ComputeClient.ListInstances(ctx, req)

main.go

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package main
1919
import (
2020
"flag"
2121
"os"
22+
"time"
2223

2324
infrastructurev1beta1 "github.com/oracle/cluster-api-provider-oci/api/v1beta1"
2425
infrastructurev1beta2 "github.com/oracle/cluster-api-provider-oci/api/v1beta2"
@@ -35,6 +36,7 @@ import (
3536
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3637
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3738
_ "k8s.io/client-go/plugin/pkg/client/auth"
39+
"k8s.io/client-go/tools/leaderelection/resourcelock"
3840
"k8s.io/component-base/logs"
3941
logsV1 "k8s.io/component-base/logs/api/v1"
4042
_ "k8s.io/component-base/logs/json/register"
@@ -79,6 +81,11 @@ func init() {
7981
func main() {
8082
var metricsAddr string
8183
var enableLeaderElection bool
84+
var leaderElectionNamespace string
85+
var leaderElectionLeaseDuration time.Duration
86+
var leaderElectionRenewDeadline time.Duration
87+
var leaderElectionRetryPeriod time.Duration
88+
var watchNamespace string
8289
var probeAddr string
8390
var webhookPort int
8491

@@ -91,6 +98,30 @@ func main() {
9198
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
9299
"Enable leader election for controller manager. "+
93100
"Enabling this will ensure there is only one active controller manager.")
101+
flag.StringVar(
102+
&leaderElectionNamespace,
103+
"leader-election-namespace",
104+
"",
105+
"Namespace that the controller performs leader election in. If unspecified, the controller will discover which namespace it is running in.",
106+
)
107+
flag.DurationVar(
108+
&leaderElectionLeaseDuration,
109+
"leader-elect-lease-duration",
110+
15*time.Second,
111+
"Interval at which non-leader candidates will wait to force acquire leadership (duration string)",
112+
)
113+
flag.DurationVar(
114+
&leaderElectionRenewDeadline,
115+
"leader-elect-renew-deadline",
116+
10*time.Second,
117+
"Duration that the leading controller manager will retry refreshing leadership before giving up (duration string)",
118+
)
119+
flag.DurationVar(
120+
&leaderElectionRetryPeriod,
121+
"leader-elect-retry-period",
122+
2*time.Second,
123+
"Duration the LeaderElector clients should wait between tries of actions (duration string)",
124+
)
94125
flag.IntVar(&webhookPort,
95126
"webhook-port",
96127
9443,
@@ -122,6 +153,12 @@ func main() {
122153
true,
123154
"Initialize OCI clients on startup",
124155
)
156+
flag.StringVar(
157+
&watchNamespace,
158+
"namespace",
159+
"",
160+
"Namespace that the controller watches to reconcile cluster-api objects. If unspecified, the controller watches for cluster-api objects across all namespaces.",
161+
)
125162

126163
opts := zap.Options{
127164
Development: true,
@@ -142,13 +179,19 @@ func main() {
142179
ctrl.SetLogger(klog.Background())
143180

144181
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
145-
Scheme: scheme,
146-
MetricsBindAddress: metricsAddr,
147-
Port: webhookPort,
148-
HealthProbeBindAddress: probeAddr,
149-
LeaderElection: enableLeaderElection,
150-
LeaderElectionID: "controller-leader-elect-capoci",
151-
CertDir: webhookCertDir,
182+
Scheme: scheme,
183+
MetricsBindAddress: metricsAddr,
184+
Port: webhookPort,
185+
HealthProbeBindAddress: probeAddr,
186+
LeaderElection: enableLeaderElection,
187+
LeaderElectionID: "controller-leader-elect-capoci",
188+
LeaderElectionResourceLock: resourcelock.LeasesResourceLock,
189+
LeaderElectionNamespace: leaderElectionNamespace,
190+
LeaseDuration: &leaderElectionLeaseDuration,
191+
RenewDeadline: &leaderElectionRenewDeadline,
192+
RetryPeriod: &leaderElectionRetryPeriod,
193+
CertDir: webhookCertDir,
194+
Namespace: watchNamespace,
152195
})
153196
if err != nil {
154197
setupLog.Error(err, "unable to start manager")

0 commit comments

Comments
 (0)