From 7533a100d49b341e824ab3ff00cdcb7e419d4f82 Mon Sep 17 00:00:00 2001 From: Wei Zhang Date: Sat, 7 Jan 2023 17:38:38 +0800 Subject: [PATCH] feat: add namespace support for nerdctl container Signed-off-by: Wei Zhang --- .../connhelper/nerdctlcontainer/nerdctlcontainer.go | 12 ++++++++++-- cmd/buildctl/main.go | 1 + 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/client/connhelper/nerdctlcontainer/nerdctlcontainer.go b/client/connhelper/nerdctlcontainer/nerdctlcontainer.go index f3e3cbcdd..80f879485 100644 --- a/client/connhelper/nerdctlcontainer/nerdctlcontainer.go +++ b/client/connhelper/nerdctlcontainer/nerdctlcontainer.go @@ -25,7 +25,12 @@ func Helper(u *url.URL) (*connhelper.ConnectionHelper, error) { return &connhelper.ConnectionHelper{ ContextDialer: func(ctx context.Context, addr string) (net.Conn, error) { // using background context because context remains active for the duration of the process, after dial has completed - return commandconn.New(context.Background(), "nerdctl", []string{"exec", "-i", sp.Container, "buildctl", "dial-stdio"}...) + args := []string{"exec"} + if sp.Namespace != "" { + args = append(args, "--namespace", sp.Namespace) + } + args = append(args, "-i", sp.Container, "buildctl", "dial-stdio") + return commandconn.New(context.Background(), "nerdctl", args...) }, }, nil } @@ -33,14 +38,17 @@ func Helper(u *url.URL) (*connhelper.ConnectionHelper, error) { // Spec type Spec struct { Container string + Namespace string } // SpecFromURL creates Spec from URL. -// URL is like nerdctl-container:// +// URL is like nerdctl-container://?namespace= // Only part is mandatory. func SpecFromURL(u *url.URL) (*Spec, error) { + q := u.Query() sp := Spec{ Container: u.Hostname(), + Namespace: q.Get("namespace"), } if sp.Container == "" { return nil, errors.New("url lacks container name") diff --git a/cmd/buildctl/main.go b/cmd/buildctl/main.go index 8203fc9d7..7ac7c4542 100644 --- a/cmd/buildctl/main.go +++ b/cmd/buildctl/main.go @@ -6,6 +6,7 @@ import ( _ "github.com/moby/buildkit/client/connhelper/dockercontainer" _ "github.com/moby/buildkit/client/connhelper/kubepod" + _ "github.com/moby/buildkit/client/connhelper/nerdctlcontainer" _ "github.com/moby/buildkit/client/connhelper/podmancontainer" _ "github.com/moby/buildkit/client/connhelper/ssh" bccommon "github.com/moby/buildkit/cmd/buildctl/common"