Skip to content

Commit

Permalink
Add extra finalizers to stop metal3data/dataClaim from being deletable
Browse files Browse the repository at this point in the history
while in use.

Signed-off-by: Max Rantil <[email protected]>
  • Loading branch information
Max Rantil committed Feb 29, 2024
1 parent c9a0a3b commit 4985eb8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 19 deletions.
8 changes: 8 additions & 0 deletions baremetal/metal3data_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ func (m *DataManager) SetFinalizer() {
infrav1.DataFinalizer,
)
}
if !Contains(m.Data.Finalizers, infrav1.MachineFinalizer) {
m.Data.Finalizers = append(m.Data.Finalizers,
infrav1.MachineFinalizer,
)
}
}

// UnsetFinalizer unsets finalizer.
Expand All @@ -96,6 +101,9 @@ func (m *DataManager) UnsetFinalizer() {
m.Data.Finalizers = Filter(m.Data.Finalizers,
infrav1.DataFinalizer,
)
m.Data.Finalizers = Filter(m.Data.Finalizers,
infrav1.MachineFinalizer,
)
}

// clearError clears error message from Metal3Data status.
Expand Down
62 changes: 43 additions & 19 deletions baremetal/metal3datatemplate_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package baremetal

import (
"context"
"fmt"
"strconv"

"github.com/go-logr/logr"
Expand Down Expand Up @@ -232,6 +231,14 @@ func (m *DataTemplateManager) updateData(ctx context.Context,
return indexes, err
}
} else {
// Check if this claim is in use. Does it have any owners?
// If it is no longer in use, proceed to delete the associated data
if len(dataClaim.OwnerReferences) > 0 {
m.Log.Info("Metal3DataClaim is still in use (has owners). Cannot delete Metal3Data.",
"Metal3DataClaim", dataClaim.Name, "Owners", dataClaim.OwnerReferences)
return indexes, nil
}

indexes, err = m.deleteData(ctx, dataClaim, indexes)
if err != nil {
return indexes, err
Expand Down Expand Up @@ -305,17 +312,18 @@ func (m *DataTemplateManager) createData(ctx context.Context,
}
m.Log.Info("Index", "Claim", dataClaim.Name, "index", claimIndex)

// Create the Metal3Data object, with an Owner ref to the Metal3Machine
// (curOwnerRef) and to the Metal3DataTemplate
// Create the Metal3Data object, with an Owner ref to the dataClaim,
// the dataTemplate, and the dataClaims owners. Also add a finalizer.
dataObject := &infrav1.Metal3Data{
TypeMeta: metav1.TypeMeta{
Kind: "Metal3Data",
APIVersion: infrav1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: dataName,
Namespace: m.DataTemplate.Namespace,
Labels: dataClaim.Labels,
Name: dataName,
Namespace: m.DataTemplate.Namespace,
Finalizers: []string{infrav1.MachineFinalizer},
Labels: dataClaim.Labels,
OwnerReferences: []metav1.OwnerReference{
{
Controller: ptr.To(true),
Expand Down Expand Up @@ -374,53 +382,69 @@ func (m *DataTemplateManager) createData(ctx context.Context,
return indexes, nil
}

// DeleteDatas deletes old secrets.
// deleteData removes the finalizers from the DataClaim and deletes the associated Metal3Data.
func (m *DataTemplateManager) deleteData(ctx context.Context,
dataClaim *infrav1.Metal3DataClaim, indexes map[int]string,
) (map[int]string, error) {
var dataName string
m.Log.Info("Deleting Claim", "Metal3DataClaim", dataClaim.Name)
m.Log.Info("Deleting Metal3Data associated with Metal3DataClaim", "Metal3DataClaim", dataClaim.Name)

dataClaimIndex, ok := m.DataTemplate.Status.Indexes[dataClaim.Name]
dataIndex, ok := m.DataTemplate.Status.Indexes[dataClaim.Name]
if ok {
// Try to get the Metal3Data. if it succeeds, delete it
tmpM3Data := &infrav1.Metal3Data{}
metal3Data := &infrav1.Metal3Data{}
var dataName string

if m.DataTemplate.Spec.TemplateReference != "" {
dataName = m.DataTemplate.Spec.TemplateReference + "-" + strconv.Itoa(dataClaimIndex)
dataName = m.DataTemplate.Spec.TemplateReference + "-" + strconv.Itoa(dataIndex)
} else {
dataName = m.DataTemplate.Name + "-" + strconv.Itoa(dataClaimIndex)
dataName = m.DataTemplate.Name + "-" + strconv.Itoa(dataIndex)
}

key := client.ObjectKey{
Name: dataName,
Namespace: m.DataTemplate.Namespace,
}
err := m.client.Get(ctx, key, tmpM3Data)
err := m.client.Get(ctx, key, metal3Data)
if err != nil && !apierrors.IsNotFound(err) {
dataClaim.Status.ErrorMessage = ptr.To("Failed to get associated Metal3Data object")
return indexes, err
} else if err == nil {
// Delete the secret with metadata
fmt.Println(tmpM3Data.Name)
err = m.client.Delete(ctx, tmpM3Data)
// Remove the finalizer
metal3Data.Finalizers = Filter(metal3Data.Finalizers,
infrav1.MachineFinalizer,
)
err = updateObject(ctx, m.client, metal3Data)
if err != nil && !apierrors.IsNotFound(err) {
m.Log.Info("Unable to remove finalizer from Metal3Data", "Metal3Data", metal3Data.Name)
return indexes, err
}
// Delete the Metal3Data
err = deleteObject(ctx, m.client, metal3Data)
if err != nil && !apierrors.IsNotFound(err) {
dataClaim.Status.ErrorMessage = ptr.To("Failed to delete associated Metal3Data object")
return indexes, err
}
m.Log.Info("Deleted Metal3Data", "Metal3Data", metal3Data.Name)
}
}

dataClaim.Status.RenderedData = nil
dataClaim.Finalizers = Filter(dataClaim.Finalizers,
infrav1.DataClaimFinalizer,
)

m.Log.Info("Deleted Claim", "Metal3DataClaim", dataClaim.Name)
err := updateObject(ctx, m.client, dataClaim)
if err != nil && !apierrors.IsNotFound(err) {
m.Log.Info("Unable to remove finalizers from Metal3DataClaim", "Metal3DataClaim", dataClaim.Name)
return indexes, err
}

if ok {
delete(m.DataTemplate.Status.Indexes, dataClaim.Name)
delete(indexes, dataClaimIndex)
delete(indexes, dataIndex)
}

m.Log.Info("Deleted Metal3DataClaim", "Metal3DataClaim", dataClaim.Name)
m.updateStatusTimestamp()
return indexes, nil
}

0 comments on commit 4985eb8

Please sign in to comment.