-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Tiger Kaovilai <[email protected]>
- Loading branch information
Showing
2 changed files
with
116 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |