Skip to content

Commit 747039f

Browse files
authored
Remove HorizontalPodAutoscaler from owned and watched resources (#671)
* Remove HorizontalPodAutoscaler from owned and watched resources Signed-off-by: Leo Christy Jesuraj <[email protected]> * Add options to disable watch and enable watch on HPA Signed-off-by: Leo Christy Jesuraj <[email protected]> * Change env name Signed-off-by: Leo Christy Jesuraj <[email protected]> --------- Signed-off-by: Leo Christy Jesuraj <[email protected]>
1 parent 135ff70 commit 747039f

File tree

2 files changed

+63
-36
lines changed

2 files changed

+63
-36
lines changed

internal/controller/runtimecomponent_controller.go

+42-36
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/application-stacks/runtime-component-operator/common"
2626
"github.com/pkg/errors"
2727

28+
kcontroller "sigs.k8s.io/controller-runtime/pkg/controller"
2829
"sigs.k8s.io/controller-runtime/pkg/event"
2930
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3031

@@ -36,7 +37,6 @@ import (
3637
"github.com/go-logr/logr"
3738

3839
ctrl "sigs.k8s.io/controller-runtime"
39-
kcontroller "sigs.k8s.io/controller-runtime/pkg/controller"
4040

4141
appstacksv1 "github.com/application-stacks/runtime-component-operator/api/v1"
4242
imagev1 "github.com/openshift/api/image/v1"
@@ -616,44 +616,50 @@ func (r *RuntimeComponentReconciler) SetupWithManager(mgr ctrl.Manager) error {
616616
},
617617
}
618618

619-
maxConcurrentReconciles := appstacksutils.GetMaxConcurrentReconciles()
619+
b := ctrl.NewControllerManagedBy(mgr).For(&appstacksv1.RuntimeComponent{}, builder.WithPredicates(pred))
620620

621-
b := ctrl.NewControllerManagedBy(mgr).For(&appstacksv1.RuntimeComponent{}, builder.WithPredicates(pred)).
622-
Owns(&corev1.Service{}, builder.WithPredicates(predSubResource)).
623-
Owns(&corev1.Secret{}, builder.WithPredicates(predSubResource)).
624-
Owns(&appsv1.Deployment{}, builder.WithPredicates(predSubResWithGenCheck)).
625-
Owns(&appsv1.StatefulSet{}, builder.WithPredicates(predSubResWithGenCheck)).
626-
Owns(&autoscalingv1.HorizontalPodAutoscaler{}, builder.WithPredicates(predSubResource)).
627-
WithOptions(kcontroller.Options{
628-
MaxConcurrentReconciles: maxConcurrentReconciles,
629-
})
621+
if !appstacksutils.GetOperatorDisableWatches() {
622+
b = b.Owns(&corev1.Service{}, builder.WithPredicates(predSubResource)).
623+
Owns(&corev1.Secret{}, builder.WithPredicates(predSubResource)).
624+
Owns(&appsv1.Deployment{}, builder.WithPredicates(predSubResWithGenCheck)).
625+
Owns(&appsv1.StatefulSet{}, builder.WithPredicates(predSubResWithGenCheck))
630626

631-
ok, _ := r.IsGroupVersionSupported(routev1.SchemeGroupVersion.String(), "Route")
632-
if ok {
633-
b = b.Owns(&routev1.Route{}, builder.WithPredicates(predSubResource))
634-
}
635-
ok, _ = r.IsGroupVersionSupported(networkingv1.SchemeGroupVersion.String(), "Ingress")
636-
if ok {
637-
b = b.Owns(&networkingv1.Ingress{}, builder.WithPredicates(predSubResource))
638-
}
639-
ok, _ = r.IsGroupVersionSupported(servingv1.SchemeGroupVersion.String(), "Service")
640-
if ok {
641-
b = b.Owns(&servingv1.Service{}, builder.WithPredicates(predSubResource))
642-
}
643-
ok, _ = r.IsGroupVersionSupported(prometheusv1.SchemeGroupVersion.String(), "ServiceMonitor")
644-
if ok {
645-
b = b.Owns(&prometheusv1.ServiceMonitor{}, builder.WithPredicates(predSubResource))
646-
}
647-
ok, _ = r.IsGroupVersionSupported(imagev1.SchemeGroupVersion.String(), "ImageStream")
648-
if ok {
649-
b = b.Watches(&imagev1.ImageStream{}, &EnqueueRequestsForCustomIndexField{
650-
Matcher: &ImageStreamMatcher{
651-
Klient: mgr.GetClient(),
652-
WatchNamespaces: watchNamespaces,
653-
},
654-
})
627+
if appstacksutils.GetOperatorWatchHPA() {
628+
b = b.Owns(&autoscalingv1.HorizontalPodAutoscaler{}, builder.WithPredicates(predSubResource))
629+
}
630+
631+
ok, _ := r.IsGroupVersionSupported(routev1.SchemeGroupVersion.String(), "Route")
632+
if ok {
633+
b = b.Owns(&routev1.Route{}, builder.WithPredicates(predSubResource))
634+
}
635+
ok, _ = r.IsGroupVersionSupported(networkingv1.SchemeGroupVersion.String(), "Ingress")
636+
if ok {
637+
b = b.Owns(&networkingv1.Ingress{}, builder.WithPredicates(predSubResource))
638+
}
639+
ok, _ = r.IsGroupVersionSupported(servingv1.SchemeGroupVersion.String(), "Service")
640+
if ok {
641+
b = b.Owns(&servingv1.Service{}, builder.WithPredicates(predSubResource))
642+
}
643+
ok, _ = r.IsGroupVersionSupported(prometheusv1.SchemeGroupVersion.String(), "ServiceMonitor")
644+
if ok {
645+
b = b.Owns(&prometheusv1.ServiceMonitor{}, builder.WithPredicates(predSubResource))
646+
}
647+
ok, _ = r.IsGroupVersionSupported(imagev1.SchemeGroupVersion.String(), "ImageStream")
648+
if ok {
649+
b = b.Watches(&imagev1.ImageStream{}, &EnqueueRequestsForCustomIndexField{
650+
Matcher: &ImageStreamMatcher{
651+
Klient: mgr.GetClient(),
652+
WatchNamespaces: watchNamespaces,
653+
},
654+
})
655+
}
655656
}
656-
return b.Complete(r)
657+
658+
maxConcurrentReconciles := appstacksutils.GetMaxConcurrentReconciles()
659+
660+
return b.WithOptions(kcontroller.Options{
661+
MaxConcurrentReconciles: maxConcurrentReconciles,
662+
}).Complete(r)
657663
}
658664

659665
func getMonitoringEnabledLabelName(ba common.BaseComponent) string {

utils/utils.go

+21
Original file line numberDiff line numberDiff line change
@@ -1910,3 +1910,24 @@ func parseEnvAsPositiveInt(envValue string) int {
19101910
}
19111911
return -1
19121912
}
1913+
1914+
// Returns the env setting for Operator watching HorizontalPodAutoscaler (HPA)
1915+
func GetOperatorWatchHPA() bool {
1916+
return parseEnvAsBool(os.Getenv("OPERATOR_WATCH_HPA"))
1917+
}
1918+
1919+
// Returns the env setting for disabling all watches
1920+
func GetOperatorDisableWatches() bool {
1921+
return parseEnvAsBool(os.Getenv("OPERATOR_DISABLE_WATCHES"))
1922+
}
1923+
1924+
// Parses env as bool or returns false on failure
1925+
func parseEnvAsBool(envValue string) bool {
1926+
if envValue != "" {
1927+
boolVal, err := strconv.ParseBool(envValue)
1928+
if err == nil {
1929+
return boolVal
1930+
}
1931+
}
1932+
return false
1933+
}

0 commit comments

Comments
 (0)