Skip to content

Commit

Permalink
Add options to customize leader election (#257)
Browse files Browse the repository at this point in the history
* Add options to customize leader election
  • Loading branch information
shyamradhakrishnan committed May 5, 2023
1 parent 268f4ae commit 73640ad
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
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 @@ -19,6 +19,7 @@ package main
import (
"flag"
"os"
"time"

infrastructurev1beta1 "github.com/oracle/cluster-api-provider-oci/api/v1beta1"
infrastructurev1beta2 "github.com/oracle/cluster-api-provider-oci/api/v1beta2"
Expand All @@ -35,6 +36,7 @@ import (
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/leaderelection/resourcelock"
"k8s.io/component-base/logs"
logsV1 "k8s.io/component-base/logs/api/v1"
_ "k8s.io/component-base/logs/json/register"
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

0 comments on commit 73640ad

Please sign in to comment.