2019-03-26 06:14:39 +08:00
|
|
|
package driver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-03-10 08:46:06 +08:00
|
|
|
"os"
|
2019-03-26 06:14:39 +08:00
|
|
|
"sort"
|
2020-08-28 10:27:56 +08:00
|
|
|
"strings"
|
2020-09-21 08:44:16 +08:00
|
|
|
"sync"
|
2020-08-28 10:27:56 +08:00
|
|
|
|
|
|
|
"k8s.io/client-go/rest"
|
2019-03-26 06:14:39 +08:00
|
|
|
|
|
|
|
dockerclient "github.com/docker/docker/client"
|
2020-09-21 08:44:16 +08:00
|
|
|
"github.com/moby/buildkit/client"
|
2020-12-15 14:53:32 +08:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2019-03-26 06:14:39 +08:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Factory interface {
|
|
|
|
Name() string
|
|
|
|
Usage() string
|
2022-04-25 18:28:40 +08:00
|
|
|
Priority(ctx context.Context, endpoint string, api dockerclient.APIClient) int
|
2019-03-26 06:14:39 +08:00
|
|
|
New(ctx context.Context, cfg InitConfig) (Driver, error)
|
2019-04-13 07:39:06 +08:00
|
|
|
AllowsInstances() bool
|
2019-03-26 06:14:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type BuildkitConfig struct {
|
|
|
|
// Entitlements []string
|
|
|
|
// Rootless bool
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:27:56 +08:00
|
|
|
type KubeClientConfig interface {
|
|
|
|
ClientConfig() (*rest.Config, error)
|
|
|
|
Namespace() (string, bool, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type KubeClientConfigInCluster struct{}
|
|
|
|
|
|
|
|
func (k KubeClientConfigInCluster) ClientConfig() (*rest.Config, error) {
|
|
|
|
return rest.InClusterConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k KubeClientConfigInCluster) Namespace() (string, bool, error) {
|
2022-03-10 08:46:06 +08:00
|
|
|
namespace, err := os.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace")
|
2020-08-28 10:27:56 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
return strings.TrimSpace(string(namespace)), true, nil
|
|
|
|
}
|
|
|
|
|
2019-03-26 06:14:39 +08:00
|
|
|
type InitConfig struct {
|
|
|
|
// This object needs updates to be generic for different drivers
|
2019-10-21 14:02:37 +08:00
|
|
|
Name string
|
2022-04-25 18:28:40 +08:00
|
|
|
EndpointAddr string
|
2019-10-21 14:02:37 +08:00
|
|
|
DockerAPI dockerclient.APIClient
|
2020-08-28 10:27:56 +08:00
|
|
|
KubeClientConfig KubeClientConfig
|
2019-10-21 14:02:37 +08:00
|
|
|
BuildkitFlags []string
|
2021-11-03 13:48:20 +08:00
|
|
|
Files map[string][]byte
|
2019-10-21 14:02:37 +08:00
|
|
|
DriverOpts map[string]string
|
2020-11-16 12:49:58 +08:00
|
|
|
Auth Auth
|
2020-12-15 14:53:32 +08:00
|
|
|
Platforms []specs.Platform
|
2019-10-21 14:02:37 +08:00
|
|
|
// ContextPathHash can be used for determining pods in the driver instance
|
|
|
|
ContextPathHash string
|
2019-03-26 06:14:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var drivers map[string]Factory
|
|
|
|
|
|
|
|
func Register(f Factory) {
|
|
|
|
if drivers == nil {
|
|
|
|
drivers = map[string]Factory{}
|
|
|
|
}
|
|
|
|
drivers[f.Name()] = f
|
|
|
|
}
|
|
|
|
|
2022-04-25 18:28:40 +08:00
|
|
|
func GetDefaultFactory(ctx context.Context, ep string, c dockerclient.APIClient, instanceRequired bool) (Factory, error) {
|
2019-03-26 06:14:39 +08:00
|
|
|
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 {
|
2019-04-13 07:39:06 +08:00
|
|
|
if instanceRequired && !f.AllowsInstances() {
|
|
|
|
continue
|
|
|
|
}
|
2022-04-25 18:28:40 +08:00
|
|
|
dd = append(dd, p{f: f, priority: f.Priority(ctx, ep, c)})
|
2019-03-26 06:14:39 +08:00
|
|
|
}
|
|
|
|
sort.Slice(dd, func(i, j int) bool {
|
|
|
|
return dd[i].priority < dd[j].priority
|
|
|
|
})
|
|
|
|
return dd[0].f, nil
|
|
|
|
}
|
2019-03-26 10:01:31 +08:00
|
|
|
|
2022-08-16 17:58:23 +08:00
|
|
|
func GetFactory(name string, instanceRequired bool) (Factory, error) {
|
2019-04-13 07:39:06 +08:00
|
|
|
for _, f := range drivers {
|
|
|
|
if f.Name() == name {
|
2022-08-16 17:58:23 +08:00
|
|
|
if instanceRequired && !f.AllowsInstances() {
|
|
|
|
return nil, errors.Errorf("additional instances of driver %q cannot be created", name)
|
|
|
|
}
|
|
|
|
return f, nil
|
2019-04-13 07:39:06 +08:00
|
|
|
}
|
|
|
|
}
|
2022-08-16 17:58:23 +08:00
|
|
|
return nil, errors.Errorf("failed to find driver %q", name)
|
2019-04-13 07:39:06 +08:00
|
|
|
}
|
|
|
|
|
2022-04-25 18:28:40 +08:00
|
|
|
func GetDriver(ctx context.Context, name string, f Factory, endpointAddr string, api dockerclient.APIClient, auth Auth, kcc KubeClientConfig, flags []string, files map[string][]byte, do map[string]string, platforms []specs.Platform, contextPathHash string) (Driver, error) {
|
2019-04-11 08:42:04 +08:00
|
|
|
ic := InitConfig{
|
2022-04-25 18:28:40 +08:00
|
|
|
EndpointAddr: endpointAddr,
|
2019-10-21 14:02:37 +08:00
|
|
|
DockerAPI: api,
|
|
|
|
KubeClientConfig: kcc,
|
|
|
|
Name: name,
|
|
|
|
BuildkitFlags: flags,
|
|
|
|
DriverOpts: do,
|
2020-11-16 12:49:58 +08:00
|
|
|
Auth: auth,
|
2020-12-15 14:53:32 +08:00
|
|
|
Platforms: platforms,
|
2019-10-21 14:02:37 +08:00
|
|
|
ContextPathHash: contextPathHash,
|
2021-11-03 13:48:20 +08:00
|
|
|
Files: files,
|
2019-04-11 08:42:04 +08:00
|
|
|
}
|
2019-03-26 10:01:31 +08:00
|
|
|
if f == nil {
|
|
|
|
var err error
|
2022-04-25 18:28:40 +08:00
|
|
|
f, err = GetDefaultFactory(ctx, endpointAddr, api, false)
|
2019-03-26 10:01:31 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2020-09-21 08:44:16 +08:00
|
|
|
d, err := f.New(ctx, ic)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &cachedDriver{Driver: d}, nil
|
2019-03-26 10:01:31 +08:00
|
|
|
}
|
2019-05-26 06:45:20 +08:00
|
|
|
|
2022-08-16 17:58:23 +08:00
|
|
|
func GetFactories(instanceRequired bool) []Factory {
|
2021-09-29 21:33:19 +08:00
|
|
|
ds := make([]Factory, 0, len(drivers))
|
|
|
|
for _, d := range drivers {
|
2022-08-16 17:58:23 +08:00
|
|
|
if instanceRequired && !d.AllowsInstances() {
|
|
|
|
continue
|
|
|
|
}
|
2021-09-29 21:33:19 +08:00
|
|
|
ds = append(ds, d)
|
|
|
|
}
|
|
|
|
sort.Slice(ds, func(i, j int) bool {
|
|
|
|
return ds[i].Name() < ds[j].Name()
|
|
|
|
})
|
|
|
|
return ds
|
2019-05-26 06:45:20 +08:00
|
|
|
}
|
2020-09-21 08:44:16 +08:00
|
|
|
|
|
|
|
type cachedDriver struct {
|
|
|
|
Driver
|
|
|
|
client *client.Client
|
|
|
|
err error
|
|
|
|
once sync.Once
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *cachedDriver) Client(ctx context.Context) (*client.Client, error) {
|
|
|
|
d.once.Do(func() {
|
|
|
|
d.client, d.err = d.Driver.Client(ctx)
|
|
|
|
})
|
|
|
|
return d.client, d.err
|
|
|
|
}
|