Skip to content

Commit e53b930

Browse files
Adoption for stand-alone objects should ignore objects generated by MachineDeployments
1 parent bda7e5c commit e53b930

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

internal/controllers/machine/machine_controller.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster,
260260
return ctrl.Result{}, err
261261
}
262262

263-
// If the Machine belongs to a cluster, add an owner reference.
263+
// If the machine is a stand-alone one, meaning not originated from a MachineDeployment, then set it as directly
264+
// owned by the Cluster (if not already present).
264265
if r.shouldAdopt(m) {
265266
m.OwnerReferences = util.EnsureOwnerRef(m.OwnerReferences, metav1.OwnerReference{
266267
APIVersion: clusterv1.GroupVersion.String(),
@@ -741,8 +742,20 @@ func (r *Reconciler) reconcileDeleteExternal(ctx context.Context, m *clusterv1.M
741742
return obj, nil
742743
}
743744

745+
// shouldAdopt returns true if the Machine should be adopted as a stand-alone Machine directly owned by the Cluster.
744746
func (r *Reconciler) shouldAdopt(m *clusterv1.Machine) bool {
745-
return metav1.GetControllerOf(m) == nil && !util.HasOwner(m.OwnerReferences, clusterv1.GroupVersion.String(), []string{"Cluster"})
747+
// if the machine is controlled by something (MS or KCP), or if it is a stand-alone machine directly owned by the Cluster, then no-op.
748+
if metav1.GetControllerOf(m) != nil || util.HasOwner(m.OwnerReferences, clusterv1.GroupVersion.String(), []string{"Cluster"}) {
749+
return false
750+
}
751+
752+
// If the Machine is originated by a MachineDeployment, this prevents it from being adopted as a stand-alone Machine.
753+
// Note: this is required because after restore from a backup both the Machine controller and the
754+
// MachineSet controller are racing to adopt Machines, see https://github.com/kubernetes-sigs/cluster-api/issues/7529
755+
if _, ok := m.Labels[clusterv1.MachineDeploymentUniqueLabel]; ok {
756+
return false
757+
}
758+
return true
746759
}
747760

748761
func (r *Reconciler) watchClusterNodes(ctx context.Context, cluster *clusterv1.Cluster) error {

internal/controllers/machineset/machineset_controller.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ func (r *Reconciler) reconcile(ctx context.Context, cluster *clusterv1.Cluster,
209209
}
210210
machineSet.Labels[clusterv1.ClusterLabelName] = machineSet.Spec.ClusterName
211211

212+
// If the machine set is a stand alone one, meaning not originated from a MachineDeployment, then set it as directly
213+
// owned by the Cluster (if not already present).
212214
if r.shouldAdopt(machineSet) {
213215
machineSet.OwnerReferences = util.EnsureOwnerRef(machineSet.OwnerReferences, metav1.OwnerReference{
214216
APIVersion: clusterv1.GroupVersion.String(),
@@ -635,8 +637,20 @@ func (r *Reconciler) getMachineSetsForMachine(ctx context.Context, m *clusterv1.
635637
return mss, nil
636638
}
637639

640+
// shouldAdopt returns true if the MachineSet should be adopted as a stand-alone MachineSet directly owned by the Cluster.
638641
func (r *Reconciler) shouldAdopt(ms *clusterv1.MachineSet) bool {
639-
return !util.HasOwner(ms.OwnerReferences, clusterv1.GroupVersion.String(), []string{"MachineDeployment", "Cluster"})
642+
// if the MachineSet is controlled by a MachineDeployment, or if it is a stand-alone MachinesSet directly owned by the Cluster, then no-op.
643+
if util.HasOwner(ms.OwnerReferences, clusterv1.GroupVersion.String(), []string{"MachineDeployment", "Cluster"}) {
644+
return false
645+
}
646+
647+
// If the MachineSet is originated by a MachineDeployment, this prevents it from being adopted as a stand-alone MachineSet.
648+
// Note: this is required because after restore from a backup both the MachineSet controller and the
649+
// MachineDeployment controller are racing to adopt MachineSets, see https://github.com/kubernetes-sigs/cluster-api/issues/7529
650+
if _, ok := ms.Labels[clusterv1.MachineDeploymentUniqueLabel]; ok {
651+
return false
652+
}
653+
return true
640654
}
641655

642656
// updateStatus updates the Status field for the MachineSet

0 commit comments

Comments
 (0)