Skip to content
This repository was archived by the owner on May 28, 2021. It is now read-only.

add Aliyun oss storage provider #260

Open
wants to merge 2 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
241 changes: 216 additions & 25 deletions Gopkg.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ required = [
[[constraint]]
name = "github.com/aws/aws-sdk-go"
version = "1.12.41"

[[constraint]]
name = "github.com/aliyun/aliyun-oss-go-sdk"
version = "1.9.5"
3 changes: 2 additions & 1 deletion pkg/apis/mysql/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ type S3StorageProvider struct {
// StorageProvider defines the configuration for storing a Backup in a storage
// service.
type StorageProvider struct {
S3 *S3StorageProvider `json:"s3"`
S3 *S3StorageProvider `json:"s3"`
ProviderType string `json:"providerType"`
}

// BackupSpec defines the specification for a MySQL backup. This includes what should be backed up,
Expand Down
106 changes: 106 additions & 0 deletions pkg/backup/storage/oss/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright 2018 Oracle and/or its affiliates. All rights reserved.
//
// 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 oss

import (
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/golang/glog"
"github.com/pkg/errors"
"io"

"k8s.io/apimachinery/pkg/util/validation/field"

"github.com/oracle/mysql-operator/pkg/apis/mysql/v1alpha1"
)

// Provider is storage implementation of provider.Interface.
type Provider struct {
v1alpha1.S3StorageProvider

oss *oss.Client
}

// NewProvider creates a new S3 (compatible) storage provider.
func NewProvider(provider *v1alpha1.S3StorageProvider, credentials map[string]string) (*Provider, error) {
accessKey, secretKey, err := getCredentials(credentials)
if err != nil {
return nil, errors.WithStack(err)
}

client, err := oss.New(provider.Endpoint, accessKey, secretKey)

if err != nil {
return nil, errors.WithStack(err)
}

return &Provider{
S3StorageProvider: *provider,
oss: client,
}, nil
}

// Store the given data at the given key.
func (p *Provider) Store(key string, body io.ReadCloser) error {
glog.V(2).Infof("Storing backup (provider=\"S3\", endpoint=%q, bucket=%q, key=%q)", p.Endpoint, p.Bucket, key)

defer body.Close()

bucket, err := p.oss.Bucket(p.Bucket)
if err != nil {
return errors.Wrapf(err, "error storing backup (provider=\"S3\", endpoint=%q, bucket=%q, key=%q)", p.Endpoint, p.Bucket, key)
}
err = bucket.PutObject(key, body)
return errors.Wrapf(err, "error storing backup (provider=\"S3\", endpoint=%q, bucket=%q, key=%q)", p.Endpoint, p.Bucket, key)
}

// Retrieve the given key from S3 storage service.
func (p *Provider) Retrieve(key string) (io.ReadCloser, error) {
glog.V(2).Infof("Retrieving backup (provider=\"s3\", endpoint=%q, bucket=%q, key=%q)", p.Endpoint, p.Bucket, key)
bucket, err := p.oss.Bucket(p.Bucket)
if err != nil {
return nil, errors.Wrapf(err, "error retrieving backup (provider='S3', endpoint='%s', bucket='%s', key='%s')", p.Endpoint, p.Bucket, key)
}
obj, err := bucket.GetObject(key)
if err != nil {
return nil, errors.Wrapf(err, "error retrieving backup (provider='S3', endpoint='%s', bucket='%s', key='%s')", p.Endpoint, p.Bucket, key)
}

return obj, nil
}

// getCredentials gets an accesskey and secretKey from the provided map.
func getCredentials(credentials map[string]string) (string, string, error) {
allErrs := field.ErrorList{}
fldPath := field.NewPath("data")

if credentials == nil {
return "", "", errors.New("no credentials provided")
}

accessKey, ok := credentials["accessKey"]
if !ok {
allErrs = append(allErrs, field.Required(fldPath.Child("accessKey"), ""))
}
secretKey, ok := credentials["secretKey"]
if !ok {
allErrs = append(allErrs, field.Required(fldPath.Child("secretKey"), ""))
}

if len(allErrs) > 0 {
return "", "", allErrs.ToAggregate()
}

return accessKey, secretKey, nil
}
11 changes: 9 additions & 2 deletions pkg/backup/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
package storage

import (
"github.com/oracle/mysql-operator/pkg/backup/storage/oss"
"github.com/oracle/mysql-operator/pkg/backup/storage/s3"
"io"

"github.com/oracle/mysql-operator/pkg/apis/mysql/v1alpha1"
"github.com/oracle/mysql-operator/pkg/backup/storage/s3"
)

// Interface abstracts the underlying storage provider.
Expand All @@ -33,5 +34,11 @@ type Interface interface {
// NewStorageProvider accepts a secret map and uses its contents to determine the
// desired object storage provider implementation.
func NewStorageProvider(config v1alpha1.StorageProvider, credentials map[string]string) (Interface, error) {
return s3.NewProvider(config.S3, credentials)
if config.ProviderType == "s3" {
return s3.NewProvider(config.S3, credentials)
} else if config.ProviderType == "oss" {
return oss.NewProvider(config.S3, credentials)
} else {
return nil, nil
}
}
30 changes: 30 additions & 0 deletions vendor/github.com/aliyun/aliyun-oss-go-sdk/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

119 changes: 119 additions & 0 deletions vendor/github.com/aliyun/aliyun-oss-go-sdk/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading