/
ocmagenthandler_servicemonitor.go
103 lines (95 loc) · 3.24 KB
/
ocmagenthandler_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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package ocmagenthandler
import (
"reflect"
monitorv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
ocmagentv1alpha1 "github.com/openshift/ocm-agent-operator/api/v1alpha1"
oah "github.com/openshift/ocm-agent-operator/pkg/consts/ocmagenthandler"
)
func buildOCMAgentServiceMonitor(ocmAgent ocmagentv1alpha1.OcmAgent) monitorv1.ServiceMonitor {
namespacedName := oah.BuildNamespacedName(ocmAgent.Name + "-metrics")
labels := map[string]string{
"app": ocmAgent.Name,
}
sm := monitorv1.ServiceMonitor{
ObjectMeta: metav1.ObjectMeta{
Name: namespacedName.Name,
Namespace: namespacedName.Namespace,
},
Spec: monitorv1.ServiceMonitorSpec{
Selector: metav1.LabelSelector{
MatchLabels: labels,
},
Endpoints: []monitorv1.Endpoint{{
Port: oah.OCMAgentMetricsPortName,
Path: "/metrics",
}},
},
}
return sm
}
// ensureServiceMonitor ensures that an OCMAgent serviceMonitor exists on the cluster
// and that its configuration matches what is expected.
func (o *ocmAgentHandler) ensureServiceMonitor(ocmAgent ocmagentv1alpha1.OcmAgent) error {
namespacedName := oah.BuildNamespacedName(ocmAgent.Name + "-metrics")
foundResource := &monitorv1.ServiceMonitor{}
populationFunc := func() monitorv1.ServiceMonitor {
return buildOCMAgentServiceMonitor(ocmAgent)
}
// Does the resource already exist?
o.Log.Info("ensuring serviceMonitor exists", "resource", namespacedName.String())
if err := o.Client.Get(o.Ctx, namespacedName, foundResource); err != nil {
if k8serrors.IsNotFound(err) {
// It does not exist, so must be created.
o.Log.Info("An OCMAgent serviceMonitor does not exist; will be created.")
// Populate the resource with the template
resource := populationFunc()
// Set the controller reference
if err := controllerutil.SetControllerReference(&ocmAgent, &resource, o.Scheme); err != nil {
return err
}
// and create it
err = o.Client.Create(o.Ctx, &resource)
if err != nil {
return err
}
} else {
// Return unexpectedly
return err
}
} else {
// It does exist, check if it is what we expected
resource := populationFunc()
if !reflect.DeepEqual(foundResource.Spec, resource.Spec) {
// Specs aren't equal, update and fix.
o.Log.Info("An OCMAgent serviceMonitor exists but contains unexpected configuration. Restoring.")
foundResource = resource.DeepCopy()
if err = o.Client.Update(o.Ctx, foundResource); err != nil {
return err
}
}
}
return nil
}
func (o *ocmAgentHandler) ensureServiceMonitorDeleted(ocmAgent ocmagentv1alpha1.OcmAgent) error {
namespacedName := oah.BuildNamespacedName(ocmAgent.Name + "-metrics")
foundResource := &monitorv1.ServiceMonitor{}
// Does the resource already exist?
o.Log.Info("ensuring serviceMonitor removed", "resource", namespacedName.String())
if err := o.Client.Get(o.Ctx, namespacedName, foundResource); err != nil {
if !k8serrors.IsNotFound(err) {
// Return unexpected error
return err
} else {
// Resource deleted
return nil
}
}
err := o.Client.Delete(o.Ctx, foundResource)
if err != nil {
return err
}
return nil
}