Skip to content
This repository has been archived by the owner on Mar 28, 2020. It is now read-only.

Commit

Permalink
refactoring and address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
Jingtao Ren committed Jan 25, 2018
1 parent daaea5d commit e26c421
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 44 deletions.
6 changes: 1 addition & 5 deletions Gopkg.lock

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

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@

[[constraint]]
name = "golang.org/x/time"

[[constraint]]
name = "github.com/Azure/azure-sdk-for-go"
version = "v11.3.0-beta"
58 changes: 58 additions & 0 deletions pkg/apis/etcd/v1beta2/zz_generated.deepcopy.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ import (
// Deprecated: deepcopy registration will go away when static deepcopy is fully implemented.
func GetGeneratedDeepCopyFuncs() []conversion.GeneratedDeepCopyFunc {
return []conversion.GeneratedDeepCopyFunc{
{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*ABSBackupSource).DeepCopyInto(out.(*ABSBackupSource))
return nil
}, InType: reflect.TypeOf(&ABSBackupSource{})},
{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*ABSRestoreSource).DeepCopyInto(out.(*ABSRestoreSource))
return nil
}, InType: reflect.TypeOf(&ABSRestoreSource{})},
{Fn: func(in interface{}, out interface{}, c *conversion.Cloner) error {
in.(*BackupSource).DeepCopyInto(out.(*BackupSource))
return nil
Expand Down Expand Up @@ -131,6 +139,38 @@ func GetGeneratedDeepCopyFuncs() []conversion.GeneratedDeepCopyFunc {
}
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *ABSBackupSource) DeepCopyInto(out *ABSBackupSource) {
*out = *in
return
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ABSBackupSource.
func (in *ABSBackupSource) DeepCopy() *ABSBackupSource {
if in == nil {
return nil
}
out := new(ABSBackupSource)
in.DeepCopyInto(out)
return out
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *ABSRestoreSource) DeepCopyInto(out *ABSRestoreSource) {
*out = *in
return
}

// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ABSRestoreSource.
func (in *ABSRestoreSource) DeepCopy() *ABSRestoreSource {
if in == nil {
return nil
}
out := new(ABSRestoreSource)
in.DeepCopyInto(out)
return out
}

// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *BackupSource) DeepCopyInto(out *BackupSource) {
*out = *in
Expand All @@ -143,6 +183,15 @@ func (in *BackupSource) DeepCopyInto(out *BackupSource) {
**out = **in
}
}
if in.ABS != nil {
in, out := &in.ABS, &out.ABS
if *in == nil {
*out = nil
} else {
*out = new(ABSBackupSource)
**out = **in
}
}
return
}

Expand Down Expand Up @@ -597,6 +646,15 @@ func (in *RestoreSource) DeepCopyInto(out *RestoreSource) {
**out = **in
}
}
if in.ABS != nil {
in, out := &in.ABS, &out.ABS
if *in == nil {
*out = nil
} else {
*out = new(ABSRestoreSource)
**out = **in
}
}
return
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/backup/reader/abs_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type absReader struct {
abs *storage.BlobStorageClient
}

// NewABSReader return a Reader implementation to read a file from ABS in the form of absReader
func NewABSReader(abs *storage.BlobStorageClient) Reader {
return &absReader{abs}
}
Expand All @@ -53,6 +54,5 @@ func (absr *absReader) Open(path string) (io.ReadCloser, error) {
}

blob := containerRef.GetBlobReference(key)
getBlobOpts := &storage.GetBlobOptions{}
return blob.Get(getBlobOpts)
return blob.Get(&storage.GetBlobOptions{})
}
12 changes: 5 additions & 7 deletions pkg/backup/writer/abs_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ import (
"fmt"
"io"

"github.com/Azure/azure-sdk-for-go/storage"
"github.com/coreos/etcd-operator/pkg/backup/util"

"github.com/Azure/azure-sdk-for-go/storage"
"github.com/pborman/uuid"
)

