Passing ks.logger in the call chain
Signed-off-by: hu-keyu <hzldd999@gmail.com>pull/8868/head
parent
eea978315b
commit
de90c3e170
|
@ -18,6 +18,7 @@ package backend
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/kopia/kopia/repo/blob"
|
||||
|
||||
|
@ -29,17 +30,19 @@ type AzureBackend struct {
|
|||
option azure.Option
|
||||
}
|
||||
|
||||
func (c *AzureBackend) Setup(ctx context.Context, flags map[string]string) error {
|
||||
func (c *AzureBackend) Setup(ctx context.Context, flags map[string]string, logger logrus.FieldLogger) error {
|
||||
if flags[udmrepo.StoreOptionCACert] != "" {
|
||||
flags["caCertEncoded"] = "true"
|
||||
}
|
||||
c.option = azure.Option{
|
||||
Config: flags,
|
||||
Limits: setupLimits(ctx, flags),
|
||||
Logger: logger,
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *AzureBackend) Connect(ctx context.Context, isCreate bool) (blob.Storage, error) {
|
||||
func (c *AzureBackend) Connect(ctx context.Context, isCreate bool, logger logrus.FieldLogger) (blob.Storage, error) {
|
||||
c.option.Logger = logger
|
||||
return azure.NewStorage(ctx, &c.option, false)
|
||||
}
|
||||
|
|
|
@ -21,8 +21,6 @@ import (
|
|||
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/vmware-tanzu/velero/pkg/util/logging"
|
||||
|
||||
"github.com/kopia/kopia/repo/blob"
|
||||
"github.com/kopia/kopia/repo/blob/azure"
|
||||
"github.com/kopia/kopia/repo/blob/throttling"
|
||||
|
@ -36,12 +34,13 @@ const (
|
|||
)
|
||||
|
||||
func init() {
|
||||
blob.AddSupportedStorage(storageType, Option{}, NewStorage)
|
||||
blob.AddSupportedStorage(storageType, Option{Logger: logrus.New()}, NewStorage)
|
||||
}
|
||||
|
||||
type Option struct {
|
||||
Config map[string]string `json:"config" kopia:"sensitive"`
|
||||
Limits throttling.Limits
|
||||
Logger logrus.FieldLogger
|
||||
}
|
||||
|
||||
type Storage struct {
|
||||
|
@ -59,7 +58,7 @@ func (s *Storage) ConnectionInfo() blob.ConnectionInfo {
|
|||
func NewStorage(ctx context.Context, option *Option, isCreate bool) (blob.Storage, error) {
|
||||
cfg := option.Config
|
||||
|
||||
client, _, err := azureutil.NewStorageClient(logging.DefaultLogger(logrus.InfoLevel, logging.FormatJSON), cfg)
|
||||
client, _, err := azureutil.NewStorageClient(option.Logger, cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ package backend
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/kopia/kopia/repo/blob"
|
||||
)
|
||||
|
@ -26,8 +27,8 @@ import (
|
|||
// the backend storage
|
||||
type Store interface {
|
||||
// Setup setups the variables to a specific backend storage
|
||||
Setup(ctx context.Context, flags map[string]string) error
|
||||
Setup(ctx context.Context, flags map[string]string, logger logrus.FieldLogger) error
|
||||
|
||||
// Connect connects to a specific backend storage with the storage variables
|
||||
Connect(ctx context.Context, isCreate bool) (blob.Storage, error)
|
||||
Connect(ctx context.Context, isCreate bool, logger logrus.FieldLogger) (blob.Storage, error)
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ package backend
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/sirupsen/logrus"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/kopia/kopia/repo/blob"
|
||||
|
@ -36,7 +37,7 @@ const (
|
|||
defaultDirMode = 0o700
|
||||
)
|
||||
|
||||
func (c *FsBackend) Setup(ctx context.Context, flags map[string]string) error {
|
||||
func (c *FsBackend) Setup(ctx context.Context, flags map[string]string, logger logrus.FieldLogger) error {
|
||||
path, err := mustHaveString(udmrepo.StoreOptionFsPath, flags)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -53,7 +54,7 @@ func (c *FsBackend) Setup(ctx context.Context, flags map[string]string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *FsBackend) Connect(ctx context.Context, isCreate bool) (blob.Storage, error) {
|
||||
func (c *FsBackend) Connect(ctx context.Context, isCreate bool, logger logrus.FieldLogger) (blob.Storage, error) {
|
||||
if !filepath.IsAbs(c.options.Path) {
|
||||
return nil, errors.Errorf("filesystem repository path is not absolute, path: %s", c.options.Path)
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ package backend
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/kopia/kopia/repo/blob"
|
||||
"github.com/kopia/kopia/repo/blob/gcs"
|
||||
|
@ -29,7 +30,7 @@ type GCSBackend struct {
|
|||
options gcs.Options
|
||||
}
|
||||
|
||||
func (c *GCSBackend) Setup(ctx context.Context, flags map[string]string) error {
|
||||
func (c *GCSBackend) Setup(ctx context.Context, flags map[string]string, logger logrus.FieldLogger) error {
|
||||
var err error
|
||||
c.options.BucketName, err = mustHaveString(udmrepo.StoreOptionOssBucket, flags)
|
||||
if err != nil {
|
||||
|
@ -49,6 +50,6 @@ func (c *GCSBackend) Setup(ctx context.Context, flags map[string]string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *GCSBackend) Connect(ctx context.Context, isCreate bool) (blob.Storage, error) {
|
||||
func (c *GCSBackend) Connect(ctx context.Context, isCreate bool, logger logrus.FieldLogger) (blob.Storage, error) {
|
||||
return gcs.New(ctx, &c.options, false)
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ package mocks
|
|||
|
||||
import (
|
||||
context "context"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
blob "github.com/kopia/kopia/repo/blob"
|
||||
|
||||
|
@ -16,7 +17,7 @@ type Store struct {
|
|||
}
|
||||
|
||||
// Connect provides a mock function with given fields: ctx, isCreate
|
||||
func (_m *Store) Connect(ctx context.Context, isCreate bool) (blob.Storage, error) {
|
||||
func (_m *Store) Connect(ctx context.Context, isCreate bool, logger logrus.FieldLogger) (blob.Storage, error) {
|
||||
ret := _m.Called(ctx, isCreate)
|
||||
|
||||
var r0 blob.Storage
|
||||
|
@ -39,7 +40,7 @@ func (_m *Store) Connect(ctx context.Context, isCreate bool) (blob.Storage, erro
|
|||
}
|
||||
|
||||
// Setup provides a mock function with given fields: ctx, flags
|
||||
func (_m *Store) Setup(ctx context.Context, flags map[string]string) error {
|
||||
func (_m *Store) Setup(ctx context.Context, flags map[string]string, logger logrus.FieldLogger) error {
|
||||
ret := _m.Called(ctx, flags)
|
||||
|
||||
var r0 error
|
||||
|
|
|
@ -18,6 +18,7 @@ package backend
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/kopia/kopia/repo/blob"
|
||||
"github.com/kopia/kopia/repo/blob/s3"
|
||||
|
@ -29,7 +30,7 @@ type S3Backend struct {
|
|||
options s3.Options
|
||||
}
|
||||
|
||||
func (c *S3Backend) Setup(ctx context.Context, flags map[string]string) error {
|
||||
func (c *S3Backend) Setup(ctx context.Context, flags map[string]string, logger logrus.FieldLogger) error {
|
||||
var err error
|
||||
c.options.BucketName, err = mustHaveString(udmrepo.StoreOptionOssBucket, flags)
|
||||
if err != nil {
|
||||
|
@ -51,6 +52,6 @@ func (c *S3Backend) Setup(ctx context.Context, flags map[string]string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (c *S3Backend) Connect(ctx context.Context, isCreate bool) (blob.Storage, error) {
|
||||
func (c *S3Backend) Connect(ctx context.Context, isCreate bool, logger logrus.FieldLogger) (blob.Storage, error) {
|
||||
return s3.New(ctx, &c.options, false)
|
||||
}
|
||||
|
|
|
@ -96,13 +96,13 @@ func (ks *kopiaRepoService) Init(ctx context.Context, repoOption udmrepo.RepoOpt
|
|||
repoCtx := kopia.SetupKopiaLog(ctx, ks.logger)
|
||||
|
||||
if createNew {
|
||||
if err := CreateBackupRepo(repoCtx, repoOption); err != nil {
|
||||
if err := CreateBackupRepo(repoCtx, repoOption, ks.logger); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return writeInitParameters(repoCtx, repoOption, ks.logger)
|
||||
}
|
||||
return ConnectBackupRepo(repoCtx, repoOption)
|
||||
return ConnectBackupRepo(repoCtx, repoOption, ks.logger)
|
||||
}
|
||||
|
||||
func (ks *kopiaRepoService) Open(ctx context.Context, repoOption udmrepo.RepoOptions) (udmrepo.BackupRepo, error) {
|
||||
|
|
|
@ -18,6 +18,7 @@ package kopialib
|
|||
|
||||
import (
|
||||
"context"
|
||||
"github.com/sirupsen/logrus"
|
||||
"strings"
|
||||
|
||||
"github.com/kopia/kopia/repo"
|
||||
|
@ -44,17 +45,17 @@ var backendStores = []kopiaBackendStore{
|
|||
|
||||
// CreateBackupRepo creates a Kopia repository and then connect to it.
|
||||
// The storage must be empty, otherwise, it will fail
|
||||
func CreateBackupRepo(ctx context.Context, repoOption udmrepo.RepoOptions) error {
|
||||
func CreateBackupRepo(ctx context.Context, repoOption udmrepo.RepoOptions, logger logrus.FieldLogger) error {
|
||||
if repoOption.ConfigFilePath == "" {
|
||||
return errors.New("invalid config file path")
|
||||
}
|
||||
|
||||
backendStore, err := setupBackendStore(ctx, repoOption.StorageType, repoOption.StorageOptions)
|
||||
backendStore, err := setupBackendStore(ctx, repoOption.StorageType, repoOption.StorageOptions, logger)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error to setup backend storage")
|
||||
}
|
||||
|
||||
st, err := backendStore.store.Connect(ctx, true)
|
||||
st, err := backendStore.store.Connect(ctx, true, logger)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error to connect to storage")
|
||||
}
|
||||
|
@ -74,17 +75,17 @@ func CreateBackupRepo(ctx context.Context, repoOption udmrepo.RepoOptions) error
|
|||
|
||||
// ConnectBackupRepo connects to an existing Kopia repository.
|
||||
// If the repository doesn't exist, it will fail
|
||||
func ConnectBackupRepo(ctx context.Context, repoOption udmrepo.RepoOptions) error {
|
||||
func ConnectBackupRepo(ctx context.Context, repoOption udmrepo.RepoOptions, logger logrus.FieldLogger) error {
|
||||
if repoOption.ConfigFilePath == "" {
|
||||
return errors.New("invalid config file path")
|
||||
}
|
||||
|
||||
backendStore, err := setupBackendStore(ctx, repoOption.StorageType, repoOption.StorageOptions)
|
||||
backendStore, err := setupBackendStore(ctx, repoOption.StorageType, repoOption.StorageOptions, logger)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error to setup backend storage")
|
||||
}
|
||||
|
||||
st, err := backendStore.store.Connect(ctx, false)
|
||||
st, err := backendStore.store.Connect(ctx, false, logger)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "error to connect to storage")
|
||||
}
|
||||
|
@ -107,13 +108,13 @@ func findBackendStore(storage string) *kopiaBackendStore {
|
|||
return nil
|
||||
}
|
||||
|
||||
func setupBackendStore(ctx context.Context, storageType string, storageOptions map[string]string) (*kopiaBackendStore, error) {
|
||||
func setupBackendStore(ctx context.Context, storageType string, storageOptions map[string]string, logger logrus.FieldLogger) (*kopiaBackendStore, error) {
|
||||
backendStore := findBackendStore(storageType)
|
||||
if backendStore == nil {
|
||||
return nil, errors.New("error to find storage type")
|
||||
}
|
||||
|
||||
err := backendStore.store.Setup(ctx, storageOptions)
|
||||
err := backendStore.store.Setup(ctx, storageOptions, logger)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "error to setup storage")
|
||||
}
|
||||
|
|
|
@ -18,6 +18,7 @@ package kopialib
|
|||
|
||||
import (
|
||||
"context"
|
||||
velerotest "github.com/vmware-tanzu/velero/pkg/test"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
@ -124,6 +125,7 @@ func TestCreateBackupRepo(t *testing.T) {
|
|||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
logger := velerotest.NewLogger()
|
||||
backendStores = []kopiaBackendStore{
|
||||
{udmrepo.StorageTypeAzure, "fake store", tc.backendStore},
|
||||
{udmrepo.StorageTypeFs, "fake store", tc.backendStore},
|
||||
|
@ -141,7 +143,7 @@ func TestCreateBackupRepo(t *testing.T) {
|
|||
tc.returnStore.On("GetBlob", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.getBlobErr)
|
||||
}
|
||||
|
||||
err := CreateBackupRepo(context.Background(), tc.repoOptions)
|
||||
err := CreateBackupRepo(context.Background(), tc.repoOptions, logger)
|
||||
|
||||
if tc.expectedErr == "" {
|
||||
assert.NoError(t, err)
|
||||
|
@ -207,6 +209,7 @@ func TestConnectBackupRepo(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
logger := velerotest.NewLogger()
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
backendStores = []kopiaBackendStore{
|
||||
|
@ -225,7 +228,7 @@ func TestConnectBackupRepo(t *testing.T) {
|
|||
tc.returnStore.On("GetBlob", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(tc.getBlobErr)
|
||||
}
|
||||
|
||||
err := ConnectBackupRepo(context.Background(), tc.repoOptions)
|
||||
err := ConnectBackupRepo(context.Background(), tc.repoOptions, logger)
|
||||
|
||||
if tc.expectedErr == "" {
|
||||
assert.NoError(t, err)
|
||||
|
|
Loading…
Reference in New Issue