From 8438557ff714424b029e9903bde0f861c5e54350 Mon Sep 17 00:00:00 2001 From: Tonis Tiigi Date: Mon, 25 Mar 2019 15:14:39 -0700 Subject: [PATCH] driver: basic types startpoint Signed-off-by: Tonis Tiigi --- driver/driver.go | 36 ++++++++++++++++++++++++++++++ driver/manager.go | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 driver/driver.go create mode 100644 driver/manager.go diff --git a/driver/driver.go b/driver/driver.go new file mode 100644 index 00000000..b01d724c --- /dev/null +++ b/driver/driver.go @@ -0,0 +1,36 @@ +package driver + +import ( + "context" + + "github.com/moby/buildkit/client" + specs "github.com/opencontainers/image-spec/specs-go/v1" + "github.com/pkg/errors" +) + +type Logger func(*client.SolveStatus) + +type Status int + +const ( + Terminated Status = iota + Starting + Running + Stopping + Stopped +) + +type Info struct { + Status Status + Platforms []specs.Platform +} + +var ErrNotRunning = errors.Errorf("driver not running") + +type Driver interface { + Bootstrap(context.Context, Logger) error + Info(context.Context) (Info, error) + Stop(ctx context.Context, force bool) error + Rm(ctx context.Context, force bool) error + Client() (client.Client, error) +} diff --git a/driver/manager.go b/driver/manager.go new file mode 100644 index 00000000..b78224f8 --- /dev/null +++ b/driver/manager.go @@ -0,0 +1,56 @@ +package driver + +import ( + "context" + "sort" + + dockerclient "github.com/docker/docker/client" + "github.com/pkg/errors" +) + +type Factory interface { + Name() string + Usage() string + Priority() int // take initConfig? + New(ctx context.Context, cfg InitConfig) (Driver, error) +} + +type BuildkitConfig struct { + // Entitlements []string + // Rootless bool +} + +type InitConfig struct { + // This object needs updates to be generic for different drivers + Name string + DockerAPI dockerclient.APIClient + BuildkitConfig BuildkitConfig + Meta map[string]interface{} +} + +var drivers map[string]Factory + +func Register(f Factory) { + if drivers == nil { + drivers = map[string]Factory{} + } + drivers[f.Name()] = f +} + +func GetDefaultDriver() (Factory, error) { + if len(drivers) == 0 { + return nil, errors.Errorf("no drivers available") + } + type p struct { + f Factory + priority int + } + dd := make([]p, 0, len(drivers)) + for _, f := range drivers { + dd = append(dd, p{f: f, priority: f.Priority()}) + } + sort.Slice(dd, func(i, j int) bool { + return dd[i].priority < dd[j].priority + }) + return dd[0].f, nil +}