@@ -17,6 +17,7 @@ limitations under the License.
1717package main
1818
1919import (
20+ "context"
2021 "crypto/tls"
2122 "flag"
2223 "os"
@@ -50,6 +51,24 @@ import (
5051 // +kubebuilder:scaffold:imports
5152)
5253
54+ type StartupReconciler struct {
55+ engine * rules.InMemoryEngine
56+ }
57+
58+ func (s * StartupReconciler ) Start (ctx context.Context ) error {
59+ setupLog .Info ("Running initial full reconciliation" )
60+ if err := s .engine .Reconcile (ctx ); err != nil {
61+ setupLog .Error (err , "failed to run initial reconciliation" )
62+ return err
63+ }
64+ setupLog .Info ("Initial reconciliation completed successfully" )
65+ return nil
66+ }
67+
68+ func (s * StartupReconciler ) NeedLeaderElection () bool {
69+ return true
70+ }
71+
5372var (
5473 scheme = runtime .NewScheme ()
5574 setupLog = ctrl .Log .WithName ("setup" )
@@ -144,7 +163,7 @@ func main() {
144163
145164 // Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
146165 // More info:
147- // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.21.0 /pkg/metrics/server
166+ // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.22.1 /pkg/metrics/server
148167 // - https://book.kubebuilder.io/reference/metrics.html
149168 metricsServerOptions := metricsserver.Options {
150169 BindAddress : metricsAddr ,
@@ -156,7 +175,7 @@ func main() {
156175 // FilterProvider is used to protect the metrics endpoint with authn/authz.
157176 // These configurations ensure that only authorized users and service accounts
158177 // can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
159- // https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.21.0 /pkg/metrics/filters#WithAuthenticationAndAuthorization
178+ // https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.22.1 /pkg/metrics/filters#WithAuthenticationAndAuthorization
160179 metricsServerOptions .FilterProvider = filters .WithAuthenticationAndAuthorization
161180 }
162181
@@ -232,12 +251,16 @@ func main() {
232251
233252 var engine rules.InMemoryEngine
234253 if len (targetNamespaces ) > 0 {
235- engine = rules .NewInMemoryEngineWithNamespaces (mgr .GetClient (), targetNamespaces )
254+ engine = rules .NewInMemoryEngineWithNamespaces (mgr .GetClient (), mgr . GetScheme (), targetNamespaces )
236255 } else {
237- engine = rules .NewInMemoryEngine (mgr .GetClient (), targetNamespaceRegex )
256+ engine = rules .NewInMemoryEngine (mgr .GetClient (), mgr . GetScheme (), targetNamespaceRegex )
238257 }
239258
240- // Create the rules engine instance
259+ // Add startup runnable to perform initial full reconciliation
260+ if err := mgr .Add (& StartupReconciler {engine : & engine }); err != nil {
261+ setupLog .Error (err , "unable to add startup reconciler" )
262+ os .Exit (1 )
263+ }
241264
242265 // Create new prometheus metrics collector instance
243266 octopusMetricsCollector := metrics .NewOctopusMetricsCollector (mgr .GetClient (), & engine )
0 commit comments