feat: add namespace support for nerdctl container

Signed-off-by: Wei Zhang <kweizh@gmail.com>
This commit is contained in:
Wei Zhang
2023-01-07 17:38:38 +08:00
parent b55dab7c68
commit 7533a100d4
2 changed files with 11 additions and 2 deletions

View File

@@ -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://<container>
// URL is like nerdctl-container://<container>?namespace=<namespace>
// Only <container> 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")

View File

@@ -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"