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 options to customize leader election #257

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions cloud/scope/load_balancer_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,12 @@ func (s *ClusterScope) GetLoadBalancers(ctx context.Context) (*loadbalancer.Load
return nil, errors.New("cluster api tags have been modified out of context")
}
}
var page *string;
var page *string
for {
lbs, err := s.LoadBalancerClient.ListLoadBalancers(ctx, loadbalancer.ListLoadBalancersRequest{
CompartmentId: common.String(s.GetCompartmentId()),
DisplayName: common.String(s.GetControlPlaneLoadBalancerName()),
Page: page,
Page: page,
})
if err != nil {
s.Logger.Error(err, "Failed to list lb by name")
Expand Down
4 changes: 2 additions & 2 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ func (m *MachineScope) getMachineFromOCID(ctx context.Context, instanceID *strin
// GetMachineByDisplayName returns the machine from the compartment if there is a matching DisplayName,
// and it was created by the cluster
func (m *MachineScope) GetMachineByDisplayName(ctx context.Context, name string) (*core.Instance, error) {
var page *string;
for {
var page *string
for {
req := core.ListInstancesRequest{DisplayName: common.String(name),
CompartmentId: common.String(m.getCompartmentId()), Page: page}
resp, err := m.ComputeClient.ListInstances(ctx, req)
Expand Down
57 changes: 50 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package main

import (
"flag"
"k8s.io/client-go/tools/leaderelection/resourcelock"
joekr marked this conversation as resolved.
Show resolved Hide resolved
"os"
"time"

infrastructurev1beta1 "github.com/oracle/cluster-api-provider-oci/api/v1beta1"
infrastructurev1beta2 "github.com/oracle/cluster-api-provider-oci/api/v1beta2"
Expand Down Expand Up @@ -79,6 +81,11 @@ func init() {
func main() {
var metricsAddr string
var enableLeaderElection bool
var leaderElectionNamespace string
var leaderElectionLeaseDuration time.Duration
var leaderElectionRenewDeadline time.Duration
var leaderElectionRetryPeriod time.Duration
var watchNamespace string
var probeAddr string
var webhookPort int

Expand All @@ -91,6 +98,30 @@ func main() {
flag.BoolVar(&enableLeaderElection, "leader-elect", false,
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
flag.StringVar(
&leaderElectionNamespace,
"leader-election-namespace",
"",
"Namespace that the controller performs leader election in. If unspecified, the controller will discover which namespace it is running in.",
)
flag.DurationVar(
&leaderElectionLeaseDuration,
"leader-elect-lease-duration",
15*time.Second,
"Interval at which non-leader candidates will wait to force acquire leadership (duration string)",
)
flag.DurationVar(
&leaderElectionRenewDeadline,
"leader-elect-renew-deadline",
10*time.Second,
"Duration that the leading controller manager will retry refreshing leadership before giving up (duration string)",
)
flag.DurationVar(
&leaderElectionRetryPeriod,
"leader-elect-retry-period",
2*time.Second,
"Duration the LeaderElector clients should wait between tries of actions (duration string)",
)
flag.IntVar(&webhookPort,
"webhook-port",
9443,
Expand Down Expand Up @@ -122,6 +153,12 @@ func main() {
true,
"Initialize OCI clients on startup",
)
flag.StringVar(
&watchNamespace,
"namespace",
"",
"Namespace that the controller watches to reconcile cluster-api objects. If unspecified, the controller watches for cluster-api objects across all namespaces.",
)

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

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: webhookPort,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "controller-leader-elect-capoci",
CertDir: webhookCertDir,
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: webhookPort,
HealthProbeBindAddress: probeAddr,
LeaderElection: enableLeaderElection,
LeaderElectionID: "controller-leader-elect-capoci",
LeaderElectionResourceLock: resourcelock.LeasesResourceLock,
LeaderElectionNamespace: leaderElectionNamespace,
LeaseDuration: &leaderElectionLeaseDuration,
RenewDeadline: &leaderElectionRenewDeadline,
RetryPeriod: &leaderElectionRetryPeriod,
CertDir: webhookCertDir,
Namespace: watchNamespace,
})
if err != nil {
setupLog.Error(err, "unable to start manager")
Expand Down