@@ -24,7 +24,9 @@ import (
2424)
2525
2626const (
27- mainResourceName = "prometheus"
27+ mainResourceName = "prometheus"
28+ serviceMonitorAccountingLabel = "accounting"
29+ serviceMonitorAccountingValue = "kai"
2830)
2931
3032func prometheusForKAIConfig (
@@ -96,7 +98,7 @@ func prometheusForKAIConfig(
9698 if config .ServiceMonitor != nil && * config .ServiceMonitor .Enabled {
9799 prometheusSpec .ServiceMonitorSelector = & metav1.LabelSelector {
98100 MatchLabels : map [string ]string {
99- "accounting" : mainResourceName ,
101+ serviceMonitorAccountingLabel : serviceMonitorAccountingValue ,
100102 },
101103 }
102104 prometheusSpec .ServiceMonitorNamespaceSelector = & metav1.LabelSelector {}
@@ -136,18 +138,26 @@ func serviceMonitorsForKAIConfig(
136138 return nil , err
137139 }
138140
139- serviceMonitorObj .GetLabels ()["accounting" ] = mainResourceName
141+ serviceMonitorObj .GetLabels ()[serviceMonitorAccountingLabel ] = serviceMonitorAccountingValue
142+
143+ namespaces := []string {kaiConfig .Spec .Namespace }
144+ if kaiService .Namespaces != nil {
145+ namespaces = kaiService .Namespaces
146+ }
147+
148+ labelSelector := map [string ]string {"app" : kaiService .Name }
149+ if kaiService .LabelSelector != nil {
150+ labelSelector = kaiService .LabelSelector
151+ }
140152
141153 // Set the ServiceMonitor spec from configuration
142154 serviceMonitorSpec := monitoringv1.ServiceMonitorSpec {
143155 JobLabel : kaiService .JobLabel ,
144156 NamespaceSelector : monitoringv1.NamespaceSelector {
145- MatchNames : [] string { kaiConfig . Spec . Namespace } ,
157+ MatchNames : namespaces ,
146158 },
147159 Selector : metav1.LabelSelector {
148- MatchLabels : map [string ]string {
149- "app" : kaiService .Name ,
150- },
160+ MatchLabels : labelSelector ,
151161 },
152162 Endpoints : []monitoringv1.Endpoint {
153163 {
@@ -172,35 +182,6 @@ func serviceMonitorsForKAIConfig(
172182 serviceMonitors = append (serviceMonitors , serviceMonitorObj )
173183 }
174184
175- kubeStateMetric := & monitoringv1.ServiceMonitor {
176- ObjectMeta : metav1.ObjectMeta {
177- Name : "kube-state-metrics" ,
178- Namespace : kaiConfig .Spec .Namespace ,
179- Labels : map [string ]string {
180- "accounting" : mainResourceName ,
181- },
182- },
183- Spec : monitoringv1.ServiceMonitorSpec {
184- JobLabel : "kube-state-metrics" ,
185- NamespaceSelector : monitoringv1.NamespaceSelector {
186- MatchNames : []string {"monitoring" , "default" },
187- },
188- Selector : metav1.LabelSelector {
189- MatchLabels : map [string ]string {
190- "app.kubernetes.io/name" : "kube-state-metrics" ,
191- },
192- },
193- Endpoints : []monitoringv1.Endpoint {
194- {
195- Port : "http" ,
196- BearerTokenFile : "/var/run/secrets/kubernetes.io/serviceaccount/token" ,
197- Interval : "30s" ,
198- },
199- },
200- },
201- }
202-
203- serviceMonitors = append (serviceMonitors , kubeStateMetric )
204185 return serviceMonitors , nil
205186}
206187
0 commit comments