/
clusterurlmonitor_supplement.go
276 lines (241 loc) · 10.8 KB
/
clusterurlmonitor_supplement.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package clusterurlmonitor
import (
"fmt"
"net/url"
"reflect"
"strings"
configv1 "github.com/openshift/api/config/v1"
hypershiftv1beta1 "github.com/openshift/hypershift/api/v1beta1"
"github.com/openshift/route-monitor-operator/api/v1alpha1"
"github.com/openshift/route-monitor-operator/pkg/alert"
blackboxexporterconsts "github.com/openshift/route-monitor-operator/pkg/consts/blackboxexporter"
utilreconcile "github.com/openshift/route-monitor-operator/pkg/util/reconcile"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
)
const (
hcpClusterAnnotation = "hypershift.openshift.io/cluster"
)
// Takes care that right PrometheusRules for the defined ClusterURLMonitor are in place
func (s *ClusterUrlMonitorReconciler) EnsurePrometheusRuleExists(clusterUrlMonitor v1alpha1.ClusterUrlMonitor) (utilreconcile.Result, error) {
// If .spec.skipPrometheusRule is true, ensure that the PrometheusRule does NOT exist
if clusterUrlMonitor.Spec.SkipPrometheusRule {
// Cleanup any existing PrometheusRules and update the status
if err := s.Prom.DeletePrometheusRuleDeployment(clusterUrlMonitor.Status.PrometheusRuleRef); err != nil {
return utilreconcile.RequeueReconcileWith(err)
}
updated, _ := s.Common.SetResourceReference(&clusterUrlMonitor.Status.PrometheusRuleRef, types.NamespacedName{})
if updated {
return s.Common.UpdateMonitorResourceStatus(&clusterUrlMonitor)
}
return utilreconcile.ContinueReconcile()
}
// We shouldn't create prometheusrules for HCP clusterUrlMonitors, since alerting is implemented in the upstream RHOBS tenant
if clusterUrlMonitor.Spec.DomainRef == v1alpha1.ClusterDomainRefHCP {
return utilreconcile.ContinueReconcile()
}
clusterDomain, err := s.GetClusterDomain(clusterUrlMonitor)
if err != nil {
return utilreconcile.RequeueReconcileWith(err)
}
spec := clusterUrlMonitor.Spec
clusterUrl := spec.Prefix + clusterDomain + ":" + spec.Port + spec.Suffix
parsedSlo, err := s.Common.ParseMonitorSLOSpecs(clusterUrl, clusterUrlMonitor.Spec.Slo)
if s.Common.SetErrorStatus(&clusterUrlMonitor.Status.ErrorStatus, err) {
return s.Common.UpdateMonitorResourceStatus(&clusterUrlMonitor)
}
if parsedSlo == "" {
err = s.Prom.DeletePrometheusRuleDeployment(clusterUrlMonitor.Status.PrometheusRuleRef)
if err != nil {
return utilreconcile.RequeueReconcileWith(err)
}
updated, _ := s.Common.SetResourceReference(&clusterUrlMonitor.Status.PrometheusRuleRef, types.NamespacedName{})
if updated {
return s.Common.UpdateMonitorResourceStatus(&clusterUrlMonitor)
}
return utilreconcile.StopReconcile()
}
namespacedName := types.NamespacedName{Namespace: clusterUrlMonitor.Namespace, Name: clusterUrlMonitor.Name}
template := alert.TemplateForPrometheusRuleResource(clusterUrl, parsedSlo, namespacedName)
err = s.Prom.UpdatePrometheusRuleDeployment(template)
if err != nil {
return utilreconcile.RequeueReconcileWith(err)
}
// Update PrometheusRuleReference in ClusterUrlMonitor if necessary
updated, _ := s.Common.SetResourceReference(&clusterUrlMonitor.Status.PrometheusRuleRef, namespacedName)
if updated {
return s.Common.UpdateMonitorResourceStatus(&clusterUrlMonitor)
}
return utilreconcile.ContinueReconcile()
}
func isClusterVersionAvailable(hcp hypershiftv1beta1.HostedControlPlane) error {
condition := meta.FindStatusCondition(hcp.Status.Conditions, string(hypershiftv1beta1.ClusterVersionAvailable))
if condition == nil || condition.Status != metav1.ConditionTrue {
return fmt.Errorf("The cluster API is not yet available")
}
return nil
}
// Takes care that right ServiceMonitor for the defined ClusterURLMonitor are in place
func (s *ClusterUrlMonitorReconciler) EnsureServiceMonitorExists(clusterUrlMonitor v1alpha1.ClusterUrlMonitor) (utilreconcile.Result, error) {
clusterDomain, err := s.GetClusterDomain(clusterUrlMonitor)
if err != nil {
return utilreconcile.RequeueReconcileWith(err)
}
namespacedName := types.NamespacedName{Name: clusterUrlMonitor.Name, Namespace: clusterUrlMonitor.Namespace}
spec := clusterUrlMonitor.Spec
clusterUrl := spec.Prefix + clusterDomain + ":" + spec.Port + spec.Suffix
isHCP := (clusterUrlMonitor.Spec.DomainRef == v1alpha1.ClusterDomainRefHCP)
var id string
if isHCP {
var hcp hypershiftv1beta1.HostedControlPlane
id, err = s.Common.GetHypershiftClusterID(clusterUrlMonitor.Namespace)
if err != nil {
return utilreconcile.RequeueReconcileWith(err)
}
hcp, err = s.Common.GetHCP(clusterUrlMonitor.Namespace)
if err != nil {
return utilreconcile.RequeueReconcileWith(err)
}
err = isClusterVersionAvailable(hcp)
if err != nil {
return utilreconcile.RequeueReconcileWith(err)
}
} else {
id, err = s.Common.GetOSDClusterID()
if err != nil {
return utilreconcile.RequeueReconcileWith(err)
}
}
owner := metav1.NewControllerRef(&clusterUrlMonitor.ObjectMeta, clusterUrlMonitor.GroupVersionKind())
if err := s.ServiceMonitor.TemplateAndUpdateServiceMonitorDeployment(clusterUrl, s.BlackBoxExporter.GetBlackBoxExporterNamespace(), namespacedName, id, isHCP, false, owner); err != nil {
return utilreconcile.RequeueReconcileWith(err)
}
// Update RouteMonitor ServiceMonitorRef if required
updated, err := s.Common.SetResourceReference(&clusterUrlMonitor.Status.ServiceMonitorRef, namespacedName)
if err != nil {
return utilreconcile.RequeueReconcileWith(err)
}
if updated {
return s.Common.UpdateMonitorResourceStatus(&clusterUrlMonitor)
}
return utilreconcile.ContinueReconcile()
}
// Ensures that all dependencies related to a ClusterUrlMonitor are deleted
func (s *ClusterUrlMonitorReconciler) EnsureMonitorAndDependenciesAbsent(clusterUrlMonitor v1alpha1.ClusterUrlMonitor) (utilreconcile.Result, error) {
if clusterUrlMonitor.DeletionTimestamp == nil {
return utilreconcile.ContinueReconcile()
}
isHCP := (clusterUrlMonitor.Spec.DomainRef == v1alpha1.ClusterDomainRefHCP)
err := s.ServiceMonitor.DeleteServiceMonitorDeployment(clusterUrlMonitor.Status.ServiceMonitorRef, isHCP)
if err != nil {
return utilreconcile.RequeueReconcileWith(err)
}
shouldDelete, err := s.BlackBoxExporter.ShouldDeleteBlackBoxExporterResources()
if err != nil {
return utilreconcile.RequeueReconcileWith(err)
}
if shouldDelete == blackboxexporterconsts.DeleteBlackBoxExporter {
err := s.BlackBoxExporter.EnsureBlackBoxExporterResourcesAbsent()
if err != nil {
return utilreconcile.RequeueReconcileWith(err)
}
}
err = s.Prom.DeletePrometheusRuleDeployment(clusterUrlMonitor.Status.PrometheusRuleRef)
if err != nil {
return utilreconcile.RequeueReconcileWith(err)
}
if s.Common.DeleteFinalizer(&clusterUrlMonitor, FinalizerKey) {
// ignore the output as we want to remove the PrevFinalizerKey anyways
s.Common.DeleteFinalizer(&clusterUrlMonitor, PrevFinalizerKey)
return s.Common.UpdateMonitorResource(&clusterUrlMonitor)
}
return utilreconcile.ContinueReconcile()
}
func (s *ClusterUrlMonitorReconciler) EnsureFinalizerSet(clusterUrlMonitor v1alpha1.ClusterUrlMonitor) (utilreconcile.Result, error) {
if s.Common.SetFinalizer(&clusterUrlMonitor, FinalizerKey) {
// ignore the output as we want to remove the PrevFinalizerKey anyways
s.Common.DeleteFinalizer(&clusterUrlMonitor, PrevFinalizerKey)
return s.Common.UpdateMonitorResource(&clusterUrlMonitor)
}
return utilreconcile.ContinueReconcile()
}
// GetClusterUrlMonitor return the ClusterUrlMonitor that is tested
func (s *ClusterUrlMonitorReconciler) GetClusterUrlMonitor(req ctrl.Request) (v1alpha1.ClusterUrlMonitor, utilreconcile.Result, error) {
ClusterUrlMonitor := v1alpha1.ClusterUrlMonitor{}
err := s.Client.Get(s.Ctx, req.NamespacedName, &ClusterUrlMonitor)
if err != nil {
// If this is an unknown error
if !k8serrors.IsNotFound(err) {
res, err := utilreconcile.RequeueReconcileWith(err)
return v1alpha1.ClusterUrlMonitor{}, res, err
}
s.Log.V(2).Info("StopRequeue", "As ClusterUrlMonitor is 'NotFound', stopping requeue", nil)
return v1alpha1.ClusterUrlMonitor{}, utilreconcile.StopOperation(), nil
}
// if the resource is empty, we should terminate
emptyClustUrlMonitor := v1alpha1.ClusterUrlMonitor{}
if reflect.DeepEqual(ClusterUrlMonitor, emptyClustUrlMonitor) {
return v1alpha1.ClusterUrlMonitor{}, utilreconcile.StopOperation(), nil
}
return ClusterUrlMonitor, utilreconcile.ContinueOperation(), nil
}
// GetClusterDomain returns the baseDomain for a cluster, using the correct method based on it's type
func (s *ClusterUrlMonitorReconciler) GetClusterDomain(monitor v1alpha1.ClusterUrlMonitor) (string, error) {
if monitor.Spec.DomainRef == v1alpha1.ClusterDomainRefHCP {
return s.getHypershiftClusterDomain(monitor)
}
return s.getInfraClusterDomain()
}
// getInfraClusterDomain returns a normal OSD/ROSA cluster's domain based on it's infrastructure object
func (s *ClusterUrlMonitorReconciler) getInfraClusterDomain() (string, error) {
clusterInfra := configv1.Infrastructure{}
err := s.Client.Get(s.Ctx, types.NamespacedName{Name: "cluster"}, &clusterInfra)
if err != nil {
return "", err
}
return removeSubdomain("api", clusterInfra.Status.APIServerURL)
}
// getHypershiftClusterDomain returns a hypershift hosted cluster's domain based on it's hostedCluster object
func (s *ClusterUrlMonitorReconciler) getHypershiftClusterDomain(monitor v1alpha1.ClusterUrlMonitor) (string, error) {
clusterHCP, err := s.Common.GetHCP(monitor.Namespace)
if err != nil {
return "", fmt.Errorf("failed to retrieve HostedControlPlane for hosted cluster: %w", err)
}
clusterAnnotation := clusterHCP.Annotations[hcpClusterAnnotation]
annotationTokens := strings.Split(clusterAnnotation, "/")
if len(annotationTokens) != 2 {
return "", fmt.Errorf("invalid annotation for HostedControlPlane '%s': expected <namespace>/<hostedcluster name>, got %s", clusterHCP.Name, clusterAnnotation)
}
// Retrieve hostedCluster using HCP annotation
hostedCluster := hypershiftv1beta1.HostedCluster{}
hcReq := types.NamespacedName{
Namespace: annotationTokens[0],
Name: annotationTokens[1],
}
err = s.Client.Get(s.Ctx, hcReq, &hostedCluster)
if err != nil {
return "", err
}
return removeSubdomain("rosa", hostedCluster.Spec.DNS.BaseDomain)
}
func removeSubdomain(subdomain, clusterURL string) (string, error) {
// url.Parse requires a 'http://' or 'https://' prefix in order
// to function properly
if !strings.HasPrefix(clusterURL, "https://") && !strings.HasPrefix(clusterURL, "http://") {
clusterURL = fmt.Sprintf("https://%s", clusterURL)
}
u, err := url.Parse(clusterURL)
if err != nil {
return "", err
}
// the hostname format is api.basename so cutting at the first '.' will give
// us the base name
before, baseName, _ := strings.Cut(u.Hostname(), ".")
if before != subdomain {
baseName = strings.Join([]string{before, baseName}, ".")
}
return baseName, nil
}