forked from IBM/ibm-common-service-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommonservice_controller.go
More file actions
595 lines (530 loc) · 23.3 KB
/
commonservice_controller.go
File metadata and controls
595 lines (530 loc) · 23.3 KB
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 2022 IBM Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package controllers
import (
"context"
"fmt"
"os"
"reflect"
"strings"
olmv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/klog"
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/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
apiv3 "github.com/IBM/ibm-common-service-operator/v4/api/v3"
"github.com/IBM/ibm-common-service-operator/v4/internal/controller/bootstrap"
util "github.com/IBM/ibm-common-service-operator/v4/internal/controller/common"
"github.com/IBM/ibm-common-service-operator/v4/internal/controller/configurationcollector"
"github.com/IBM/ibm-common-service-operator/v4/internal/controller/constant"
odlm "github.com/IBM/operand-deployment-lifecycle-manager/v4/api/v1alpha1"
)
// CommonServiceReconciler reconciles a CommonService object
type CommonServiceReconciler struct {
*bootstrap.Bootstrap
Scheme *runtime.Scheme
Recorder record.EventRecorder
}
func (r *CommonServiceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
klog.Infof("Reconciling CommonService: %s", req.NamespacedName)
// Fetch the CommonService instance
instance := &apiv3.CommonService{}
if req.Name == constant.MasterCR && util.Contains(strings.Split(r.Bootstrap.CSData.WatchNamespaces, ","), req.Namespace) && req.Namespace != r.Bootstrap.CSData.OperatorNs {
if err := r.Bootstrap.Client.Get(ctx, req.NamespacedName, instance); err != nil {
if errors.IsNotFound(err) {
klog.Infof("Finished reconciling to delete CommonService: %s/%s", req.NamespacedName.Namespace, req.NamespacedName.Name)
}
return ctrl.Result{}, client.IgnoreNotFound(err)
}
return r.ReconcileNonConfigurableCR(ctx, instance)
}
if err := r.Reader.Get(ctx, req.NamespacedName, instance); err != nil {
if errors.IsNotFound(err) {
if err := r.handleDelete(ctx); err != nil {
return ctrl.Result{}, err
}
// Generate Issuer and Certificate CR
if err := r.Bootstrap.DeployCertManagerCR(); err != nil {
return ctrl.Result{}, err
}
klog.Infof("Finished reconciling to delete CommonService: %s/%s", req.NamespacedName.Namespace, req.NamespacedName.Name)
}
return ctrl.Result{}, client.IgnoreNotFound(err)
}
if !instance.Spec.License.Accept {
klog.Error("Accept license by changing .spec.license.accept to true in the CommonService CR. Operator will not proceed until then")
}
if os.Getenv("NO_OLM") == "true" {
klog.Infof("Reconciling CommonService: %s in No OLM environment", req.NamespacedName)
return r.NoOLMReconcile(ctx, req, instance)
}
// If the CommonService CR is not paused, continue to reconcile
if !r.reconcilePauseRequest(instance) {
if r.checkNamespace(req.NamespacedName.String()) {
return r.ReconcileMasterCR(ctx, instance)
}
return r.ReconcileGeneralCR(ctx, instance)
}
// If the CommonService CR is paused, update the status to pending
if err := r.updatePhase(ctx, instance, apiv3.CRPending); err != nil {
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, err)
return ctrl.Result{}, err
}
klog.Infof("%s/%s is in pending status due to pause request", instance.Namespace, instance.Name)
return ctrl.Result{}, nil
}
func (r *CommonServiceReconciler) ReconcileMasterCR(ctx context.Context, instance *apiv3.CommonService) (ctrl.Result, error) {
var statusErr error
// capture original copy early to compare status changes later and avoid unnecessary Status().Update calls
originalInstance := instance.DeepCopy()
originalStatus := originalInstance.Status
// Defer to Set error/ready/warning condition and update status only if changed
defer func() {
if err := r.Bootstrap.CheckWarningCondition(instance); err != nil {
klog.Warning(err)
return
}
if statusErr != nil {
klog.V(2).Infof("CommonService CR: %s/%s in error status", instance.Namespace, instance.Name)
instance.SetErrorCondition(constant.MasterCR, apiv3.ConditionTypeError, corev1.ConditionTrue, apiv3.ConditionReasonError, statusErr.Error())
} else {
klog.V(2).Infof("CommonService CR: %s/%s in ready status", instance.Namespace, instance.Name)
instance.SetReadyCondition(constant.KindCR, apiv3.ConditionTypeReady, corev1.ConditionTrue)
}
// update status only when it actually changed
if !reflect.DeepEqual(originalStatus, instance.Status) {
klog.V(2).Infof("Updating status in CommonService CR: %s/%s", instance.Namespace, instance.Name)
if err := r.Client.Status().Update(ctx, instance); err != nil {
klog.Warning(err)
}
} else {
klog.V(2).Infof("No status change for CommonService: %s/%s, skipping update", instance.Namespace, instance.Name)
}
}()
operatorDeployed, servicesDeployed := r.Bootstrap.CheckDeployStatus(ctx)
instance.UpdateConfigStatus(&r.Bootstrap.CSData, operatorDeployed, servicesDeployed)
r.Bootstrap.CSData.CPFSNs = string(instance.Status.ConfigStatus.OperatorNamespace)
r.Bootstrap.CSData.ServicesNs = string(instance.Status.ConfigStatus.ServicesNamespace)
r.Bootstrap.CSData.CatalogSourceName = string(instance.Status.ConfigStatus.CatalogName)
r.Bootstrap.CSData.CatalogSourceNs = string(instance.Status.ConfigStatus.CatalogNamespace)
var forceUpdateODLMCRs bool
if !reflect.DeepEqual(originalInstance.Status, instance.Status) {
forceUpdateODLMCRs = true
}
if statusErr = r.Client.Status().Patch(ctx, instance, client.MergeFrom(originalInstance)); statusErr != nil {
return ctrl.Result{}, fmt.Errorf("error while patching CommonService.Status: %v", statusErr)
}
if instance.Status.Phase == "" {
// Set "Reconciling" condition and "Initializing" for phase
instance.SetPendingCondition(constant.MasterCR, apiv3.ConditionTypeReconciling, corev1.ConditionTrue, apiv3.ConditionReasonReconcile, apiv3.ConditionMessageReconcile)
instance.Status.Phase = apiv3.CRInitializing
if statusErr = r.Client.Status().Update(ctx, instance); statusErr != nil {
klog.Errorf("Fail to update %s/%s: %v", instance.Namespace, instance.Name, statusErr)
return ctrl.Result{}, statusErr
}
} else {
if statusErr = r.updatePhase(ctx, instance, apiv3.CRUpdating); statusErr != nil {
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, statusErr)
return ctrl.Result{}, statusErr
}
}
// Creating/updating common-service-maps, skip when installing in AllNamespace Mode
if r.Bootstrap.CSData.WatchNamespaces != "" {
cm, err := r.Bootstrap.GetCmOfMapCs(ctx)
if err != nil {
if errors.IsNotFound(err) {
klog.Infof("Creating common-service-maps ConfigMap in kube-public")
if err = r.Bootstrap.CreateCsMaps(); err != nil {
klog.Errorf("Failed to create common-service-maps ConfigMap: %v", err)
os.Exit(1)
}
} else if strings.Contains(err.Error(), "not permitted") || strings.Contains(err.Error(), "no permission") {
klog.V(2).Infof("Skipping common-service-maps getting operations: %v", err)
} else {
klog.Errorf("Failed to get common-service-maps: %v", err)
os.Exit(1)
}
} else {
// Update common-service-maps via bootstrap wrapper (includes SSAR)
if err := r.Bootstrap.UpdateCsMaps(cm); err != nil {
klog.Errorf("Failed to update common-service-maps: %v", err)
os.Exit(1)
}
}
} else {
// check if the servicesNamespace is created
ns := &corev1.Namespace{}
if err := r.Reader.Get(ctx, types.NamespacedName{Name: r.Bootstrap.CSData.ServicesNs}, ns); err != nil {
if errors.IsNotFound(err) {
klog.Errorf("Not found servicesNamespace %s specified in the common-service CR.", r.Bootstrap.CSData.ServicesNs)
if err := r.updatePhase(ctx, instance, apiv3.CRFailed); err != nil {
klog.Error(err)
}
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, err)
return ctrl.Result{}, err
}
}
}
typeCorrect, err := r.Bootstrap.CheckClusterType(util.GetServicesNamespace(r.Reader))
if err != nil {
klog.Errorf("Failed to verify cluster type %v", err)
if err := r.updatePhase(ctx, instance, apiv3.CRFailed); err != nil {
klog.Error(err)
}
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, err)
return ctrl.Result{}, err
}
if !typeCorrect {
klog.Error("Cluster type specificed in the ibm-cpp-config isn't correct")
if statusErr = r.updatePhase(ctx, instance, apiv3.CRFailed); statusErr != nil {
klog.Error(statusErr)
}
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, statusErr)
return ctrl.Result{}, statusErr
}
// Init common service bootstrap resource
// Including namespace-scope configmap
// Deploy OperandConfig and OperandRegistry
if statusErr = r.Bootstrap.InitResources(instance, forceUpdateODLMCRs); statusErr != nil {
if statusErr := r.updatePhase(ctx, instance, apiv3.CRFailed); statusErr != nil {
klog.Error(statusErr)
}
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, statusErr)
return ctrl.Result{}, statusErr
}
// Generate Issuer and Certificate CR
if statusErr = r.Bootstrap.DeployCertManagerCR(); statusErr != nil {
klog.Errorf("Failed to deploy cert manager CRs: %v", statusErr)
if statusErr = r.updatePhase(ctx, instance, apiv3.CRFailed); statusErr != nil {
klog.Error(statusErr)
}
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, statusErr)
return ctrl.Result{}, statusErr
}
var isEqual bool
if isEqual, statusErr = r.updateOperatorConfig(ctx, instance.Spec.OperatorConfigs); statusErr != nil {
if statusErr := r.updatePhase(ctx, instance, apiv3.CRFailed); statusErr != nil {
klog.Error(statusErr)
}
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, statusErr)
return ctrl.Result{}, statusErr
} else if isEqual {
r.Recorder.Event(instance, corev1.EventTypeNormal, "Noeffect", fmt.Sprintf("No update, replica sizings in the OperatorConfig %s/%s are larger than the profile from CommonService CR %s/%s", r.Bootstrap.CSData.OperatorNs, "common-service", instance.Namespace, instance.Name))
}
if statusErr = configurationcollector.CreateUpdateConfig(r.Bootstrap); statusErr != nil {
if statusErr := r.updatePhase(ctx, instance, apiv3.CRFailed); statusErr != nil {
klog.Error(statusErr)
}
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, statusErr)
return ctrl.Result{}, statusErr
}
// Wait for Postgres Cluster image to be updated
if statusErr = r.Bootstrap.UpdatePostgresClusterImage(ctx, instance); statusErr != nil {
klog.Errorf("Failed to update Postgres Cluster image: %v", statusErr)
if statusErr := r.updatePhase(ctx, instance, apiv3.CRFailed); statusErr != nil {
klog.Error(statusErr)
}
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, statusErr)
return ctrl.Result{}, statusErr
}
if statusErr = r.Bootstrap.PropagateDefaultCR(instance); statusErr != nil {
klog.Error(statusErr)
return ctrl.Result{}, statusErr
}
if statusErr = r.Bootstrap.UpdateResourceLabel(instance); statusErr != nil {
klog.Error(statusErr)
return ctrl.Result{}, statusErr
}
if statusErr = r.Bootstrap.UpdateManageCertRotationLabel(instance); statusErr != nil {
klog.Error(statusErr)
return ctrl.Result{}, statusErr
}
// Set Succeeded phase
if statusErr = r.updatePhase(ctx, instance, apiv3.CRSucceeded); statusErr != nil {
klog.Error(statusErr)
return ctrl.Result{}, statusErr
}
if optStatusReady, optStatusErr := r.Bootstrap.CheckSubOperatorStatus(instance); optStatusErr != nil {
klog.Errorf("Failed to check the status of the operators in the OperandRegistry: %v", optStatusErr)
return ctrl.Result{}, optStatusErr
} else if !optStatusReady {
klog.Infof("Operators in the OperandRegistry are not deployed yet, skip operator status update")
}
klog.Infof("Finished reconciling CommonService: %s/%s", instance.Namespace, instance.Name)
return ctrl.Result{}, nil
}
// ReconcileGeneralCR is for setting the OperandConfig
func (r *CommonServiceReconciler) ReconcileGeneralCR(ctx context.Context, instance *apiv3.CommonService) (ctrl.Result, error) {
if instance.Status.Phase == "" {
if err := r.updatePhase(ctx, instance, apiv3.CRInitializing); err != nil {
klog.Error(err)
return ctrl.Result{}, err
}
} else {
if err := r.updatePhase(ctx, instance, apiv3.CRUpdating); err != nil {
klog.Error(err)
return ctrl.Result{}, err
}
}
instance.UpdateNonMasterConfigStatus(&r.Bootstrap.CSData)
opcon := util.NewUnstructured("operator.ibm.com", "OperandConfig", "v1alpha1")
opconKey := types.NamespacedName{
Name: "common-service",
Namespace: r.Bootstrap.CSData.ServicesNs,
}
if err := r.Reader.Get(ctx, opconKey, opcon); err != nil {
klog.Errorf("failed to get OperandConfig %s: %v", opconKey.String(), err)
if err := r.updatePhase(ctx, instance, apiv3.CRFailed); err != nil {
klog.Error(err)
}
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, err)
return ctrl.Result{}, err
}
// Generate Issuer and Certificate CR
if err := r.Bootstrap.DeployCertManagerCR(); err != nil {
klog.Errorf("Failed to deploy cert manager CRs: %v", err)
if err := r.updatePhase(ctx, instance, apiv3.CRFailed); err != nil {
klog.Error(err)
}
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, err)
return ctrl.Result{}, err
}
// Extract configurations using new extractor for subsequent updates
newConfigs, serviceControllerMapping, err := ExtractCommonServiceConfigs(instance, r.Bootstrap.CSData.ServicesNs)
if err != nil {
if err := r.updatePhase(ctx, instance, apiv3.CRFailed); err != nil {
klog.Error(err)
}
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, err)
return ctrl.Result{}, err
}
// Update OperandConfig (single update for subsequent reconciliations)
isEqual, err := r.updateOperandConfig(ctx, newConfigs, serviceControllerMapping)
if err != nil {
if err := r.updatePhase(ctx, instance, apiv3.CRFailed); err != nil {
klog.Error(err)
}
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, err)
return ctrl.Result{}, err
}
// Create Event if there is no update in OperandConfig after applying current CR
if isEqual {
r.Recorder.Event(instance, corev1.EventTypeNormal, "Noeffect", fmt.Sprintf("No update, resource sizings in the OperandConfig %s/%s are larger than the profile from CommonService CR %s/%s", r.Bootstrap.CSData.OperatorNs, "common-service", instance.Namespace, instance.Name))
}
// Wait for Postgres Cluster image to be updated
if err := r.Bootstrap.UpdatePostgresClusterImage(ctx, instance); err != nil {
klog.Errorf("Failed to update Postgres Cluster image: %v", err)
if err := r.updatePhase(ctx, instance, apiv3.CRFailed); err != nil {
klog.Error(err)
}
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, err)
return ctrl.Result{}, err
}
if err := r.Bootstrap.UpdateResourceLabel(instance); err != nil {
klog.Error(err)
return ctrl.Result{}, err
}
if err = r.Bootstrap.UpdateManageCertRotationLabel(instance); err != nil {
klog.Error(err)
return ctrl.Result{}, err
}
// Set Ready condition
instance.SetReadyCondition(constant.KindCR, apiv3.ConditionTypeReady, corev1.ConditionTrue)
if err := r.Client.Status().Update(ctx, instance); err != nil {
klog.Warning(err)
return ctrl.Result{}, err
}
if err := r.updatePhase(ctx, instance, apiv3.CRSucceeded); err != nil {
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, err)
return ctrl.Result{}, err
}
klog.Infof("Finished reconciling CommonService: %s/%s", instance.Namespace, instance.Name)
return ctrl.Result{}, nil
}
// ReconileNonConfigurableCR is for setting the cloned Master CR status for advanced topologies
func (r *CommonServiceReconciler) ReconcileNonConfigurableCR(ctx context.Context, instance *apiv3.CommonService) (ctrl.Result, error) {
if instance.Status.Phase == "" {
if err := r.updatePhase(ctx, instance, apiv3.CRInitializing); err != nil {
klog.Error(err)
return ctrl.Result{}, err
}
} else {
if err := r.updatePhase(ctx, instance, apiv3.CRUpdating); err != nil {
klog.Error(err)
return ctrl.Result{}, err
}
}
originalInstance := instance.DeepCopy()
instance.UpdateNonMasterConfigStatus(&r.Bootstrap.CSData)
if !reflect.DeepEqual(originalInstance.Status, instance.Status) {
r.Recorder.Event(instance, corev1.EventTypeNormal, "Noeffect", fmt.Sprintf("No update, this resource is the clone of Common Service CR named %s from namespace %s", constant.MasterCR, r.Bootstrap.CSData.OperatorNs))
if err := r.Client.Status().Update(ctx, instance); err != nil {
return ctrl.Result{}, err
}
}
// Set Ready condition
instance.SetReadyCondition(constant.KindCR, apiv3.ConditionTypeReady, corev1.ConditionTrue)
if err := r.Client.Status().Update(ctx, instance); err != nil {
klog.Warning(err)
return ctrl.Result{}, err
}
if err := r.updatePhase(ctx, instance, apiv3.CRSucceeded); err != nil {
klog.Errorf("Fail to reconcile %s/%s: %v", instance.Namespace, instance.Name, err)
return ctrl.Result{}, err
}
klog.Infof("Finished reconciling CommonService: %s/%s", instance.Namespace, instance.Name)
return ctrl.Result{}, nil
}
func (r *CommonServiceReconciler) mappingToCsRequestForConfigMaps(ctx context.Context, object client.Object) []reconcile.Request {
configMap, ok := object.(*corev1.ConfigMap)
if !ok {
return nil
}
// Check two configmaps: common-service-maps and ibm-cpp-config
if (configMap.Name == constant.CsMapConfigMap && configMap.Namespace == constant.CsMapConfigMapNs) ||
(configMap.Name == constant.IBMCPPCONFIG && configMap.Namespace == r.Bootstrap.CSData.ServicesNs) {
return []reconcile.Request{
{NamespacedName: types.NamespacedName{
Name: constant.MasterCR,
Namespace: r.Bootstrap.CSData.OperatorNs,
}},
}
}
return nil
}
func (r *CommonServiceReconciler) mappingToCsRequestForOperandRegistry(ctx context.Context, object client.Object) []reconcile.Request {
operandRegistry, ok := object.(*odlm.OperandRegistry)
if !ok {
// It's not an OperandRegistry, ignore
return nil
}
if operandRegistry.Name == constant.MasterCR && operandRegistry.Namespace == r.Bootstrap.CSData.ServicesNs {
if isNonNoopOperandReconcile(operandRegistry) {
// Enqueue a reconciliation request for the corresponding CommonService
return []reconcile.Request{
{NamespacedName: types.NamespacedName{Name: constant.MasterCR, Namespace: r.Bootstrap.CSData.OperatorNs}},
}
}
}
return nil
}
func (r *CommonServiceReconciler) isODLMManagedSubscription(ctx context.Context, object client.Object) []reconcile.Request {
subscription, ok := object.(*olmv1alpha1.Subscription)
if !ok {
// It's not an Subscription, ignore
return nil
}
if subscription.GetLabels()[constant.OpreqLabel] == "true" {
// Enqueue a reconciliation request for the corresponding CommonService
return []reconcile.Request{
{NamespacedName: types.NamespacedName{Name: constant.MasterCR, Namespace: r.Bootstrap.CSData.OperatorNs}},
}
}
return nil
}
// isNonNoopOperandReconcile checks only no-op operators trigger the reconcile
func isNonNoopOperandReconcile(operandRegistry *odlm.OperandRegistry) bool {
if operandRegistry.Status.OperatorsStatus != nil {
// List all requested operators
for operator := range operandRegistry.Status.OperatorsStatus {
// If there is a requested operator's installMode is "no-op", then skip reconcile
for _, op := range operandRegistry.Spec.Operators {
if op.Name == operator && op.InstallMode == "no-op" {
klog.Infof("The operator %s with 'no-op' installMode is still requested in OperandRegistry, skip reconciliation", operator)
return false
}
}
}
}
return true
}
func (r *CommonServiceReconciler) SetupWithManager(mgr ctrl.Manager) error {
// Set up configuration merger for single-stage OperandConfig creation
// This injects the merge logic into bootstrap without creating import cycles
r.Bootstrap.SetConfigMerger(CreateMergerFunc(r))
klog.Info("Configuration merger initialized for single-stage OperandConfig creation")
controller := ctrl.NewControllerManagedBy(mgr).
// AnnotationChangedPredicate is intended to be used in conjunction with the GenerationChangedPredicate
For(&apiv3.CommonService{}, builder.WithPredicates(
predicate.Or(
predicate.GenerationChangedPredicate{},
predicate.AnnotationChangedPredicate{},
predicate.LabelChangedPredicate{}))).
Watches(
&corev1.ConfigMap{},
handler.EnqueueRequestsFromMapFunc(r.mappingToCsRequestForConfigMaps),
builder.WithPredicates(predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool { return true },
UpdateFunc: func(e event.UpdateEvent) bool { return true },
DeleteFunc: func(e event.DeleteEvent) bool { return !e.DeleteStateUnknown },
}))
if isOpregAPI, err := r.Bootstrap.CheckCRD(constant.OpregAPIGroupVersion, constant.OpregKind); err != nil {
klog.Errorf("Failed to check if OperandRegistry CRD exists: %v", err)
return err
} else if isOpregAPI {
controller = controller.Watches(
&odlm.OperandRegistry{},
handler.EnqueueRequestsFromMapFunc(r.mappingToCsRequestForOperandRegistry),
builder.WithPredicates(predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
oldOperandRegistry, ok := e.ObjectOld.(*odlm.OperandRegistry)
if !ok {
return false
}
newOperandRegistry, ok := e.ObjectNew.(*odlm.OperandRegistry)
if !ok {
return false
}
// Return true if the length of .status.operatorsStatus array has changed, indicating that a operator has been added or removed
return len(oldOperandRegistry.Status.OperatorsStatus) != len(newOperandRegistry.Status.OperatorsStatus)
},
},
))
}
if isSubscriptionAPI, err := r.Bootstrap.CheckCRD(constant.SubscriptionAPIGroupVersion, constant.SubscriptionKind); err != nil {
klog.Errorf("Failed to check if Subscription CRD exists: %v", err)
return err
} else if isSubscriptionAPI {
klog.Infof("Subscription CRD exists, start watching Subscription")
controller = controller.Watches(
&olmv1alpha1.Subscription{},
handler.EnqueueRequestsFromMapFunc(r.isODLMManagedSubscription),
builder.WithPredicates(predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
oldObject := e.ObjectOld.(*olmv1alpha1.Subscription)
newObject := e.ObjectNew.(*olmv1alpha1.Subscription)
return (oldObject.Status.InstalledCSV != "" && newObject.Status.InstalledCSV != "" && oldObject.Status.InstalledCSV != newObject.Status.InstalledCSV)
},
DeleteFunc: func(e event.DeleteEvent) bool {
return !e.DeleteStateUnknown
},
CreateFunc: func(e event.CreateEvent) bool {
return true
},
},
))
}
return controller.Complete(r)
}