@@ -20,6 +20,7 @@ import (
2020 "context"
2121 "embed"
2222 "fmt"
23+ "os"
2324 "path/filepath"
2425 "strings"
2526
@@ -41,11 +42,13 @@ import (
4142)
4243
4344const (
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"
4952)
5053
5154type ArgoCDMetricsReconciler struct {
@@ -170,6 +173,11 @@ func (r *ArgoCDMetricsReconciler) Reconcile(ctx context.Context, request reconci
170173 return reconcile.Result {}, err
171174 }
172175
176+ err = r .reconcileOperatorMetricsServiceMonitor (reqLogger )
177+ if err != nil {
178+ return reconcile.Result {}, err
179+ }
180+
173181 return reconcile.Result {}, nil
174182}
175183
@@ -274,6 +282,38 @@ func (r *ArgoCDMetricsReconciler) createServiceMonitorIfAbsent(namespace string,
274282 return err
275283}
276284
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+
277317func (r * ArgoCDMetricsReconciler ) createPrometheusRuleIfAbsent (namespace string , argocd * argoapp.ArgoCD , reqLogger logr.Logger ) error {
278318 alertRule := newPrometheusRule (namespace )
279319 existingAlertRule := & monitoringv1.PrometheusRule {}
0 commit comments