github.com/percona/percona-xtradb-cluster-operator@v1.14.0/pkg/pxc/backup/storage/options.go (about)

     1  package storage
     2  
     3  import (
     4  	"context"
     5  
     6  	api "github.com/percona/percona-xtradb-cluster-operator/pkg/apis/pxc/v1"
     7  	"github.com/pkg/errors"
     8  	corev1 "k8s.io/api/core/v1"
     9  	"k8s.io/apimachinery/pkg/types"
    10  	"sigs.k8s.io/controller-runtime/pkg/client"
    11  )
    12  
    13  type Options interface {
    14  	Type() api.BackupStorageType
    15  }
    16  
    17  func GetOptionsFromBackup(ctx context.Context, cl client.Client, cluster *api.PerconaXtraDBCluster, backup *api.PerconaXtraDBClusterBackup) (Options, error) {
    18  	switch {
    19  	case backup.Status.S3 != nil:
    20  		return getS3Options(ctx, cl, cluster, backup)
    21  	case backup.Status.Azure != nil:
    22  		return getAzureOptions(ctx, cl, backup)
    23  	default:
    24  		return nil, errors.Errorf("unknown storage type %s", backup.Status.StorageType)
    25  	}
    26  }
    27  
    28  func getAzureOptions(ctx context.Context, cl client.Client, backup *api.PerconaXtraDBClusterBackup) (*AzureOptions, error) {
    29  	secret := new(corev1.Secret)
    30  	err := cl.Get(ctx, types.NamespacedName{
    31  		Name:      backup.Status.Azure.CredentialsSecret,
    32  		Namespace: backup.Namespace,
    33  	}, secret)
    34  	if err != nil {
    35  		return nil, errors.Wrap(err, "failed to get secret")
    36  	}
    37  	accountName := string(secret.Data["AZURE_STORAGE_ACCOUNT_NAME"])
    38  	accountKey := string(secret.Data["AZURE_STORAGE_ACCOUNT_KEY"])
    39  
    40  	container, prefix := backup.Status.Azure.ContainerAndPrefix()
    41  	if container == "" {
    42  		container, prefix = backup.Status.Destination.BucketAndPrefix()
    43  	}
    44  
    45  	if container == "" {
    46  		return nil, errors.New("container name is not set")
    47  	}
    48  
    49  	return &AzureOptions{
    50  		StorageAccount: accountName,
    51  		AccessKey:      accountKey,
    52  		Endpoint:       backup.Status.Azure.Endpoint,
    53  		Container:      container,
    54  		Prefix:         prefix,
    55  	}, nil
    56  }
    57  
    58  func getS3Options(ctx context.Context, cl client.Client, cluster *api.PerconaXtraDBCluster, backup *api.PerconaXtraDBClusterBackup) (*S3Options, error) {
    59  	secret := new(corev1.Secret)
    60  	err := cl.Get(ctx, types.NamespacedName{
    61  		Name:      backup.Status.S3.CredentialsSecret,
    62  		Namespace: backup.Namespace,
    63  	}, secret)
    64  	if client.IgnoreNotFound(err) != nil {
    65  		return nil, errors.Wrap(err, "failed to get secret")
    66  	}
    67  	accessKeyID := string(secret.Data["AWS_ACCESS_KEY_ID"])
    68  	secretAccessKey := string(secret.Data["AWS_SECRET_ACCESS_KEY"])
    69  
    70  	bucket, prefix := backup.Status.S3.BucketAndPrefix()
    71  	if bucket == "" {
    72  		bucket, prefix = backup.Status.Destination.BucketAndPrefix()
    73  	}
    74  
    75  	if bucket == "" {
    76  		return nil, errors.New("bucket name is not set")
    77  	}
    78  
    79  	region := backup.Status.S3.Region
    80  	if region == "" {
    81  		region = "us-east-1"
    82  	}
    83  
    84  	verifyTLS := true
    85  	if backup.Status.VerifyTLS != nil && !*backup.Status.VerifyTLS {
    86  		verifyTLS = false
    87  	}
    88  	if cluster != nil && cluster.Spec.Backup != nil && len(cluster.Spec.Backup.Storages) > 0 {
    89  		storage, ok := cluster.Spec.Backup.Storages[backup.Spec.StorageName]
    90  		if ok && storage.VerifyTLS != nil {
    91  			verifyTLS = *storage.VerifyTLS
    92  		}
    93  	}
    94  
    95  	return &S3Options{
    96  		Endpoint:        backup.Status.S3.EndpointURL,
    97  		AccessKeyID:     accessKeyID,
    98  		SecretAccessKey: secretAccessKey,
    99  		BucketName:      bucket,
   100  		Prefix:          prefix,
   101  		Region:          region,
   102  		VerifyTLS:       verifyTLS,
   103  	}, nil
   104  }
   105  
   106  var _ = Options(new(S3Options))
   107  
   108  type S3Options struct {
   109  	Endpoint        string
   110  	AccessKeyID     string
   111  	SecretAccessKey string
   112  	BucketName      string
   113  	Prefix          string
   114  	Region          string
   115  	VerifyTLS       bool
   116  }
   117  
   118  func (o *S3Options) Type() api.BackupStorageType {
   119  	return api.BackupStorageS3
   120  }
   121  
   122  var _ = Options(new(AzureOptions))
   123  
   124  type AzureOptions struct {
   125  	StorageAccount string
   126  	AccessKey      string
   127  	Endpoint       string
   128  	Container      string
   129  	Prefix         string
   130  }
   131  
   132  func (o *AzureOptions) Type() api.BackupStorageType {
   133  	return api.BackupStorageAzure
   134  }