Merge pull request #9168 from priyansh17/issue-#9029
Run the E2E test on kind / build (push) Failing after 5s
Run the E2E test on kind / setup-test-matrix (push) Successful in 2s
Run the E2E test on kind / run-e2e-test (push) Has been skipped
Main CI / Build (push) Failing after 3s

Add context logger utilities for enhanced logging support
This commit is contained in:
lyndon-li
2025-08-15 10:48:17 +08:00
committed by GitHub
7 changed files with 61 additions and 7 deletions
+1
View File
@@ -0,0 +1 @@
Introduced context-based logger for backend implementations (Azure, GCS, S3, and Filesystem)
@@ -25,6 +25,7 @@ import (
"github.com/vmware-tanzu/velero/pkg/repository/udmrepo"
"github.com/vmware-tanzu/velero/pkg/repository/udmrepo/kopialib/backend/azure"
"github.com/vmware-tanzu/velero/pkg/repository/udmrepo/kopialib/backend/logging"
)
type AzureBackend struct {
@@ -38,12 +39,11 @@ func (c *AzureBackend) Setup(ctx context.Context, flags map[string]string, logge
c.option = azure.Option{
Config: flags,
Limits: setupLimits(ctx, flags),
Logger: logger,
}
return nil
}
func (c *AzureBackend) Connect(ctx context.Context, isCreate bool, logger logrus.FieldLogger) (blob.Storage, error) {
c.option.Logger = logger
ctx = logging.WithLogger(ctx, logger)
return azure.NewStorage(ctx, &c.option, false)
}
@@ -19,13 +19,12 @@ 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/vmware-tanzu/velero/pkg/repository/udmrepo"
"github.com/vmware-tanzu/velero/pkg/repository/udmrepo/kopialib/backend/logging"
azureutil "github.com/vmware-tanzu/velero/pkg/util/azure"
)
@@ -34,13 +33,12 @@ const (
)
func init() {
blob.AddSupportedStorage(storageType, Option{Logger: logrus.New()}, NewStorage)
blob.AddSupportedStorage(storageType, Option{}, NewStorage)
}
type Option struct {
Config map[string]string `json:"config" kopia:"sensitive"`
Limits throttling.Limits
Logger logrus.FieldLogger
}
type Storage struct {
@@ -58,7 +56,10 @@ 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(option.Logger, cfg)
// Get logger from context
logger := logging.LoggerFromContext(ctx)
client, _, err := azureutil.NewStorageClient(logger, cfg)
if err != nil {
return nil, err
}
@@ -73,6 +74,8 @@ func NewStorage(ctx context.Context, option *Option, isCreate bool) (blob.Storag
return nil, err
}
logger.Info("Successfully created Azure storage backend")
return &Storage{
Option: option,
Storage: azStorage,
@@ -27,6 +27,7 @@ import (
"github.com/pkg/errors"
"github.com/vmware-tanzu/velero/pkg/repository/udmrepo"
"github.com/vmware-tanzu/velero/pkg/repository/udmrepo/kopialib/backend/logging"
)
type FsBackend struct {
@@ -50,6 +51,8 @@ func (c *FsBackend) Setup(ctx context.Context, flags map[string]string, logger l
c.options.FileMode = defaultFileMode
c.options.DirectoryMode = defaultDirMode
ctx = logging.WithLogger(ctx, logger)
c.options.Limits = setupLimits(ctx, flags)
return nil
@@ -59,6 +62,7 @@ func (c *FsBackend) Connect(ctx context.Context, isCreate bool, logger logrus.Fi
if !filepath.IsAbs(c.options.Path) {
return nil, errors.Errorf("filesystem repository path is not absolute, path: %s", c.options.Path)
}
ctx = logging.WithLogger(ctx, logger)
return filesystem.New(ctx, &c.options, isCreate)
}
@@ -25,6 +25,7 @@ import (
"github.com/kopia/kopia/repo/blob/gcs"
"github.com/vmware-tanzu/velero/pkg/repository/udmrepo"
"github.com/vmware-tanzu/velero/pkg/repository/udmrepo/kopialib/backend/logging"
)
type GCSBackend struct {
@@ -46,11 +47,14 @@ func (c *GCSBackend) Setup(ctx context.Context, flags map[string]string, logger
c.options.Prefix = optionalHaveString(udmrepo.StoreOptionPrefix, flags)
c.options.ReadOnly = optionalHaveBool(ctx, udmrepo.StoreOptionGcsReadonly, flags)
ctx = logging.WithLogger(ctx, logger)
c.options.Limits = setupLimits(ctx, flags)
return nil
}
func (c *GCSBackend) Connect(ctx context.Context, isCreate bool, logger logrus.FieldLogger) (blob.Storage, error) {
ctx = logging.WithLogger(ctx, logger)
return gcs.New(ctx, &c.options, false)
}
@@ -0,0 +1,38 @@
/*
Copyright the Velero contributors.
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 logging
import (
"context"
"github.com/sirupsen/logrus"
)
type ctxKeyLogger struct{}
// WithLogger returns a new context with the provided logger.
func WithLogger(ctx context.Context, logger logrus.FieldLogger) context.Context {
return context.WithValue(ctx, ctxKeyLogger{}, logger)
}
// LoggerFromContext retrieves the logger from the context, or returns a default logger if none found.
func LoggerFromContext(ctx context.Context) logrus.FieldLogger {
if logger, ok := ctx.Value(ctxKeyLogger{}).(logrus.FieldLogger); ok && logger != nil {
return logger
}
return logrus.New()
}
@@ -25,6 +25,7 @@ import (
"github.com/kopia/kopia/repo/blob/s3"
"github.com/vmware-tanzu/velero/pkg/repository/udmrepo"
"github.com/vmware-tanzu/velero/pkg/repository/udmrepo/kopialib/backend/logging"
)
type S3Backend struct {
@@ -48,11 +49,14 @@ func (c *S3Backend) Setup(ctx context.Context, flags map[string]string, logger l
c.options.SessionToken = optionalHaveString(udmrepo.StoreOptionS3Token, flags)
c.options.RootCA = optionalHaveBase64(ctx, udmrepo.StoreOptionCACert, flags)
ctx = logging.WithLogger(ctx, logger)
c.options.Limits = setupLimits(ctx, flags)
return nil
}
func (c *S3Backend) Connect(ctx context.Context, isCreate bool, logger logrus.FieldLogger) (blob.Storage, error) {
ctx = logging.WithLogger(ctx, logger)
return s3.New(ctx, &c.options, false)
}