storage/driver: plumb contexts into factories

...and driver constructors when applicable.

Signed-off-by: Cory Snider <csnider@mirantis.com>
This commit is contained in:
Cory Snider 2023-10-27 17:33:55 -04:00
parent 6c694cbcf6
commit b45b6d18b8
11 changed files with 29 additions and 29 deletions

View file

@ -1377,7 +1377,7 @@ const (
repositoryWithGenericStorageError = "genericstorageerr"
)
func (factory *storageManifestErrDriverFactory) Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
func (factory *storageManifestErrDriverFactory) Create(ctx context.Context, parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
// Initialize the mock driver
errGenericStorage := errors.New("generic storage error")
return &mockErrorDriver{

View file

@ -116,7 +116,7 @@ func NewApp(ctx context.Context, config *configuration.Configuration) *App {
storageParams["useragent"] = fmt.Sprintf("distribution/%s %s", version.Version, runtime.Version())
var err error
app.driver, err = factory.Create(config.Storage.Type(), storageParams)
app.driver, err = factory.Create(app, config.Storage.Type(), storageParams)
if err != nil {
// TODO(stevvooe): Move the creation of a service into a protected
// method, where this is created lazily. Its status can be queried via

View file

@ -53,12 +53,6 @@ var GCCmd = &cobra.Command{
os.Exit(1)
}
driver, err := factory.Create(config.Storage.Type(), config.Storage.Parameters())
if err != nil {
fmt.Fprintf(os.Stderr, "failed to construct %s driver: %v", config.Storage.Type(), err)
os.Exit(1)
}
ctx := dcontext.Background()
ctx, err = configureLogging(ctx, config)
if err != nil {
@ -66,6 +60,12 @@ var GCCmd = &cobra.Command{
os.Exit(1)
}
driver, err := factory.Create(ctx, config.Storage.Type(), config.Storage.Parameters())
if err != nil {
fmt.Fprintf(os.Stderr, "failed to construct %s driver: %v", config.Storage.Type(), err)
os.Exit(1)
}
registry, err := storage.NewRegistry(ctx, driver)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to construct registry: %v", err)

View file

@ -46,16 +46,16 @@ func init() {
type azureDriverFactory struct{}
func (factory *azureDriverFactory) Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
func (factory *azureDriverFactory) Create(ctx context.Context, parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
params, err := NewParameters(parameters)
if err != nil {
return nil, err
}
return New(params)
return New(ctx, params)
}
// New constructs a new Driver from parameters
func New(params *Parameters) (*Driver, error) {
func New(ctx context.Context, params *Parameters) (*Driver, error) {
azClient, err := newAzureClient(params)
if err != nil {
return nil, err

View file

@ -68,7 +68,7 @@ func init() {
if err != nil {
return nil, err
}
return New(params)
return New(context.Background(), params)
}
// Skip Azure storage driver tests if environment variable parameters are not provided

View file

@ -1,6 +1,7 @@
package factory
import (
"context"
"fmt"
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver"
@ -23,7 +24,7 @@ type StorageDriverFactory interface {
// Create returns a new storagedriver.StorageDriver with the given parameters
// Parameters will vary by driver and may be ignored
// Each parameter key must only consist of lowercase letters and numbers
Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error)
Create(ctx context.Context, parameters map[string]interface{}) (storagedriver.StorageDriver, error)
}
// Register makes a storage driver available by the provided name.
@ -46,12 +47,12 @@ func Register(name string, factory StorageDriverFactory) {
// parameters. To use a driver, the StorageDriverFactory must first be
// registered with the given name. If no drivers are found, an
// InvalidStorageDriverError is returned
func Create(name string, parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
func Create(ctx context.Context, name string, parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
driverFactory, ok := driverFactories[name]
if !ok {
return nil, InvalidStorageDriverError{name}
}
return driverFactory.Create(parameters)
return driverFactory.Create(ctx, parameters)
}
// InvalidStorageDriverError records an attempt to construct an unregistered storage driver

View file

@ -40,7 +40,7 @@ func init() {
// filesystemDriverFactory implements the factory.StorageDriverFactory interface
type filesystemDriverFactory struct{}
func (factory *filesystemDriverFactory) Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
func (factory *filesystemDriverFactory) Create(ctx context.Context, parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
return FromParameters(parameters)
}

View file

@ -87,8 +87,8 @@ func init() {
type gcsDriverFactory struct{}
// Create StorageDriver from parameters
func (factory *gcsDriverFactory) Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
return FromParameters(parameters)
func (factory *gcsDriverFactory) Create(ctx context.Context, parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
return FromParameters(ctx, parameters)
}
var _ storagedriver.StorageDriver = &driver{}
@ -118,8 +118,7 @@ type baseEmbed struct {
// FromParameters constructs a new Driver with a given parameters map
// Required parameters:
// - bucket
func FromParameters(parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
ctx := context.TODO()
func FromParameters(ctx context.Context, parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
bucket, ok := parameters["bucket"]
if !ok || fmt.Sprint(bucket) == "" {
return nil, fmt.Errorf("No bucket parameter provided")
@ -229,11 +228,11 @@ func FromParameters(parameters map[string]interface{}) (storagedriver.StorageDri
gcs: gcs,
}
return New(params)
return New(ctx, params)
}
// New constructs a new driver
func New(params driverParameters) (storagedriver.StorageDriver, error) {
func New(ctx context.Context, params driverParameters) (storagedriver.StorageDriver, error) {
rootDirectory := strings.Trim(params.rootDirectory, "/")
if rootDirectory != "" {
rootDirectory += "/"

View file

@ -21,7 +21,7 @@ func init() {
// inMemoryDriverFacotry implements the factory.StorageDriverFactory interface.
type inMemoryDriverFactory struct{}
func (factory *inMemoryDriverFactory) Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
func (factory *inMemoryDriverFactory) Create(ctx context.Context, parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
return New(), nil
}

View file

@ -150,8 +150,8 @@ func init() {
// s3DriverFactory implements the factory.StorageDriverFactory interface
type s3DriverFactory struct{}
func (factory *s3DriverFactory) Create(parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
return FromParameters(parameters)
func (factory *s3DriverFactory) Create(ctx context.Context, parameters map[string]interface{}) (storagedriver.StorageDriver, error) {
return FromParameters(ctx, parameters)
}
var _ storagedriver.StorageDriver = &driver{}
@ -189,7 +189,7 @@ type Driver struct {
// - region
// - bucket
// - encrypt
func FromParameters(parameters map[string]interface{}) (*Driver, error) {
func FromParameters(ctx context.Context, parameters map[string]interface{}) (*Driver, error) {
// Providing no values for these is valid in case the user is authenticating
// with an IAM on an ec2 instance (in which case the instance credentials will
// be summoned when GetAuth is called)
@ -468,7 +468,7 @@ func FromParameters(parameters map[string]interface{}) (*Driver, error) {
getS3LogLevelFromParam(parameters["loglevel"]),
}
return New(params)
return New(ctx, params)
}
func getS3LogLevelFromParam(param interface{}) aws.LogLevelType {
@ -529,7 +529,7 @@ func getParameterAsInt64(parameters map[string]interface{}, name string, default
// New constructs a new Driver with the given AWS credentials, region, encryption flag, and
// bucketName
func New(params DriverParameters) (*Driver, error) {
func New(ctx context.Context, params DriverParameters) (*Driver, error) {
if !params.V4Auth &&
(params.RegionEndpoint == "" ||
strings.Contains(params.RegionEndpoint, "s3.amazonaws.com")) {

View file

@ -141,7 +141,7 @@ func init() {
getS3LogLevelFromParam(logLevel),
}
return New(parameters)
return New(context.Background(), parameters)
}
// Skip S3 storage driver tests if environment variable parameters are not provided