From dd287c8364b3e9789bf8e182873dcc21f7acfdf2 Mon Sep 17 00:00:00 2001 From: Isaac Jimeno Date: Mon, 6 May 2024 10:50:37 +0200 Subject: [PATCH] DVO-209: CPU leak fix (#323) * Fix request namespaces and sleep if nothing to validate * Fix namespaces length setting * Fix end loop on no namespace and logging info * Minor refactor and cleaning debug comments * Fix added elapsed time as a const --- pkg/controller/const.go | 4 ++++ pkg/controller/generic_reconciler.go | 15 ++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/pkg/controller/const.go b/pkg/controller/const.go index 6ff8c3e8..d0f7f216 100644 --- a/pkg/controller/const.go +++ b/pkg/controller/const.go @@ -11,6 +11,10 @@ const ( // number of resource instances for any kubernetes resource defaultListLimit = 5 + // default time (in seconds) to skip the loop in the generic reconciler + // if there are no namespaces to validate in the cluster + defaultNoNamespacesElapseTime = 10 + // EnvKubeClientQPS overrides defaultKubeClientQPS EnvKubeClientQPS string = "KUBECLIENT_QPS" diff --git a/pkg/controller/generic_reconciler.go b/pkg/controller/generic_reconciler.go index 9aa17ac6..b0e5ebbc 100644 --- a/pkg/controller/generic_reconciler.go +++ b/pkg/controller/generic_reconciler.go @@ -9,6 +9,7 @@ import ( "strconv" "strings" "sync" + "time" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -117,6 +118,19 @@ func (gr *GenericReconciler) Start(ctx context.Context) error { return nil default: gr.logger.Info("Reconciliation loop has started") + + // Skips validation if no valid namespaces in the cluster. Avoids CPU overusage + gr.watchNamespaces.resetCache() + if namespaces, err := gr.watchNamespaces.getWatchNamespaces(ctx, gr.client); err != nil { + gr.logger.Error(err, "getting watched namespaces") + continue + + } else if namespaces == nil || len(*namespaces) == 0 { + gr.logger.Info("No namespaces to validate, skipping loop") + time.Sleep(defaultNoNamespacesElapseTime * time.Second) + continue + } + if err := gr.reconcileEverything(ctx); err != nil && !errors.Is(err, context.Canceled) { // TODO: Improve error handling so that error can be returned to manager from here // this is done to make sure errors caused by skew in k8s version on server and @@ -177,7 +191,6 @@ func (gr *GenericReconciler) reconcileEverything(ctx context.Context) error { "Kind", resource.Kind) } - gr.watchNamespaces.resetCache() namespaces, err := gr.watchNamespaces.getWatchNamespaces(ctx, gr.client) if err != nil { return fmt.Errorf("getting watched namespaces: %w", err)