Skip to content

Commit

Permalink
🐛 Fix patch in MachineSet adoption
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <vincepri@vmware.com>
  • Loading branch information
vincepri committed Sep 20, 2019
1 parent 44491e4 commit 3bb6817
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion controllers/machineset_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ func shouldExcludeMachine(machineSet *clusterv1.MachineSet, machine *clusterv1.M

// adoptOrphan sets the MachineSet as a controller OwnerReference to the Machine.
func (r *MachineSetReconciler) adoptOrphan(machineSet *clusterv1.MachineSet, machine *clusterv1.Machine) error {
patch := client.MergeFrom(machine)
patch := client.MergeFrom(machine.DeepCopy())
newRef := *metav1.NewControllerRef(machineSet, machineSetKind)
machine.OwnerReferences = append(machine.OwnerReferences, newRef)
return r.Client.Patch(context.Background(), machine, patch)
Expand Down
11 changes: 10 additions & 1 deletion controllers/machineset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package controllers

import (
"context"
"reflect"
"testing"
"time"
Expand Down Expand Up @@ -387,6 +388,8 @@ func TestShouldExcludeMachine(t *testing.T) {
}

func TestAdoptOrphan(t *testing.T) {
RegisterTestingT(t)

m := clusterv1.Machine{
ObjectMeta: metav1.ObjectMeta{
Name: "orphanMachine",
Expand Down Expand Up @@ -426,7 +429,13 @@ func TestAdoptOrphan(t *testing.T) {
Log: log.Log,
}
for _, tc := range testCases {
r.adoptOrphan(&tc.machineSet, &tc.machine)
err := r.adoptOrphan(tc.machineSet.DeepCopy(), tc.machine.DeepCopy())
Expect(err).ToNot(HaveOccurred())

key := client.ObjectKey{Namespace: tc.machine.Namespace, Name: tc.machine.Name}
err = r.Client.Get(context.Background(), key, &tc.machine)
Expect(err).ToNot(HaveOccurred())

got := tc.machine.GetOwnerReferences()
if !reflect.DeepEqual(got, tc.expected) {
t.Errorf("Case %s. Got: %+v, expected: %+v", tc.machine.Name, got, tc.expected)
Expand Down

0 comments on commit 3bb6817

Please sign in to comment.