mirror of
https://github.com/vmware-tanzu/velero.git
synced 2026-01-05 21:14:56 +00:00
Implement context-based logging utilities for UDM repositories
Signed-off-by: Priyansh Choudhary <im1706@gmail.com>
This commit is contained in:
@@ -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/logging"
|
||||
)
|
||||
|
||||
type AzureBackend struct {
|
||||
@@ -43,6 +44,6 @@ func (c *AzureBackend) Setup(ctx context.Context, flags map[string]string, logge
|
||||
}
|
||||
|
||||
func (c *AzureBackend) Connect(ctx context.Context, isCreate bool, logger logrus.FieldLogger) (blob.Storage, error) {
|
||||
ctx = udmrepo.WithLogger(ctx, logger)
|
||||
ctx = logging.WithLogger(ctx, logger)
|
||||
return azure.NewStorage(ctx, &c.option, false)
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/kopia/kopia/repo/blob/throttling"
|
||||
|
||||
"github.com/vmware-tanzu/velero/pkg/repository/udmrepo"
|
||||
"github.com/vmware-tanzu/velero/pkg/repository/udmrepo/logging"
|
||||
azureutil "github.com/vmware-tanzu/velero/pkg/util/azure"
|
||||
)
|
||||
|
||||
@@ -56,7 +57,7 @@ func NewStorage(ctx context.Context, option *Option, isCreate bool) (blob.Storag
|
||||
cfg := option.Config
|
||||
|
||||
// Get logger from context
|
||||
logger := udmrepo.LoggerFromContext(ctx)
|
||||
logger := logging.LoggerFromContext(ctx)
|
||||
|
||||
client, _, err := azureutil.NewStorageClient(logger, cfg)
|
||||
if err != nil {
|
||||
|
||||
@@ -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/logging"
|
||||
)
|
||||
|
||||
type FsBackend struct {
|
||||
@@ -50,7 +51,7 @@ func (c *FsBackend) Setup(ctx context.Context, flags map[string]string, logger l
|
||||
c.options.FileMode = defaultFileMode
|
||||
c.options.DirectoryMode = defaultDirMode
|
||||
|
||||
ctx = udmrepo.WithLogger(ctx, logger)
|
||||
ctx = logging.WithLogger(ctx, logger)
|
||||
|
||||
c.options.Limits = setupLimits(ctx, flags)
|
||||
|
||||
@@ -61,7 +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 = udmrepo.WithLogger(ctx, logger)
|
||||
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/logging"
|
||||
)
|
||||
|
||||
type GCSBackend struct {
|
||||
@@ -46,7 +47,7 @@ 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 = udmrepo.WithLogger(ctx, logger)
|
||||
ctx = logging.WithLogger(ctx, logger)
|
||||
|
||||
c.options.Limits = setupLimits(ctx, flags)
|
||||
|
||||
@@ -54,6 +55,6 @@ func (c *GCSBackend) Setup(ctx context.Context, flags map[string]string, logger
|
||||
}
|
||||
|
||||
func (c *GCSBackend) Connect(ctx context.Context, isCreate bool, logger logrus.FieldLogger) (blob.Storage, error) {
|
||||
ctx = udmrepo.WithLogger(ctx, logger)
|
||||
ctx = logging.WithLogger(ctx, logger)
|
||||
return gcs.New(ctx, &c.options, false)
|
||||
}
|
||||
|
||||
@@ -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/logging"
|
||||
)
|
||||
|
||||
type S3Backend struct {
|
||||
@@ -48,7 +49,7 @@ 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 = udmrepo.WithLogger(ctx, logger)
|
||||
ctx = logging.WithLogger(ctx, logger)
|
||||
|
||||
c.options.Limits = setupLimits(ctx, flags)
|
||||
|
||||
@@ -56,6 +57,6 @@ func (c *S3Backend) Setup(ctx context.Context, flags map[string]string, logger l
|
||||
}
|
||||
|
||||
func (c *S3Backend) Connect(ctx context.Context, isCreate bool, logger logrus.FieldLogger) (blob.Storage, error) {
|
||||
ctx = udmrepo.WithLogger(ctx, logger)
|
||||
ctx = logging.WithLogger(ctx, logger)
|
||||
return s3.New(ctx, &c.options, false)
|
||||
}
|
||||
|
||||
38
pkg/repository/udmrepo/logging/context.go
Normal file
38
pkg/repository/udmrepo/logging/context.go
Normal file
@@ -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()
|
||||
}
|
||||
@@ -20,8 +20,6 @@ import (
|
||||
"context"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type ID string
|
||||
@@ -159,17 +157,4 @@ type ObjectWriter interface {
|
||||
Result() (ID, error)
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user