Debian Codegraph @

buildkit/client/connhelper/nerdctlcontainer/nerdctlcontainer.go

raw
1 // Package nerdctlcontainer provides connhelper for nerdctl-container://<container>
2 package nerdctlcontainer
3
4 import (
5 "context"
6 "net"
7 "net/url"
8
9 "github.com/docker/cli/cli/connhelper/commandconn"
10 "github.com/moby/buildkit/client/connhelper"
11 "github.com/pkg/errors"
12 )
13
14 func init() {
15 connhelper.Register("nerdctl-container", Helper)
16 }
17
18 // Helper returns helper for connecting to a Nerdctl container.
19 // Requires BuildKit v0.5.0 or later in the container.
20 func Helper(u *url.URL) (*connhelper.ConnectionHelper, error) {
21 sp, err := SpecFromURL(u)
22 if err != nil {
23 return nil, err
24 }
25 return &connhelper.ConnectionHelper{
26 ContextDialer: func(ctx context.Context, addr string) (net.Conn, error) {
27 // using background context because context remains active for the duration of the process, after dial has completed
28 args := []string{"exec"}
29 if sp.Namespace != "" {
30 args = append(args, "--namespace", sp.Namespace)
31 }
32 args = append(args, "-i", sp.Container, "buildctl", "dial-stdio")
33 return commandconn.New(context.Background(), "nerdctl", args...)
34 },
35 }, nil
36 }
37
38 // Spec
39 type Spec struct {
40 Container string
41 Namespace string
42 }
43
44 // SpecFromURL creates Spec from URL.
45 // URL is like nerdctl-container://<container>?namespace=<namespace>
46 // Only <container> part is mandatory.
47 func SpecFromURL(u *url.URL) (*Spec, error) {
48 q := u.Query()
49 sp := Spec{
50 Container: u.Hostname(),
51 Namespace: q.Get("namespace"),
52 }
53 if sp.Container == "" {
54 return nil, errors.New("url lacks container name")
55 }
56 return &sp, nil
57 }