provider.go 1.25 KB
Newer Older
Tomasz Maczukin's avatar
Tomasz Maczukin committed
1 2 3 4
package providers

import (
	"context"
5
	"fmt"
Tomasz Maczukin's avatar
Tomasz Maczukin committed
6

Steve Azzopardi's avatar
Steve Azzopardi committed
7 8 9 10
	"gitlab.com/gitlab-org/ci-cd/custom-executor-drivers/autoscaler/config"
	"gitlab.com/gitlab-org/ci-cd/custom-executor-drivers/autoscaler/executors"
	"gitlab.com/gitlab-org/ci-cd/custom-executor-drivers/autoscaler/internal/factories"
	"gitlab.com/gitlab-org/ci-cd/custom-executor-drivers/autoscaler/internal/logging"
11
	"gitlab.com/gitlab-org/ci-cd/custom-executor-drivers/autoscaler/vm"
Tomasz Maczukin's avatar
Tomasz Maczukin committed
12 13
)

14 15
const factoryType = "provider"

Tomasz Maczukin's avatar
Tomasz Maczukin committed
16
type Provider interface {
17 18 19
	Get(ctx context.Context, name string) (vm.VirtualMachine, error)
	Create(ctx context.Context, e executors.Executor, vm *vm.VirtualMachine) error
	Delete(ctx context.Context, vm vm.VirtualMachine) error
Tomasz Maczukin's avatar
Tomasz Maczukin committed
20 21
}

22
type Factory func(cfg config.Global, logger logging.Logger) (Provider, error)
Tomasz Maczukin's avatar
Tomasz Maczukin committed
23

24
var factoriesRegistry = factories.NewRegistry(factoryType)
Tomasz Maczukin's avatar
Tomasz Maczukin committed
25 26

func MustRegister(name string, f Factory) {
27
	factoriesRegistry.MustRegister(name, f)
Tomasz Maczukin's avatar
Tomasz Maczukin committed
28 29
}

30
func Factorize(cfg config.Global, logger logging.Logger) (Provider, error) {
31 32 33
	factory, err := factoriesRegistry.Get(cfg.Provider)
	if err != nil {
		return nil, err
Tomasz Maczukin's avatar
Tomasz Maczukin committed
34 35
	}

36 37 38 39 40 41
	p, err := factory.(Factory)(cfg, logger)
	if err != nil {
		return nil, fmt.Errorf("couldn't factorize provider: %w", err)
	}

	return newCacheProviderDecorator(cfg, logger, p), nil
Tomasz Maczukin's avatar
Tomasz Maczukin committed
42
}