@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"embed"
22
22
"fmt"
23
+ "os"
23
24
"path/filepath"
24
25
"strings"
25
26
@@ -41,11 +42,13 @@ import (
41
42
)
42
43
43
44
const (
44
- readRoleNameFormat = "%s-read"
45
- readRoleBindingNameFormat = "%s-prometheus-k8s-read-binding"
46
- alertRuleName = "gitops-operator-argocd-alerts"
47
- dashboardNamespace = "openshift-config-managed"
48
- dashboardFolder = "dashboards"
45
+ readRoleNameFormat = "%s-read"
46
+ readRoleBindingNameFormat = "%s-prometheus-k8s-read-binding"
47
+ alertRuleName = "gitops-operator-argocd-alerts"
48
+ dashboardNamespace = "openshift-config-managed"
49
+ dashboardFolder = "dashboards"
50
+ operatorMetricsServiceName = "openshift-gitops-operator-metrics-service"
51
+ operatorMetricsMonitorName = "openshift-gitops-operator-metrics-monitor"
49
52
)
50
53
51
54
type ArgoCDMetricsReconciler struct {
@@ -170,6 +173,11 @@ func (r *ArgoCDMetricsReconciler) Reconcile(ctx context.Context, request reconci
170
173
return reconcile.Result {}, err
171
174
}
172
175
176
+ err = r .reconcileOperatorMetricsServiceMonitor (reqLogger )
177
+ if err != nil {
178
+ return reconcile.Result {}, err
179
+ }
180
+
173
181
return reconcile.Result {}, nil
174
182
}
175
183
@@ -274,6 +282,38 @@ func (r *ArgoCDMetricsReconciler) createServiceMonitorIfAbsent(namespace string,
274
282
return err
275
283
}
276
284
285
+ func (r * ArgoCDMetricsReconciler ) reconcileOperatorMetricsServiceMonitor (reqLogger logr.Logger ) error {
286
+
287
+ data , err := os .ReadFile (operatorPodNamespacePath )
288
+ if err != nil {
289
+ reqLogger .Error (err , "Error retrieving operator's running namespace" )
290
+ return err
291
+ }
292
+
293
+ operatorNS := string (data )
294
+ desiredMetricsServerName := operatorMetricsServiceName + "." + operatorNS + ".svc"
295
+
296
+ existingServiceMonitor := & monitoringv1.ServiceMonitor {}
297
+ err = r .Client .Get (context .TODO (), types.NamespacedName {Name : operatorMetricsMonitorName , Namespace : operatorNS }, existingServiceMonitor )
298
+
299
+ if err != nil {
300
+ if ! errors .IsNotFound (err ) {
301
+ reqLogger .Error (err , "Error querying for ServiceMonitor" , "Namespace" , operatorNS , "Name" , operatorMetricsMonitorName )
302
+ return err
303
+ }
304
+
305
+ // no svc monitor found, nothing to do
306
+ return nil
307
+ }
308
+
309
+ if existingServiceMonitor .Spec .Endpoints [0 ].TLSConfig .ServerName != desiredMetricsServerName {
310
+ existingServiceMonitor .Spec .Endpoints [0 ].TLSConfig .ServerName = desiredMetricsServerName
311
+ return r .Client .Update (context .TODO (), existingServiceMonitor )
312
+ }
313
+
314
+ return nil
315
+ }
316
+
277
317
func (r * ArgoCDMetricsReconciler ) createPrometheusRuleIfAbsent (namespace string , argocd * argoapp.ArgoCD , reqLogger logr.Logger ) error {
278
318
alertRule := newPrometheusRule (namespace )
279
319
existingAlertRule := & monitoringv1.PrometheusRule {}
0 commit comments