From 747039fc116f4371aa8bf8c33b83f4a5e43caec7 Mon Sep 17 00:00:00 2001 From: Leo Christy Jesuraj Date: Fri, 6 Dec 2024 23:07:35 -0500 Subject: [PATCH] Remove HorizontalPodAutoscaler from owned and watched resources (#671) * Remove HorizontalPodAutoscaler from owned and watched resources Signed-off-by: Leo Christy Jesuraj * Add options to disable watch and enable watch on HPA Signed-off-by: Leo Christy Jesuraj * Change env name Signed-off-by: Leo Christy Jesuraj --------- Signed-off-by: Leo Christy Jesuraj --- .../controller/runtimecomponent_controller.go | 78 ++++++++++--------- utils/utils.go | 21 +++++ 2 files changed, 63 insertions(+), 36 deletions(-) diff --git a/internal/controller/runtimecomponent_controller.go b/internal/controller/runtimecomponent_controller.go index 0e6fe5ce..276c87d7 100644 --- a/internal/controller/runtimecomponent_controller.go +++ b/internal/controller/runtimecomponent_controller.go @@ -25,6 +25,7 @@ import ( "github.com/application-stacks/runtime-component-operator/common" "github.com/pkg/errors" + kcontroller "sigs.k8s.io/controller-runtime/pkg/controller" "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/reconcile" @@ -36,7 +37,6 @@ import ( "github.com/go-logr/logr" ctrl "sigs.k8s.io/controller-runtime" - kcontroller "sigs.k8s.io/controller-runtime/pkg/controller" appstacksv1 "github.com/application-stacks/runtime-component-operator/api/v1" imagev1 "github.com/openshift/api/image/v1" @@ -616,44 +616,50 @@ func (r *RuntimeComponentReconciler) SetupWithManager(mgr ctrl.Manager) error { }, } - maxConcurrentReconciles := appstacksutils.GetMaxConcurrentReconciles() + b := ctrl.NewControllerManagedBy(mgr).For(&appstacksv1.RuntimeComponent{}, builder.WithPredicates(pred)) - b := ctrl.NewControllerManagedBy(mgr).For(&appstacksv1.RuntimeComponent{}, builder.WithPredicates(pred)). - Owns(&corev1.Service{}, builder.WithPredicates(predSubResource)). - Owns(&corev1.Secret{}, builder.WithPredicates(predSubResource)). - Owns(&appsv1.Deployment{}, builder.WithPredicates(predSubResWithGenCheck)). - Owns(&appsv1.StatefulSet{}, builder.WithPredicates(predSubResWithGenCheck)). - Owns(&autoscalingv1.HorizontalPodAutoscaler{}, builder.WithPredicates(predSubResource)). - WithOptions(kcontroller.Options{ - MaxConcurrentReconciles: maxConcurrentReconciles, - }) + if !appstacksutils.GetOperatorDisableWatches() { + b = b.Owns(&corev1.Service{}, builder.WithPredicates(predSubResource)). + Owns(&corev1.Secret{}, builder.WithPredicates(predSubResource)). + Owns(&appsv1.Deployment{}, builder.WithPredicates(predSubResWithGenCheck)). + Owns(&appsv1.StatefulSet{}, builder.WithPredicates(predSubResWithGenCheck)) - ok, _ := r.IsGroupVersionSupported(routev1.SchemeGroupVersion.String(), "Route") - if ok { - b = b.Owns(&routev1.Route{}, builder.WithPredicates(predSubResource)) - } - ok, _ = r.IsGroupVersionSupported(networkingv1.SchemeGroupVersion.String(), "Ingress") - if ok { - b = b.Owns(&networkingv1.Ingress{}, builder.WithPredicates(predSubResource)) - } - ok, _ = r.IsGroupVersionSupported(servingv1.SchemeGroupVersion.String(), "Service") - if ok { - b = b.Owns(&servingv1.Service{}, builder.WithPredicates(predSubResource)) - } - ok, _ = r.IsGroupVersionSupported(prometheusv1.SchemeGroupVersion.String(), "ServiceMonitor") - if ok { - b = b.Owns(&prometheusv1.ServiceMonitor{}, builder.WithPredicates(predSubResource)) - } - ok, _ = r.IsGroupVersionSupported(imagev1.SchemeGroupVersion.String(), "ImageStream") - if ok { - b = b.Watches(&imagev1.ImageStream{}, &EnqueueRequestsForCustomIndexField{ - Matcher: &ImageStreamMatcher{ - Klient: mgr.GetClient(), - WatchNamespaces: watchNamespaces, - }, - }) + if appstacksutils.GetOperatorWatchHPA() { + b = b.Owns(&autoscalingv1.HorizontalPodAutoscaler{}, builder.WithPredicates(predSubResource)) + } + + ok, _ := r.IsGroupVersionSupported(routev1.SchemeGroupVersion.String(), "Route") + if ok { + b = b.Owns(&routev1.Route{}, builder.WithPredicates(predSubResource)) + } + ok, _ = r.IsGroupVersionSupported(networkingv1.SchemeGroupVersion.String(), "Ingress") + if ok { + b = b.Owns(&networkingv1.Ingress{}, builder.WithPredicates(predSubResource)) + } + ok, _ = r.IsGroupVersionSupported(servingv1.SchemeGroupVersion.String(), "Service") + if ok { + b = b.Owns(&servingv1.Service{}, builder.WithPredicates(predSubResource)) + } + ok, _ = r.IsGroupVersionSupported(prometheusv1.SchemeGroupVersion.String(), "ServiceMonitor") + if ok { + b = b.Owns(&prometheusv1.ServiceMonitor{}, builder.WithPredicates(predSubResource)) + } + ok, _ = r.IsGroupVersionSupported(imagev1.SchemeGroupVersion.String(), "ImageStream") + if ok { + b = b.Watches(&imagev1.ImageStream{}, &EnqueueRequestsForCustomIndexField{ + Matcher: &ImageStreamMatcher{ + Klient: mgr.GetClient(), + WatchNamespaces: watchNamespaces, + }, + }) + } } - return b.Complete(r) + + maxConcurrentReconciles := appstacksutils.GetMaxConcurrentReconciles() + + return b.WithOptions(kcontroller.Options{ + MaxConcurrentReconciles: maxConcurrentReconciles, + }).Complete(r) } func getMonitoringEnabledLabelName(ba common.BaseComponent) string { diff --git a/utils/utils.go b/utils/utils.go index 860559c1..3dd94d14 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1910,3 +1910,24 @@ func parseEnvAsPositiveInt(envValue string) int { } return -1 } + +// Returns the env setting for Operator watching HorizontalPodAutoscaler (HPA) +func GetOperatorWatchHPA() bool { + return parseEnvAsBool(os.Getenv("OPERATOR_WATCH_HPA")) +} + +// Returns the env setting for disabling all watches +func GetOperatorDisableWatches() bool { + return parseEnvAsBool(os.Getenv("OPERATOR_DISABLE_WATCHES")) +} + +// Parses env as bool or returns false on failure +func parseEnvAsBool(envValue string) bool { + if envValue != "" { + boolVal, err := strconv.ParseBool(envValue) + if err == nil { + return boolVal + } + } + return false +}