@@ -2,6 +2,7 @@ package operators
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
5
6
"sync"
6
7
7
8
"github.com/go-logr/logr"
@@ -45,9 +46,7 @@ type AdoptionReconciler struct {
45
46
// SetupWithManager adds the operator reconciler to the given controller manager.
46
47
func (r * AdoptionReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
47
48
// Trigger operator events from the events of their compoenents.
48
- enqueueSub := & handler.EnqueueRequestsFromMapFunc {
49
- ToRequests : handler .ToRequestsFunc (r .mapToSubscriptions ),
50
- }
49
+ enqueueSub := handler .EnqueueRequestsFromMapFunc (r .mapToSubscriptions )
51
50
52
51
// Create multiple controllers for resource types that require automatic adoption
53
52
err := ctrl .NewControllerManagedBy (mgr ).
@@ -60,12 +59,8 @@ func (r *AdoptionReconciler) SetupWithManager(mgr ctrl.Manager) error {
60
59
}
61
60
62
61
var (
63
- enqueueCSV = & handler.EnqueueRequestsFromMapFunc {
64
- ToRequests : handler .ToRequestsFunc (r .mapToClusterServiceVersions ),
65
- }
66
- enqueueProviders = & handler.EnqueueRequestsFromMapFunc {
67
- ToRequests : handler .ToRequestsFunc (r .mapToProviders ),
68
- }
62
+ enqueueCSV = handler .EnqueueRequestsFromMapFunc (r .mapToClusterServiceVersions )
63
+ enqueueProviders = handler .EnqueueRequestsFromMapFunc (r .mapToProviders )
69
64
)
70
65
err = ctrl .NewControllerManagedBy (mgr ).
71
66
For (& operatorsv1alpha1.ClusterServiceVersion {}).
@@ -113,13 +108,12 @@ func NewAdoptionReconciler(cli client.Client, log logr.Logger, scheme *runtime.S
113
108
}
114
109
115
110
// ReconcileSubscription labels the CSVs installed by a Subscription as components of an operator named after the subscribed package and install namespace.
116
- func (r * AdoptionReconciler ) ReconcileSubscription (req ctrl.Request ) (reconcile.Result , error ) {
111
+ func (r * AdoptionReconciler ) ReconcileSubscription (ctx context. Context , req ctrl.Request ) (reconcile.Result , error ) {
117
112
// Set up a convenient log object so we don't have to type request over and over again
118
113
log := r .log .WithValues ("request" , req )
119
114
log .V (4 ).Info ("reconciling subscription" )
120
115
121
116
// Fetch the Subscription from the cache
122
- ctx := context .TODO ()
123
117
in := & operatorsv1alpha1.Subscription {}
124
118
if err := r .Get (ctx , req .NamespacedName , in ); err != nil {
125
119
if apierrors .IsNotFound (err ) {
@@ -176,13 +170,12 @@ func (r *AdoptionReconciler) ReconcileSubscription(req ctrl.Request) (reconcile.
176
170
}
177
171
178
172
// ReconcileClusterServiceVersion projects the component labels of a given CSV onto all resources owned by it.
179
- func (r * AdoptionReconciler ) ReconcileClusterServiceVersion (req ctrl.Request ) (reconcile.Result , error ) {
173
+ func (r * AdoptionReconciler ) ReconcileClusterServiceVersion (ctx context. Context , req ctrl.Request ) (reconcile.Result , error ) {
180
174
// Set up a convenient log object so we don't have to type request over and over again
181
175
log := r .log .WithValues ("request" , req )
182
176
log .V (4 ).Info ("reconciling csv" )
183
177
184
178
// Fetch the CSV from the cache
185
- ctx := context .TODO ()
186
179
in := & operatorsv1alpha1.ClusterServiceVersion {}
187
180
if err := r .Get (ctx , req .NamespacedName , in ); err != nil {
188
181
if apierrors .IsNotFound (err ) {
@@ -265,15 +258,20 @@ func (r *AdoptionReconciler) adopt(ctx context.Context, operator *decorators.Ope
265
258
return nil
266
259
}
267
260
268
- if err := r .Get (ctx , types.NamespacedName {Namespace : m .GetNamespace (), Name : m .GetName ()}, component ); err != nil {
261
+ cObj , ok := component .(client.Object )
262
+ if ! ok {
263
+ return fmt .Errorf ("Unable to typecast runtime.Object to client.Object" )
264
+ }
265
+
266
+ if err := r .Get (ctx , types.NamespacedName {Namespace : m .GetNamespace (), Name : m .GetName ()}, cObj ); err != nil {
269
267
if apierrors .IsNotFound (err ) {
270
268
r .log .Error (err , "component not found" )
271
269
err = nil
272
270
}
273
271
274
272
return err
275
273
}
276
- candidate := component .DeepCopyObject ()
274
+ candidate := cObj .DeepCopyObject ()
277
275
278
276
adopted , err := operator .AdoptComponent (candidate )
279
277
if err != nil {
@@ -282,14 +280,21 @@ func (r *AdoptionReconciler) adopt(ctx context.Context, operator *decorators.Ope
282
280
283
281
if adopted {
284
282
// Only update if freshly adopted
285
- r .log .Info ("component adopted" , "component" , candidate )
286
- return r .Patch (ctx , candidate , client .MergeFrom (component ))
283
+ pCObj , ok := candidate .(client.Object )
284
+ if ! ok {
285
+ return fmt .Errorf ("Unable to typecast runtime.Object to client.Object" )
286
+ }
287
+ return r .Patch (ctx , pCObj , client .MergeFrom (cObj ))
287
288
}
288
289
289
290
return nil
290
291
}
291
292
292
293
func (r * AdoptionReconciler ) disown (ctx context.Context , operator * decorators.Operator , component runtime.Object ) error {
294
+ cObj , ok := component .(client.Object )
295
+ if ! ok {
296
+ return fmt .Errorf ("Unable to typecast runtime.Object to client.Object" )
297
+ }
293
298
candidate := component .DeepCopyObject ()
294
299
disowned , err := operator .DisownComponent (candidate )
295
300
if err != nil {
@@ -303,7 +308,11 @@ func (r *AdoptionReconciler) disown(ctx context.Context, operator *decorators.Op
303
308
304
309
// Only update if freshly disowned
305
310
r .log .V (4 ).Info ("component disowned" , "component" , candidate )
306
- return r .Patch (ctx , candidate , client .MergeFrom (component ))
311
+ uCObj , ok := candidate .(client.Object )
312
+ if ! ok {
313
+ return fmt .Errorf ("Unable to typecast runtime.Object to client.Object" )
314
+ }
315
+ return r .Patch (ctx , uCObj , client .MergeFrom (cObj ))
307
316
}
308
317
309
318
func (r * AdoptionReconciler ) adoptees (ctx context.Context , operator decorators.Operator , csv * operatorsv1alpha1.ClusterServiceVersion ) ([]runtime.Object , error ) {
@@ -335,7 +344,11 @@ func (r *AdoptionReconciler) adoptees(ctx context.Context, operator decorators.O
335
344
}
336
345
opt := client.MatchingLabelsSelector {Selector : selector }
337
346
for _ , list := range componentLists {
338
- if err := r .List (ctx , list , opt ); err != nil {
347
+ cList , ok := list .(client.ObjectList )
348
+ if ! ok {
349
+ return nil , fmt .Errorf ("Unable to typecast runtime.Object to client.ObjectList" )
350
+ }
351
+ if err := r .List (ctx , cList , opt ); err != nil {
339
352
return nil , err
340
353
}
341
354
}
@@ -415,16 +428,16 @@ func (r *AdoptionReconciler) adoptInstallPlan(ctx context.Context, operator *dec
415
428
return utilerrors .NewAggregate (errs )
416
429
}
417
430
418
- func (r * AdoptionReconciler ) mapToSubscriptions (obj handler. MapObject ) (requests []reconcile.Request ) {
419
- if obj . Meta == nil {
431
+ func (r * AdoptionReconciler ) mapToSubscriptions (obj client. Object ) (requests []reconcile.Request ) {
432
+ if obj == nil {
420
433
return
421
434
}
422
435
423
436
// Requeue all Subscriptions in the resource namespace
424
437
// The Subscription reconciler will sort out the important changes
425
438
ctx := context .TODO ()
426
439
subs := & operatorsv1alpha1.SubscriptionList {}
427
- if err := r .List (ctx , subs , client .InNamespace (obj .Meta . GetNamespace ())); err != nil {
440
+ if err := r .List (ctx , subs , client .InNamespace (obj .GetNamespace ())); err != nil {
428
441
r .log .Error (err , "error listing subscriptions" )
429
442
}
430
443
@@ -444,15 +457,15 @@ func (r *AdoptionReconciler) mapToSubscriptions(obj handler.MapObject) (requests
444
457
return
445
458
}
446
459
447
- func (r * AdoptionReconciler ) mapToClusterServiceVersions (obj handler. MapObject ) (requests []reconcile.Request ) {
448
- if obj . Meta == nil {
460
+ func (r * AdoptionReconciler ) mapToClusterServiceVersions (obj client. Object ) (requests []reconcile.Request ) {
461
+ if obj == nil {
449
462
return
450
463
}
451
464
452
465
// Get all owner CSV from owner labels if cluster scoped
453
- namespace := obj .Meta . GetNamespace ()
466
+ namespace := obj .GetNamespace ()
454
467
if namespace == metav1 .NamespaceAll {
455
- name , ns , ok := ownerutil .GetOwnerByKindLabel (obj . Meta , operatorsv1alpha1 .ClusterServiceVersionKind )
468
+ name , ns , ok := ownerutil .GetOwnerByKindLabel (obj , operatorsv1alpha1 .ClusterServiceVersionKind )
456
469
if ok {
457
470
nsn := types.NamespacedName {Namespace : ns , Name : name }
458
471
requests = append (requests , reconcile.Request {NamespacedName : nsn })
@@ -461,7 +474,7 @@ func (r *AdoptionReconciler) mapToClusterServiceVersions(obj handler.MapObject)
461
474
}
462
475
463
476
// Get all owner CSVs from OwnerReferences
464
- owners := ownerutil .GetOwnersByKind (obj . Meta , operatorsv1alpha1 .ClusterServiceVersionKind )
477
+ owners := ownerutil .GetOwnersByKind (obj , operatorsv1alpha1 .ClusterServiceVersionKind )
465
478
for _ , owner := range owners {
466
479
nsn := types.NamespacedName {Namespace : namespace , Name : owner .Name }
467
480
requests = append (requests , reconcile.Request {NamespacedName : nsn })
@@ -470,8 +483,8 @@ func (r *AdoptionReconciler) mapToClusterServiceVersions(obj handler.MapObject)
470
483
return
471
484
}
472
485
473
- func (r * AdoptionReconciler ) mapToProviders (obj handler. MapObject ) (requests []reconcile.Request ) {
474
- if obj . Meta == nil {
486
+ func (r * AdoptionReconciler ) mapToProviders (obj client. Object ) (requests []reconcile.Request ) {
487
+ if obj == nil {
475
488
return nil
476
489
}
477
490
@@ -489,7 +502,7 @@ func (r *AdoptionReconciler) mapToProviders(obj handler.MapObject) (requests []r
489
502
NamespacedName : types.NamespacedName {Namespace : csv .GetNamespace (), Name : csv .GetName ()},
490
503
}
491
504
for _ , provided := range csv .Spec .CustomResourceDefinitions .Owned {
492
- if provided .Name == obj .Meta . GetName () {
505
+ if provided .Name == obj .GetName () {
493
506
requests = append (requests , request )
494
507
break
495
508
}
0 commit comments