mirror of
https://gitea.com/Lydanne/buildx.git
synced 2025-05-18 09:17:49 +08:00

Tested with `kind` and GKE. Note: "nodes" shown in `docker buildx ls` are unrelated to Kubernetes "nodes". Probably buildx should come up with an alternative term. Usage: $ kind create cluster $ export KUBECONFIG="$(kind get kubeconfig-path --name="kind")" $ docker buildx create --driver kubernetes --driver-opt replicas=3 --use $ docker buildx build -t foo --load . `--load` loads the image into the local Docker. Driver opts: - `image=IMAGE` - Sets the container image to be used for running buildkit. - `namespace=NS` - Sets the Kubernetes namespace. Defaults to the current namespace. - `replicas=N` - Sets the number of `Pod` replicas. Defaults to 1. - `rootless=(true|false)` - Run the container as a non-root user without `securityContext.privileged`. Defaults to false. - `loadbalance=(sticky|random)` - Load-balancing strategy. If set to "sticky", the pod is chosen using the hash of the context path. Defaults to "sticky" Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
102 lines
2.3 KiB
Go
102 lines
2.3 KiB
Go
package driver
|
|
|
|
import (
|
|
"context"
|
|
"sort"
|
|
|
|
dockerclient "github.com/docker/docker/client"
|
|
"github.com/pkg/errors"
|
|
"k8s.io/client-go/tools/clientcmd"
|
|
)
|
|
|
|
type Factory interface {
|
|
Name() string
|
|
Usage() string
|
|
Priority(context.Context, dockerclient.APIClient) int
|
|
New(ctx context.Context, cfg InitConfig) (Driver, error)
|
|
AllowsInstances() bool
|
|
}
|
|
|
|
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
|
|
KubeClientConfig clientcmd.ClientConfig
|
|
BuildkitFlags []string
|
|
ConfigFile string
|
|
DriverOpts map[string]string
|
|
// ContextPathHash can be used for determining pods in the driver instance
|
|
ContextPathHash string
|
|
}
|
|
|
|
var drivers map[string]Factory
|
|
|
|
func Register(f Factory) {
|
|
if drivers == nil {
|
|
drivers = map[string]Factory{}
|
|
}
|
|
drivers[f.Name()] = f
|
|
}
|
|
|
|
func GetDefaultFactory(ctx context.Context, c dockerclient.APIClient, instanceRequired bool) (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 {
|
|
if instanceRequired && !f.AllowsInstances() {
|
|
continue
|
|
}
|
|
dd = append(dd, p{f: f, priority: f.Priority(ctx, c)})
|
|
}
|
|
sort.Slice(dd, func(i, j int) bool {
|
|
return dd[i].priority < dd[j].priority
|
|
})
|
|
return dd[0].f, nil
|
|
}
|
|
|
|
func GetFactory(name string, instanceRequired bool) Factory {
|
|
for _, f := range drivers {
|
|
if instanceRequired && !f.AllowsInstances() {
|
|
continue
|
|
}
|
|
if f.Name() == name {
|
|
return f
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func GetDriver(ctx context.Context, name string, f Factory, api dockerclient.APIClient, kcc clientcmd.ClientConfig, flags []string, config string, do map[string]string, contextPathHash string) (Driver, error) {
|
|
ic := InitConfig{
|
|
DockerAPI: api,
|
|
KubeClientConfig: kcc,
|
|
Name: name,
|
|
BuildkitFlags: flags,
|
|
ConfigFile: config,
|
|
DriverOpts: do,
|
|
ContextPathHash: contextPathHash,
|
|
}
|
|
if f == nil {
|
|
var err error
|
|
f, err = GetDefaultFactory(ctx, api, false)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return f.New(ctx, ic)
|
|
}
|
|
|
|
func GetFactories() map[string]Factory {
|
|
return drivers
|
|
}
|