Skip to content

Commit

Permalink
fixup! feat: Garbage collector
Browse files Browse the repository at this point in the history
Signed-off-by: Mateus Oliveira <[email protected]>
  • Loading branch information
mateusoliveira43 committed Jan 29, 2025
1 parent 9d6cd83 commit a216e41
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func main() {
// TODO user input
Frequency: 5 * time.Minute,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "GarbageCollector")
setupLog.Error(err, "unable to setup GarbageCollector controller with manager")
os.Exit(1)
}

Expand Down
6 changes: 6 additions & 0 deletions internal/common/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,11 @@ func CheckVeleroBackupStorageLocationMetadata(obj client.Object) bool {
return false
}

return CheckVeleroBackupStorageLocationAnnotations(obj)
}

// CheckVeleroBackupStorageLocationAnnotations return true if Velero BackupStorageLocation object has required Non Admin annotations, false otherwise
func CheckVeleroBackupStorageLocationAnnotations(obj client.Object) bool {
annotations := obj.GetAnnotations()
if !checkLabelAnnotationValueIsValid(annotations, constant.NabslOriginNamespaceAnnotation) {
return false
Expand All @@ -590,6 +595,7 @@ func CheckVeleroBackupStorageLocationMetadata(obj client.Object) bool {

return true
}

func checkLabelValue(objLabels map[string]string, key string, value string) bool {
got, exists := objLabels[key]
if !exists {
Expand Down
31 changes: 30 additions & 1 deletion internal/controller/garbagecollector_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,37 @@ func (r *GarbageCollectorReconciler) Reconcile(ctx context.Context, _ ctrl.Reque
// TODO duplication in delete logic
// TODO do deletion in parallel?

// TODO delete BSL
// TODO delete secret as well?
veleroBackupStorageLocationList := &velerov1.BackupStorageLocationList{}
if err := r.List(ctx, veleroBackupStorageLocationList, client.InNamespace(r.OADPNamespace), labelSelector); err != nil {
logger.Error(err, "Unable to fetch BackupStorageLocations in OADP namespace")
return ctrl.Result{}, err
}
for _, backupStorageLocation := range veleroBackupStorageLocationList.Items {
annotations := backupStorageLocation.GetAnnotations()
// TODO check UUID as well?
if !function.CheckVeleroBackupStorageLocationAnnotations(&backupStorageLocation) {
logger.V(1).Info("BackupStorageLocation does not have required annotations", constant.NameString, backupStorageLocation.Name)
// TODO delete?
continue
}
nabsl := &nacv1alpha1.NonAdminBackupStorageLocation{}
err := r.Get(ctx, types.NamespacedName{
Name: annotations[constant.NabslOriginNameAnnotation],
Namespace: annotations[constant.NabslOriginNamespaceAnnotation],
}, nabsl)
if err != nil {
if !apierrors.IsNotFound(err) {
logger.Error(err, "Unable to fetch NonAdminBackupStorageLocation")
return ctrl.Result{}, err
}
if err = r.Delete(ctx, &backupStorageLocation); err != nil {
logger.Error(err, "Failed to delete orphan BackupStorageLocation", constant.NameString, backupStorageLocation.Name)
return ctrl.Result{}, err
}
logger.V(1).Info("orphan BackupStorageLocation deleted", constant.NameString, backupStorageLocation.Name)
}
}

veleroBackupList := &velerov1.BackupList{}
if err := r.List(ctx, veleroBackupList, client.InNamespace(r.OADPNamespace), labelSelector); err != nil {
Expand Down
11 changes: 10 additions & 1 deletion internal/controller/garbagecollector_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,16 @@ var _ = ginkgo.Describe("Test full reconcile loop of GarbageCollector Controller
for index := range 5 {
bsl := &velerov1.BackupStorageLocation{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("example-%v", index),
Name: fmt.Sprintf("test-garbage-collector-bsl-%v", index),
Namespace: oadpNamespace,
Labels: map[string]string{
constant.OadpLabel: constant.OadpLabelValue,
constant.ManagedByLabel: constant.ManagedByLabelValue,
},
Annotations: map[string]string{
constant.NabslOriginNamespaceAnnotation: nonAdminNamespace,
constant.NabslOriginNameAnnotation: "non-existent",
},
},
Spec: velerov1.BackupStorageLocationSpec{
StorageType: velerov1.StorageType{
Expand All @@ -194,6 +202,7 @@ var _ = ginkgo.Describe("Test full reconcile loop of GarbageCollector Controller
}()

time.Sleep(10 * time.Second)
gomega.Expect(strings.Count(ginkgo.CurrentSpecReport().CapturedGinkgoWriterOutput, "orphan BackupStorageLocation deleted")).Should(gomega.Equal(5))
gomega.Expect(strings.Count(ginkgo.CurrentSpecReport().CapturedGinkgoWriterOutput, "orphan Backup deleted")).Should(gomega.Equal(scenario.orphanBackups))
gomega.Expect(strings.Count(ginkgo.CurrentSpecReport().CapturedGinkgoWriterOutput, "orphan Restore deleted")).Should(gomega.Equal(scenario.orphanRestores))
gomega.Expect(strings.Count(ginkgo.CurrentSpecReport().CapturedGinkgoWriterOutput, "Garbage Collector Reconcile start")).Should(gomega.Equal(5))
Expand Down

0 comments on commit a216e41

Please sign in to comment.