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" 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 // Initialize the mock driver
errGenericStorage := errors.New("generic storage error") errGenericStorage := errors.New("generic storage error")
return &mockErrorDriver{ 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()) storageParams["useragent"] = fmt.Sprintf("distribution/%s %s", version.Version, runtime.Version())
var err error 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 { if err != nil {
// TODO(stevvooe): Move the creation of a service into a protected // TODO(stevvooe): Move the creation of a service into a protected
// method, where this is created lazily. Its status can be queried via // 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) 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 := dcontext.Background()
ctx, err = configureLogging(ctx, config) ctx, err = configureLogging(ctx, config)
if err != nil { if err != nil {
@ -66,6 +60,12 @@ var GCCmd = &cobra.Command{
os.Exit(1) 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) registry, err := storage.NewRegistry(ctx, driver)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "failed to construct registry: %v", err) fmt.Fprintf(os.Stderr, "failed to construct registry: %v", err)

View file

@ -46,16 +46,16 @@ func init() {
type azureDriverFactory struct{} 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) params, err := NewParameters(parameters)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return New(params) return New(ctx, params)
} }
// New constructs a new Driver from parameters // 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) azClient, err := newAzureClient(params)
if err != nil { if err != nil {
return nil, err return nil, err

View file

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

View file

@ -1,6 +1,7 @@
package factory package factory
import ( import (
"context"
"fmt" "fmt"
storagedriver "github.com/distribution/distribution/v3/registry/storage/driver" 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 // Create returns a new storagedriver.StorageDriver with the given parameters
// Parameters will vary by driver and may be ignored // Parameters will vary by driver and may be ignored
// Each parameter key must only consist of lowercase letters and numbers // 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. // 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 // parameters. To use a driver, the StorageDriverFactory must first be
// registered with the given name. If no drivers are found, an // registered with the given name. If no drivers are found, an
// InvalidStorageDriverError is returned // 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] driverFactory, ok := driverFactories[name]
if !ok { if !ok {
return nil, InvalidStorageDriverError{name} return nil, InvalidStorageDriverError{name}
} }
return driverFactory.Create(parameters) return driverFactory.Create(ctx, parameters)
} }
// InvalidStorageDriverError records an attempt to construct an unregistered storage driver // InvalidStorageDriverError records an attempt to construct an unregistered storage driver

View file

@ -40,7 +40,7 @@ func init() {
// filesystemDriverFactory implements the factory.StorageDriverFactory interface // filesystemDriverFactory implements the factory.StorageDriverFactory interface
type filesystemDriverFactory struct{} 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) return FromParameters(parameters)
} }

View file

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

View file

@ -21,7 +21,7 @@ func init() {
// inMemoryDriverFacotry implements the factory.StorageDriverFactory interface. // inMemoryDriverFacotry implements the factory.StorageDriverFactory interface.
type inMemoryDriverFactory struct{} 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 return New(), nil
} }

View file

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

View file

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