diff --git a/baremetal/metal3machine_manager.go b/baremetal/metal3machine_manager.go index 94ac7290ee..538b2e7a1e 100644 --- a/baremetal/metal3machine_manager.go +++ b/baremetal/metal3machine_manager.go @@ -474,9 +474,6 @@ func (m *MachineManager) Delete(ctx context.Context) error { if !consumerRefMatches(host.Spec.ConsumerRef, m.Metal3Machine) { m.Log.Info("host already associated with another metal3 machine", "host", host.Name) - // Remove the ownerreference to this machine, even if the consumer ref - // references another machine. - host.OwnerReferences, err = m.DeleteOwnerRef(host.OwnerReferences) if err != nil { return err } @@ -660,8 +657,6 @@ func (m *MachineManager) Delete(ctx context.Context) error { host.Spec.ConsumerRef = nil - // Remove the ownerreference to this machine. - host.OwnerReferences, err = m.DeleteOwnerRef(host.OwnerReferences) if err != nil { return err } @@ -712,12 +707,6 @@ func (m *MachineManager) Update(ctx context.Context) error { return err } - // ensure that the BMH specs are correctly set. - err = m.setHostSpec(ctx, host) - if err != nil { - return err - } - err = helper.Patch(ctx, host) if err != nil { return err @@ -1122,13 +1111,6 @@ func (m *MachineManager) setHostConsumerRef(_ context.Context, host *bmov1alpha1 APIVersion: m.Metal3Machine.APIVersion, } - // Set OwnerReferences. - hostOwnerReferences, err := m.SetOwnerRef(host.OwnerReferences, true) - if err != nil { - return err - } - host.OwnerReferences = hostOwnerReferences - // Delete nodeReuseLabelName from host. m.Log.Info("Deleting nodeReuseLabelName from host, if any") @@ -1397,46 +1379,6 @@ func (m *MachineManager) SetProviderID(providerID string) { m.SetConditionMetal3MachineToTrue(infrav1.KubernetesNodeReadyCondition) } -// SetOwnerRef adds an ownerreference to this Metal3Machine. -func (m *MachineManager) SetOwnerRef(refList []metav1.OwnerReference, controller bool) ([]metav1.OwnerReference, error) { - return setOwnerRefInList(refList, controller, m.Metal3Machine.TypeMeta, - m.Metal3Machine.ObjectMeta, - ) -} - -// DeleteOwnerRef removes the ownerreference to this Metal3Machine. -func (m *MachineManager) DeleteOwnerRef(refList []metav1.OwnerReference) ([]metav1.OwnerReference, error) { - return deleteOwnerRefFromList(refList, m.Metal3Machine.TypeMeta, - m.Metal3Machine.ObjectMeta, - ) -} - -// DeleteOwnerRefFromList removes the ownerreference to this Metal3Machine. -func deleteOwnerRefFromList(refList []metav1.OwnerReference, - objType metav1.TypeMeta, objMeta metav1.ObjectMeta, -) ([]metav1.OwnerReference, error) { - if len(refList) == 0 { - return refList, nil - } - index, err := findOwnerRefFromList(refList, objType, objMeta) - if err != nil { - if ok := errors.As(err, ¬FoundErr); !ok { - return nil, err - } - return refList, nil - } - if len(refList) == 1 { - return []metav1.OwnerReference{}, nil - } - refListLen := len(refList) - 1 - refList[index] = refList[refListLen] - refList, err = deleteOwnerRefFromList(refList[:refListLen], objType, objMeta) - if err != nil { - return nil, err - } - return refList, nil -} - // FindOwnerRef checks if an ownerreference to this Metal3Machine exists // and returns the index. func (m *MachineManager) FindOwnerRef(refList []metav1.OwnerReference) (int, error) { diff --git a/baremetal/metal3machine_manager_test.go b/baremetal/metal3machine_manager_test.go index 71c9e50ac2..60e5df5844 100644 --- a/baremetal/metal3machine_manager_test.go +++ b/baremetal/metal3machine_manager_test.go @@ -3396,144 +3396,6 @@ var _ = Describe("Metal3Machine manager", func() { Controller bool } - DescribeTable("Test DeleteOwnerRef", - func(tc testCaseOwnerRef) { - machineMgr, err := NewMachineManager(nil, nil, nil, nil, &tc.M3Machine, - logr.Discard(), - ) - Expect(err).NotTo(HaveOccurred()) - - refList, err := machineMgr.DeleteOwnerRef(tc.OwnerRefs) - Expect(err).ToNot(HaveOccurred()) - _, err = machineMgr.FindOwnerRef(refList) - Expect(err).To(HaveOccurred()) - }, - Entry("Empty list", testCaseOwnerRef{ - M3Machine: *newMetal3Machine("myName", nil, nil, nil), - OwnerRefs: []metav1.OwnerReference{}, - }), - Entry("Absent", testCaseOwnerRef{ - M3Machine: *newMetal3Machine("myName", nil, nil, nil), - OwnerRefs: []metav1.OwnerReference{ - { - APIVersion: "abc.com/v1", - Kind: "def", - Name: "ghi", - UID: "adfasdf", - }, - }, - }), - Entry("Present 0", testCaseOwnerRef{ - M3Machine: *newMetal3Machine("myName", nil, nil, nil), - OwnerRefs: []metav1.OwnerReference{ - { - Kind: "M3Machine", - APIVersion: infrav1.GroupVersion.String(), - Name: "myName", - UID: "adfasdf", - }, - { - APIVersion: "abc.com/v1", - Kind: "def", - Name: "ghi", - UID: "adfasdf", - }, - }, - }), - Entry("Present 1", testCaseOwnerRef{ - M3Machine: *newMetal3Machine("myName", nil, nil, nil), - OwnerRefs: []metav1.OwnerReference{ - { - APIVersion: "abc.com/v1", - Kind: "def", - Name: "ghi", - UID: "adfasdf", - }, - { - Kind: "M3Machine", - APIVersion: infrav1.GroupVersion.String(), - Name: "myName", - UID: "adfasdf", - }, - }, - }), - Entry("Present", testCaseOwnerRef{ - M3Machine: *newMetal3Machine("myName", nil, nil, nil), - OwnerRefs: []metav1.OwnerReference{ - { - Kind: "M3Machine", - APIVersion: infrav1.GroupVersion.String(), - Name: "myName", - UID: "adfasdf", - }, - }, - }), - ) - - DescribeTable("Test SetOwnerRef", - func(tc testCaseOwnerRef) { - machineMgr, err := NewMachineManager(nil, nil, nil, nil, &tc.M3Machine, - logr.Discard(), - ) - Expect(err).NotTo(HaveOccurred()) - - refList, err := machineMgr.SetOwnerRef(tc.OwnerRefs, tc.Controller) - Expect(err).ToNot(HaveOccurred()) - index, err := machineMgr.FindOwnerRef(refList) - Expect(err).ToNot(HaveOccurred()) - Expect(*refList[index].Controller).To(BeEquivalentTo(tc.Controller)) - }, - Entry("Empty list", testCaseOwnerRef{ - M3Machine: *newMetal3Machine("myName", nil, nil, nil), - OwnerRefs: []metav1.OwnerReference{}, - }), - Entry("Absent", testCaseOwnerRef{ - M3Machine: *newMetal3Machine("myName", nil, nil, nil), - OwnerRefs: []metav1.OwnerReference{ - { - APIVersion: "abc.com/v1", - Kind: "def", - Name: "ghi", - UID: "adfasdf", - }, - }, - }), - Entry("Present 0", testCaseOwnerRef{ - M3Machine: *newMetal3Machine("myName", nil, nil, nil), - OwnerRefs: []metav1.OwnerReference{ - { - Kind: "M3Machine", - APIVersion: infrav1.GroupVersion.String(), - Name: "myName", - UID: "adfasdf", - }, - { - APIVersion: "abc.com/v1", - Kind: "def", - Name: "ghi", - UID: "adfasdf", - }, - }, - }), - Entry("Present 1", testCaseOwnerRef{ - M3Machine: *newMetal3Machine("myName", nil, nil, nil), - OwnerRefs: []metav1.OwnerReference{ - { - APIVersion: "abc.com/v1", - Kind: "def", - Name: "ghi", - UID: "adfasdf", - }, - { - Kind: "M3Machine", - APIVersion: infrav1.GroupVersion.String(), - Name: "myName", - UID: "adfasdf", - }, - }, - }), - ) - type testCaseM3MetaData struct { M3Machine *infrav1.Metal3Machine Machine *clusterv1.Machine