Skip to content

Commit d8cd89f

Browse files
committed
fixup
1 parent 1b10a4f commit d8cd89f

File tree

2 files changed

+40
-31
lines changed

2 files changed

+40
-31
lines changed

internal/controllers/machineset/machineset_controller.go

+39-29
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (r *Reconciler) SetupWithManager(ctx context.Context, mgr ctrl.Manager, opt
143143
return nil
144144
}
145145

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) {
147147
machineSet := &clusterv1.MachineSet{}
148148
if err := r.Client.Get(ctx, req.NamespacedName, machineSet); err != nil {
149149
if apierrors.IsNotFound(err) {
@@ -190,6 +190,15 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
190190
defer func() {
191191
r.reconcileStatus(ctx, s)
192192

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+
193202
// Always attempt to patch the object and status after each reconciliation.
194203
if err := patchMachineSet(ctx, patchHelper, s.machineSet); err != nil {
195204
reterr = kerrors.NewAggregate([]error{reterr, err})
@@ -207,7 +216,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
207216
reconcileDelete := append(
208217
alwaysReconcile,
209218
wrapErrMachineSetReconcileFunc(r.reconcileDelete, "failed to reconcile delete"),
210-
wrapErrMachineSetReconcileFunc(r.updateStatus, "failed to update status"),
211219
)
212220
return doReconcile(ctx, s, reconcileDelete)
213221
}
@@ -220,8 +228,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
220228
}
221229

222230
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"),
225232
)
226233
reconcileNormal = append(reconcileNormal,
227234
alwaysReconcile...,
@@ -230,7 +237,6 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (_ ctrl.Re
230237
wrapErrMachineSetReconcileFunc(r.reconcileUnhealthyMachines, "failed to reconcile unhealthy machines"),
231238
wrapErrMachineSetReconcileFunc(r.syncMachines, "failed to sync machines"),
232239
wrapErrMachineSetReconcileFunc(r.syncReplicas, "failed to sync replicas"),
233-
wrapErrMachineSetReconcileFunc(r.updateStatus, "failed to update status"),
234240
)
235241

236242
result, err := doReconcile(ctx, s, reconcileNormal)
@@ -297,13 +303,24 @@ func patchMachineSet(ctx context.Context, patchHelper *patch.Helper, machineSet
297303
return patchHelper.Patch(ctx, machineSet, options...)
298304
}
299305

300-
func (r *Reconciler) setClusterLabels(_ context.Context, s *scope) (ctrl.Result, error) {
306+
func (r *Reconciler) reconcileMachineSetOwnerAndLabels(_ context.Context, s *scope) (ctrl.Result, error) {
301307
// Reconcile and retrieve the Cluster object.
302308
if s.machineSet.Labels == nil {
303309
s.machineSet.Labels = make(map[string]string)
304310
}
305311
s.machineSet.Labels[clusterv1.ClusterNameLabel] = s.machineSet.Spec.ClusterName
306312

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+
307324
// Make sure selector and template to be in the same cluster.
308325
if s.machineSet.Spec.Selector.MatchLabels == nil {
309326
s.machineSet.Spec.Selector.MatchLabels = make(map[string]string)
@@ -319,21 +336,6 @@ func (r *Reconciler) setClusterLabels(_ context.Context, s *scope) (ctrl.Result,
319336
return ctrl.Result{}, nil
320337
}
321338

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-
337339
func (r *Reconciler) reconcileInfrastructure(ctx context.Context, s *scope) (ctrl.Result, error) {
338340
// Make sure to reconcile the external infrastructure reference.
339341
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 {
974976
return !r.isDeploymentChild(ms)
975977
}
976978

977-
// updateStatus updates the Status field for the MachineSet
979+
// reconcileV1Beta1Status updates the Status field for the MachineSet
978980
// 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 {
980982
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")
982984
}
983985

984986
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")
986988
}
987989

988990
log := ctrl.LoggerFrom(ctx)
@@ -992,7 +994,7 @@ func (r *Reconciler) updateStatus(ctx context.Context, s *scope) (ctrl.Result, e
992994
// This is necessary for CRDs including scale subresources.
993995
selector, err := metav1.LabelSelectorAsSelector(&s.machineSet.Spec.Selector)
994996
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)
996998
}
997999
newStatus.Selector = selector.String()
9981000

@@ -1085,6 +1087,10 @@ func (r *Reconciler) updateStatus(ctx context.Context, s *scope) (ctrl.Result, e
10851087
// source ref (reason@machine/name) so the problem can be easily tracked down to its source machine.
10861088
conditions.SetAggregate(s.machineSet, clusterv1.MachinesReadyCondition, collections.FromMachines(s.machines...).ConditionGetters(), conditions.AddSourceRef())
10871089

1090+
return nil
1091+
}
1092+
1093+
func requeueAfterWaitingForNodes(s *scope) ctrl.Result {
10881094
var replicas int32
10891095
if s.machineSet.Spec.Replicas != nil {
10901096
replicas = *s.machineSet.Spec.Replicas
@@ -1101,15 +1107,15 @@ func (r *Reconciler) updateStatus(ctx context.Context, s *scope) (ctrl.Result, e
11011107
s.machineSet.Status.ReadyReplicas == replicas &&
11021108
s.machineSet.Status.AvailableReplicas != replicas {
11031109
minReadyResult := ctrl.Result{RequeueAfter: time.Duration(s.machineSet.Spec.MinReadySeconds) * time.Second}
1104-
return minReadyResult, nil
1110+
return minReadyResult
11051111
}
11061112

11071113
// Quickly reconcile until the nodes become Ready.
11081114
if s.machineSet.Status.ReadyReplicas != replicas {
1109-
return ctrl.Result{RequeueAfter: 15 * time.Second}, nil
1115+
return ctrl.Result{RequeueAfter: 15 * time.Second}
11101116
}
11111117

1112-
return ctrl.Result{}, nil
1118+
return ctrl.Result{}
11131119
}
11141120

11151121
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
12681274
obj, err := external.Get(ctx, r.Client, ref, cluster.Namespace)
12691275
if err != nil {
12701276
if apierrors.IsNotFound(err) {
1277+
if !ms.DeletionTimestamp.IsZero() {
1278+
// Tolerate object not found when the machineSet is being deleted.
1279+
return nil
1280+
}
12711281
if _, ok := ms.Labels[clusterv1.MachineDeploymentNameLabel]; !ok {
12721282
// If the MachineSet is not in a MachineDeployment, return the error immediately.
12731283
return err

internal/controllers/machineset/machineset_controller_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1038,8 +1038,7 @@ func TestMachineSetReconciler_updateStatusResizedCondition(t *testing.T) {
10381038
machineSet: tc.machineSet,
10391039
machines: tc.machines,
10401040
}
1041-
_, err := msr.updateStatus(ctx, s)
1042-
g.Expect(err).ToNot(HaveOccurred())
1041+
g.Expect(msr.reconcileV1Beta1Status(ctx, s)).To(Succeed())
10431042
gotCond := conditions.Get(tc.machineSet, clusterv1.ResizedCondition)
10441043
g.Expect(gotCond).ToNot(BeNil())
10451044
g.Expect(gotCond.Status).To(Equal(corev1.ConditionFalse))

0 commit comments

Comments
 (0)