Skip to content

Commit

Permalink
Prevent orphaned InfrastructureMachines
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziopandini committed Dec 14, 2021
1 parent 2618fea commit e8592c3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
12 changes: 12 additions & 0 deletions controllers/machineset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,12 @@ func (r *MachineSetReconciler) syncReplicas(ctx context.Context, ms *clusterv1.M
ClusterName: machine.Spec.ClusterName,
Labels: machine.Labels,
Annotations: machine.Annotations,
OwnerRef: &metav1.OwnerReference{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "MachineSet",
Name: ms.Name,
UID: ms.UID,
},
})
if err != nil {
conditions.MarkFalse(ms, clusterv1.MachinesCreatedCondition, clusterv1.BootstrapTemplateCloningFailedReason, clusterv1.ConditionSeverityError, err.Error())
Expand All @@ -391,6 +397,12 @@ func (r *MachineSetReconciler) syncReplicas(ctx context.Context, ms *clusterv1.M
ClusterName: machine.Spec.ClusterName,
Labels: machine.Labels,
Annotations: machine.Annotations,
OwnerRef: &metav1.OwnerReference{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "MachineSet",
Name: ms.Name,
UID: ms.UID,
},
})
if err != nil {
conditions.MarkFalse(ms, clusterv1.MachinesCreatedCondition, clusterv1.InfrastructureTemplateCloningFailedReason, clusterv1.ConditionSeverityError, err.Error())
Expand Down
22 changes: 18 additions & 4 deletions controllers/machineset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,13 @@ func TestMachineSetReconciler(t *testing.T) {
g.Expect(im.GetAnnotations()).To(HaveKeyWithValue("annotation-1", "true"), "have annotations of MachineTemplate applied")
g.Expect(im.GetAnnotations()).To(HaveKeyWithValue("precedence", "MachineSet"), "the annotations from the MachineSpec template to overwrite the infrastructure template ones")
g.Expect(im.GetLabels()).To(HaveKeyWithValue("label-1", "true"), "have labels of MachineTemplate applied")
hasMSOwnerRef := false
for _, o := range im.GetOwnerReferences() {
if o.Kind == machineSetKind.Kind {
hasMSOwnerRef = true
}
}
g.Expect(hasMSOwnerRef).To(BeTrue(), "have ownerRef to MachineSet")
}

t.Log("Creating a BootstrapConfig for each Machine")
Expand All @@ -244,6 +251,13 @@ func TestMachineSetReconciler(t *testing.T) {
g.Expect(im.GetAnnotations()).To(HaveKeyWithValue("annotation-1", "true"), "have annotations of MachineTemplate applied")
g.Expect(im.GetAnnotations()).To(HaveKeyWithValue("precedence", "MachineSet"), "the annotations from the MachineSpec template to overwrite the bootstrap config template ones")
g.Expect(im.GetLabels()).To(HaveKeyWithValue("label-1", "true"), "have labels of MachineTemplate applied")
hasMSOwnerRef := false
for _, o := range im.GetOwnerReferences() {
if o.Kind == machineSetKind.Kind {
hasMSOwnerRef = true
}
}
g.Expect(hasMSOwnerRef).To(BeTrue(), "have ownerRef to MachineSet")
}

// Set the infrastructure reference as ready.
Expand Down Expand Up @@ -527,7 +541,7 @@ func TestMachineSetToMachines(t *testing.T) {
OwnerReferences: []metav1.OwnerReference{
{
Name: "Owner",
Kind: "MachineSet",
Kind: machineSetKind.Kind,
Controller: &controller,
},
},
Expand Down Expand Up @@ -608,7 +622,7 @@ func TestShouldExcludeMachine(t *testing.T) {
OwnerReferences: []metav1.OwnerReference{
{
Name: "Owner",
Kind: "MachineSet",
Kind: machineSetKind.Kind,
Controller: &controller,
UID: "not-1",
},
Expand All @@ -628,7 +642,7 @@ func TestShouldExcludeMachine(t *testing.T) {
OwnerReferences: []metav1.OwnerReference{
{
Name: "Owner",
Kind: "MachineSet",
Kind: machineSetKind.Kind,
Controller: &controller,
UID: "1",
},
Expand Down Expand Up @@ -709,7 +723,7 @@ func TestAdoptOrphan(t *testing.T) {
expected: []metav1.OwnerReference{
{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "MachineSet",
Kind: machineSetKind.Kind,
Name: "adoptOrphanMachine",
UID: "",
Controller: &controller,
Expand Down

0 comments on commit e8592c3

Please sign in to comment.