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-1.2] OADP-1670: Add owner reference to cloud storage BSLs. #975

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions controllers/bsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ func (r *DPAReconciler) ReconcileBackupStorageLocations(log logr.Logger) (bool,
if err != nil {
return err
}
err = controllerutil.SetControllerReference(&dpa, &bsl, r.Scheme)
if err != nil {
return err
}
bsl.Spec.BackupSyncPeriod = bslSpec.CloudStorage.BackupSyncPeriod
bsl.Spec.Config = bslSpec.CloudStorage.Config
if bucket.Spec.EnableSharedConfig != nil && *bucket.Spec.EnableSharedConfig {
Expand Down
141 changes: 141 additions & 0 deletions controllers/bsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1425,3 +1425,144 @@ func TestDPAReconciler_ensureBackupLocationHasVeleroOrCloudStorage(t *testing.T)
})
}
}

func TestDPAReconciler_ReconcileBackupStorageLocations(t *testing.T) {
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "cloud-credentials",
Namespace: "test-ns",
},
}
cs := &oadpv1alpha1.CloudStorage{
ObjectMeta: metav1.ObjectMeta{
Name: "test-cs",
Namespace: "test-ns",
},
Spec: oadpv1alpha1.CloudStorageSpec{
CreationSecret: v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Name: "cloud-credentials",
},
Key: "credentials",
},
Name: "test-cs",
Provider: "aws",
},
}

tests := []struct {
name string
dpa *oadpv1alpha1.DataProtectionApplication
secret *corev1.Secret
cs *oadpv1alpha1.CloudStorage
want bool
wantErr bool
}{
{
name: "check owner references on Velero BSL",
dpa: &oadpv1alpha1.DataProtectionApplication{
ObjectMeta: metav1.ObjectMeta{
Name: "test-dpa",
Namespace: "test-ns",
},
Spec: oadpv1alpha1.DataProtectionApplicationSpec{
BackupLocations: []oadpv1alpha1.BackupLocation{
{
Velero: &velerov1.BackupStorageLocationSpec{
Provider: "aws",
},
},
},
},
},
cs: cs,
secret: secret,
want: true,
wantErr: false,
},
{
name: "check owner references on CloudStorage BSL",
dpa: &oadpv1alpha1.DataProtectionApplication{
ObjectMeta: metav1.ObjectMeta{
Name: "test-dpa",
Namespace: "test-ns",
},
Spec: oadpv1alpha1.DataProtectionApplicationSpec{
BackupLocations: []oadpv1alpha1.BackupLocation{
{
CloudStorage: &oadpv1alpha1.CloudStorageLocation{
CloudStorageRef: v1.LocalObjectReference{
Name: "test-cs",
},
Credential: &v1.SecretKeySelector{
LocalObjectReference: v1.LocalObjectReference{
Name: "cloud-credentials",
},
Key: "credentials",
},
},
},
},
},
},
cs: cs,
secret: secret,
want: true,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fakeClient, err := getFakeClientFromObjects(tt.dpa, tt.secret, tt.cs)
if err != nil {
t.Errorf("error in creating fake client, likely programmer error")
}
r := &DPAReconciler{
Client: fakeClient,
Scheme: fakeClient.Scheme(),
Log: logr.Discard(),
Context: newContextForTest(tt.name),
NamespacedName: types.NamespacedName{
Namespace: tt.dpa.Namespace,
Name: tt.dpa.Name,
},
EventRecorder: record.NewFakeRecorder(10),
}
wantBSL := &velerov1.BackupStorageLocation{
ObjectMeta: metav1.ObjectMeta{
Name: "test-dpa-1",
Namespace: "test-ns",
OwnerReferences: []metav1.OwnerReference{{
APIVersion: oadpv1alpha1.SchemeBuilder.GroupVersion.String(),
Kind: "DataProtectionApplication",
Name: tt.dpa.Name,
UID: tt.dpa.UID,
Controller: pointer.BoolPtr(true),
BlockOwnerDeletion: pointer.BoolPtr(true),
}},
},
}
got, err := r.ReconcileBackupStorageLocations(r.Log)
if (err != nil) != tt.wantErr {
t.Errorf("ReconcileBackupStorageLocations() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ReconcileBackupStorageLocations() got = %v, want %v", got, tt.want)
}
if (err != nil) != tt.wantErr {
t.Errorf("ReconcileBackupStorageLocations() error = %v, wantErr %v", err, tt.wantErr)
return
}
bsl := &velerov1.BackupStorageLocation{}
err = r.Get(r.Context, client.ObjectKey{Namespace: "test-ns", Name: "test-dpa-1"}, bsl)
if (err != nil) != tt.wantErr {
t.Errorf("ReconcileBackupStorageLocations() error =%v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(bsl.OwnerReferences, wantBSL.OwnerReferences) {
t.Errorf("ReconcileBackupStorageLocations() expected BSL owner references to be %#v, got %#v", wantBSL.OwnerReferences, bsl.OwnerReferences)
}
})
}
}
93 changes: 93 additions & 0 deletions controllers/vsl_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package controllers

