From 43c05a7a2001cca84a4ac01d5a91724fc5df3c50 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Thu, 25 Mar 2021 13:33:15 +0200 Subject: [PATCH 1/2] Set leader election deadline to 30s Signed-off-by: Stefan Prodan --- api/go.mod | 2 +- api/go.sum | 4 ++-- go.mod | 2 +- go.sum | 4 ++-- main.go | 47 ++++++++++++++++++++++++----------------------- 5 files changed, 30 insertions(+), 29 deletions(-) diff --git a/api/go.mod b/api/go.mod index 49bbe3d1..52b3fcce 100644 --- a/api/go.mod +++ b/api/go.mod @@ -5,7 +5,7 @@ go 1.15 require ( github.com/fluxcd/pkg/apis/kustomize v0.0.1 github.com/fluxcd/pkg/apis/meta v0.8.0 - github.com/fluxcd/pkg/runtime v0.8.5 + github.com/fluxcd/pkg/runtime v0.10.1 k8s.io/apiextensions-apiserver v0.20.2 k8s.io/apimachinery v0.20.2 sigs.k8s.io/controller-runtime v0.8.3 diff --git a/api/go.sum b/api/go.sum index 2b0ee6ee..a1ec7f51 100644 --- a/api/go.sum +++ b/api/go.sum @@ -92,8 +92,8 @@ github.com/fluxcd/pkg/apis/kustomize v0.0.1 h1:TkA80R0GopRY27VJqzKyS6ifiKIAfwBd7 github.com/fluxcd/pkg/apis/kustomize v0.0.1/go.mod h1:JAFPfnRmcrAoG1gNiA8kmEXsnOBuDyZ/F5X4DAQcVV0= github.com/fluxcd/pkg/apis/meta v0.8.0 h1:wqWpUsxhKHB1ZztcvOz+vnyhdKW9cWmjFp8Vci/XOdk= github.com/fluxcd/pkg/apis/meta v0.8.0/go.mod h1:yHuY8kyGHYz22I0jQzqMMGCcHViuzC/WPdo9Gisk8Po= -github.com/fluxcd/pkg/runtime v0.8.5 h1:ynh8fszbLQ3QSisQBNOABEUTnvt+/QfCdaL6gOJQcoQ= -github.com/fluxcd/pkg/runtime v0.8.5/go.mod h1:JD0eZIn5xkTeHHQUWXSqJPIh/ecO0d0qrUKbSVHnpnw= +github.com/fluxcd/pkg/runtime v0.10.1 h1:NV0pe6lFzodKBIz0dT3xkoR0wJnTCicXwM/v/d5T0+Y= +github.com/fluxcd/pkg/runtime v0.10.1/go.mod h1:JD0eZIn5xkTeHHQUWXSqJPIh/ecO0d0qrUKbSVHnpnw= github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= diff --git a/go.mod b/go.mod index 31f5da7d..c6437429 100644 --- a/go.mod +++ b/go.mod @@ -10,7 +10,7 @@ require ( github.com/fluxcd/kustomize-controller/api v0.9.3 github.com/fluxcd/pkg/apis/kustomize v0.0.1 github.com/fluxcd/pkg/apis/meta v0.8.0 - github.com/fluxcd/pkg/runtime v0.8.5 + github.com/fluxcd/pkg/runtime v0.10.1 github.com/fluxcd/pkg/testserver v0.0.2 github.com/fluxcd/pkg/untar v0.0.5 github.com/fluxcd/source-controller/api v0.9.1 diff --git a/go.sum b/go.sum index 90ff4d55..cc5154f3 100644 --- a/go.sum +++ b/go.sum @@ -197,8 +197,8 @@ github.com/fluxcd/pkg/apis/kustomize v0.0.1 h1:TkA80R0GopRY27VJqzKyS6ifiKIAfwBd7 github.com/fluxcd/pkg/apis/kustomize v0.0.1/go.mod h1:JAFPfnRmcrAoG1gNiA8kmEXsnOBuDyZ/F5X4DAQcVV0= github.com/fluxcd/pkg/apis/meta v0.8.0 h1:wqWpUsxhKHB1ZztcvOz+vnyhdKW9cWmjFp8Vci/XOdk= github.com/fluxcd/pkg/apis/meta v0.8.0/go.mod h1:yHuY8kyGHYz22I0jQzqMMGCcHViuzC/WPdo9Gisk8Po= -github.com/fluxcd/pkg/runtime v0.8.5 h1:ynh8fszbLQ3QSisQBNOABEUTnvt+/QfCdaL6gOJQcoQ= -github.com/fluxcd/pkg/runtime v0.8.5/go.mod h1:JD0eZIn5xkTeHHQUWXSqJPIh/ecO0d0qrUKbSVHnpnw= +github.com/fluxcd/pkg/runtime v0.10.1 h1:NV0pe6lFzodKBIz0dT3xkoR0wJnTCicXwM/v/d5T0+Y= +github.com/fluxcd/pkg/runtime v0.10.1/go.mod h1:JD0eZIn5xkTeHHQUWXSqJPIh/ecO0d0qrUKbSVHnpnw= github.com/fluxcd/pkg/testserver v0.0.2 h1:SoaMtO9cE5p/wl2zkGudzflnEHd9mk68CGjZOo7w0Uk= github.com/fluxcd/pkg/testserver v0.0.2/go.mod h1:pgUZTh9aQ44FSTQo+5NFlh7YMbUfdz1B80DalW7k96Y= github.com/fluxcd/pkg/untar v0.0.5 h1:UGI3Ch1UIEIaqQvMicmImL1s9npQa64DJ/ozqHKB7gk= diff --git a/main.go b/main.go index c7309b35..971dcd16 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,7 @@ import ( "github.com/fluxcd/pkg/runtime/client" "github.com/fluxcd/pkg/runtime/events" + "github.com/fluxcd/pkg/runtime/leaderelection" "github.com/fluxcd/pkg/runtime/logger" "github.com/fluxcd/pkg/runtime/metrics" "github.com/fluxcd/pkg/runtime/pprof" @@ -56,33 +57,29 @@ func init() { func main() { var ( - metricsAddr string - eventsAddr string - healthAddr string - enableLeaderElection bool - concurrent int - requeueDependency time.Duration - clientOptions client.Options - logOptions logger.Options - watchAllNamespaces bool - httpRetry int + metricsAddr string + eventsAddr string + healthAddr string + concurrent int + requeueDependency time.Duration + clientOptions client.Options + logOptions logger.Options + leaderElectionOptions leaderelection.Options + watchAllNamespaces bool + httpRetry int ) flag.StringVar(&metricsAddr, "metrics-addr", ":8080", "The address the metric endpoint binds to.") flag.StringVar(&eventsAddr, "events-addr", "", "The address of the events receiver.") flag.StringVar(&healthAddr, "health-addr", ":9440", "The address the health endpoint binds to.") - flag.BoolVar(&enableLeaderElection, "enable-leader-election", false, - "Enable leader election for controller manager. "+ - "Enabling this will ensure there is only one active controller manager.") flag.IntVar(&concurrent, "concurrent", 4, "The number of concurrent kustomize reconciles.") flag.DurationVar(&requeueDependency, "requeue-dependency", 30*time.Second, "The interval at which failing dependencies are reevaluated.") flag.BoolVar(&watchAllNamespaces, "watch-all-namespaces", true, "Watch for custom resources in all namespaces, if set to false it will only watch the runtime namespace.") flag.IntVar(&httpRetry, "http-retry", 9, "The maximum number of retries when failing to fetch artifacts over HTTP.") - flag.Bool("log-json", false, "Set logging to JSON format.") - flag.CommandLine.MarkDeprecated("log-json", "Please use --log-encoding=json instead.") clientOptions.BindFlags(flag.CommandLine) logOptions.BindFlags(flag.CommandLine) + leaderElectionOptions.BindFlags(flag.CommandLine) flag.Parse() ctrl.SetLogger(logger.NewLogger(logOptions)) @@ -107,14 +104,18 @@ func main() { restConfig := client.GetConfigOrDie(clientOptions) mgr, err := ctrl.NewManager(restConfig, ctrl.Options{ - Scheme: scheme, - MetricsBindAddress: metricsAddr, - HealthProbeBindAddress: healthAddr, - Port: 9443, - LeaderElection: enableLeaderElection, - LeaderElectionID: "7593cc5d.fluxcd.io", - Namespace: watchNamespace, - Logger: ctrl.Log, + Scheme: scheme, + MetricsBindAddress: metricsAddr, + HealthProbeBindAddress: healthAddr, + Port: 9443, + LeaderElection: leaderElectionOptions.Enable, + LeaderElectionReleaseOnCancel: leaderElectionOptions.ReleaseOnCancel, + LeaseDuration: &leaderElectionOptions.LeaseDuration, + RenewDeadline: &leaderElectionOptions.RenewDeadline, + RetryPeriod: &leaderElectionOptions.RetryPeriod, + LeaderElectionID: "7593cc5d.fluxcd.io", + Namespace: watchNamespace, + Logger: ctrl.Log, }) if err != nil { setupLog.Error(err, "unable to start manager") From ca1760b3641f1aa3fa78075c4f132f03f842a6c0 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Thu, 25 Mar 2021 14:20:34 +0200 Subject: [PATCH 2/2] Use controller name in LeaderElectionID Signed-off-by: Stefan Prodan --- main.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 971dcd16..f89c43c1 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "fmt" "os" "time" @@ -42,6 +43,8 @@ import ( // +kubebuilder:scaffold:imports ) +const controllerName = "kustomize-controller" + var ( scheme = runtime.NewScheme() setupLog = ctrl.Log.WithName("setup") @@ -86,7 +89,7 @@ func main() { var eventRecorder *events.Recorder if eventsAddr != "" { - if er, err := events.NewRecorder(eventsAddr, "kustomize-controller"); err != nil { + if er, err := events.NewRecorder(eventsAddr, controllerName); err != nil { setupLog.Error(err, "unable to create event recorder") os.Exit(1) } else { @@ -113,7 +116,7 @@ func main() { LeaseDuration: &leaderElectionOptions.LeaseDuration, RenewDeadline: &leaderElectionOptions.RenewDeadline, RetryPeriod: &leaderElectionOptions.RetryPeriod, - LeaderElectionID: "7593cc5d.fluxcd.io", + LeaderElectionID: fmt.Sprintf("%s-leader-election", controllerName), Namespace: watchNamespace, Logger: ctrl.Log, }) @@ -128,7 +131,7 @@ func main() { if err = (&controllers.KustomizationReconciler{ Client: mgr.GetClient(), Scheme: mgr.GetScheme(), - EventRecorder: mgr.GetEventRecorderFor("kustomize-controller"), + EventRecorder: mgr.GetEventRecorderFor(controllerName), ExternalEventRecorder: eventRecorder, MetricsRecorder: metricsRecorder, StatusPoller: polling.NewStatusPoller(mgr.GetClient(), mgr.GetRESTMapper()),