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

fix: Delete ETCDMachineSnapshot resources if their corresponding snapshot gets deleted #1067

Merged
merged 1 commit into from
Feb 3, 2025
Merged
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
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
Loading