Skip to content

Commit

Permalink
fix: Delete ETCDMachineSnapshot resources if their corresponding snap…
Browse files Browse the repository at this point in the history
…shot gets deleted
  • Loading branch information
yiannistri committed Jan 31, 2025
1 parent 73487cf commit 56adf2d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion exp/etcdrestore/controllers/etcdsnapshotsync_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (r *EtcdSnapshotSyncReconciler) etcdSnapshotFile(ctx context.Context, clust
}

if o.GetObjectKind().GroupVersionKind() != gvk {
log.Error(fmt.Errorf("got a different object"), "objectGVK", o.GetObjectKind().GroupVersionKind().String())
log.Error(fmt.Errorf("got a different object"), "unexpected object, will not enqueue request", "objectGVK", o.GetObjectKind().GroupVersionKind().String())
return nil
}

Expand Down
31 changes: 31 additions & 0 deletions exp/etcdrestore/controllers/snapshotters/rke2snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,37 @@ func (s *RKE2Snapshotter) Sync(ctx context.Context) error {
return fmt.Errorf("failed to create/modify EtcdMachineSnapshot status: %w", err)
}

// Get all ETCDMachineSnapshot resources for this cluster
etcdMachineSnapshotList := &snapshotrestorev1.ETCDMachineSnapshotList{}
if err := s.List(ctx, etcdMachineSnapshotList, client.MatchingFields{
"spec.clusterName": s.cluster.Name,
}); err != nil {
return fmt.Errorf("failed to list ETCDMachineSnapshot resources for cluster %s: %w", s.cluster.Name, err)
}

// Delete any ETCDMachineSnapshot resources marked as Done that don't have a corresponding ETCDSnapshotFile
for _, snapshot := range etcdMachineSnapshotList.Items {
if snapshot.Status.Phase != snapshotrestorev1.ETCDSnapshotPhaseDone {
continue
}

found := false
for _, snapshotFile := range etcdnapshotFileList.Items {
if *snapshot.Status.SnapshotFileName == snapshotFile.Name {
found = true
log.V(5).Info("Found corresponding ETCDSnapshotFile for ETCDMachineSnapshot", "name", snapshot.Name, "snapshotFileName", snapshotFile.Name)
break
}
}

if !found {
log.Info("Deleting ETCDMachineSnapshot without a corresponding ETCDSnapshotFile", "name", snapshot.Name)
if err := s.Delete(ctx, &snapshot); err != nil {
return fmt.Errorf("failed to delete ETCDMachineSnapshot: %w", err)
}
}
}

return nil
}

Expand Down
9 changes: 8 additions & 1 deletion exp/etcdrestore/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,19 @@ func setupReconcilers(ctx context.Context, mgr ctrl.Manager) {

setupLog.Info("enabling ETCDMachineSnapshot controller")

if err := mgr.GetFieldIndexer().IndexField(ctx, &snapshotrestorev1.ETCDMachineSnapshot{}, "spec.clusterName", func(o client.Object) []string {
return []string{o.(*snapshotrestorev1.ETCDMachineSnapshot).Spec.ClusterName}
}); err != nil {
setupLog.Error(err, "unable to create field indexer for ETCDMachineSnapshot")
os.Exit(1)
}

if err := (&expcontrollers.ETCDMachineSnapshotReconciler{
Client: mgr.GetClient(),
Tracker: tracker,
WatchFilterValue: watchFilterValue,
}).SetupWithManager(ctx, mgr, controller.Options{MaxConcurrentReconciles: concurrencyNumber}); err != nil {
setupLog.Error(err, "unable to create EtcdMachineSnapshot controller")
setupLog.Error(err, "unable to create ETCDMachineSnapshot controller")
os.Exit(1)
}

Expand Down

0 comments on commit 56adf2d

Please sign in to comment.