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

Commit

Permalink
*: support backup timeout in backup operator
Browse files Browse the repository at this point in the history
  • Loading branch information
fanminshi committed Feb 6, 2018
1 parent a641328 commit a0799fb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
8 changes: 5 additions & 3 deletions pkg/backup/backup_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"

"github.com/coreos/etcd-operator/pkg/backup/writer"
"github.com/coreos/etcd-operator/pkg/util/constants"

"github.com/coreos/etcd/clientv3"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -91,10 +92,11 @@ func getClientWithMaxRev(ctx context.Context, endpoints []string, tc *tls.Config
maxRev := int64(0)
errors := make([]string, 0)
for _, endpoint := range endpoints {
// TODO: update clientv3 to 3.2.x and thenuse ctx as in clientv3.Config.
cfg := clientv3.Config{
Endpoints: []string{endpoint},
Context: ctx,
TLS: tc,
Endpoints: []string{endpoint},
DialTimeout: constants.DefaultDialTimeout,
TLS: tc,
}
etcdcli, err := clientv3.New(cfg)
if err != nil {
Expand Down
7 changes: 5 additions & 2 deletions pkg/controller/backup-operator/abs_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package controller

import (
"context"
"crypto/tls"
"fmt"

Expand All @@ -27,7 +28,8 @@ import (
)

// 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) {
func handleABS(ctx context.Context, kubecli kubernetes.Interface, s *api.ABSBackupSource, endpoints []string, clientTLSSecret, namespace string) (*api.BackupStatus, error) {
// TODO: controls NewClientFromSecret with ctx. This depends on upstream kubernetes to support API calls with ctx.
cli, err := absfactory.NewClientFromSecret(kubecli, namespace, s.ABSSecret)
if err != nil {
return nil, err
Expand All @@ -39,7 +41,8 @@ func handleABS(kubecli kubernetes.Interface, s *api.ABSBackupSource, endpoints [
}

bm := backup.NewBackupManagerFromWriter(kubecli, writer.NewABSWriter(cli.ABS), tlsConfig, endpoints, namespace)
rev, etcdVersion, err := bm.SaveSnap(s.Path)

rev, etcdVersion, err := bm.SaveSnap(ctx, s.Path)
if err != nil {
return nil, fmt.Errorf("failed to save snapshot (%v)", err)
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/controller/backup-operator/s3_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package controller

import (
"context"
"crypto/tls"
"fmt"

Expand All @@ -28,7 +29,8 @@ import (

// TODO: replace this with generic backend interface for other options (PV, Azure)
// handleS3 saves etcd cluster's backup to specificed S3 path.
func handleS3(kubecli kubernetes.Interface, s *api.S3BackupSource, endpoints []string, clientTLSSecret, namespace string) (*api.BackupStatus, error) {
func handleS3(ctx context.Context, kubecli kubernetes.Interface, s *api.S3BackupSource, endpoints []string, clientTLSSecret, namespace string) (*api.BackupStatus, error) {
// TODO: controls NewClientFromSecret with ctx. This depends on upstream kubernetes to support API calls with ctx.
cli, err := s3factory.NewClientFromSecret(kubecli, namespace, s.Endpoint, s.AWSSecret)
if err != nil {
return nil, err
Expand All @@ -41,7 +43,8 @@ func handleS3(kubecli kubernetes.Interface, s *api.S3BackupSource, endpoints []s
}

bm := backup.NewBackupManagerFromWriter(kubecli, writer.NewS3Writer(cli.S3), tlsConfig, endpoints, namespace)
rev, etcdVersion, err := bm.SaveSnap(s.Path)

rev, etcdVersion, err := bm.SaveSnap(ctx, s.Path)
if err != nil {
return nil, fmt.Errorf("failed to save snapshot (%v)", err)
}
Expand Down
15 changes: 13 additions & 2 deletions pkg/controller/backup-operator/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
package controller

import (
"context"
"errors"
"time"

api "github.com/coreos/etcd-operator/pkg/apis/etcd/v1beta2"
"github.com/coreos/etcd-operator/pkg/util/constants"

"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -119,15 +122,23 @@ func (b *Backup) handleBackup(spec *api.BackupSpec) (*api.BackupStatus, error) {
return nil, err
}

// When BackupPolicy.Timeout <= 0, use default DefaultBackupTimeout.
backupTimeout := time.Duration(constants.DefaultBackupTimeout)
if spec.BackupPolicy != nil && spec.BackupPolicy.TimeoutInSecond > 0 {
backupTimeout = time.Duration(spec.BackupPolicy.TimeoutInSecond) * time.Second
}

ctx, cancel := context.WithTimeout(context.Background(), backupTimeout)
defer cancel()
switch spec.StorageType {
case api.BackupStorageTypeS3:
bs, err := handleS3(b.kubecli, spec.S3, spec.EtcdEndpoints, spec.ClientTLSSecret, b.namespace)
bs, err := handleS3(ctx, b.kubecli, spec.S3, spec.EtcdEndpoints, spec.ClientTLSSecret, b.namespace)
if err != nil {
return nil, err
}
return bs, nil
case api.BackupStorageTypeABS:
bs, err := handleABS(b.kubecli, spec.ABS, spec.EtcdEndpoints, spec.ClientTLSSecret, b.namespace)
bs, err := handleABS(ctx, b.kubecli, spec.ABS, spec.EtcdEndpoints, spec.ClientTLSSecret, b.namespace)
if err != nil {
return nil, err
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/util/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ package constants
import "time"

const (
DefaultDialTimeout = 5 * time.Second
DefaultRequestTimeout = 5 * time.Second
DefaultSnapshotTimeout = 1 * time.Minute
DefaultDialTimeout = 5 * time.Second
DefaultRequestTimeout = 5 * time.Second
// DefaultBackupTimeout is the maximal allowed time of retriving and saving etcd backup.
DefaultBackupTimeout = 1 * time.Minute
DefaultSnapshotInterval = 1800 * time.Second

DefaultBackupPodHTTPPort = 19999
Expand Down

0 comments on commit a0799fb

Please sign in to comment.