Skip to content

Commit 5a4b059

Browse files
Merge pull request redhat-developer#622 from jaideepr97/fix-svc-monitor
fix: reconcile operator metrics service monitor based on installation namespace
2 parents 3debea1 + 9d3838e commit 5a4b059

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

controllers/argocd_controller.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,12 @@ import (
4545
)
4646

4747
const (
48-
argocdNS = "openshift-gitops"
49-
depracatedArgoCDNS = "openshift-pipelines-app-delivery"
50-
consoleLinkName = "argocd"
51-
argocdRouteName = "openshift-gitops-server"
52-
iconFilePath = "/argo.png"
48+
argocdNS = "openshift-gitops"
49+
depracatedArgoCDNS = "openshift-pipelines-app-delivery"
50+
consoleLinkName = "argocd"
51+
argocdRouteName = "openshift-gitops-server"
52+
iconFilePath = "/argo.png"
53+
operatorPodNamespacePath = "/var/run/secrets/kubernetes.io/serviceaccount/namespace"
5354
)
5455

5556
var (

controllers/argocd_metrics_controller.go

+45-5
Original file line numberDiff line numberDiff line change
@@ -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

4344
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"
4952
)
5053

5154
type 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+
277317
func (r *ArgoCDMetricsReconciler) createPrometheusRuleIfAbsent(namespace string, argocd *argoapp.ArgoCD, reqLogger logr.Logger) error {
278318
alertRule := newPrometheusRule(namespace)
279319
existingAlertRule := &monitoringv1.PrometheusRule{}

0 commit comments

Comments
 (0)