@@ -186,60 +186,64 @@ func (r *Reconciler) getCurrentMachineDeploymentState(ctx context.Context, bluep
186
186
}
187
187
188
188
// Make sure that the name of the MachineDeployment stays unique.
189
- // If we've already have seen a MachineDeployment with the same name
189
+ // If we've already seen a MachineDeployment with the same name
190
190
// this is an error, probably caused from manual modifications or a race condition.
191
191
if _ , ok := state [mdTopologyName ]; ok {
192
192
return nil , fmt .Errorf ("duplicate %s found for label %s: %s" , tlog.KObj {Obj : m }, clusterv1 .ClusterTopologyMachineDeploymentLabelName , mdTopologyName )
193
193
}
194
194
195
- mdClassName := getMDClassName (cluster , mdTopologyName )
196
- if mdClassName == "" {
197
- return nil , fmt .Errorf ("failed to find MachineDeployment topology %s in %s" , mdTopologyName , tlog.KObj {Obj : cluster })
198
- }
199
-
200
- mdBluePrint , ok := blueprintMachineDeployments [mdClassName ]
201
- if ! ok {
202
- return nil , fmt .Errorf ("failed to find MachineDeployment class %s in ClusterClass" , mdClassName )
203
- }
204
-
205
- // Gets the BootstrapTemplate
195
+ // Gets the bootstrapRef.
206
196
bootstrapRef := m .Spec .Template .Spec .Bootstrap .ConfigRef
207
197
if bootstrapRef == nil {
208
198
return nil , fmt .Errorf ("%s does not have a reference to a Bootstrap Config" , tlog.KObj {Obj : m })
209
199
}
210
- ref , err := alignRefAPIVersion (mdBluePrint .BootstrapTemplate , bootstrapRef )
211
- if err != nil {
212
- return nil , errors .Wrap (err , fmt .Sprintf ("%s Bootstrap reference could not be retrieved" , tlog.KObj {Obj : m }))
200
+ // Gets the infraRef.
201
+ infraRef := & m .Spec .Template .Spec .InfrastructureRef
202
+ if infraRef .Name == "" {
203
+ return nil , fmt .Errorf ("%s does not have a reference to a InfrastructureMachineTemplate" , tlog.KObj {Obj : m })
204
+ }
205
+
206
+ // If the mdTopology exists in the Cluster, lookup the corresponding mdBluePrint and align
207
+ // the apiVersions in the bootstrapRef and infraRef.
208
+ // If the mdTopology doesn't exist, do nothing (this can happen if the mdTopology was deleted).
209
+ // **Note** We can't check if the MachineDeployment has a DeletionTimestamp, because at this point it could not be set yet.
210
+ if mdTopologyExistsInCluster , mdClassName := getMDClassName (cluster , mdTopologyName ); mdTopologyExistsInCluster {
211
+ mdBluePrint , ok := blueprintMachineDeployments [mdClassName ]
212
+ if ! ok {
213
+ return nil , fmt .Errorf ("failed to find MachineDeployment class %s in ClusterClass" , mdClassName )
214
+ }
215
+ bootstrapRef , err = alignRefAPIVersion (mdBluePrint .BootstrapTemplate , bootstrapRef )
216
+ if err != nil {
217
+ return nil , errors .Wrap (err , fmt .Sprintf ("%s Bootstrap reference could not be retrieved" , tlog.KObj {Obj : m }))
218
+ }
219
+ infraRef , err = alignRefAPIVersion (mdBluePrint .InfrastructureMachineTemplate , infraRef )
220
+ if err != nil {
221
+ return nil , errors .Wrap (err , fmt .Sprintf ("%s Infrastructure reference could not be retrieved" , tlog.KObj {Obj : m }))
222
+ }
213
223
}
214
- b , err := r .getReference (ctx , ref )
224
+
225
+ // Get the BootstrapTemplate.
226
+ bootstrapTemplate , err := r .getReference (ctx , bootstrapRef )
215
227
if err != nil {
216
228
return nil , errors .Wrap (err , fmt .Sprintf ("%s Bootstrap reference could not be retrieved" , tlog.KObj {Obj : m }))
217
229
}
218
230
// check that the referenced object has the ClusterTopologyOwnedLabel label.
219
231
// Nb. This is to make sure that a managed topology cluster does not have a reference to an object that is not
220
232
// owned by the topology.
221
- if ! labels .IsTopologyOwned (b ) {
222
- return nil , fmt .Errorf ("BootstrapTemplate object %s referenced from MD %s is not topology owned" , tlog.KObj {Obj : b }, tlog.KObj {Obj : m })
233
+ if ! labels .IsTopologyOwned (bootstrapTemplate ) {
234
+ return nil , fmt .Errorf ("BootstrapTemplate object %s referenced from MD %s is not topology owned" , tlog.KObj {Obj : bootstrapTemplate }, tlog.KObj {Obj : m })
223
235
}
224
236
225
- // Gets the InfrastructureMachineTemplate
226
- infraRef := m .Spec .Template .Spec .InfrastructureRef
227
- if infraRef .Name == "" {
228
- return nil , fmt .Errorf ("%s does not have a reference to a InfrastructureMachineTemplate" , tlog.KObj {Obj : m })
229
- }
230
- ref , err = alignRefAPIVersion (mdBluePrint .InfrastructureMachineTemplate , & infraRef )
231
- if err != nil {
232
- return nil , errors .Wrap (err , fmt .Sprintf ("%s Infrastructure reference could not be retrieved" , tlog.KObj {Obj : m }))
233
- }
234
- infra , err := r .getReference (ctx , ref )
237
+ // Get the InfraMachineTemplate.
238
+ infraMachineTemplate , err := r .getReference (ctx , infraRef )
235
239
if err != nil {
236
240
return nil , errors .Wrap (err , fmt .Sprintf ("%s Infrastructure reference could not be retrieved" , tlog.KObj {Obj : m }))
237
241
}
238
242
// check that the referenced object has the ClusterTopologyOwnedLabel label.
239
243
// Nb. This is to make sure that a managed topology cluster does not have a reference to an object that is not
240
244
// owned by the topology.
241
- if ! labels .IsTopologyOwned (infra ) {
242
- return nil , fmt .Errorf ("InfrastructureMachineTemplate object %s referenced from MD %s is not topology owned" , tlog.KObj {Obj : infra }, tlog.KObj {Obj : m })
245
+ if ! labels .IsTopologyOwned (infraMachineTemplate ) {
246
+ return nil , fmt .Errorf ("InfrastructureMachineTemplate object %s referenced from MD %s is not topology owned" , tlog.KObj {Obj : infraMachineTemplate }, tlog.KObj {Obj : m })
243
247
}
244
248
245
249
// Gets the MachineHealthCheck.
@@ -257,8 +261,8 @@ func (r *Reconciler) getCurrentMachineDeploymentState(ctx context.Context, bluep
257
261
258
262
state [mdTopologyName ] = & scope.MachineDeploymentState {
259
263
Object : m ,
260
- BootstrapTemplate : b ,
261
- InfrastructureMachineTemplate : infra ,
264
+ BootstrapTemplate : bootstrapTemplate ,
265
+ InfrastructureMachineTemplate : infraMachineTemplate ,
262
266
MachineHealthCheck : mhc ,
263
267
}
264
268
}
@@ -293,15 +297,15 @@ func alignRefAPIVersion(templateFromClusterClass *unstructured.Unstructured, cur
293
297
}
294
298
295
299
// getMDClassName retrieves the MDClass name by looking up the MDTopology in the Cluster.
296
- func getMDClassName (cluster * clusterv1.Cluster , mdTopologyName string ) string {
300
+ func getMDClassName (cluster * clusterv1.Cluster , mdTopologyName string ) ( bool , string ) {
297
301
if cluster .Spec .Topology .Workers == nil {
298
- return ""
302
+ return false , ""
299
303
}
300
304
301
305
for _ , mdTopology := range cluster .Spec .Topology .Workers .MachineDeployments {
302
306
if mdTopology .Name == mdTopologyName {
303
- return mdTopology .Class
307
+ return true , mdTopology .Class
304
308
}
305
309
}
306
- return ""
310
+ return false , ""
307
311
}
0 commit comments