2018-06-06 21:32:28 +00:00
|
|
|
/*
|
2019-03-29 20:32:43 +00:00
|
|
|
Copyright 2018, 2019 the Velero contributors.
|
2018-06-06 21:32:28 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package restic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-09-19 18:51:30 +00:00
|
|
|
"path"
|
2018-06-06 21:32:28 +00:00
|
|
|
"strings"
|
|
|
|
|
2019-01-25 03:33:07 +00:00
|
|
|
velerov1api "github.com/heptio/velero/pkg/apis/velero/v1"
|
|
|
|
"github.com/heptio/velero/pkg/cloudprovider/aws"
|
|
|
|
"github.com/heptio/velero/pkg/persistence"
|
2018-06-06 21:32:28 +00:00
|
|
|
)
|
|
|
|
|
2018-06-15 03:24:01 +00:00
|
|
|
type BackendType string
|
|
|
|
|
|
|
|
const (
|
2019-04-16 16:21:50 +00:00
|
|
|
AWSBackend BackendType = "velero.io/aws"
|
|
|
|
AzureBackend BackendType = "velero.io/azure"
|
|
|
|
GCPBackend BackendType = "velero.io/gcp"
|
2018-06-15 03:24:01 +00:00
|
|
|
)
|
|
|
|
|
2018-06-18 17:54:07 +00:00
|
|
|
// this func is assigned to a package-level variable so it can be
|
|
|
|
// replaced when unit-testing
|
|
|
|
var getAWSBucketRegion = aws.GetBucketRegion
|
|
|
|
|
2018-06-06 21:32:28 +00:00
|
|
|
// getRepoPrefix returns the prefix of the value of the --repo flag for
|
|
|
|
// restic commands, i.e. everything except the "/<repo-name>".
|
2019-01-25 03:33:07 +00:00
|
|
|
func getRepoPrefix(location *velerov1api.BackupStorageLocation) string {
|
2018-09-19 18:51:30 +00:00
|
|
|
var provider, bucket, prefix, bucketAndPrefix string
|
|
|
|
|
|
|
|
if location.Spec.ObjectStorage != nil {
|
|
|
|
layout := persistence.NewObjectStoreLayout(location.Spec.ObjectStorage.Prefix)
|
|
|
|
|
|
|
|
bucket = location.Spec.ObjectStorage.Bucket
|
|
|
|
prefix = layout.GetResticDir()
|
2018-06-06 21:32:28 +00:00
|
|
|
}
|
2018-09-19 18:51:30 +00:00
|
|
|
bucketAndPrefix = path.Join(bucket, prefix)
|
2018-06-06 21:32:28 +00:00
|
|
|
|
2019-04-16 16:21:50 +00:00
|
|
|
var locationSpecProvider = location.Spec.Provider
|
|
|
|
if !strings.Contains(locationSpecProvider, "/") {
|
|
|
|
locationSpecProvider = "velero.io/" + locationSpecProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
switch BackendType(locationSpecProvider) {
|
2018-06-22 00:20:53 +00:00
|
|
|
case AWSBackend:
|
|
|
|
var url string
|
|
|
|
switch {
|
|
|
|
// non-AWS, S3-compatible object store
|
2018-08-09 20:28:57 +00:00
|
|
|
case location.Spec.Config["s3Url"] != "":
|
|
|
|
url = location.Spec.Config["s3Url"]
|
2018-06-22 00:20:53 +00:00
|
|
|
default:
|
2018-06-18 17:54:07 +00:00
|
|
|
region, err := getAWSBucketRegion(bucket)
|
2018-06-22 00:20:53 +00:00
|
|
|
if err != nil {
|
|
|
|
url = "s3.amazonaws.com"
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
url = fmt.Sprintf("s3-%s.amazonaws.com", region)
|
|
|
|
}
|
|
|
|
|
2019-03-29 20:32:43 +00:00
|
|
|
return fmt.Sprintf("s3:%s/%s", strings.TrimSuffix(url, "/"), bucketAndPrefix)
|
2018-06-06 21:32:28 +00:00
|
|
|
case AzureBackend:
|
2018-09-19 18:51:30 +00:00
|
|
|
provider = "azure"
|
2018-06-06 21:32:28 +00:00
|
|
|
case GCPBackend:
|
2018-09-19 18:51:30 +00:00
|
|
|
provider = "gs"
|
2018-06-06 21:32:28 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 18:51:30 +00:00
|
|
|
return fmt.Sprintf("%s:%s:/%s", provider, bucket, prefix)
|
2018-06-06 21:32:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetRepoIdentifier returns the string to be used as the value of the --repo flag in
|
|
|
|
// restic commands for the given repository.
|
2019-01-25 03:33:07 +00:00
|
|
|
func GetRepoIdentifier(location *velerov1api.BackupStorageLocation, name string) string {
|
2018-08-09 20:28:57 +00:00
|
|
|
prefix := getRepoPrefix(location)
|
2018-06-06 21:32:28 +00:00
|
|
|
|
|
|
|
return fmt.Sprintf("%s/%s", strings.TrimSuffix(prefix, "/"), name)
|
|
|
|
}
|