@@ -20,15 +20,26 @@ package controllers
20
20
21
21
import (
22
22
"context"
23
+ "reflect"
23
24
"time"
24
25
25
26
"github.com/go-logr/logr"
26
27
apierrors "k8s.io/apimachinery/pkg/api/errors"
27
28
"k8s.io/apimachinery/pkg/runtime"
28
29
"k8s.io/apimachinery/pkg/types"
30
+ clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
29
31
"sigs.k8s.io/cluster-api/util"
32
+ "sigs.k8s.io/cluster-api/util/annotations"
33
+ "sigs.k8s.io/cluster-api/util/predicates"
30
34
ctrl "sigs.k8s.io/controller-runtime"
35
+ "sigs.k8s.io/controller-runtime/pkg/builder"
31
36
"sigs.k8s.io/controller-runtime/pkg/client"
37
+ "sigs.k8s.io/controller-runtime/pkg/controller"
38
+ "sigs.k8s.io/controller-runtime/pkg/event"
39
+ "sigs.k8s.io/controller-runtime/pkg/handler"
40
+ "sigs.k8s.io/controller-runtime/pkg/predicate"
41
+ "sigs.k8s.io/controller-runtime/pkg/reconcile"
42
+ "sigs.k8s.io/controller-runtime/pkg/source"
32
43
33
44
infrav1 "sigs.k8s.io/cluster-api-provider-nested/api/v1alpha4"
34
45
controlplanev1 "sigs.k8s.io/cluster-api-provider-nested/controlplane/nested/api/v1alpha4"
@@ -48,10 +59,51 @@ type NestedClusterReconciler struct {
48
59
}
49
60
50
61
// SetupWithManager sets up the controller with the Manager.
51
- func (r * NestedClusterReconciler ) SetupWithManager (mgr ctrl.Manager ) error {
62
+ func (r * NestedClusterReconciler ) SetupWithManager (ctx context.Context , mgr ctrl.Manager , options controller.Options ) error {
63
+ clusterToInfraFn := util .ClusterToInfrastructureMapFunc (infrav1 .GroupVersion .WithKind ("NestedCluster" ))
64
+ log := ctrl .LoggerFrom (ctx )
65
+
52
66
return ctrl .NewControllerManagedBy (mgr ).
53
- For (& infrav1.NestedCluster {}).
54
- Owns (& controlplanev1.NestedControlPlane {}).
67
+ WithOptions (options ).
68
+ For (& infrav1.NestedCluster {},
69
+ builder .WithPredicates (
70
+ predicate.Funcs {
71
+ // Avoid reconciling if the event triggering the reconciliation is related to incremental status updates
72
+ UpdateFunc : func (e event.UpdateEvent ) bool {
73
+ oldCluster := e .ObjectOld .(* infrav1.NestedCluster ).DeepCopy ()
74
+ newCluster := e .ObjectNew .(* infrav1.NestedCluster ).DeepCopy ()
75
+ oldCluster .Status = infrav1.NestedClusterStatus {}
76
+ newCluster .Status = infrav1.NestedClusterStatus {}
77
+ oldCluster .ObjectMeta .ResourceVersion = ""
78
+ newCluster .ObjectMeta .ResourceVersion = ""
79
+ return ! reflect .DeepEqual (oldCluster , newCluster )
80
+ },
81
+ },
82
+ ),
83
+ ).
84
+ Watches (
85
+ & source.Kind {Type : & clusterv1.Cluster {}},
86
+ handler .EnqueueRequestsFromMapFunc (func (o client.Object ) []reconcile.Request {
87
+ requests := clusterToInfraFn (o )
88
+ if len (requests ) < 1 {
89
+ return nil
90
+ }
91
+
92
+ c := & infrav1.NestedCluster {}
93
+ if err := r .Client .Get (ctx , requests [0 ].NamespacedName , c ); err != nil {
94
+ log .V (4 ).Error (err , "Failed to get Nested cluster" )
95
+ return nil
96
+ }
97
+
98
+ if annotations .IsExternallyManaged (c ) {
99
+ log .V (4 ).Info ("Nested cluster is externally managed, skipping mapping." )
100
+ return nil
101
+ }
102
+ return requests
103
+ }),
104
+ builder .WithPredicates (predicates .ClusterUnpaused (ctrl .LoggerFrom (ctx ))),
105
+ ).
106
+ WithEventFilter (predicates .ResourceIsNotExternallyManaged (ctrl .LoggerFrom (ctx ))).
55
107
Complete (r )
56
108
}
57
109
0 commit comments