Skip to content

Commit 8f68ef2

Browse files
committed
Fixed service monitors
1 parent 24206f0 commit 8f68ef2

File tree

3 files changed

+24
-38
lines changed

3 files changed

+24
-38
lines changed

deployments/kai-scheduler/templates/rbac/prometheus-binding.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ metadata:
77
name: kai-prometheus
88
subjects:
99
- kind: ServiceAccount
10-
name: kai-prometheus
10+
name: prometheus
1111
namespace: kai-scheduler
1212
roleRef:
1313
kind: ClusterRole

pkg/operator/operands/common/common.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@ var KaiServicesForServiceMonitor = []struct {
3131
Name string
3232
Port string
3333
JobLabel string
34+
35+
LabelSelector map[string]string // optional, if not provided, "app": NAME will be used
36+
Namespaces []string // optional, if not provided, the service will be monitored in the namespace of the KAI config
3437
}{
35-
{"queue-controller", "metrics", "queue-controller"},
38+
{"queue-controller", "metrics", "queue-controller", nil, nil},
39+
{"kube-state-metrics", "http", "kube-state-metrics",
40+
map[string]string{"app.kubernetes.io/name": "kube-state-metrics"}, []string{"monitoring", "default"}},
3641
}
3742

3843
func AllControllersAvailable(

pkg/operator/operands/prometheus/resources.go

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ import (
2424
)
2525

2626
const (
27-
mainResourceName = "prometheus"
27+
mainResourceName = "prometheus"
28+
serviceMonitorAccountingLabel = "accounting"
29+
serviceMonitorAccountingValue = "kai"
2830
)
2931

3032
func 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

Comments
 (0)