diff --git a/changelogs/unreleased/8875-hu-keyu b/changelogs/unreleased/8875-hu-keyu new file mode 100644 index 000000000..f1a292cb5 --- /dev/null +++ b/changelogs/unreleased/8875-hu-keyu @@ -0,0 +1 @@ +Pass the logger in kopia related operations. \ No newline at end of file diff --git a/pkg/repository/udmrepo/kopialib/backend/azure.go b/pkg/repository/udmrepo/kopialib/backend/azure.go index ea7659f93..ead95406c 100644 --- a/pkg/repository/udmrepo/kopialib/backend/azure.go +++ b/pkg/repository/udmrepo/kopialib/backend/azure.go @@ -19,6 +19,8 @@ package backend import ( "context" + "github.com/sirupsen/logrus" + "github.com/kopia/kopia/repo/blob" "github.com/vmware-tanzu/velero/pkg/repository/udmrepo" @@ -29,17 +31,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) } diff --git a/pkg/repository/udmrepo/kopialib/backend/azure/azure_storage_wrapper.go b/pkg/repository/udmrepo/kopialib/backend/azure/azure_storage_wrapper.go index 046b0459a..967de3dea 100644 --- a/pkg/repository/udmrepo/kopialib/backend/azure/azure_storage_wrapper.go +++ b/pkg/repository/udmrepo/kopialib/backend/azure/azure_storage_wrapper.go @@ -19,10 +19,11 @@ package azure import ( "context" + "github.com/sirupsen/logrus" + "github.com/kopia/kopia/repo/blob" "github.com/kopia/kopia/repo/blob/azure" "github.com/kopia/kopia/repo/blob/throttling" - "github.com/sirupsen/logrus" "github.com/vmware-tanzu/velero/pkg/repository/udmrepo" azureutil "github.com/vmware-tanzu/velero/pkg/util/azure" @@ -33,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 { @@ -56,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(logrus.New(), cfg) + client, _, err := azureutil.NewStorageClient(option.Logger, cfg) if err != nil { return nil, err } diff --git a/pkg/repository/udmrepo/kopialib/backend/azure_test.go b/pkg/repository/udmrepo/kopialib/backend/azure_test.go index 107f2ea94..910351bf5 100644 --- a/pkg/repository/udmrepo/kopialib/backend/azure_test.go +++ b/pkg/repository/udmrepo/kopialib/backend/azure_test.go @@ -20,6 +20,8 @@ import ( "context" "testing" + velerotest "github.com/vmware-tanzu/velero/pkg/test" + "github.com/kopia/kopia/repo/blob/throttling" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -29,6 +31,7 @@ import ( func TestAzureSetup(t *testing.T) { backend := AzureBackend{} + logger := velerotest.NewLogger() flags := map[string]string{ "key": "value", @@ -40,7 +43,7 @@ func TestAzureSetup(t *testing.T) { UploadBytesPerSecond: 200, } - err := backend.Setup(context.Background(), flags) + err := backend.Setup(context.Background(), flags, logger) require.NoError(t, err) assert.Equal(t, flags, backend.option.Config) assert.Equal(t, limits, backend.option.Limits) diff --git a/pkg/repository/udmrepo/kopialib/backend/backend.go b/pkg/repository/udmrepo/kopialib/backend/backend.go index 999313863..c173eabc6 100644 --- a/pkg/repository/udmrepo/kopialib/backend/backend.go +++ b/pkg/repository/udmrepo/kopialib/backend/backend.go @@ -19,6 +19,8 @@ package backend import ( "context" + "github.com/sirupsen/logrus" + "github.com/kopia/kopia/repo/blob" ) @@ -26,8 +28,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) } diff --git a/pkg/repository/udmrepo/kopialib/backend/file_system.go b/pkg/repository/udmrepo/kopialib/backend/file_system.go index cf314ff59..98f272c46 100644 --- a/pkg/repository/udmrepo/kopialib/backend/file_system.go +++ b/pkg/repository/udmrepo/kopialib/backend/file_system.go @@ -20,6 +20,8 @@ import ( "context" "path/filepath" + "github.com/sirupsen/logrus" + "github.com/kopia/kopia/repo/blob" "github.com/kopia/kopia/repo/blob/filesystem" "github.com/pkg/errors" @@ -36,7 +38,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 +55,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) } diff --git a/pkg/repository/udmrepo/kopialib/backend/file_system_test.go b/pkg/repository/udmrepo/kopialib/backend/file_system_test.go index fe9b8e624..6a3503658 100644 --- a/pkg/repository/udmrepo/kopialib/backend/file_system_test.go +++ b/pkg/repository/udmrepo/kopialib/backend/file_system_test.go @@ -20,6 +20,8 @@ import ( "context" "testing" + velerotest "github.com/vmware-tanzu/velero/pkg/test" + "github.com/kopia/kopia/repo/blob/filesystem" "github.com/stretchr/testify/assert" @@ -63,11 +65,12 @@ func TestFSSetup(t *testing.T) { }, } + logger := velerotest.NewLogger() for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { fsFlags := FsBackend{} - err := fsFlags.Setup(context.Background(), tc.flags) + err := fsFlags.Setup(context.Background(), tc.flags, logger) if tc.expectedErr == "" { assert.NoError(t, err) diff --git a/pkg/repository/udmrepo/kopialib/backend/gcs.go b/pkg/repository/udmrepo/kopialib/backend/gcs.go index ee96abc51..243f054dc 100644 --- a/pkg/repository/udmrepo/kopialib/backend/gcs.go +++ b/pkg/repository/udmrepo/kopialib/backend/gcs.go @@ -19,6 +19,8 @@ package backend import ( "context" + "github.com/sirupsen/logrus" + "github.com/kopia/kopia/repo/blob" "github.com/kopia/kopia/repo/blob/gcs" @@ -29,7 +31,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 +51,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) } diff --git a/pkg/repository/udmrepo/kopialib/backend/gcs_test.go b/pkg/repository/udmrepo/kopialib/backend/gcs_test.go index 759e9baae..cac843acf 100644 --- a/pkg/repository/udmrepo/kopialib/backend/gcs_test.go +++ b/pkg/repository/udmrepo/kopialib/backend/gcs_test.go @@ -20,6 +20,8 @@ import ( "context" "testing" + velerotest "github.com/vmware-tanzu/velero/pkg/test" + "github.com/kopia/kopia/repo/blob/gcs" "github.com/stretchr/testify/assert" @@ -85,11 +87,12 @@ func TestGcsSetup(t *testing.T) { }, } + logger := velerotest.NewLogger() for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { gcsFlags := GCSBackend{} - err := gcsFlags.Setup(context.Background(), tc.flags) + err := gcsFlags.Setup(context.Background(), tc.flags, logger) if tc.expectedErr == "" { assert.NoError(t, err) diff --git a/pkg/repository/udmrepo/kopialib/backend/mocks/Store.go b/pkg/repository/udmrepo/kopialib/backend/mocks/Store.go index 7c09aa9ef..528cd2e2d 100644 --- a/pkg/repository/udmrepo/kopialib/backend/mocks/Store.go +++ b/pkg/repository/udmrepo/kopialib/backend/mocks/Store.go @@ -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 diff --git a/pkg/repository/udmrepo/kopialib/backend/s3.go b/pkg/repository/udmrepo/kopialib/backend/s3.go index 075be9af1..2f7f7d93c 100644 --- a/pkg/repository/udmrepo/kopialib/backend/s3.go +++ b/pkg/repository/udmrepo/kopialib/backend/s3.go @@ -19,6 +19,8 @@ package backend import ( "context" + "github.com/sirupsen/logrus" + "github.com/kopia/kopia/repo/blob" "github.com/kopia/kopia/repo/blob/s3" @@ -29,7 +31,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 +53,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) } diff --git a/pkg/repository/udmrepo/kopialib/backend/s3_test.go b/pkg/repository/udmrepo/kopialib/backend/s3_test.go index df96de36e..7b11a56ba 100644 --- a/pkg/repository/udmrepo/kopialib/backend/s3_test.go +++ b/pkg/repository/udmrepo/kopialib/backend/s3_test.go @@ -20,6 +20,8 @@ import ( "context" "testing" + velerotest "github.com/vmware-tanzu/velero/pkg/test" + "github.com/kopia/kopia/repo/blob/s3" "github.com/stretchr/testify/assert" @@ -115,11 +117,12 @@ func TestS3Setup(t *testing.T) { }, } + logger := velerotest.NewLogger() for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { s3Flags := S3Backend{} - err := s3Flags.Setup(context.Background(), tc.flags) + err := s3Flags.Setup(context.Background(), tc.flags, logger) if tc.expectedErr == "" { assert.NoError(t, err) diff --git a/pkg/repository/udmrepo/kopialib/lib_repo.go b/pkg/repository/udmrepo/kopialib/lib_repo.go index 3937657aa..d1eeb88d0 100644 --- a/pkg/repository/udmrepo/kopialib/lib_repo.go +++ b/pkg/repository/udmrepo/kopialib/lib_repo.go @@ -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) { diff --git a/pkg/repository/udmrepo/kopialib/repo_init.go b/pkg/repository/udmrepo/kopialib/repo_init.go index b1a13e2e5..76f8b0e45 100644 --- a/pkg/repository/udmrepo/kopialib/repo_init.go +++ b/pkg/repository/udmrepo/kopialib/repo_init.go @@ -20,6 +20,8 @@ import ( "context" "strings" + "github.com/sirupsen/logrus" + "github.com/kopia/kopia/repo" "github.com/kopia/kopia/repo/blob" "github.com/pkg/errors" @@ -44,17 +46,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 +76,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 +109,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") } diff --git a/pkg/repository/udmrepo/kopialib/repo_init_test.go b/pkg/repository/udmrepo/kopialib/repo_init_test.go index f91296d1f..a21546607 100644 --- a/pkg/repository/udmrepo/kopialib/repo_init_test.go +++ b/pkg/repository/udmrepo/kopialib/repo_init_test.go @@ -20,6 +20,8 @@ import ( "context" "testing" + velerotest "github.com/vmware-tanzu/velero/pkg/test" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" @@ -124,6 +126,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 +144,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 +210,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 +229,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)