github.com/verrazzano/verrazzano@v1.7.0/application-operator/mcagent/mcagent_metrics.go (about) 1 // Copyright (c) 2022, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package mcagent 5 6 import ( 7 v1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" 8 "k8s.io/apimachinery/pkg/api/meta" 9 "sigs.k8s.io/controller-runtime/pkg/client" 10 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" 11 ) 12 13 const prometheusClusterNameLabel = "verrazzano_cluster" 14 15 func (s *Syncer) updatePrometheusMonitorsClusterName() error { 16 err := s.updateServiceMonitorsClusterName() 17 if err != nil { 18 return err 19 } 20 err = s.updatePodMonitorsClusterName() 21 return err 22 } 23 24 // updateServiceMonitorsClusterName - updates the cluster name in all ServiceMonitors in the managed 25 // cluster 26 func (s *Syncer) updateServiceMonitorsClusterName() error { 27 serviceMonitors, err := s.listManagedClusterServiceMonitors() 28 if err != nil { 29 return err 30 } 31 for _, svcMonitor := range serviceMonitors.Items { 32 _, err = controllerutil.CreateOrUpdate(s.Context, s.LocalClient, svcMonitor, func() error { 33 for _, endpoint := range svcMonitor.Spec.Endpoints { 34 updated := updateClusterNameRelabelConfigs(endpoint.RelabelConfigs, s.ManagedClusterName) 35 if updated { 36 s.Log.Infof("Updating managed cluster name to %s for service monitor %s", s.ManagedClusterName, svcMonitor.Name) 37 } 38 } 39 return nil 40 }) 41 if err != nil { 42 s.Log.Errorf("Error updating managed cluster name in metrics for service monitor %s: %v", svcMonitor.Name, err) 43 return err 44 } 45 } 46 return nil 47 } 48 49 // updatePodMonitorsClusterName - updates the cluster name in all PodMonitors in the managed 50 // cluster 51 func (s *Syncer) updatePodMonitorsClusterName() error { 52 podMonitors, err := s.listManagedClusterPodMonitors() 53 if err != nil { 54 return err 55 } 56 for _, podMonitor := range podMonitors.Items { 57 _, err = controllerutil.CreateOrUpdate(s.Context, s.LocalClient, podMonitor, func() error { 58 for _, endpoint := range podMonitor.Spec.PodMetricsEndpoints { 59 updated := updateClusterNameRelabelConfigs(endpoint.RelabelConfigs, s.ManagedClusterName) 60 if updated { 61 s.Log.Infof("Updating managed cluster name to %s for pod monitor %s", s.ManagedClusterName, podMonitor.Name) 62 } 63 } 64 return nil 65 }) 66 if err != nil { 67 s.Log.Errorf("Error updating managed cluster name in metrics for pod monitor %s: %v", podMonitor.Name, err) 68 return err 69 } 70 } 71 return nil 72 } 73 74 // updateClusterNameRelabelConfigs - given a list of relabel configs, finds the one for the 75 // verrazzano_cluster name target label, and updates the cluster name replacement value 76 func updateClusterNameRelabelConfigs(configs []*v1.RelabelConfig, clusterName string) bool { 77 updated := false 78 for _, relabelCfg := range configs { 79 if relabelCfg.TargetLabel == prometheusClusterNameLabel { 80 if relabelCfg.Replacement != clusterName { 81 updated = true 82 } 83 relabelCfg.Replacement = clusterName 84 } 85 } 86 return updated 87 } 88 89 func (s *Syncer) listManagedClusterServiceMonitors() (*v1.ServiceMonitorList, error) { 90 listOptions := &client.ListOptions{Namespace: ""} 91 serviceMonitorsList := v1.ServiceMonitorList{} 92 err := s.LocalClient.List(s.Context, &serviceMonitorsList, listOptions) 93 // Only return an error if the Prometheus CRDs are installed 94 if err != nil && !meta.IsNoMatchError(err) { 95 return nil, err 96 } 97 return &serviceMonitorsList, nil 98 } 99 100 func (s *Syncer) listManagedClusterPodMonitors() (*v1.PodMonitorList, error) { 101 listOptions := &client.ListOptions{Namespace: ""} 102 podMonitorsList := v1.PodMonitorList{} 103 err := s.LocalClient.List(s.Context, &podMonitorsList, listOptions) 104 // Only return an error if the Prometheus CRDs are installed 105 if err != nil && !meta.IsNoMatchError(err) { 106 return nil, err 107 } 108 return &podMonitorsList, nil 109 }