/
service_monitor.go
73 lines (61 loc) · 2.17 KB
/
service_monitor.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
package elasticsearch
import (
"context"
"fmt"
"github.com/ViaQ/logerr/kverrors"
"github.com/openshift/elasticsearch-operator/internal/manifests/servicemonitor"
monitoringv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
prometheusCAFile = "/etc/prometheus/configmaps/serving-certs-ca-bundle/service-ca.crt"
)
// CreateOrUpdateServiceMonitors ensures the existence of ServiceMonitors for Elasticsearch cluster
func (er *ElasticsearchRequest) CreateOrUpdateServiceMonitors() error {
dpl := er.cluster
serviceMonitorName := fmt.Sprintf("monitor-%s-%s", dpl.Name, "cluster")
labelsWithDefault := appendDefaultLabel(dpl.Name, dpl.Labels)
labelsWithDefault["scrape-metrics"] = "enabled"
tlsConfig := monitoringv1.TLSConfig{
SafeTLSConfig: monitoringv1.SafeTLSConfig{
// ServerName can be e.g. elasticsearch-metrics.openshift-logging.svc
ServerName: fmt.Sprintf("%s-%s.%s.svc", dpl.Name, "metrics", dpl.Namespace),
},
CAFile: prometheusCAFile,
}
endpoints := []monitoringv1.Endpoint{
{
Port: dpl.Name,
Path: "/metrics",
Scheme: "https",
BearerTokenFile: "/var/run/secrets/kubernetes.io/serviceaccount/token",
TLSConfig: &tlsConfig,
},
{
Port: dpl.Name,
Path: "/_prometheus/metrics",
Scheme: "https",
BearerTokenFile: "/var/run/secrets/kubernetes.io/serviceaccount/token",
TLSConfig: &tlsConfig,
},
}
monitor := servicemonitor.New(serviceMonitorName, dpl.Namespace, labelsWithDefault).
WithJobLabel("monitor-elasticsearch").
WithSelector(metav1.LabelSelector{
MatchLabels: labelsWithDefault,
}).
WithNamespaceSelector(monitoringv1.NamespaceSelector{
MatchNames: []string{dpl.Namespace},
}).
WithEndpoints(endpoints...).
Build()
dpl.AddOwnerRefTo(monitor)
err := servicemonitor.CreateOrUpdate(context.TODO(), er.ll, er.client, monitor, servicemonitor.Equal, servicemonitor.Mutate)
if err != nil {
return kverrors.Wrap(err, "failed to create or update elasticsearch servicemonitor",
"cluster", er.cluster.Name,
"namespace", er.cluster.Namespace,
)
}
return nil
}