@@ -111,26 +111,23 @@ func (gr *GenericReconciler) AddToManager(mgr manager.Manager) error {
111
111
func (gr * GenericReconciler ) Start (ctx context.Context ) error {
112
112
go gr .LookForConfigUpdates (ctx )
113
113
114
+ interval , err := getValidationInterval ()
115
+ if err != nil {
116
+ return err
117
+ }
118
+ t := time .NewTicker (interval )
119
+ err = gr .reconcileEverything (ctx )
120
+ if err != nil && ! errors .Is (err , context .Canceled ) {
121
+ gr .logger .Error (err , "error fetching and validating resource types" )
122
+ }
114
123
for {
115
124
select {
116
125
case <- ctx .Done ():
126
+ t .Stop ()
117
127
// stop reconciling
118
128
return nil
119
- default :
129
+ case <- t . C :
120
130
gr .logger .Info ("Reconciliation loop has started" )
121
-
122
- // Skips validation if no valid namespaces in the cluster. Avoids CPU overusage
123
- gr .watchNamespaces .resetCache ()
124
- if namespaces , err := gr .watchNamespaces .getWatchNamespaces (ctx , gr .client ); err != nil {
125
- gr .logger .Error (err , "getting watched namespaces" )
126
- continue
127
-
128
- } else if namespaces == nil || len (* namespaces ) == 0 {
129
- gr .logger .Info ("No namespaces to validate, skipping loop" )
130
- time .Sleep (defaultNoNamespacesElapseTime * time .Second )
131
- continue
132
- }
133
-
134
131
if err := gr .reconcileEverything (ctx ); err != nil && ! errors .Is (err , context .Canceled ) {
135
132
// TODO: Improve error handling so that error can be returned to manager from here
136
133
// this is done to make sure errors caused by skew in k8s version on server and
@@ -191,6 +188,7 @@ func (gr *GenericReconciler) reconcileEverything(ctx context.Context) error {
191
188
"Kind" , resource .Kind )
192
189
}
193
190
191
+ gr .watchNamespaces .resetCache ()
194
192
namespaces , err := gr .watchNamespaces .getWatchNamespaces (ctx , gr .client )
195
193
if err != nil {
196
194
return fmt .Errorf ("getting watched namespaces: %w" , err )
@@ -423,3 +421,15 @@ func (gr GenericReconciler) getNamespacedResourcesGVK(resources []metav1.APIReso
423
421
}
424
422
return namespacedResources
425
423
}
424
+
425
+ // getValidationInterval tries to lookup the VALIDATION_CHECK_INTERVAL
426
+ // environment variable and parse the value as the time duration.
427
+ // If the variable lookup fails then the default duration is 2 minutes.
428
+ func getValidationInterval () (time.Duration , error ) {
429
+ validIntString , ok := os .LookupEnv (EnvValidationCheckInterval )
430
+ if ! ok {
431
+ validIntString = "2m"
432
+ }
433
+
434
+ return time .ParseDuration (validIntString )
435
+ }
0 commit comments