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

Setting NaBSL prefix #172

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions internal/common/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,13 @@ func checkLabelAnnotationValueIsValid(labelsOrAnnotations map[string]string, key
func GetLogger(ctx context.Context, obj client.Object, key string) logr.Logger {
return log.FromContext(ctx).WithValues(key, types.NamespacedName{Name: obj.GetName(), Namespace: obj.GetNamespace()})
}

// ComputePrefixForObjectStorage returns the prefix to be used for the BackupStorageLocation.
// If a custom prefix is provided, it returns "<namespace>/<customPrefix>".
// Otherwise, it returns the namespace name.
func ComputePrefixForObjectStorage(namespace, customPrefix string) string {
if len(customPrefix) > 0 {
return fmt.Sprintf("%s/%s", namespace, customPrefix)
}
return namespace
}
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,12 @@ func (r *NonAdminBackupStorageLocationReconciler) createVeleroBSL(ctx context.Co
).Result()
}

// NaBSL/BSL must have a unique prefix for proper function of the non-admin backup sync controller
// 1. Check if user has specified the prefix as "foo" in NaBSL creation, then prefix used would be <non-admin-ns>/foo
// 2. TODO use the value from enforced spec if specified by the admin, if specified as "bar" then prefix used would be <non-admin-ns>/bar
// 3. If none of the above, then we will use the non-admin user's namespace name as prefix
prefix := function.ComputePrefixForObjectStorage(nabsl.Namespace, nabsl.Spec.BackupStorageLocationSpec.ObjectStorage.Prefix)

op, err := controllerutil.CreateOrUpdate(ctx, r.Client, veleroBsl, func() error {
veleroBsl.Spec = *nabsl.Spec.BackupStorageLocationSpec.DeepCopy()

Expand All @@ -551,6 +557,9 @@ func (r *NonAdminBackupStorageLocationReconciler) createVeleroBSL(ctx context.Co
Key: nabsl.Spec.BackupStorageLocationSpec.Credential.Key,
}

// Set prefix
veleroBsl.Spec.ObjectStorage.Prefix = prefix

return nil
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (

nacv1alpha1 "github.com/migtools/oadp-non-admin/api/v1alpha1"
"github.com/migtools/oadp-non-admin/internal/common/constant"
"github.com/migtools/oadp-non-admin/internal/common/function"
)

type nonAdminBackupStorageLocationClusterValidationScenario struct {
Expand Down Expand Up @@ -464,3 +465,28 @@ var _ = ginkgo.Describe("Test full reconcile loop of NonAdminBackupStorageLocati
}),
)
})

var _ = ginkgo.Describe("ComputePrefixForObjectStorage", func() {
type prefixTestScenario struct {
namespace string
customPrefix string
expectedPrefix string
}

ginkgo.DescribeTable("should compute the correct prefix",
func(sc prefixTestScenario) {
result := function.ComputePrefixForObjectStorage(sc.namespace, sc.customPrefix)
gomega.Expect(result).To(gomega.Equal(sc.expectedPrefix))
},
ginkgo.Entry("without custom prefix", prefixTestScenario{
namespace: "test-nac-ns",
customPrefix: "",
expectedPrefix: "test-nac-ns",
}),
ginkgo.Entry("with custom prefix", prefixTestScenario{
namespace: "test-nac-ns",
customPrefix: "foo",
expectedPrefix: "test-nac-ns/foo",
}),
)
})