-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathservicemonitor.go
81 lines (64 loc) · 2.07 KB
/
servicemonitor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package resources
import (
"errors"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/labels"
"github.com/ydb-platform/ydb-kubernetes-operator/internal/metrics"
)
type ServiceMonitorBuilder struct {
client.Object
Name string
MetricsServices []metrics.Service
TargetPort int
Options *api.MonitoringOptions
Labels labels.Labels
SelectorLabels labels.Labels
}
func (b *ServiceMonitorBuilder) Build(obj client.Object) error {
sm, ok := obj.(*monitoringv1.ServiceMonitor)
if !ok {
return errors.New("failed to cast to ServiceMonitor object")
}
if sm.ObjectMeta.Name == "" {
sm.ObjectMeta.Name = b.Object.GetName()
}
sm.ObjectMeta.Namespace = b.GetNamespace()
sm.ObjectMeta.Labels = b.Labels
sm.Spec.Endpoints = b.buildEndpoints()
sm.Spec.NamespaceSelector = monitoringv1.NamespaceSelector{
MatchNames: []string{
b.GetNamespace(),
},
}
sm.Spec.Selector = metav1.LabelSelector{
MatchLabels: b.SelectorLabels,
}
return nil
}
func (b *ServiceMonitorBuilder) buildEndpoints() []monitoringv1.Endpoint {
endpoints := make([]monitoringv1.Endpoint, 0, len(b.MetricsServices))
for _, service := range b.MetricsServices {
metricRelabelings := service.Relabelings
if len(b.Options.MetricRelabelings) > 0 {
metricRelabelings = append(metricRelabelings, b.Options.MetricRelabelings...)
}
endpoints = append(endpoints, monitoringv1.Endpoint{
Path: service.Path,
TargetPort: &intstr.IntOrString{IntVal: int32(b.TargetPort)},
MetricRelabelConfigs: metricRelabelings,
})
}
return endpoints
}
func (b *ServiceMonitorBuilder) Placeholder(cr client.Object) client.Object {
return &monitoringv1.ServiceMonitor{
ObjectMeta: metav1.ObjectMeta{
Name: cr.GetName(),
Namespace: cr.GetNamespace(),
},
}
}