Expand All @@ -38,7 +39,7 @@ func NewABSWriter(abs *storage.BlobStorageClient) Writer {

const (
// AzureBlobBlockChunkLimitInBytes 100MiB is the limit
AzureBlobBlockChunkLimitInBytes = 104857600
AzureBlobBlockChunkLimitInBytes = 100 * 1024 * 1024
)

// Write writes the backup file to the given abs path, "<abs-container-name>/<key>".
Expand All @@ -59,9 +60,7 @@ func (absw *absWriter) Write(path string, r io.Reader) (int64, error) {
}

blob := containerRef.GetBlobReference(key)
putBlobOpts := storage.PutBlobOptions{}

err = blob.CreateBlockBlob(&putBlobOpts)
err = blob.CreateBlockBlob(&storage.PutBlobOptions{})
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -92,8 +91,7 @@ func (absw *absWriter) Write(path string, r io.Reader) (int64, error) {
return 0, err
}

getBlobOpts := &storage.GetBlobOptions{}
_, err = blob.Get(getBlobOpts)
_, err = blob.Get(&storage.GetBlobOptions{})
if err != nil {
return 0, err
}
Expand Down
14 changes: 2 additions & 12 deletions pkg/controller/backup-operator/abs_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ import (
"github.com/coreos/etcd-operator/pkg/backup"
"github.com/coreos/etcd-operator/pkg/backup/writer"
"github.com/coreos/etcd-operator/pkg/util/azureutil/absfactory"
"github.com/coreos/etcd-operator/pkg/util/etcdutil"
"github.com/coreos/etcd-operator/pkg/util/k8sutil"

"k8s.io/client-go/kubernetes"
)

// TODO: replace this with generic backend interface for other options (PV, Azure)
// handleABS saves etcd cluster's backup to specificed ABS path.
func handleABS(kubecli kubernetes.Interface, s *api.ABSBackupSource, endpoints []string, clientTLSSecret, namespace string) (*api.BackupStatus, error) {
cli, err := absfactory.NewClientFromSecret(kubecli, namespace, s.ABSSecret)
Expand All @@ -37,15 +34,8 @@ func handleABS(kubecli kubernetes.Interface, s *api.ABSBackupSource, endpoints [
}

var tlsConfig *tls.Config
if len(clientTLSSecret) != 0 {
d, err := k8sutil.GetTLSDataFromSecret(kubecli, namespace, clientTLSSecret)
if err != nil {
return nil, fmt.Errorf("failed to get TLS data from secret (%v): %v", clientTLSSecret, err)
}
tlsConfig, err = etcdutil.NewTLSConfig(d.CertData, d.KeyData, d.CAData)
if err != nil {
return nil, fmt.Errorf("failed to constructs tls config: %v", err)
}
if tlsConfig, err = generateTLSConfig(kubecli, clientTLSSecret, namespace); err != nil {
return nil, err
}

bm := backup.NewBackupManagerFromWriter(kubecli, writer.NewABSWriter(cli.ABS), tlsConfig, endpoints, namespace)
Expand Down
13 changes: 2 additions & 11 deletions pkg/controller/backup-operator/s3_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import (
"github.com/coreos/etcd-operator/pkg/backup"
"github.com/coreos/etcd-operator/pkg/backup/writer"
"github.com/coreos/etcd-operator/pkg/util/awsutil/s3factory"
"github.com/coreos/etcd-operator/pkg/util/etcdutil"
"github.com/coreos/etcd-operator/pkg/util/k8sutil"

"k8s.io/client-go/kubernetes"
)
Expand All @@ -38,15 +36,8 @@ func handleS3(kubecli kubernetes.Interface, s *api.S3BackupSource, endpoints []s
defer cli.Close()

var tlsConfig *tls.Config
if len(clientTLSSecret) != 0 {
d, err := k8sutil.GetTLSDataFromSecret(kubecli, namespace, clientTLSSecret)
if err != nil {
return nil, fmt.Errorf("failed to get TLS data from secret (%v): %v", clientTLSSecret, err)
}
tlsConfig, err = etcdutil.NewTLSConfig(d.CertData, d.KeyData, d.CAData)
if err != nil {
return nil, fmt.Errorf("failed to constructs tls config: %v", err)
}
if tlsConfig, err = generateTLSConfig(kubecli, clientTLSSecret, namespace); err != nil {
return nil, err
}

bm := backup.NewBackupManagerFromWriter(kubecli, writer.NewS3Writer(cli.S3), tlsConfig, endpoints, namespace)
Expand Down
40 changes: 40 additions & 0 deletions pkg/controller/backup-operator/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2017 The etcd-operator Authors
//
// 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 controller

import (
"crypto/tls"
"fmt"

"github.com/coreos/etcd-operator/pkg/util/etcdutil"
"github.com/coreos/etcd-operator/pkg/util/k8sutil"

"k8s.io/client-go/kubernetes"
)

func generateTLSConfig(kubecli kubernetes.Interface, clientTLSSecret, namespace string) (*tls.Config, error) {
var tlsConfig *tls.Config
if len(clientTLSSecret) != 0 {
d, err := k8sutil.GetTLSDataFromSecret(kubecli, namespace, clientTLSSecret)
if err != nil {
return nil, fmt.Errorf("failed to get TLS data from secret (%v): %v", clientTLSSecret, err)
}
tlsConfig, err = etcdutil.NewTLSConfig(d.CertData, d.KeyData, d.CAData)
if err != nil {
return nil, fmt.Errorf("failed to constructs tls config: %v", err)
}
}
return tlsConfig, nil
}
8 changes: 1 addition & 7 deletions pkg/util/azureutil/absfactory/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ import (
"k8s.io/client-go/kubernetes"
)

const (
tmpdir = "/tmp"
)

// ABSClient is a wrapper of ABS client that provides cleanup functionality.
type ABSClient struct {
ABS *storage.BlobStorageClient
Expand All @@ -41,7 +37,6 @@ func NewClientFromSecret(kubecli kubernetes.Interface, namespace, absSecret stri
}
}()

w = &ABSClient{}
se, err := kubecli.CoreV1().Secrets(namespace).Get(absSecret, metav1.GetOptions{})
if err != nil {
return nil, fmt.Errorf("failed to get k8s secret: %v", err)
Expand All @@ -58,6 +53,5 @@ func NewClientFromSecret(kubecli kubernetes.Interface, namespace, absSecret stri
}

abs := bc.GetBlobService()
w.ABS = &abs
return w, nil
return &ABSClient{ABS: &abs}, nil
}

0 comments on commit e26c421

Please sign in to comment.