mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
Clean up/make consistent the APIs for registering core components, make Docker an optional component at compile time (#1111)
This commit is contained in:
@@ -12,7 +12,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/fnproject/fn/api/agent/drivers"
|
||||
"github.com/fnproject/fn/api/agent/drivers/docker"
|
||||
"github.com/fnproject/fn/api/agent/protocol"
|
||||
"github.com/fnproject/fn/api/common"
|
||||
"github.com/fnproject/fn/api/id"
|
||||
@@ -142,7 +141,11 @@ func New(da CallHandler, options ...AgentOption) Agent {
|
||||
logrus.Infof("agent starting cfg=%+v", a.cfg)
|
||||
|
||||
if a.driver == nil {
|
||||
a.driver = NewDockerDriver(&a.cfg)
|
||||
d, err := NewDockerDriver(&a.cfg)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Fatal("failed to create docker driver ")
|
||||
}
|
||||
a.driver = d
|
||||
}
|
||||
|
||||
a.resources = NewResourceTracker(&a.cfg)
|
||||
@@ -201,8 +204,8 @@ func WithCallOverrider(fn CallOverrider) AgentOption {
|
||||
}
|
||||
|
||||
// NewDockerDriver creates a default docker driver from agent config
|
||||
func NewDockerDriver(cfg *AgentConfig) *docker.DockerDriver {
|
||||
return docker.NewDocker(drivers.Config{
|
||||
func NewDockerDriver(cfg *AgentConfig) (drivers.Driver, error) {
|
||||
return drivers.New("docker", drivers.Config{
|
||||
DockerNetworks: cfg.DockerNetworks,
|
||||
ServerVersion: cfg.MinDockerVersion,
|
||||
PreForkPoolSize: cfg.PreForkPoolSize,
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
_ "github.com/fnproject/fn/api/agent/drivers/docker"
|
||||
"github.com/fnproject/fn/api/id"
|
||||
"github.com/fnproject/fn/api/logs"
|
||||
"github.com/fnproject/fn/api/models"
|
||||
|
||||
@@ -646,3 +646,9 @@ func (w *waitResult) wait(ctx context.Context) (status string, err error) {
|
||||
}
|
||||
|
||||
var _ drivers.Driver = &DockerDriver{}
|
||||
|
||||
func init() {
|
||||
drivers.Register("docker", func(config drivers.Config) (drivers.Driver, error) {
|
||||
return NewDocker(config), nil
|
||||
})
|
||||
}
|
||||
|
||||
26
api/agent/drivers/register.go
Normal file
26
api/agent/drivers/register.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package drivers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type DriverFunc func(config Config) (Driver, error)
|
||||
|
||||
var drivers = make(map[string]DriverFunc)
|
||||
|
||||
// Register adds a container driver by name to this process
|
||||
func Register(name string, driverFunc DriverFunc) {
|
||||
logrus.Infof("Registering container driver '%s'", name)
|
||||
drivers[name] = driverFunc
|
||||
}
|
||||
|
||||
// New Instantiates a driver by name
|
||||
func New(driverName string, config Config) (Driver, error) {
|
||||
driverFunc, ok := drivers[driverName]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("agent driver \"%s\" is not registered", driverName)
|
||||
}
|
||||
return driverFunc(config)
|
||||
}
|
||||
Reference in New Issue
Block a user