From 6cfb31ba2d6f3e8c0f26f85970dd4780163f2db1 Mon Sep 17 00:00:00 2001 From: Jaromir Wysoglad Date: Tue, 22 Oct 2024 16:04:21 -0400 Subject: [PATCH] [OSPRH-10800] Watch MetricStorage from Autoscaling MetricStorage is our main source for autoscaling metrics. We default to MetricStorage if other Prometheus instance isn't explicitly configured, so we need to ensure that Autoscaling is configured correctly to access this MetricStorage. We previously decided to not enforce an order of deployment of telemetry resources, so it's pretty valid to first deploy Autoscaling and only after that deploy a MetricStorage. But if MetricStorage isn't deployed, Autoscaling doesn't know if it should configure Aodh to use TLS for Prometheus access or not. Without watching the MetricStorage resources, Autoscaling doesn't know if a MetricStorage CR is created or changed. So it's possible to do the following: - Enable Autoscaling (it gets automatically configured to not use TLS for Prometheus) - Enable MetricStorage with TLS enabled - Aodh can't access Prometheus because of TLS issues. With this PR Autoscaling reacts to MetricStorage changes and so Aodh gets always correctly configured. --- controllers/autoscaling_controller.go | 34 +++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/controllers/autoscaling_controller.go b/controllers/autoscaling_controller.go index aefc28e1..e1f92143 100644 --- a/controllers/autoscaling_controller.go +++ b/controllers/autoscaling_controller.go @@ -788,6 +788,35 @@ func (r *AutoscalingReconciler) SetupWithManager(ctx context.Context, mgr ctrl.M } return nil } + metricStorageFn := func(_ context.Context, o client.Object) []reconcile.Request { + result := []reconcile.Request{} + + // get all autoscaling CRs + autoscalings := &telemetryv1.AutoscalingList{} + listOpts := []client.ListOption{ + client.InNamespace(o.GetNamespace()), + } + if err := r.Client.List(context.Background(), autoscalings, listOpts...); err != nil { + Log.Error(err, "Unable to retrieve Autoscaling CRs %w") + return nil + } + + for _, cr := range autoscalings.Items { + if cr.Spec.PrometheusHost == "" { + // the autoscaling is using MetricStorage for metrics + name := client.ObjectKey{ + Namespace: o.GetNamespace(), + Name: cr.Name, + } + Log.Info(fmt.Sprintf("MetricStorage %s is used by Autoscaling CR %s", o.GetName(), cr.Name)) + result = append(result, reconcile.Request{NamespacedName: name}) + } + } + if len(result) > 0 { + return result + } + return nil + } // index autoscalingCaBundleSecretNameField if err := mgr.GetFieldIndexer().IndexField(context.Background(), &telemetryv1.Autoscaling{}, autoscalingCaBundleSecretNameField, func(rawObj client.Object) []string { // Extract the secret name from the spec, if one is provided @@ -848,6 +877,11 @@ func (r *AutoscalingReconciler) SetupWithManager(ctx context.Context, mgr ctrl.M handler.EnqueueRequestsFromMapFunc(r.findObjectsForSrc), builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), ). + Watches( + &telemetryv1.MetricStorage{}, + handler.EnqueueRequestsFromMapFunc(metricStorageFn), + builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}), + ). Complete(r) }