@@ -17,30 +17,35 @@ limitations under the License.
1717package main
1818
1919import (
20+ "fmt"
2021 "os"
2122 "time"
2223
23- "github.com/fluxcd/pkg/runtime/acl"
24- "github.com/fluxcd/pkg/runtime/probes"
2524 flag "github.com/spf13/pflag"
2625 corev1 "k8s.io/api/core/v1"
2726 "k8s.io/apimachinery/pkg/runtime"
2827 utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2928 clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3029 _ "k8s.io/client-go/plugin/pkg/client/auth"
3130 "k8s.io/utils/ptr"
32- ctrl "sigs.k8s.io/controller-runtime"
31+ ctrlruntime "sigs.k8s.io/controller-runtime"
3332 ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
3433 ctrlcfg "sigs.k8s.io/controller-runtime/pkg/config"
3534 metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
3635
37- artcfg "github.com/fluxcd/pkg/artifact/config"
38- artdigest "github.com/fluxcd/pkg/artifact/digest"
39- artsrv "github.com/fluxcd/pkg/artifact/server"
40- artstore "github.com/fluxcd/pkg/artifact/storage"
36+ "github.com/fluxcd/pkg/artifact/config"
37+ "github.com/fluxcd/pkg/artifact/digest"
38+ "github.com/fluxcd/pkg/artifact/server"
39+ "github.com/fluxcd/pkg/artifact/storage"
40+ "github.com/fluxcd/pkg/runtime/acl"
4141 "github.com/fluxcd/pkg/runtime/client"
42+ ctrl "github.com/fluxcd/pkg/runtime/controller"
43+ "github.com/fluxcd/pkg/runtime/features"
44+ "github.com/fluxcd/pkg/runtime/jitter"
45+ "github.com/fluxcd/pkg/runtime/leaderelection"
4246 "github.com/fluxcd/pkg/runtime/logger"
4347 "github.com/fluxcd/pkg/runtime/pprof"
48+ "github.com/fluxcd/pkg/runtime/probes"
4449 sourcev1 "github.com/fluxcd/source-controller/api/v1"
4550
4651 swapi "github.com/fluxcd/source-watcher/api/v1beta1"
@@ -50,7 +55,7 @@ import (
5055
5156var (
5257 scheme = runtime .NewScheme ()
53- setupLog = ctrl .Log .WithName ("setup" )
58+ setupLog = ctrlruntime .Log .WithName ("setup" )
5459)
5560
5661func init () {
@@ -64,66 +69,83 @@ func main() {
6469 const controllerName = "source-watcher"
6570
6671 var (
67- metricsAddr string
68- healthAddr string
69- enableLeaderElection bool
70- concurrent int
71- httpRetry int
72- requeueDependency time.Duration
73- artifactOptions artcfg.Options
74- aclOptions acl.Options
75- clientOptions client.Options
76- logOptions logger.Options
72+ metricsAddr string
73+ healthAddr string
74+ concurrent int
75+ httpRetry int
76+ reconciliationTimeout time.Duration
77+ requeueDependency time.Duration
78+ artifactOptions config.Options
79+ aclOptions acl.Options
80+ clientOptions client.Options
81+ logOptions logger.Options
82+ leaderElectionOptions leaderelection.Options
83+ rateLimiterOptions ctrl.RateLimiterOptions
84+ intervalJitterOptions jitter.IntervalOptions
85+ featureGates features.FeatureGates
7786 )
7887
7988 flag .IntVar (& concurrent , "concurrent" , 10 , "The number of concurrent resource reconciles." )
8089 flag .StringVar (& metricsAddr , "metrics-addr" , ":8080" , "The address the metric endpoint binds to." )
8190 flag .StringVar (& healthAddr , "health-addr" , ":9440" , "The address the health endpoint binds to." )
82- flag .BoolVar (& enableLeaderElection , "enable-leader-election" , false ,
83- "Enable leader election for controller manager. " +
84- "Enabling this will ensure there is only one active controller manager." )
8591 flag .IntVar (& httpRetry , "http-retry" , 9 ,
8692 "The maximum number of retries when failing to fetch artifacts over HTTP." )
93+ flag .DurationVar (& reconciliationTimeout , "reconciliation-timeout" , 10 * time .Minute ,
94+ "The maximum duration of a reconciliation." )
8795 flag .DurationVar (& requeueDependency , "requeue-dependency" , 5 * time .Second ,
8896 "The interval at which failing dependencies are reevaluated." )
8997
9098 artifactOptions .BindFlags (flag .CommandLine )
9199 aclOptions .BindFlags (flag .CommandLine )
92100 clientOptions .BindFlags (flag .CommandLine )
93101 logOptions .BindFlags (flag .CommandLine )
102+ leaderElectionOptions .BindFlags (flag .CommandLine )
103+ rateLimiterOptions .BindFlags (flag .CommandLine )
104+ intervalJitterOptions .BindFlags (flag .CommandLine )
105+ featureGates .BindFlags (flag .CommandLine )
94106
95107 flag .Parse ()
96108
97- ctrl .SetLogger (logger .NewLogger (logOptions ))
109+ ctrlruntime .SetLogger (logger .NewLogger (logOptions ))
98110
99- algo , err := artdigest .AlgorithmForName (artifactOptions .ArtifactDigestAlgo )
111+ digestAlgo , err := digest .AlgorithmForName (artifactOptions .ArtifactDigestAlgo )
100112 if err != nil {
101113 setupLog .Error (err , "unable to configure canonical digest algorithm" )
102114 os .Exit (1 )
103115 }
104- artdigest .Canonical = algo
116+ digest .Canonical = digestAlgo
105117
106- storage , err := artstore .New (& artifactOptions )
118+ artifactStorage , err := storage .New (& artifactOptions )
107119 if err != nil {
108120 setupLog .Error (err , "unable to configure artifact storage" )
109121 os .Exit (1 )
110122 }
111- setupLog .Info ("storage setup for " + storage .BasePath )
123+ setupLog .Info ("storage setup for " + artifactStorage .BasePath )
112124
113- ctx := ctrl .SetupSignalHandler ()
114- mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
125+ if err := intervalJitterOptions .SetGlobalJitter (nil ); err != nil {
126+ setupLog .Error (err , "unable to set global jitter" )
127+ os .Exit (1 )
128+ }
129+
130+ ctx := ctrlruntime .SetupSignalHandler ()
131+ leaderElectionID := fmt .Sprintf ("%s-%s" , controllerName , "leader-election" )
132+ mgr , err := ctrlruntime .NewManager (ctrlruntime .GetConfigOrDie (), ctrlruntime.Options {
115133 Scheme : scheme ,
116134 Metrics : metricsserver.Options {
117135 BindAddress : metricsAddr ,
118136 ExtraHandlers : pprof .GetHandlers (),
119137 },
120138 HealthProbeBindAddress : healthAddr ,
121- LeaderElection : enableLeaderElection ,
122- LeaderElectionID : controllerName ,
123- LeaderElectionReleaseOnCancel : true ,
139+ LeaderElection : leaderElectionOptions .Enable ,
140+ LeaderElectionID : leaderElectionID ,
141+ LeaderElectionReleaseOnCancel : leaderElectionOptions .ReleaseOnCancel ,
142+ LeaseDuration : & leaderElectionOptions .LeaseDuration ,
143+ RenewDeadline : & leaderElectionOptions .RenewDeadline ,
144+ Logger : ctrlruntime .Log ,
124145 Controller : ctrlcfg.Controller {
125146 MaxConcurrentReconciles : concurrent ,
126147 RecoverPanic : ptr .To (true ),
148+ ReconciliationTimeout : reconciliationTimeout ,
127149 },
128150 Client : ctrlclient.Options {
129151 Cache : & ctrlclient.CacheOptions {
@@ -136,38 +158,44 @@ func main() {
136158 os .Exit (1 )
137159 }
138160
161+ // Note that the liveness check will pass beyond this point, but the readiness
162+ // check will continue to fail until this controller instance is elected leader.
163+ probes .SetupChecks (mgr , setupLog )
164+
139165 if err = (& controller.ArtifactGeneratorReconciler {
140166 ControllerName : controllerName ,
141167 Client : mgr .GetClient (),
142168 APIReader : mgr .GetAPIReader (),
143169 Scheme : mgr .GetScheme (),
144170 EventRecorder : mgr .GetEventRecorderFor (controllerName ),
145- Storage : storage ,
171+ Storage : artifactStorage ,
146172 ArtifactFetchRetries : httpRetry ,
147173 DependencyRequeueInterval : requeueDependency ,
148174 NoCrossNamespaceRefs : aclOptions .NoCrossNamespaceRefs ,
149- }).SetupWithManager (ctx , mgr ); err != nil {
175+ }).SetupWithManager (ctx , mgr , controller.ArtifactGeneratorReconcilerOptions {
176+ RateLimiter : ctrl .GetRateLimiter (rateLimiterOptions ),
177+ }); err != nil {
150178 setupLog .Error (err , "unable to create controller" , "controller" , swapi .ArtifactGeneratorKind )
151179 os .Exit (1 )
152180 }
153181 // +kubebuilder:scaffold:builder
154182
155183 go func () {
156- // Block until our controller manager is elected leader. We presume our
184+ // Block until our controller manager is elected leader.We presume our
157185 // entire process will terminate if we lose leadership, so we don't need
158186 // to handle that.
159187 <- mgr .Elected ()
160188
161189 // Start the artifact server if running as leader.
190+ // This will make the readiness check pass and Kubernetes will start
191+ // routing traffic from kustomize-controller and helm-controller to this instance.
162192 setupLog .Info ("starting storage server on " + artifactOptions .StorageAddress )
163- if err := artsrv .Start (ctx , & artifactOptions ); err != nil {
193+ if err := server .Start (ctx , & artifactOptions ); err != nil {
164194 setupLog .Error (err , "artifact server error" )
165195 os .Exit (1 )
166196 }
167197 }()
168198
169- probes .SetupChecks (mgr , setupLog )
170-
171199 setupLog .Info ("starting manager" )
172200 if err := mgr .Start (ctx ); err != nil {
173201 setupLog .Error (err , "problem running manager" )
0 commit comments