Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Remove m3m ownerRef from bmh #1008

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 0 additions & 58 deletions baremetal/metal3machine_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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, &notFoundErr); !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) {
Expand Down
147 changes: 1 addition & 146 deletions baremetal/metal3machine_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1201,8 +1201,6 @@ var _ = Describe("Metal3Machine manager", func() {
Expect(tc.Host.Spec.ConsumerRef.Namespace).
To(Equal(m3mconfig.Namespace))
Expect(tc.Host.Spec.ConsumerRef.Kind).To(Equal("Metal3Machine"))
_, err = machineMgr.FindOwnerRef(tc.Host.OwnerReferences)
Expect(err).NotTo(HaveOccurred())

if tc.expectNodeReuseLabelDeleted {
Expect(tc.Host.Labels[nodeReuseLabelName]).To(Equal(""))
Expand Down Expand Up @@ -3068,12 +3066,7 @@ var _ = Describe("Metal3Machine manager", func() {
&savedHost,
)
Expect(err).NotTo(HaveOccurred())
_, err = machineMgr.FindOwnerRef(savedHost.OwnerReferences)
if tc.ExpectOwnerRef {
Expect(err).NotTo(HaveOccurred())
} else {
Expect(err).To(HaveOccurred())
}

if tc.ExpectClusterLabel {
// get the BMC credential
savedCred := corev1.Secret{}
Expand Down Expand Up @@ -3396,144 +3389,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
Expand Down