From 50e0fad78000d905337b2844011a99e4f4bcd35a Mon Sep 17 00:00:00 2001 From: Alex Suraci Date: Sun, 29 May 2022 22:01:55 -0400 Subject: [PATCH] resolve defaults in main rather than runtime init otherwise no services get forwarded and the remote end tries to talk to a local buildkit instead --- cmd/bass/main.go | 20 ++++++++++++++++++++ pkg/runtimes/buildkit.go | 29 ++++++----------------------- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/cmd/bass/main.go b/cmd/bass/main.go index 83a47ee8..301c2a16 100644 --- a/cmd/bass/main.go +++ b/cmd/bass/main.go @@ -6,9 +6,11 @@ import ( "net" "net/http" _ "net/http/pprof" + "net/url" "os" "runtime/pprof" + "github.com/adrg/xdg" flag "github.com/spf13/pflag" "github.com/vito/bass/pkg/bass" "github.com/vito/bass/pkg/cli" @@ -79,15 +81,33 @@ func main() { } } +var buildkitAddrs = bass.RuntimeAddrs{} + var DefaultConfig = bass.Config{ Runtimes: []bass.RuntimeConfig{ { Platform: bass.LinuxPlatform, Runtime: runtimes.BuildkitName, + Addrs: buildkitAddrs, }, }, } +func init() { + // support respecting XDG_RUNTIME_DIR instead of assuming /run/ + sockPath, _ := xdg.SearchConfigFile("bass/buildkitd.sock") + + if sockPath == "" { + sockPath, _ = xdg.SearchRuntimeFile("buildkit/buildkitd.sock") + } + + if sockPath == "" { + sockPath = "/run/buildkit/buildkitd.sock" + } + + buildkitAddrs[runtimes.BuildkitdAddrName] = &url.URL{Scheme: "unix", Path: sockPath} +} + func root(ctx context.Context) error { if showVersion { printVersion(ctx) diff --git a/pkg/runtimes/buildkit.go b/pkg/runtimes/buildkit.go index 446d2eee..afa2f672 100644 --- a/pkg/runtimes/buildkit.go +++ b/pkg/runtimes/buildkit.go @@ -8,7 +8,6 @@ import ( "fmt" "io" "io/fs" - "net/url" "os" "path" "path/filepath" @@ -17,7 +16,6 @@ import ( "text/tabwriter" "time" - "github.com/adrg/xdg" "github.com/containerd/containerd/platforms" "github.com/docker/distribution/reference" "github.com/moby/buildkit/client" @@ -96,7 +94,12 @@ func NewBuildkit(_ bass.RuntimePool, addrs bass.RuntimeAddrs, cfg *bass.Scope) ( } } - client, err := dialBuildkit(addrs) + addr, found := addrs.Service(BuildkitdAddrName) + if !found { + return nil, fmt.Errorf("service not configured: %s", BuildkitdAddrName) + } + + client, err := kitdclient.New(context.TODO(), addr.String()) if err != nil { return nil, fmt.Errorf("dial buildkit: %w", err) } @@ -126,26 +129,6 @@ func NewBuildkit(_ bass.RuntimePool, addrs bass.RuntimeAddrs, cfg *bass.Scope) ( }, nil } -func dialBuildkit(addrs bass.RuntimeAddrs) (*kitdclient.Client, error) { - addr, found := addrs.Service(BuildkitdAddrName) - if !found { - // support respecting XDG_RUNTIME_DIR instead of assuming /run/ - sockPath, _ := xdg.SearchConfigFile("bass/buildkitd.sock") - - if sockPath == "" { - sockPath, _ = xdg.SearchRuntimeFile("buildkit/buildkitd.sock") - } - - if sockPath == "" { - sockPath = "/run/buildkit/buildkitd.sock" - } - - addr = &url.URL{Scheme: "unix", Path: sockPath} - } - - return kitdclient.New(context.TODO(), addr.String()) -} - func (runtime *Buildkit) Resolve(ctx context.Context, imageRef bass.ThunkImageRef) (bass.ThunkImageRef, error) { ref, err := imageRef.Ref() if err != nil {