Skip to content

Commit

Permalink
Remove HorizontalPodAutoscaler from owned and watched resources (#671)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
leochr authored Dec 7, 2024
1 parent 135ff70 commit 747039f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 36 deletions.
78 changes: 42 additions & 36 deletions internal/controller/runtimecomponent_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 21 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 747039f

Please sign in to comment.