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

OADP-3050: Remove unwanted BSL/VSLs on create/edit DPA #1525

Merged
merged 1 commit into from
Sep 30, 2024
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
31 changes: 31 additions & 0 deletions controllers/bsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package controllers
import (
"errors"
"fmt"
"slices"

"github.com/go-logr/logr"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
Expand Down Expand Up @@ -100,6 +101,8 @@ func (r *DPAReconciler) ReconcileBackupStorageLocations(log logr.Logger) (bool,
if err := r.Get(r.Context, r.NamespacedName, &dpa); err != nil {
return false, err
}

dpaBSLNames := []string{}
// Loop through all configured BSLs
for i, bslSpec := range dpa.Spec.BackupLocations {
// Create BSL as is, we can safely assume they are valid from
Expand All @@ -110,6 +113,7 @@ func (r *DPAReconciler) ReconcileBackupStorageLocations(log logr.Logger) (bool,
if bslSpec.Name != "" {
bslName = bslSpec.Name
}
dpaBSLNames = append(dpaBSLNames, bslName)

bsl := velerov1.BackupStorageLocation{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -196,6 +200,33 @@ func (r *DPAReconciler) ReconcileBackupStorageLocations(log logr.Logger) (bool,
)
}
}

dpaBSLs := velerov1.BackupStorageLocationList{}
dpaBSLLabels := map[string]string{
"app.kubernetes.io/name": common.OADPOperatorVelero,
"app.kubernetes.io/managed-by": common.OADPOperator,
"app.kubernetes.io/component": "bsl",
}
err := r.List(r.Context, &dpaBSLs, client.InNamespace(r.NamespacedName.Namespace), client.MatchingLabels(dpaBSLLabels))
if err != nil {
return false, err
}
// If current BSLs do not match the spec, delete extra BSLs
if len(dpaBSLNames) != len(dpaBSLs.Items) {
for _, bsl := range dpaBSLs.Items {
if !slices.Contains(dpaBSLNames, bsl.Name) {
if err := r.Delete(r.Context, &bsl); err != nil {
return false, err
}
// Record event for BSL deletion
r.EventRecorder.Event(&bsl,
corev1.EventTypeNormal,
"BackupStorageLocationDeleted",
fmt.Sprintf("BackupStorageLocation %s created by OADP in namespace %s was deleted as it was not in DPA spec.", bsl.Name, bsl.Namespace))
}
}
}

return true, nil
}

Expand Down
32 changes: 32 additions & 0 deletions controllers/vsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package controllers
import (
"errors"
"fmt"
"slices"
"strings"

"github.com/go-logr/logr"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

oadpv1alpha1 "github.com/openshift/oadp-operator/api/v1alpha1"
Expand Down Expand Up @@ -216,6 +218,7 @@ func (r *DPAReconciler) ReconcileVolumeSnapshotLocations(log logr.Logger) (bool,
return false, err
}

dpaVSLNames := []string{}
// Loop through all configured VSLs
for i, vslSpec := range dpa.Spec.SnapshotLocations {
// Create VSL as is, we can safely assume they are valid from
Expand All @@ -226,6 +229,7 @@ func (r *DPAReconciler) ReconcileVolumeSnapshotLocations(log logr.Logger) (bool,
if vslSpec.Name != "" {
vslName = vslSpec.Name
}
dpaVSLNames = append(dpaVSLNames, vslName)

vsl := velerov1.VolumeSnapshotLocation{
ObjectMeta: metav1.ObjectMeta{
Expand Down Expand Up @@ -272,6 +276,34 @@ func (r *DPAReconciler) ReconcileVolumeSnapshotLocations(log logr.Logger) (bool,
}

}

dpaVSLs := velerov1.VolumeSnapshotLocationList{}
dpaVslLabels := map[string]string{
"app.kubernetes.io/name": common.OADPOperatorVelero,
"app.kubernetes.io/managed-by": common.OADPOperator,
"app.kubernetes.io/component": "vsl",
}
err := r.List(r.Context, &dpaVSLs, client.InNamespace(r.NamespacedName.Namespace), client.MatchingLabels(dpaVslLabels))
if err != nil {
return false, err
}

// If current VSLs do not match the spec, delete extra VSLs
if len(dpaVSLNames) != len(dpaVSLs.Items) {
for _, vsl := range dpaVSLs.Items {
if !slices.Contains(dpaVSLNames, vsl.Name) {
if err := r.Delete(r.Context, &vsl); err != nil {
return false, err
}
// Record event for VSL deletion
r.EventRecorder.Event(&vsl,
corev1.EventTypeNormal,
"VolumeSnapshotLocationDeleted",
fmt.Sprintf("VolumeSnapshotLocation %s created by OADP in namespace %s was deleted as it was not in DPA spec.", vsl.Name, vsl.Namespace))
}
}
}

return true, nil
}

Expand Down
25 changes: 11 additions & 14 deletions tests/e2e/dpa_deployment_suite_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package e2e_test

import (
"fmt"
"log"
"strings"
"time"
Expand Down Expand Up @@ -190,29 +191,25 @@ var _ = ginkgo.Describe("Configuration testing for DPA Custom Resource", func()
log.Printf("Checking for BSL spec")
gomega.Expect(dpaCR.DoesBSLSpecMatchesDpa(namespace, *bsl.Velero)).To(gomega.BeTrue())
}
} else {
log.Println("Checking no BSLs are deployed")
_, err = dpaCR.ListBSLs()
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(err.Error()).To(gomega.Equal(fmt.Sprintf("no BSL in %s namespace", namespace)))
}
// TODO bug
// else {
// log.Println("Checking no BSLs are deployed")
// _, err = dpaCR.ListBSLs()
// Expect(err).To(HaveOccurred())
// Expect(err.Error()).To(Equal(fmt.Sprintf("no BSL in %s namespace", namespace)))
// }

if len(installCase.DpaSpec.SnapshotLocations) > 0 {
// TODO Check if VSLs are available creating new backup/restore test with VSL
for _, vsl := range installCase.DpaSpec.SnapshotLocations {
log.Printf("Checking for VSL spec")
gomega.Expect(dpaCR.DoesVSLSpecMatchesDpa(namespace, *vsl.Velero)).To(gomega.BeTrue())
}
} else {
log.Println("Checking no VSLs are deployed")
_, err = dpaCR.ListVSLs()
gomega.Expect(err).To(gomega.HaveOccurred())
gomega.Expect(err.Error()).To(gomega.Equal(fmt.Sprintf("no VSL in %s namespace", namespace)))
}
// TODO bug
// else {
// log.Println("Checking no VSLs are deployed")
// _, err = dpaCR.ListVSLs()
// Expect(err).To(HaveOccurred())
// Expect(err.Error()).To(Equal(fmt.Sprintf("no VSL in %s namespace", namespace)))
// }

if len(installCase.DpaSpec.Configuration.Velero.PodConfig.Tolerations) > 0 {
log.Printf("Checking for velero tolerances")
Expand Down