@@ -140,8 +140,23 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
140
140
return ctrl.Result {}, err
141
141
}
142
142
143
+ // Initialize the patch helper
144
+ patchHelper , err := patch .NewHelper (config , r )
145
+ if err != nil {
146
+ return ctrl.Result {}, err
147
+ }
148
+ // Attempt to Patch the KubeadmConfig object and status after each reconciliation if no error occurs.
149
+ defer func () {
150
+ if rerr == nil {
151
+ if rerr = patchHelper .Patch (ctx , config ); rerr != nil {
152
+ log .Error (rerr , "failed to patch config" )
153
+ }
154
+ }
155
+ }()
156
+
157
+ switch {
143
158
// If we've already embedded a time-limited join token into a config, but are still waiting for the token to be used, refresh it
144
- if config .Status .Ready {
159
+ case config .Status .Ready :
145
160
token := config .Spec .JoinConfiguration .Discovery .BootstrapToken .Token
146
161
147
162
// gets the remote secret interface client for the current cluster
@@ -157,29 +172,11 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
157
172
return ctrl.Result {}, errors .Wrapf (err , "failed to refresh bootstrap token" )
158
173
}
159
174
return ctrl.Result {}, nil
160
- }
161
-
162
175
// Wait patiently for the infrastructure to be ready
163
- if ! cluster .Status .InfrastructureReady {
176
+ case ! cluster .Status .InfrastructureReady :
164
177
log .Info ("Infrastructure is not ready, waiting until ready." )
165
178
return ctrl.Result {}, nil
166
- }
167
-
168
- // Initialize the patch helper
169
- patchHelper , err := patch .NewHelper (config , r )
170
- if err != nil {
171
- return ctrl.Result {}, err
172
- }
173
- // Attempt to Patch the KubeadmConfig object and status after each reconciliation if no error occurs.
174
- defer func () {
175
- if rerr == nil {
176
- if rerr = patchHelper .Patch (ctx , config ); rerr != nil {
177
- log .Error (rerr , "failed to patch config" )
178
- }
179
- }
180
- }()
181
-
182
- if ! cluster .Status .ControlPlaneInitialized {
179
+ case ! cluster .Status .ControlPlaneInitialized :
183
180
// if it's NOT a control plane machine, requeue
184
181
if ! util .IsControlPlaneMachine (machine ) {
185
182
log .Info (fmt .Sprintf ("Machine is not a control plane. If it should be a control plane, add `%s: true` as a label to the Machine" , clusterv1 .MachineControlPlaneLabelName ))
@@ -271,102 +268,102 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
271
268
config .Status .Ready = true
272
269
273
270
return ctrl.Result {}, nil
274
- }
275
-
276
271
// Every other case it's a join scenario
277
- // Nb. in this case ClusterConfiguration and JoinConfiguration should not be defined by users, but in case of misconfigurations, CABPK simply ignore them
272
+ default :
273
+ // Nb. in this case ClusterConfiguration and JoinConfiguration should not be defined by users, but in case of misconfigurations, CABPK simply ignore them
278
274
279
- // Unlock any locks that might have been set during init process
280
- r .KubeadmInitLock .Unlock (ctx , cluster )
275
+ // Unlock any locks that might have been set during init process
276
+ r .KubeadmInitLock .Unlock (ctx , cluster )
281
277
282
- if config .Spec .JoinConfiguration == nil {
283
- return ctrl.Result {}, errors .New ("Control plane already exists for the cluster, only KubeadmConfig objects with JoinConfiguration are allowed" )
284
- }
278
+ if config .Spec .JoinConfiguration == nil {
279
+ return ctrl.Result {}, errors .New ("Control plane already exists for the cluster, only KubeadmConfig objects with JoinConfiguration are allowed" )
280
+ }
285
281
286
- certificates := internalcluster .NewCertificates ()
287
- if err := certificates .Lookup (ctx , r .Client , cluster ); err != nil {
288
- log .Error (err , "unable to lookup cluster certificates" )
289
- return ctrl.Result {}, err
290
- }
291
- if err := certificates .EnsureAllExist (); err != nil {
292
- return ctrl.Result {}, err
293
- }
282
+ certificates := internalcluster .NewCertificates ()
283
+ if err := certificates .Lookup (ctx , r .Client , cluster ); err != nil {
284
+ log .Error (err , "unable to lookup cluster certificates" )
285
+ return ctrl.Result {}, err
286
+ }
287
+ if err := certificates .EnsureAllExist (); err != nil {
288
+ return ctrl.Result {}, err
289
+ }
294
290
295
- hashes , err := certificates .GetByPurpose (secret .ClusterCA ).Hashes ()
296
- if err != nil {
297
- log .Error (err , "Unable to generate Cluster CA certificate hashes" )
298
- return ctrl.Result {}, err
299
- }
300
- // TODO: move this into reconcile.Discovery so that defaults for the Discovery are all in the same place
301
- if config .Spec .JoinConfiguration .Discovery .BootstrapToken == nil {
302
- config .Spec .JoinConfiguration .Discovery .BootstrapToken = & kubeadmv1beta1.BootstrapTokenDiscovery {}
303
- }
304
- config .Spec .JoinConfiguration .Discovery .BootstrapToken .CACertHashes = hashes
291
+ hashes , err := certificates .GetByPurpose (secret .ClusterCA ).Hashes ()
292
+ if err != nil {
293
+ log .Error (err , "Unable to generate Cluster CA certificate hashes" )
294
+ return ctrl.Result {}, err
295
+ }
296
+ // TODO: move this into reconcile.Discovery so that defaults for the Discovery are all in the same place
297
+ if config .Spec .JoinConfiguration .Discovery .BootstrapToken == nil {
298
+ config .Spec .JoinConfiguration .Discovery .BootstrapToken = & kubeadmv1beta1.BootstrapTokenDiscovery {}
299
+ }
300
+ config .Spec .JoinConfiguration .Discovery .BootstrapToken .CACertHashes = hashes
305
301
306
- // ensure that joinConfiguration.Discovery is properly set for joining node on the current cluster
307
- if err := r .reconcileDiscovery (cluster , config ); err != nil {
308
- if requeueErr , ok := errors .Cause (err ).(capierrors.HasRequeueAfterError ); ok {
309
- log .Info (err .Error ())
310
- return ctrl.Result {RequeueAfter : requeueErr .GetRequeueAfter ()}, nil
302
+ // ensure that joinConfiguration.Discovery is properly set for joining node on the current cluster
303
+ if err := r .reconcileDiscovery (cluster , config ); err != nil {
304
+ if requeueErr , ok := errors .Cause (err ).(capierrors.HasRequeueAfterError ); ok {
305
+ log .Info (err .Error ())
306
+ return ctrl.Result {RequeueAfter : requeueErr .GetRequeueAfter ()}, nil
307
+ }
308
+ return ctrl.Result {}, err
311
309
}
312
- return ctrl.Result {}, err
313
- }
314
310
315
- joindata , err := kubeadmv1beta1 .ConfigurationToYAML (config .Spec .JoinConfiguration )
316
- if err != nil {
317
- log .Error (err , "failed to marshal join configuration" )
318
- return ctrl.Result {}, err
319
- }
311
+ joindata , err := kubeadmv1beta1 .ConfigurationToYAML (config .Spec .JoinConfiguration )
312
+ if err != nil {
313
+ log .Error (err , "failed to marshal join configuration" )
314
+ return ctrl.Result {}, err
315
+ }
316
+
317
+ // it's a control plane join
318
+ if util .IsControlPlaneMachine (machine ) {
319
+ if config .Spec .JoinConfiguration .ControlPlane == nil {
320
+ return ctrl.Result {}, errors .New ("Machine is a ControlPlane, but JoinConfiguration.ControlPlane is not set in the KubeadmConfig object" )
321
+ }
322
+
323
+ cloudJoinData , err := cloudinit .NewJoinControlPlane (& cloudinit.ControlPlaneJoinInput {
324
+ JoinConfiguration : joindata ,
325
+ Certificates : certificates ,
326
+ BaseUserData : cloudinit.BaseUserData {
327
+ AdditionalFiles : config .Spec .Files ,
328
+ NTP : config .Spec .NTP ,
329
+ PreKubeadmCommands : config .Spec .PreKubeadmCommands ,
330
+ PostKubeadmCommands : config .Spec .PostKubeadmCommands ,
331
+ Users : config .Spec .Users ,
332
+ },
333
+ })
334
+ if err != nil {
335
+ log .Error (err , "failed to create a control plane join configuration" )
336
+ return ctrl.Result {}, err
337
+ }
320
338
321
- // it's a control plane join
322
- if util .IsControlPlaneMachine (machine ) {
323
- if config .Spec .JoinConfiguration .ControlPlane == nil {
324
- return ctrl.Result {}, errors .New ("Machine is a ControlPlane, but JoinConfiguration.ControlPlane is not set in the KubeadmConfig object" )
339
+ config .Status .BootstrapData = cloudJoinData
340
+ config .Status .Ready = true
341
+ return ctrl.Result {}, nil
325
342
}
326
343
327
- cloudJoinData , err := cloudinit .NewJoinControlPlane (& cloudinit.ControlPlaneJoinInput {
328
- JoinConfiguration : joindata ,
329
- Certificates : certificates ,
344
+ // otherwise it is a node
345
+ if config .Spec .JoinConfiguration .ControlPlane != nil {
346
+ return ctrl.Result {}, errors .New ("Machine is a Worker, but JoinConfiguration.ControlPlane is set in the KubeadmConfig object" )
347
+ }
348
+
349
+ cloudJoinData , err := cloudinit .NewNode (& cloudinit.NodeInput {
330
350
BaseUserData : cloudinit.BaseUserData {
331
351
AdditionalFiles : config .Spec .Files ,
332
352
NTP : config .Spec .NTP ,
333
353
PreKubeadmCommands : config .Spec .PreKubeadmCommands ,
334
354
PostKubeadmCommands : config .Spec .PostKubeadmCommands ,
335
355
Users : config .Spec .Users ,
336
356
},
357
+ JoinConfiguration : joindata ,
337
358
})
338
359
if err != nil {
339
- log .Error (err , "failed to create a control plane join configuration" )
360
+ log .Error (err , "failed to create a worker join configuration" )
340
361
return ctrl.Result {}, err
341
362
}
342
-
343
363
config .Status .BootstrapData = cloudJoinData
344
364
config .Status .Ready = true
345
365
return ctrl.Result {}, nil
346
366
}
347
-
348
- // otherwise it is a node
349
- if config .Spec .JoinConfiguration .ControlPlane != nil {
350
- return ctrl.Result {}, errors .New ("Machine is a Worker, but JoinConfiguration.ControlPlane is set in the KubeadmConfig object" )
351
- }
352
-
353
- cloudJoinData , err := cloudinit .NewNode (& cloudinit.NodeInput {
354
- BaseUserData : cloudinit.BaseUserData {
355
- AdditionalFiles : config .Spec .Files ,
356
- NTP : config .Spec .NTP ,
357
- PreKubeadmCommands : config .Spec .PreKubeadmCommands ,
358
- PostKubeadmCommands : config .Spec .PostKubeadmCommands ,
359
- Users : config .Spec .Users ,
360
- },
361
- JoinConfiguration : joindata ,
362
- })
363
- if err != nil {
364
- log .Error (err , "failed to create a worker join configuration" )
365
- return ctrl.Result {}, err
366
- }
367
- config .Status .BootstrapData = cloudJoinData
368
- config .Status .Ready = true
369
- return ctrl.Result {}, nil
370
367
}
371
368
372
369
// ClusterToKubeadmConfigs is a handler.ToRequestsFunc to be used to enqeue
0 commit comments