import (
"reflect"
"testing"

"github.com/go-logr/logr"
Expand All @@ -10,6 +11,8 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
"k8s.io/utils/pointer"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func TestDPAReconciler_ValidateVolumeSnapshotLocation(t *testing.T) {
Expand Down Expand Up @@ -519,3 +522,93 @@ func TestDPAReconciler_ValidateVolumeSnapshotLocation(t *testing.T) {
}

}

func TestDPAReconciler_ReconcileVolumeSnapshotLocations(t *testing.T) {
tests := []struct {
name string
dpa *oadpv1alpha1.DataProtectionApplication
want bool
wantErr bool
}{
{
name: "check owner references on VSL",
dpa: &oadpv1alpha1.DataProtectionApplication{
ObjectMeta: metav1.ObjectMeta{
Name: "test-Velero-VSL",
Namespace: "test-ns",
},
Spec: oadpv1alpha1.DataProtectionApplicationSpec{
Configuration: &oadpv1alpha1.ApplicationConfig{
Velero: &oadpv1alpha1.VeleroConfig{},
},
SnapshotLocations: []oadpv1alpha1.SnapshotLocation{
{
Velero: &velerov1.VolumeSnapshotLocationSpec{
Provider: AWSProvider,
Config: map[string]string{
Region: "us-east-1",
},
},
},
},
},
},
want: true,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fakeClient, err := getFakeClientFromObjects(tt.dpa)
if err != nil {
t.Errorf("error in creating fake client, likely programmer error")
}
r := &DPAReconciler{
Client: fakeClient,
Scheme: fakeClient.Scheme(),
Log: logr.Discard(),
Context: newContextForTest(tt.name),
NamespacedName: types.NamespacedName{
Namespace: tt.dpa.Namespace,
Name: tt.dpa.Name,
},
EventRecorder: record.NewFakeRecorder(10),
}
wantVSL := &velerov1.VolumeSnapshotLocation{
ObjectMeta: metav1.ObjectMeta{
Name: "test-Velero-VSL-1",
Namespace: "test-ns",
OwnerReferences: []metav1.OwnerReference{{
APIVersion: oadpv1alpha1.SchemeBuilder.GroupVersion.String(),
Kind: "DataProtectionApplication",
Name: tt.dpa.Name,
UID: tt.dpa.UID,
Controller: pointer.BoolPtr(true),
BlockOwnerDeletion: pointer.BoolPtr(true),
}},
},
}
got, err := r.ReconcileVolumeSnapshotLocations(r.Log)
if (err != nil) != tt.wantErr {
t.Errorf("ReconcileVolumeSnapshotLocations() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ReconcileVolumeSnapshotLocations() got = %v, want %v", got, tt.want)
}
if (err != nil) != tt.wantErr {
t.Errorf("ReconcileVolumeSnapshotLocations() error = %v, wantErr %v", err, tt.wantErr)
return
}
vsl := &velerov1.VolumeSnapshotLocation{}
err = r.Get(r.Context, client.ObjectKey{Namespace: "test-ns", Name: "test-Velero-VSL-1"}, vsl)
if (err != nil) != tt.wantErr {
t.Errorf("ReconcileVolumeSnapshotLocations() error =%v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(vsl.OwnerReferences, wantVSL.OwnerReferences) {
t.Errorf("ReconcileVolumeSnapshotLocations() expected VSL owner references to be %#v, got %#v", wantVSL.OwnerReferences, vsl.OwnerReferences)
}
})
}
}