Skip to content

Commit 157853a

Browse files
authored
use the existing VALIDATION_CHECK_INTERVAL env var and run kube-linter validations accordingly (#347)
This reverts commit dd287c8.
1 parent 171f57f commit 157853a

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

bundle/manifests/deployment-validation-operator.clusterserviceversion.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ spec:
7777
- name: RESOURCES_PER_LIST_QUERY
7878
value: "5"
7979
- name: VALIDATION_CHECK_INTERVAL
80-
value: "2"
80+
value: "2m"
8181
- name: POD_NAME
8282
valueFrom:
8383
fieldRef:

deploy/openshift/operator.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ spec:
8080
- name: RESOURCES_PER_LIST_QUERY
8181
value: "5"
8282
- name: VALIDATION_CHECK_INTERVAL
83-
value: "2"
83+
value: "2m"
8484
- name: POD_NAME
8585
valueFrom:
8686
fieldRef:

pkg/controller/const.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@ const (
1111
// number of resource instances for any kubernetes resource
1212
defaultListLimit = 5
1313

14-
// default time (in seconds) to skip the loop in the generic reconciler
15-
// if there are no namespaces to validate in the cluster
16-
defaultNoNamespacesElapseTime = 10
17-
1814
// EnvKubeClientQPS overrides defaultKubeClientQPS
1915
EnvKubeClientQPS string = "KUBECLIENT_QPS"
2016

@@ -24,4 +20,7 @@ const (
2420
// EnvNamespaceIgnorePattern sets the pattern for ignoring namespaces from the list of namespaces
2521
// that are in the validate list of this operator
2622
EnvNamespaceIgnorePattern string = "NAMESPACE_IGNORE_PATTERN"
23+
24+
// EnvValidationCheckInterval sets the frequency of the kube-linter check validations in minutes
25+
EnvValidationCheckInterval string = "VALIDATION_CHECK_INTERVAL"
2726
)

pkg/controller/generic_reconciler.go

+24-14
Original file line numberDiff line numberDiff line change
@@ -111,26 +111,23 @@ func (gr *GenericReconciler) AddToManager(mgr manager.Manager) error {
111111
func (gr *GenericReconciler) Start(ctx context.Context) error {
112112
go gr.LookForConfigUpdates(ctx)
113113

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+
}
114123
for {
115124
select {
116125
case <-ctx.Done():
126+
t.Stop()
117127
// stop reconciling
118128
return nil
119-
default:
129+
case <-t.C:
120130
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-
134131
if err := gr.reconcileEverything(ctx); err != nil && !errors.Is(err, context.Canceled) {
135132
// TODO: Improve error handling so that error can be returned to manager from here
136133
// 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 {
191188
"Kind", resource.Kind)
192189
}
193190

191+
gr.watchNamespaces.resetCache()
194192
namespaces, err := gr.watchNamespaces.getWatchNamespaces(ctx, gr.client)
195193
if err != nil {
196194
return fmt.Errorf("getting watched namespaces: %w", err)
@@ -423,3 +421,15 @@ func (gr GenericReconciler) getNamespacedResourcesGVK(resources []metav1.APIReso
423421
}
424422
return namespacedResources
425423
}
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

Comments
 (0)