Skip to content

Commit

Permalink
Remove m3m ownerRef from bmh
Browse files Browse the repository at this point in the history
31d394f

Signed-off-by: shibaPuppy <sub951@naver.com>
  • Loading branch information
shibaPuppy committed Jun 8, 2024
1 parent 30ed6f3 commit e1f83b7
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 196 deletions.
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
138 changes: 0 additions & 138 deletions baremetal/metal3machine_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e1f83b7

Please sign in to comment.