issue8827: Pass logger in call chain (#8875)
Some checks failed
Run the E2E test on kind / build (push) Failing after 6m42s
Run the E2E test on kind / setup-test-matrix (push) Successful in 3s
Run the E2E test on kind / run-e2e-test (push) Has been skipped
Main CI / Build (push) Failing after 36s
Close stale issues and PRs / stale (push) Successful in 9s
Trivy Nightly Scan / Trivy nightly scan (velero, main) (push) Failing after 1m4s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-aws, main) (push) Failing after 56s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-gcp, main) (push) Failing after 49s
Trivy Nightly Scan / Trivy nightly scan (velero-plugin-for-microsoft-azure, main) (push) Failing after 57s

* Pass logger in call chain

Signed-off-by: hu-keyu <hzldd999@gmail.com>
This commit is contained in:
hu-keyu
2025-04-23 14:44:05 +08:00
committed by GitHub
parent c6a420bd3a
commit e06b62e3a8
15 changed files with 65 additions and 31 deletions

View File

@@ -0,0 +1 @@
Pass the logger in kopia related operations.

View File

@@ -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)
}

View File

@@ -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
}

View File

@@ -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)

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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)

View File

@@ -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)
}

View File

@@ -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)

View File

@@ -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

View File

@@ -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)
}

View File

@@ -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)

View File

@@ -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) {

View File

@@ -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")
}

View File

@@ -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)