-
Notifications
You must be signed in to change notification settings - Fork 63
/
observabilityaddon_controller.go
595 lines (545 loc) · 21.4 KB
/
observabilityaddon_controller.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
// Copyright (c) Red Hat, Inc.
// Copyright Contributors to the Open Cluster Management project
// Licensed under the Apache License 2.0
package observabilityendpoint
import (
"context"
"fmt"
"os"
"strconv"
operatorutil "github.com/stolostron/multicluster-observability-operator/operators/pkg/util"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v2"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
"github.com/stolostron/multicluster-observability-operator/operators/endpointmetrics/pkg/hypershift"
"github.com/stolostron/multicluster-observability-operator/operators/endpointmetrics/pkg/openshift"
"github.com/stolostron/multicluster-observability-operator/operators/endpointmetrics/pkg/rendering"
"github.com/stolostron/multicluster-observability-operator/operators/endpointmetrics/pkg/util"
oav1beta1 "github.com/stolostron/multicluster-observability-operator/operators/multiclusterobservability/api/v1beta1"
oav1beta2 "github.com/stolostron/multicluster-observability-operator/operators/multiclusterobservability/api/v1beta2"
operatorconfig "github.com/stolostron/multicluster-observability-operator/operators/pkg/config"
"github.com/stolostron/multicluster-observability-operator/operators/pkg/deploying"
rendererutil "github.com/stolostron/multicluster-observability-operator/operators/pkg/rendering"
)
var (
log = ctrl.Log.WithName("controllers").WithName("ObservabilityAddon")
installPrometheus, _ = strconv.ParseBool(os.Getenv(operatorconfig.InstallPrometheus))
globalRes = []*unstructured.Unstructured{}
)
const (
obAddonName = "observability-addon"
ownerLabelKey = "owner"
ownerLabelValue = "observabilityaddon"
obsAddonFinalizer = "observability.open-cluster-management.io/addon-cleanup"
promSvcName = "prometheus-k8s"
promNamespace = "openshift-monitoring"
openShiftClusterMonitoringlabel = "openshift.io/cluster-monitoring"
)
const (
defaultClusterType = ""
ocpThreeClusterType = "ocp3"
snoClusterType = "SNO"
)
var (
namespace = os.Getenv("WATCH_NAMESPACE")
hubNamespace = os.Getenv("HUB_NAMESPACE")
isHubMetricsCollector = os.Getenv("HUB_ENDPOINT_OPERATOR") == "true"
serviceAccountName = os.Getenv("SERVICE_ACCOUNT")
)
// ObservabilityAddonReconciler reconciles a ObservabilityAddon object.
type ObservabilityAddonReconciler struct {
Client client.Client
Scheme *runtime.Scheme
HubClient *util.ReloadableHubClient
}
// +kubebuilder:rbac:groups=observability.open-cluster-management.io.open-cluster-management.io,resources=observabilityaddons,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=observability.open-cluster-management.io.open-cluster-management.io,resources=observabilityaddons/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=observability.open-cluster-management.io.open-cluster-management.io,resources=observabilityaddons/finalizers,verbs=update
// Reconcile reads that state of the cluster for a ObservabilityAddon object and makes changes based on the state read
// and what is in the ObservabilityAddon.Spec
// The Controller will requeue the Request to be processed again if the returned error is non-nil or
// Result.Requeue is true, otherwise upon completion it will remove the work from the queue.
func (r *ObservabilityAddonReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := log.WithValues("Request.Namespace", req.Namespace, "Request.Name", req.Name)
log.Info("Reconciling")
isHypershift := true
if os.Getenv("UNIT_TEST") != "true" {
crdClient, err := operatorutil.GetOrCreateCRDClient()
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to get/create CRD client: %w", err)
}
isHypershift, err = operatorutil.CheckCRDExist(crdClient, "hostedclusters.hypershift.openshift.io")
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to check if the CRD hostedclusters.hypershift.openshift.io exists: %w", err)
}
}
hubObsAddon := &oav1beta1.ObservabilityAddon{}
obsAddon := &oav1beta1.ObservabilityAddon{}
deleteFlag := false
// ACM 8509: Special case for hub/local cluster metrics collection
// We do not have an ObservabilityAddon instance in the local cluster so skipping the below block
if !isHubMetricsCollector {
if err := r.ensureOpenShiftMonitoringLabelAndRole(ctx); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to ensure OpenShift monitoring label and role: %w", err)
}
// Fetch the ObservabilityAddon instance in hub cluster
fetchAddon := func() error {
return r.HubClient.Get(ctx, types.NamespacedName{Name: obAddonName, Namespace: hubNamespace}, hubObsAddon)
}
if err := fetchAddon(); err != nil {
if r.HubClient, err = r.HubClient.Reload(); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to reload the hub client: %w", err)
}
// Retry the operation once with the reloaded client
if err := fetchAddon(); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to get ObservabilityAddon in hub cluster: %w", err)
}
}
// Fetch the ObservabilityAddon instance in local cluster
err := r.Client.Get(ctx, types.NamespacedName{Name: obAddonName, Namespace: namespace}, obsAddon)
if err != nil {
if !errors.IsNotFound(err) {
return ctrl.Result{}, fmt.Errorf("failed to get observabilityaddon: %w", err)
}
obsAddon = nil
}
if obsAddon == nil {
deleteFlag = true
}
// Init finalizers
deleted, err := r.initFinalization(ctx, deleteFlag, hubObsAddon, isHypershift)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to init finalization: %w", err)
}
if deleted || deleteFlag {
return ctrl.Result{}, nil
}
}
// retrieve the hubInfo
hubSecret := &corev1.Secret{}
err := r.Client.Get(
ctx,
types.NamespacedName{Name: operatorconfig.HubInfoSecretName, Namespace: namespace},
hubSecret,
)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to get hub info secret: %w", err)
}
hubInfo := &operatorconfig.HubInfo{}
err = yaml.Unmarshal(hubSecret.Data[operatorconfig.HubInfoSecretKey], &hubInfo)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to unmarshal hub info: %w", err)
}
hubInfo.ClusterName = string(hubSecret.Data[operatorconfig.ClusterNameKey])
clusterType := defaultClusterType
clusterID := ""
// read the image configmap
imagesCM := &corev1.ConfigMap{}
err = r.Client.Get(ctx, types.NamespacedName{
Name: operatorconfig.ImageConfigMap,
Namespace: namespace,
}, imagesCM)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to get images configmap: %w", err)
}
rendering.Images = imagesCM.Data
if isHypershift {
updatedHCs, err := hypershift.ReconcileHostedClustersServiceMonitors(ctx, r.Client)
if err != nil {
log.Error(err, "Failed to create ServiceMonitors for hypershift")
} else {
log.Info("Reconciled hypershift service monitors", "updatedHCs", updatedHCs)
}
}
if !installPrometheus {
// If no prometheus service found, set status as NotSupported
promSvc := &corev1.Service{}
err = r.Client.Get(ctx, types.NamespacedName{
Name: promSvcName,
Namespace: promNamespace,
}, promSvc)
if err != nil {
if errors.IsNotFound(err) {
log.Error(err, "OCP prometheus service does not exist")
// ACM 8509: Special case for hub/local cluster metrics collection
// We do not report status for hub endpoint operator
util.ReportStatus(ctx, r.Client, obsAddon, "NotSupported", !isHubMetricsCollector)
return ctrl.Result{}, nil
}
return ctrl.Result{}, fmt.Errorf("failed to check prometheus resource: %w", err)
}
clusterID, err = openshift.GetClusterID(ctx, r.Client)
if err != nil {
if !errors.IsNotFound(err) {
return ctrl.Result{}, fmt.Errorf("failed to get cluster id: %w", err)
}
log.Error(err, "Failed to get cluster id")
// OCP 3.11 has no cluster id, set it as empty string
clusterID = ""
// to differentiate ocp 3.x
clusterType = ocpThreeClusterType
}
isSNO, err := openshift.IsSNO(ctx, r.Client)
if err != nil {
log.Error(err, "Failed to check if the cluster is SNO")
} else if isSNO {
clusterType = snoClusterType
}
err = openshift.CreateMonitoringClusterRoleBinding(ctx, log, r.Client, namespace, serviceAccountName)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to create monitoring cluster role binding: %w", err)
}
err = openshift.CreateCAConfigmap(ctx, r.Client, namespace)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to create CA configmap: %w", err)
}
} else {
// Render the prometheus templates
renderer := rendererutil.NewRenderer()
toDeploy, err := rendering.Render(renderer, r.Client, hubInfo)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to render prometheus templates: %w", err)
}
deployer := deploying.NewDeployer(r.Client)
for _, res := range toDeploy {
if res.GetNamespace() != namespace {
globalRes = append(globalRes, res)
}
if !isHubMetricsCollector {
// For kind tests we need to deploy prometheus in hub but cannot set controller
// reference as there is no observabilityaddon
// skip setting controller reference for resources that don't need it
// and for which we lack permission to set it
skipResources := []string{"Role", "RoleBinding", "ClusterRole", "ClusterRoleBinding"}
if !slices.Contains(skipResources, res.GetKind()) {
if err := controllerutil.SetControllerReference(obsAddon, res, r.Scheme); err != nil {
log.Info("Failed to set controller reference", "resource", res.GetName(), "kind", res.GetKind(), "error", err.Error())
}
}
}
if err := deployer.Deploy(res); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to deploy %s %s/%s: %w", res.GetKind(), namespace, res.GetName(), err)
}
}
}
// create or update the cluster-monitoring-config configmap and relevant resources
if err := createOrUpdateClusterMonitoringConfig(ctx, hubInfo, clusterID, r.Client, installPrometheus); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to create or update cluster monitoring config: %w", err)
}
forceRestart := req.Name == mtlsCertName || req.Name == mtlsCaName || req.Name == openshift.CaConfigmapName
if obsAddon.Spec.EnableMetrics || isHubMetricsCollector {
if isHubMetricsCollector {
mcoList := &oav1beta2.MultiClusterObservabilityList{}
err := r.HubClient.List(ctx, mcoList, client.InNamespace(corev1.NamespaceAll))
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to get multiclusterobservability: %w", err)
}
if len(mcoList.Items) != 1 {
log.Error(nil, fmt.Sprintf("Expected 1 multiclusterobservability, found %d", len(mcoList.Items)))
return ctrl.Result{}, nil
}
obsAddon.Spec = *mcoList.Items[0].Spec.ObservabilityAddonSpec
}
created, err := updateMetricsCollectors(
ctx,
r.Client,
obsAddon.Spec,
*hubInfo, clusterID,
clusterType,
1,
forceRestart)
if err != nil {
util.ReportStatus(ctx, r.Client, obsAddon, "Degraded", !isHubMetricsCollector)
return ctrl.Result{}, fmt.Errorf("failed to update metrics collectors: %w", err)
}
if created {
util.ReportStatus(ctx, r.Client, obsAddon, "Deployed", !isHubMetricsCollector)
}
} else {
deleted, err := updateMetricsCollectors(ctx, r.Client, obsAddon.Spec, *hubInfo, clusterID, clusterType, 0, false)
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to update metrics collectors: %w", err)
}
if deleted {
util.ReportStatus(ctx, r.Client, obsAddon, "Disabled", !isHubMetricsCollector)
}
}
return ctrl.Result{}, nil
}
func (r *ObservabilityAddonReconciler) initFinalization(
ctx context.Context, delete bool, hubObsAddon *oav1beta1.ObservabilityAddon,
isHypershift bool,
) (bool, error) {
if delete && slices.Contains(hubObsAddon.GetFinalizers(), obsAddonFinalizer) {
log.Info("To clean observability components/configurations in the cluster")
err := deleteMetricsCollector(ctx, r.Client, metricsCollectorName)
if err != nil {
return false, err
}
err = deleteMetricsCollector(ctx, r.Client, uwlMetricsCollectorName)
if err != nil {
return false, err
}
// revert the change to cluster monitoring stack
err = revertClusterMonitoringConfig(ctx, r.Client)
if err != nil {
return false, err
}
if isHypershift {
err = hypershift.DeleteServiceMonitors(ctx, r.Client)
if err != nil {
log.Error(err, "Failed to delete ServiceMonitors for hypershift")
return false, err
}
}
// Should we return bool from the delete functions for crb and cm? What
// is it used for? Should we use the bool before removing finalizer?
// SHould we return true if metricscollector is not found as that means
// metrics collector is not present? Moved this part up as we need to clean
// up cm and crb before we remove the finalizer - is that the right way to do it?
if !installPrometheus {
err = openshift.DeleteMonitoringClusterRoleBinding(ctx, r.Client)
if err != nil {
log.Error(err, "Failed to delete monitoring cluster role binding")
return false, err
}
log.Info("clusterrolebinding deleted")
err = openshift.DeleteCAConfigmap(ctx, r.Client, namespace)
if err != nil {
log.Error(err, "Failed to delete CA configmap")
return false, err
}
log.Info("configmap deleted")
} else {
// delete resources which is not namespace scoped or located in other namespaces
for _, res := range globalRes {
err = r.Client.Delete(context.TODO(), res)
if err != nil && !errors.IsNotFound(err) {
return false, err
}
}
}
hubObsAddon.SetFinalizers(remove(hubObsAddon.GetFinalizers(), obsAddonFinalizer))
err = r.HubClient.Update(ctx, hubObsAddon)
if err != nil {
log.Error(err, "Failed to remove finalizer to observabilityaddon", "namespace", hubObsAddon.Namespace)
return false, err
}
log.Info("Finalizer removed from observabilityaddon resource")
return true, nil
}
if !slices.Contains(hubObsAddon.GetFinalizers(), obsAddonFinalizer) {
hubObsAddon.SetFinalizers(append(hubObsAddon.GetFinalizers(), obsAddonFinalizer))
err := r.HubClient.Update(ctx, hubObsAddon)
if err != nil {
log.Error(err, "Failed to add finalizer to observabilityaddon", "namespace", hubObsAddon.Namespace)
return false, err
}
log.Info("Finalizer added to observabilityaddon resource")
}
return false, nil
}
func (r *ObservabilityAddonReconciler) ensureOpenShiftMonitoringLabelAndRole(ctx context.Context) error {
existingNs := &corev1.Namespace{}
resNS := namespace
role := rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
Name: "prometheus-k8s-addon-obs",
Namespace: resNS,
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{""},
Resources: []string{"services", "endpoints", "pods"},
Verbs: []string{"get", "list", "watch"},
},
},
}
roleBinding := rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: "prometheus-k8s-addon-obs",
Namespace: resNS,
},
RoleRef: rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "Role",
Name: "prometheus-k8s-addon-obs",
},
Subjects: []rbacv1.Subject{
{
Kind: "ServiceAccount",
Name: "prometheus-k8s",
Namespace: "openshift-monitoring",
},
},
}
err := r.Client.Get(ctx, types.NamespacedName{Name: resNS}, existingNs)
if err != nil || errors.IsNotFound(err) {
log.Error(err, fmt.Sprintf("Failed to find namespace for Endpoint Operator: %s", resNS))
return err
}
if existingNs.ObjectMeta.Labels == nil || len(existingNs.ObjectMeta.Labels) == 0 {
existingNs.ObjectMeta.Labels = make(map[string]string)
}
if _, ok := existingNs.ObjectMeta.Labels[openShiftClusterMonitoringlabel]; !ok {
log.Info(fmt.Sprintf("Adding label: %s to namespace: %s", openShiftClusterMonitoringlabel, resNS))
existingNs.ObjectMeta.Labels[openShiftClusterMonitoringlabel] = "true"
err = r.Client.Update(ctx, existingNs)
if err != nil {
log.Error(err, fmt.Sprintf("Failed to update namespace for Endpoint Operator: %s with the label: %s",
namespace, openShiftClusterMonitoringlabel))
return err
}
}
foundRole := &rbacv1.Role{}
err = r.Client.Get(ctx, types.NamespacedName{Name: role.Name, Namespace: resNS}, foundRole)
if err != nil {
if errors.IsNotFound(err) {
log.Info(fmt.Sprintf("Creating role: %s in namespace: %s", role.Name, resNS))
err = r.Client.Create(ctx, &role)
if err != nil {
log.Error(err, fmt.Sprintf("Failed to create role: %s in namespace: %s", role.Name, resNS))
return err
}
} else {
log.Error(err, fmt.Sprintf("Failed to get role: %s in namespace: %s", role.Name, resNS))
return err
}
}
foundRoleBinding := &rbacv1.RoleBinding{}
err = r.Client.Get(ctx, types.NamespacedName{Name: roleBinding.Name, Namespace: resNS}, foundRoleBinding)
if err != nil {
if errors.IsNotFound(err) {
log.Info(fmt.Sprintf("Creating role binding: %s in namespace: %s", roleBinding.Name, resNS))
err = r.Client.Create(ctx, &roleBinding)
if err != nil {
log.Error(err, fmt.Sprintf("Failed to create role binding: %s in namespace: %s", roleBinding.Name, resNS))
return err
}
} else {
log.Error(err, fmt.Sprintf("Failed to get role binding: %s in namespace: %s", roleBinding.Name, resNS))
return err
}
}
return nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *ObservabilityAddonReconciler) SetupWithManager(mgr ctrl.Manager) error {
if os.Getenv("NAMESPACE") != "" {
namespace = os.Getenv("NAMESPACE")
}
ctrlBuilder := ctrl.NewControllerManagedBy(mgr).For(
&oav1beta1.ObservabilityAddon{},
builder.WithPredicates(getPred(obAddonName, namespace, true, true, true)),
)
if isHubMetricsCollector {
ctrlBuilder = ctrlBuilder.Watches(
&source.Kind{Type: &oav1beta2.MultiClusterObservability{}},
&handler.EnqueueRequestForObject{},
)
}
return ctrlBuilder.
Watches(
&source.Kind{Type: &corev1.Secret{}},
&handler.EnqueueRequestForObject{},
builder.WithPredicates(getPred(operatorconfig.HubInfoSecretName, namespace, true, true, false)),
).
Watches(
&source.Kind{Type: &corev1.Secret{}},
&handler.EnqueueRequestForObject{},
builder.WithPredicates(getPred(mtlsCertName, namespace, true, true, false)),
).
Watches(
&source.Kind{Type: &corev1.Secret{}},
&handler.EnqueueRequestForObject{},
builder.WithPredicates(getPred(mtlsCaName, namespace, true, true, false)),
).
Watches(
&source.Kind{Type: &corev1.Secret{}},
&handler.EnqueueRequestForObject{},
builder.WithPredicates(getPred(hubAmAccessorSecretName, namespace, true, true, false)),
).
Watches(
&source.Kind{Type: &corev1.ConfigMap{}},
&handler.EnqueueRequestForObject{},
builder.WithPredicates(getPred(operatorconfig.AllowlistConfigMapName, namespace, true, true, false)),
).
Watches(
&source.Kind{Type: &corev1.ConfigMap{}},
&handler.EnqueueRequestForObject{},
builder.WithPredicates(getPred(operatorconfig.AllowlistCustomConfigMapName, "", true, true, true)),
).
Watches(
&source.Kind{Type: &corev1.ConfigMap{}},
&handler.EnqueueRequestForObject{},
builder.WithPredicates(getPred(openshift.CaConfigmapName, namespace, false, true, true)),
).
Watches(
&source.Kind{Type: &appsv1.Deployment{}},
&handler.EnqueueRequestForObject{},
builder.WithPredicates(getPred(metricsCollectorName, namespace, true, true, true)),
).
Watches(
&source.Kind{Type: &appsv1.Deployment{}},
&handler.EnqueueRequestForObject{},
builder.WithPredicates(getPred(uwlMetricsCollectorName, namespace, true, true, true)),
).
Watches(
&source.Kind{Type: &rbacv1.ClusterRoleBinding{}},
&handler.EnqueueRequestForObject{},
builder.WithPredicates(getPred(openshift.ClusterRoleBindingName, "", false, true, true)),
).
Watches(
&source.Kind{Type: &corev1.ConfigMap{}},
&handler.EnqueueRequestForObject{},
builder.WithPredicates(getPred(operatorconfig.ImageConfigMap, namespace, true, true, false)),
).
Watches(
&source.Kind{Type: &appsv1.StatefulSet{}},
&handler.EnqueueRequestForObject{},
builder.WithPredicates(getPred(operatorconfig.PrometheusUserWorkload, uwlNamespace, true, false, true)),
).
// Watch the kube-system extension-apiserver-authentication ConfigMap for changes
Watches(&source.Kind{Type: &corev1.ConfigMap{}}, handler.EnqueueRequestsFromMapFunc(
func(a client.Object) []reconcile.Request {
if a.GetName() == "extension-apiserver-authentication" && a.GetNamespace() == "kube-system" {
return []reconcile.Request{
{NamespacedName: types.NamespacedName{
Name: "metrics-collector-clientca-metric",
Namespace: namespace,
}},
{NamespacedName: types.NamespacedName{
Name: "uwl-metrics-collector-clientca-metric",
Namespace: namespace,
}},
}
}
return nil
}), builder.WithPredicates(predicate.ResourceVersionChangedPredicate{}),
).
Complete(r)
}
func remove(list []string, s string) []string {
result := []string{}
for _, v := range list {
if v != s {
result = append(result, v)
}
}
return result
}