Skip to content

Commit

Permalink
paths implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Tiger Kaovilai <[email protected]>
  • Loading branch information
kaovilai committed Feb 6, 2025
1 parent 73e62f4 commit 3e67c7a
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 5 deletions.
30 changes: 25 additions & 5 deletions internal/controller/nonadminbackup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/migtools/oadp-non-admin/internal/common/function"
"github.com/migtools/oadp-non-admin/internal/handler"
"github.com/migtools/oadp-non-admin/internal/predicate"
"github.com/migtools/oadp-non-admin/pkg/velero/storagelocation/paths"
)

// NonAdminBackupReconciler reconciles a NonAdminBackup object
Expand Down Expand Up @@ -683,7 +684,11 @@ func (r *NonAdminBackupReconciler) createVeleroBackupAndSyncWithNonAdminBackup(c
// Ensure that the NonAdminBackup's NonAdminBackupStatus is in sync
// with the VeleroBackup. Any required updates to the NonAdminBackup
// Status will be applied based on the current state of the VeleroBackup.
updated := updateNonAdminBackupVeleroBackupStatus(&nab.Status, veleroBackup)
updated, err := updateNonAdminBackupVeleroBackupStatus(r.Client, &nab.Status, veleroBackup)
if err != nil {
logger.Error(err, statusUpdateError)
return false, err
}

if updated || updatedPhase || updatedCondition || updatedQueueInfo {
if err := r.Status().Update(ctx, nab); err != nil {
Expand Down Expand Up @@ -733,9 +738,9 @@ func updateNonAdminPhase(phase *nacv1alpha1.NonAdminPhase, newPhase nacv1alpha1.

// updateNonAdminBackupVeleroBackupStatus sets the VeleroBackup status field in NonAdminBackup object status and returns true
// if the VeleroBackup fields are changed by this call.
func updateNonAdminBackupVeleroBackupStatus(status *nacv1alpha1.NonAdminBackupStatus, veleroBackup *velerov1.Backup) bool {
func updateNonAdminBackupVeleroBackupStatus(c client.Client, status *nacv1alpha1.NonAdminBackupStatus, veleroBackup *velerov1.Backup) (bool, error) {
if status == nil || veleroBackup == nil {
return false
return false, nil
}

if status.VeleroBackup == nil {
Expand All @@ -747,11 +752,26 @@ func updateNonAdminBackupVeleroBackupStatus(status *nacv1alpha1.NonAdminBackupSt
}

if reflect.DeepEqual(*status.VeleroBackup.Status, veleroBackup.Status) {
return false
return false, nil
}

status.VeleroBackup.Status = veleroBackup.Status.DeepCopy()
return true

// adds logsPath and resourceListPath to status if velero backup is completed.
if status.VeleroBackup.Status.Phase == velerov1.BackupPhaseCompleted {
// logsPath is <protocol>://<bucket>/<prefix>/backups/<backup-name>/<backup-name>-logs.gz
var err error
status.LogsPath, err = paths.BackupLogs(c, veleroBackup)
if err != nil {
return false, err
}
// resourceListPath is <protocol>://<bucket>/<prefix>/backups/<backup-name>/<backup-name>-resource-list.json.gz
status.ResourceListPath, err = paths.BackupResourceList(c, veleroBackup)
if err != nil {
return false, err
}
}
return true, nil
}

// updateNonAdminBackupDeleteBackupRequestStatus sets the VeleroDeleteBackupRequest status field in NonAdminBackup object status and returns true
Expand Down
91 changes: 91 additions & 0 deletions pkg/velero/storagelocation/paths/paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Paths package helps NAC controllers with getting relevant object store paths of interests
// generally as defined in https://github.com/vmware-tanzu/velero/blob/0a280e57863c70495a2dfb65787615a68a0e7b03/pkg/persistence/object_store_layout.go
package paths

import (
"context"
"errors"
"fmt"
"path"

velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// base path
// <protocol>://<bucket>/<prefix>
func BasePathForLocation(c client.Client, storageLocation *velerov1.BackupStorageLocation) (basepath string, err error) {
var protocol string
switch storageLocation.Spec.Provider {
case "aws":
protocol = "s3://"
// TODO: implement azure
// azure format would be source-uri, like https://srcaccount.file.core.windows.net/myshare/mydir...
// see: https://learn.microsoft.com/en-us/cli/azure/storage/file/copy?view=azure-cli-latest#az-storage-file-copy-start
// TODO: implement gcp
// gcp format would be gs://my-bucket, see: https://cloud.google.com/sdk/gcloud/reference/storage/cp
default:
return "", errors.New("Unimplemented provider")
}
return path.Join(protocol, storageLocation.Spec.ObjectStorage.Bucket, storageLocation.Spec.ObjectStorage.Prefix) , nil
}

// base path for a given backup
func BasePathForBackup(c client.Client, backup *velerov1.Backup) (basepath string, err error) {
// get storage location
storageLocationName := backup.Spec.StorageLocation
storageLocation := velerov1.BackupStorageLocation{}
err = c.Get(context.Background(), client.ObjectKey{Name: storageLocationName, Namespace: backup.Namespace}, &storageLocation)
if err != nil {
return "", errors.Join(errors.New("unable to get base path for backup"), err)
}
return BasePathForLocation(c, &storageLocation)
}

// base path for a given restore
func BasePathForRestore(c client.Client, restore *velerov1.Restore) (basepath string, err error) {
// get backup name
backupName := restore.Spec.BackupName
backup := velerov1.Backup{}
err = c.Get(context.Background(), client.ObjectKey{Name: backupName, Namespace: restore.Namespace}, &backup)
if err != nil {
return "", errors.Join(errors.New("unable to get base path for restore"), err)
}
return BasePathForBackup(c, &backup)
}

// Path to logs
// <basepath>/backups/<backup-name>/<backup-name>-logs.gz
func BackupLogs(c client.Client, backup *velerov1.Backup) (string, error){
basepath, err := BasePathForBackup(c, backup)
if err != nil {
return "", err
}
return path.Join(basepath, "backups", backup.Name, fmt.Sprintf("%s-logs.gz", backup.Name)), nil
}

func RestoreLogs(c client.Client, restore *velerov1.Restore) (string, error){
basepath, err := BasePathForRestore(c, restore)
if err != nil {
return "", err
}
return path.Join(basepath, "restores", restore.Name, fmt.Sprintf("restore-%s-logs.gz", restore.Name)), nil
}

// Path to resource list
// <basepath>/backups/<backup-name>/<backup-name>-resource-list.json.gz
func BackupResourceList(c client.Client, backup *velerov1.Backup) (string, error){
basepath, err := BasePathForBackup(c, backup)
if err != nil {
return "", err
}
return path.Join(basepath, "backups", backup.Name, fmt.Sprintf("%s-resource-list.json.gz", backup.Name)), nil
}

func RestoreResourceList (c client.Client, restore *velerov1.Restore) (string, error){
basepath, err := BasePathForRestore(c, restore)
if err != nil {
return "", err
}
return path.Join(basepath, "restores", restore.Name, fmt.Sprintf("restore-%s-resource-list.json.gz", restore.Name)), nil
}

0 comments on commit 3e67c7a

Please sign in to comment.