@@ -19,19 +19,17 @@ package controllers
19
19
import (
20
20
"context"
21
21
"fmt"
22
- "reflect"
23
22
"time"
24
23
25
24
"github.com/pkg/errors"
26
25
corev1 "k8s.io/api/core/v1"
27
26
apierrors "k8s.io/apimachinery/pkg/api/errors"
28
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
27
"k8s.io/apimachinery/pkg/runtime"
30
- "k8s.io/apimachinery/pkg/runtime/schema"
31
28
kerrors "k8s.io/apimachinery/pkg/util/errors"
32
29
"k8s.io/client-go/tools/record"
30
+ "k8s.io/utils/ptr"
33
31
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
34
- kubeadmv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1beta1 "
32
+ "sigs.k8s.io/cluster-api/controllers/external "
35
33
expv1 "sigs.k8s.io/cluster-api/exp/api/v1beta1"
36
34
"sigs.k8s.io/cluster-api/util"
37
35
"sigs.k8s.io/cluster-api/util/annotations"
@@ -41,7 +39,6 @@ import (
41
39
"sigs.k8s.io/controller-runtime/pkg/client"
42
40
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
43
41
"sigs.k8s.io/controller-runtime/pkg/handler"
44
- "sigs.k8s.io/controller-runtime/pkg/predicate"
45
42
"sigs.k8s.io/controller-runtime/pkg/reconcile"
46
43
47
44
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
63
60
Timeouts reconciler.Timeouts
64
61
WatchFilterValue string
65
62
createAzureMachinePoolService azureMachinePoolServiceCreator
66
- BootstrapConfigGVK schema. GroupVersionKind
63
+ externalTracker external. ObjectTracker
67
64
CredentialCache azure.CredentialCache
68
65
}
69
66
@@ -77,21 +74,13 @@ type (
77
74
type azureMachinePoolServiceCreator func (machinePoolScope * scope.MachinePoolScope ) (* azureMachinePoolService , error )
78
75
79
76
// NewAzureMachinePoolReconciler returns a new AzureMachinePoolReconciler instance.
80
- func NewAzureMachinePoolReconciler (client client.Client , recorder record.EventRecorder , timeouts reconciler.Timeouts , watchFilterValue , bootstrapConfigGVK string , credCache azure.CredentialCache ) * AzureMachinePoolReconciler {
81
- gvk := schema .FromAPIVersionAndKind (kubeadmv1 .GroupVersion .String (), reflect .TypeOf ((* kubeadmv1 .KubeadmConfig )(nil )).Elem ().Name ())
82
- userGVK , _ := schema .ParseKindArg (bootstrapConfigGVK )
83
-
84
- if userGVK != nil {
85
- gvk = * userGVK
86
- }
87
-
77
+ func NewAzureMachinePoolReconciler (client client.Client , recorder record.EventRecorder , timeouts reconciler.Timeouts , watchFilterValue string , credCache azure.CredentialCache ) * AzureMachinePoolReconciler {
88
78
ampr := & AzureMachinePoolReconciler {
89
- Client : client ,
90
- Recorder : recorder ,
91
- Timeouts : timeouts ,
92
- WatchFilterValue : watchFilterValue ,
93
- BootstrapConfigGVK : gvk ,
94
- CredentialCache : credCache ,
79
+ Client : client ,
80
+ Recorder : recorder ,
81
+ Timeouts : timeouts ,
82
+ WatchFilterValue : watchFilterValue ,
83
+ CredentialCache : credCache ,
95
84
}
96
85
97
86
ampr .createAzureMachinePoolService = newAzureMachinePoolService
@@ -127,9 +116,7 @@ func (ampr *AzureMachinePoolReconciler) SetupWithManager(ctx context.Context, mg
127
116
return errors .Wrap (err , "failed to create mapper for Cluster to AzureMachines" )
128
117
}
129
118
130
- config := & metav1.PartialObjectMetadata {}
131
- config .SetGroupVersionKind (ampr .BootstrapConfigGVK )
132
- return ctrl .NewControllerManagedBy (mgr ).
119
+ controller , err := ctrl .NewControllerManagedBy (mgr ).
133
120
WithOptions (options .Options ).
134
121
For (& infrav1exp.AzureMachinePool {}).
135
122
WithEventFilter (predicates .ResourceHasFilterLabel (mgr .GetScheme (), log , ampr .WatchFilterValue )).
@@ -148,12 +135,6 @@ func (ampr *AzureMachinePoolReconciler) SetupWithManager(ctx context.Context, mg
148
135
& infrav1.AzureManagedControlPlane {},
149
136
handler .EnqueueRequestsFromMapFunc (azureManagedControlPlaneMapper ),
150
137
).
151
- // watch for changes in KubeadmConfig (or any BootstrapConfig) to sync bootstrap token
152
- Watches (
153
- config ,
154
- handler .EnqueueRequestsFromMapFunc (BootstrapConfigToInfrastructureMapFunc (ctx , ampr .Client , log )),
155
- builder .WithPredicates (predicate.ResourceVersionChangedPredicate {}),
156
- ).
157
138
Watches (
158
139
& infrav1exp.AzureMachinePoolMachine {},
159
140
handler .EnqueueRequestsFromMapFunc (AzureMachinePoolMachineMapper (mgr .GetScheme (), log )),
@@ -170,13 +151,25 @@ func (ampr *AzureMachinePoolReconciler) SetupWithManager(ctx context.Context, mg
170
151
infracontroller .ClusterPauseChangeAndInfrastructureReady (mgr .GetScheme (), log ),
171
152
predicates .ResourceHasFilterLabel (mgr .GetScheme (), log , ampr .WatchFilterValue ),
172
153
),
173
- ).
174
- Complete (r )
154
+ ).Build (r )
155
+ if err != nil {
156
+ return fmt .Errorf ("creating new controller manager: %w" , err )
157
+ }
158
+
159
+ predicateLog := ptr .To (ctrl .LoggerFrom (ctx ).WithValues ("controller" , "azuremachinepool" ))
160
+ ampr .externalTracker = external.ObjectTracker {
161
+ Controller : controller ,
162
+ Cache : mgr .GetCache (),
163
+ Scheme : mgr .GetScheme (),
164
+ PredicateLogger : predicateLog ,
165
+ }
166
+
167
+ return nil
175
168
}
176
169
177
170
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azuremachinepools,verbs=get;list;watch;create;update;patch;delete
178
171
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azuremachinepools/status,verbs=get;update;patch
179
- // +kubebuilder:rbac:groups=bootstrap.cluster.x-k8s.io,resources=kubeadmconfigs;kubeadmconfigs/status ,verbs=get;list;watch
172
+ // +kubebuilder:rbac:groups=bootstrap.cluster.x-k8s.io,resources=* ,verbs=get;list;watch
180
173
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azuremachinepoolmachines,verbs=get;list;watch;create;update;patch;delete
181
174
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=azuremachinepoolmachines/status,verbs=get
182
175
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machinepools;machinepools/status,verbs=get;list;watch;update;patch
@@ -295,6 +288,27 @@ func (ampr *AzureMachinePoolReconciler) reconcileNormal(ctx context.Context, mac
295
288
return reconcile.Result {}, nil
296
289
}
297
290
291
+ // Add a Watch to the referenced Bootstrap Config
292
+ if machinePoolScope .MachinePool .Spec .Template .Spec .Bootstrap .ConfigRef != nil {
293
+ ref := machinePoolScope .MachinePool .Spec .Template .Spec .Bootstrap .ConfigRef
294
+ obj , err := external .Get (ctx , ampr .Client , ref )
295
+ if err != nil {
296
+ if apierrors .IsNotFound (errors .Cause (err )) {
297
+ return reconcile.Result {}, errors .Wrapf (err , "could not find %v %q for MachinePool %q in namespace %q, requeuing while searching for bootstrap ConfigRef" ,
298
+ ref .GroupVersionKind (), ref .Name , machinePoolScope .MachinePool .Name , ref .Namespace )
299
+ }
300
+ return reconcile.Result {}, err
301
+ }
302
+
303
+ // Ensure we add a watch to the external object, if there isn't one already.
304
+ if err := ampr .externalTracker .Watch (log , obj ,
305
+ handler .EnqueueRequestsFromMapFunc (BootstrapConfigToInfrastructureMapFunc (ampr .Client , * ampr .externalTracker .PredicateLogger )),
306
+ predicates .ResourceIsChanged (ampr .Client .Scheme (), * ampr .externalTracker .PredicateLogger )); err != nil {
307
+ return reconcile.Result {}, errors .Wrapf (err , "could not add a watcher to the object %v %q for MachinePool %q in namespace %q, requeuing" ,
308
+ ref .GroupVersionKind (), ref .Name , machinePoolScope .MachinePool .Name , ref .Namespace )
309
+ }
310
+ }
311
+
298
312
// Make sure bootstrap data is available and populated.
299
313
if machinePoolScope .MachinePool .Spec .Template .Spec .Bootstrap .DataSecretName == nil {
300
314
log .Info ("Bootstrap data secret reference is not yet available" )
0 commit comments