Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit 794be6e

Browse files
committed
refactor: move to a switch/case statement
1 parent bb188c0 commit 794be6e

File tree

1 file changed

+88
-91
lines changed

1 file changed

+88
-91
lines changed

controllers/kubeadmconfig_controller.go

Lines changed: 88 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,23 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
140140
return ctrl.Result{}, err
141141
}
142142

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 {
143158
// 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:
145160
token := config.Spec.JoinConfiguration.Discovery.BootstrapToken.Token
146161

147162
// gets the remote secret interface client for the current cluster
@@ -157,29 +172,11 @@ func (r *KubeadmConfigReconciler) Reconcile(req ctrl.Request) (_ ctrl.Result, re
157172
return ctrl.Result{}, errors.Wrapf(err, "failed to refresh bootstrap token")
158173
}
159174
return ctrl.Result{}, nil
160-
}
161-
162175
// Wait patiently for the infrastructure to be ready
163-
if !cluster.Status.InfrastructureReady {
176+
case !cluster.Status.InfrastructureReady:
164177
log.Info("Infrastructure is not ready, waiting until ready.")
165178
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:
183180
// if it's NOT a control plane machine, requeue
184181
if !util.IsControlPlaneMachine(machine) {
185182
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
271268
config.Status.Ready = true
272269

273270
return ctrl.Result{}, nil
274-
}
275-
276271
// 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
278274

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)
281277

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+
}
285281

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+
}
294290

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
305301

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
311309
}
312-
return ctrl.Result{}, err
313-
}
314310

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+
}
320338

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
325342
}
326343

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{
330350
BaseUserData: cloudinit.BaseUserData{
331351
AdditionalFiles: config.Spec.Files,
332352
NTP: config.Spec.NTP,
333353
PreKubeadmCommands: config.Spec.PreKubeadmCommands,
334354
PostKubeadmCommands: config.Spec.PostKubeadmCommands,
335355
Users: config.Spec.Users,
336356
},
357+
JoinConfiguration: joindata,
337358
})
338359
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")
340361
return ctrl.Result{}, err
341362
}
342-
343363
config.Status.BootstrapData = cloudJoinData
344364
config.Status.Ready = true
345365
return ctrl.Result{}, nil
346366
}
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
370367
}
371368

372369
// ClusterToKubeadmConfigs is a handler.ToRequestsFunc to be used to enqeue

0 commit comments

Comments
 (0)