Skip to content

Commit

Permalink
DVO-209: CPU leak fix (#323)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ncaak authored May 6, 2024
1 parent d7f475e commit dd287c8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pkg/controller/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
15 changes: 14 additions & 1 deletion pkg/controller/generic_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit dd287c8

Please sign in to comment.