diff --git a/api/go.mod b/api/go.mod index f62652d06..be6389424 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 2b0ee6ee9..a1ec7f512 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 2f8101603..377f057d7 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/fluxcd/helm-controller/api v0.8.2 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/source-controller/api v0.9.1 github.com/go-logr/logr v0.3.0 github.com/hashicorp/go-retryablehttp v0.6.8 diff --git a/go.sum b/go.sum index ac61d3612..ca320e555 100644 --- a/go.sum +++ b/go.sum @@ -237,8 +237,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/source-controller/api v0.9.1 h1:kaL+tBflccsuj3NDESPPQyKXlZXlAgyNoT2nYY02JAE= github.com/fluxcd/source-controller/api v0.9.1/go.mod h1:Vuw+7UqEUUOdkKBfTUPHwaQgbn6LL2FwqPDx2UAk7NE= github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k= diff --git a/main.go b/main.go index ba692fad9..b239f898e 100644 --- a/main.go +++ b/main.go @@ -17,6 +17,7 @@ limitations under the License. package main import ( + "fmt" "os" "time" @@ -29,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" @@ -40,6 +42,8 @@ import ( // +kubebuilder:scaffold:imports ) +const controllerName = "helm-controller" + var ( scheme = runtime.NewScheme() setupLog = ctrl.Log.WithName("setup") @@ -55,39 +59,36 @@ func init() { func main() { var ( - metricsAddr string - eventsAddr string - healthAddr string - enableLeaderElection bool - concurrent int - requeueDependency time.Duration - watchAllNamespaces bool - httpRetry int - clientOptions client.Options - logOptions logger.Options + metricsAddr string + eventsAddr string + healthAddr string + concurrent int + requeueDependency time.Duration + watchAllNamespaces bool + httpRetry int + clientOptions client.Options + logOptions logger.Options + leaderElectionOptions leaderelection.Options ) 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 HelmRelease 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.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)) var eventRecorder *events.Recorder if eventsAddr != "" { - if er, err := events.NewRecorder(eventsAddr, "helm-controller"); err != nil { + if er, err := events.NewRecorder(eventsAddr, controllerName); err != nil { setupLog.Error(err, "unable to create event recorder") os.Exit(1) } else { @@ -105,14 +106,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: "5b6ca942.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: fmt.Sprintf("%s-leader-election", controllerName), + Namespace: watchNamespace, + Logger: ctrl.Log, }) if err != nil { setupLog.Error(err, "unable to start manager") @@ -126,7 +131,7 @@ func main() { Client: mgr.GetClient(), Config: mgr.GetConfig(), Scheme: mgr.GetScheme(), - EventRecorder: mgr.GetEventRecorderFor("helm-controller"), + EventRecorder: mgr.GetEventRecorderFor(controllerName), ExternalEventRecorder: eventRecorder, MetricsRecorder: metricsRecorder, }).SetupWithManager(mgr, controllers.HelmReleaseReconcilerOptions{