@@ -143,7 +143,7 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
143
143
return nil
144
144
}
145
145
146
- func (r * Reconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (_ ctrl.Result , reterr error ) {
146
+ func (r * Reconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (retres ctrl.Result , reterr error ) {
147
147
machineSet := & clusterv1.MachineSet {}
148
148
if err := r .Client .Get (ctx , req .NamespacedName , machineSet ); err != nil {
149
149
if apierrors .IsNotFound (err ) {
@@ -190,6 +190,15 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
190
190
defer func () {
191
191
r .reconcileStatus (ctx , s )
192
192
193
+ if err := r .reconcileV1Beta1Status (ctx , s ); err != nil {
194
+ reterr = kerrors .NewAggregate ([]error {reterr , errors .Wrapf (err , "failed to update v1beta1 status" )})
195
+ }
196
+
197
+ // Adjust requeue for scaleups
198
+ if s .machineSet .DeletionTimestamp .IsZero () && reterr == nil {
199
+ retres = util .LowestNonZeroResult (retres , requeueAfterWaitingForNodes (s ))
200
+ }
201
+
193
202
// Always attempt to patch the object and status after each reconciliation.
194
203
if err := patchMachineSet (ctx , patchHelper , s .machineSet ); err != nil {
195
204
reterr = kerrors .NewAggregate ([]error {reterr , err })
@@ -207,7 +216,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
207
216
reconcileDelete := append (
208
217
alwaysReconcile ,
209
218
wrapErrMachineSetReconcileFunc (r .reconcileDelete , "failed to reconcile delete" ),
210
- wrapErrMachineSetReconcileFunc (r .updateStatus , "failed to update status" ),
211
219
)
212
220
return doReconcile (ctx , s , reconcileDelete )
213
221
}
@@ -220,8 +228,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
220
228
}
221
229
222
230
reconcileNormal := append ([]machineSetReconcileFunc {},
223
- wrapErrMachineSetReconcileFunc (r .setClusterLabels , "failed to set cluster labels" ),
224
- wrapErrMachineSetReconcileFunc (r .ensureOwnerReference , "failed to ensure ownerReference" ),
231
+ wrapErrMachineSetReconcileFunc (r .reconcileMachineSetOwnerAndLabels , "failed to set cluster labels" ),
225
232
)
226
233
reconcileNormal = append (reconcileNormal ,
227
234
alwaysReconcile ... ,
@@ -230,7 +237,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
230
237
wrapErrMachineSetReconcileFunc (r .reconcileUnhealthyMachines , "failed to reconcile unhealthy machines" ),
231
238
wrapErrMachineSetReconcileFunc (r .syncMachines , "failed to sync machines" ),
232
239
wrapErrMachineSetReconcileFunc (r .syncReplicas , "failed to sync replicas" ),
233
- wrapErrMachineSetReconcileFunc (r .updateStatus , "failed to update status" ),
234
240
)
235
241
236
242
result , err := doReconcile (ctx , s , reconcileNormal )
@@ -297,13 +303,24 @@ func patchMachineSet(ctx context.Context, patchHelper *patch.Helper, machineSet
297
303
return patchHelper .Patch (ctx , machineSet , options ... )
298
304
}
299
305
300
- func (r * Reconciler ) setClusterLabels (_ context.Context , s * scope ) (ctrl.Result , error ) {
306
+ func (r * Reconciler ) reconcileMachineSetOwnerAndLabels (_ context.Context , s * scope ) (ctrl.Result , error ) {
301
307
// Reconcile and retrieve the Cluster object.
302
308
if s .machineSet .Labels == nil {
303
309
s .machineSet .Labels = make (map [string ]string )
304
310
}
305
311
s .machineSet .Labels [clusterv1 .ClusterNameLabel ] = s .machineSet .Spec .ClusterName
306
312
313
+ // If the machine set is a stand alone one, meaning not originated from a MachineDeployment, then set it as directly
314
+ // owned by the Cluster (if not already present).
315
+ if r .shouldAdopt (s .machineSet ) {
316
+ s .machineSet .SetOwnerReferences (util .EnsureOwnerRef (s .machineSet .GetOwnerReferences (), metav1.OwnerReference {
317
+ APIVersion : clusterv1 .GroupVersion .String (),
318
+ Kind : "Cluster" ,
319
+ Name : s .cluster .Name ,
320
+ UID : s .cluster .UID ,
321
+ }))
322
+ }
323
+
307
324
// Make sure selector and template to be in the same cluster.
308
325
if s .machineSet .Spec .Selector .MatchLabels == nil {
309
326
s .machineSet .Spec .Selector .MatchLabels = make (map [string ]string )
@@ -319,21 +336,6 @@ func (r *Reconciler) setClusterLabels(_ context.Context, s *scope) (ctrl.Result,
319
336
return ctrl.Result {}, nil
320
337
}
321
338
322
- func (r * Reconciler ) ensureOwnerReference (_ context.Context , s * scope ) (ctrl.Result , error ) {
323
- // If the machine set is a stand alone one, meaning not originated from a MachineDeployment, then set it as directly
324
- // owned by the Cluster (if not already present).
325
- if r .shouldAdopt (s .machineSet ) {
326
- s .machineSet .SetOwnerReferences (util .EnsureOwnerRef (s .machineSet .GetOwnerReferences (), metav1.OwnerReference {
327
- APIVersion : clusterv1 .GroupVersion .String (),
328
- Kind : "Cluster" ,
329
- Name : s .cluster .Name ,
330
- UID : s .cluster .UID ,
331
- }))
332
- }
333
-
334
- return ctrl.Result {}, nil
335
- }
336
-
337
339
func (r * Reconciler ) reconcileInfrastructure (ctx context.Context , s * scope ) (ctrl.Result , error ) {
338
340
// Make sure to reconcile the external infrastructure reference.
339
341
if err := r .reconcileExternalTemplateReference (ctx , s .cluster , s .machineSet , & s .machineSet .Spec .Template .Spec .InfrastructureRef ); err != nil {
@@ -974,15 +976,15 @@ func (r *Reconciler) shouldAdopt(ms *clusterv1.MachineSet) bool {
974
976
return ! r .isDeploymentChild (ms )
975
977
}
976
978
977
- // updateStatus updates the Status field for the MachineSet
979
+ // reconcileV1Beta1Status updates the Status field for the MachineSet
978
980
// It checks for the current state of the replicas and updates the Status of the MachineSet.
979
- func (r * Reconciler ) updateStatus (ctx context.Context , s * scope ) (ctrl. Result , error ) {
981
+ func (r * Reconciler ) reconcileV1Beta1Status (ctx context.Context , s * scope ) error {
980
982
if s .machines == nil {
981
- return ctrl. Result {}, errors .New ("Cannot update status when s.machines is nil" )
983
+ return errors .New ("Cannot update status when s.machines is nil" )
982
984
}
983
985
984
986
if s .machineSet .Spec .Replicas == nil {
985
- return ctrl. Result {}, errors .New ("Cannot update status when s.machineSet.Spec.Replicas is nil" )
987
+ return errors .New ("Cannot update status when s.machineSet.Spec.Replicas is nil" )
986
988
}
987
989
988
990
log := ctrl .LoggerFrom (ctx )
@@ -992,7 +994,7 @@ func (r *Reconciler) updateStatus(ctx context.Context, s *scope) (ctrl.Result, e
992
994
// This is necessary for CRDs including scale subresources.
993
995
selector , err := metav1 .LabelSelectorAsSelector (& s .machineSet .Spec .Selector )
994
996
if err != nil {
995
- return ctrl. Result {}, errors .Wrapf (err , "failed to update status for MachineSet %s/%s" , s .machineSet .Namespace , s .machineSet .Name )
997
+ return errors .Wrapf (err , "failed to update status for MachineSet %s/%s" , s .machineSet .Namespace , s .machineSet .Name )
996
998
}
997
999
newStatus .Selector = selector .String ()
998
1000
@@ -1085,6 +1087,10 @@ func (r *Reconciler) updateStatus(ctx context.Context, s *scope) (ctrl.Result, e
1085
1087
// source ref (reason@machine/name) so the problem can be easily tracked down to its source machine.
1086
1088
conditions .SetAggregate (s .machineSet , clusterv1 .MachinesReadyCondition , collections .FromMachines (s .machines ... ).ConditionGetters (), conditions .AddSourceRef ())
1087
1089
1090
+ return nil
1091
+ }
1092
+
1093
+ func requeueAfterWaitingForNodes (s * scope ) ctrl.Result {
1088
1094
var replicas int32
1089
1095
if s .machineSet .Spec .Replicas != nil {
1090
1096
replicas = * s .machineSet .Spec .Replicas
@@ -1101,15 +1107,15 @@ func (r *Reconciler) updateStatus(ctx context.Context, s *scope) (ctrl.Result, e
1101
1107
s .machineSet .Status .ReadyReplicas == replicas &&
1102
1108
s .machineSet .Status .AvailableReplicas != replicas {
1103
1109
minReadyResult := ctrl.Result {RequeueAfter : time .Duration (s .machineSet .Spec .MinReadySeconds ) * time .Second }
1104
- return minReadyResult , nil
1110
+ return minReadyResult
1105
1111
}
1106
1112
1107
1113
// Quickly reconcile until the nodes become Ready.
1108
1114
if s .machineSet .Status .ReadyReplicas != replicas {
1109
- return ctrl.Result {RequeueAfter : 15 * time .Second }, nil
1115
+ return ctrl.Result {RequeueAfter : 15 * time .Second }
1110
1116
}
1111
1117
1112
- return ctrl.Result {}, nil
1118
+ return ctrl.Result {}
1113
1119
}
1114
1120
1115
1121
func (r * Reconciler ) getMachineNode (ctx context.Context , cluster * clusterv1.Cluster , machine * clusterv1.Machine ) (* corev1.Node , error ) {
@@ -1268,6 +1274,10 @@ func (r *Reconciler) reconcileExternalTemplateReference(ctx context.Context, clu
1268
1274
obj , err := external .Get (ctx , r .Client , ref , cluster .Namespace )
1269
1275
if err != nil {
1270
1276
if apierrors .IsNotFound (err ) {
1277
+ if ! ms .DeletionTimestamp .IsZero () {
1278
+ // Tolerate object not found when the machineSet is being deleted.
1279
+ return nil
1280
+ }
1271
1281
if _ , ok := ms .Labels [clusterv1 .MachineDeploymentNameLabel ]; ! ok {
1272
1282
// If the MachineSet is not in a MachineDeployment, return the error immediately.
1273
1283
return err
0 commit comments