@@ -38,6 +38,7 @@ import (
3838 "github.com/fluxcd/pkg/runtime/events"
3939 "github.com/fluxcd/pkg/runtime/logger"
4040 "github.com/fluxcd/pkg/runtime/metrics"
41+ "github.com/fluxcd/pkg/runtime/probes"
4142
4243 sourcev1 "github.com/fluxcd/source-controller/api/v1beta1"
4344 "github.com/fluxcd/source-controller/controllers"
@@ -66,6 +67,7 @@ func main() {
6667 var (
6768 metricsAddr string
6869 eventsAddr string
70+ healthAddr string
6971 enableLeaderElection bool
7072 storagePath string
7173 storageAddr string
@@ -80,6 +82,7 @@ func main() {
8082 "The address the metric endpoint binds to." )
8183 flag .StringVar (& eventsAddr , "events-addr" , envOrDefault ("EVENTS_ADDR" , "" ),
8284 "The address of the events receiver." )
85+ flag .StringVar (& healthAddr , "health-addr" , ":9440" , "The address the health endpoint binds to." )
8386 flag .BoolVar (& enableLeaderElection , "enable-leader-election" , false ,
8487 "Enable leader election for controller manager. " +
8588 "Enabling this will ensure there is only one active controller manager." )
@@ -120,24 +123,26 @@ func main() {
120123
121124 restConfig := client .GetConfigOrDie (clientOptions )
122125 mgr , err := ctrl .NewManager (restConfig , ctrl.Options {
123- Scheme : scheme ,
124- MetricsBindAddress : metricsAddr ,
125- Port : 9443 ,
126- LeaderElection : enableLeaderElection ,
127- LeaderElectionID : "305740c0.fluxcd.io" ,
128- Namespace : watchNamespace ,
129- Logger : ctrl .Log ,
126+ Scheme : scheme ,
127+ MetricsBindAddress : metricsAddr ,
128+ HealthProbeBindAddress : healthAddr ,
129+ Port : 9443 ,
130+ LeaderElection : enableLeaderElection ,
131+ LeaderElectionID : "305740c0.fluxcd.io" ,
132+ Namespace : watchNamespace ,
133+ Logger : ctrl .Log ,
130134 })
131135 if err != nil {
132136 setupLog .Error (err , "unable to start manager" )
133137 os .Exit (1 )
134138 }
135139
140+ probes .SetupChecks (mgr , setupLog )
141+
136142 if storageAdvAddr == "" {
137143 storageAdvAddr = determineAdvStorageAddr (storageAddr , setupLog )
138144 }
139145 storage := mustInitStorage (storagePath , storageAdvAddr , setupLog )
140- go startFileServer (storage .BasePath , storageAddr , setupLog )
141146
142147 if err = (& controllers.GitRepositoryReconciler {
143148 Client : mgr .GetClient (),
@@ -195,6 +200,15 @@ func main() {
195200 }
196201 // +kubebuilder:scaffold:builder
197202
203+ go func () {
204+ // Block until our controller manager is elected leader. We presume our
205+ // entire process will terminate if we lose leadership, so we don't need
206+ // to handle that.
207+ <- mgr .Elected ()
208+
209+ startFileServer (storage .BasePath , storageAddr , setupLog )
210+ }()
211+
198212 setupLog .Info ("starting manager" )
199213 if err := mgr .Start (ctrl .SetupSignalHandler ()); err != nil {
200214 setupLog .Error (err , "problem running manager" )
@@ -203,6 +217,7 @@ func main() {
203217}
204218
205219func startFileServer (path string , address string , l logr.Logger ) {
220+ l .Info ("starting file server" )
206221 fs := http .FileServer (http .Dir (path ))
207222 http .Handle ("/" , fs )
208223 err := http .ListenAndServe (address , nil )
0 commit comments