X Tutup
Skip to content

Commit a4aaa09

Browse files
committed
Update ctr so it works again on windows
Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
1 parent 61fbd23 commit a4aaa09

19 files changed

+164
-201
lines changed

client_unix.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ import (
99
"time"
1010
)
1111

12-
// DefaultAddress is the default unix socket address
13-
const DefaultAddress = "/run/containerd/containerd.sock"
14-
1512
func dialer(address string, timeout time.Duration) (net.Conn, error) {
1613
address = strings.TrimPrefix(address, "unix://")
1714
return net.DialTimeout("unix", address, timeout)

client_windows.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import (
77
winio "github.com/Microsoft/go-winio"
88
)
99

10-
// DefaultAddress is the default unix socket address
11-
const DefaultAddress = `\\.\pipe\containerd-containerd`
12-
1310
func dialer(address string, timeout time.Duration) (net.Conn, error) {
1411
return winio.DialPipe(address, &timeout)
1512
}

cmd/containerd/config_linux.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package main
22

33
import (
4-
"github.com/containerd/containerd"
54
"github.com/containerd/containerd/server"
65
)
76

87
func defaultConfig() *server.Config {
98
return &server.Config{
109
Root: "/var/lib/containerd",
1110
GRPC: server.GRPCConfig{
12-
Address: containerd.DefaultAddress,
11+
Address: server.DefaultAddress,
1312
},
1413
Debug: server.Debug{
1514
Level: "info",

cmd/containerd/config_unix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ func defaultConfig() *server.Config {
88
return &server.Config{
99
Root: "/var/lib/containerd",
1010
GRPC: server.GRPCConfig{
11-
Address: "/run/containerd/containerd.sock",
11+
Address: server.DefaultAddress,
1212
},
1313
Debug: server.Debug{
1414
Level: "info",

cmd/ctr/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"os"
66

77
"github.com/Sirupsen/logrus"
8-
"github.com/containerd/containerd"
98
"github.com/containerd/containerd/namespaces"
9+
"github.com/containerd/containerd/server"
1010
"github.com/containerd/containerd/version"
1111
"github.com/urfave/cli"
1212
)
@@ -40,7 +40,7 @@ containerd CLI
4040
cli.StringFlag{
4141
Name: "address, a",
4242
Usage: "address for containerd's GRPC server",
43-
Value: containerd.DefaultAddress,
43+
Value: server.DefaultAddress,
4444
},
4545
cli.DurationFlag{
4646
Name: "timeout",

cmd/ctr/pprof.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package main
33
import (
44
"fmt"
55
"io"
6-
"net"
76
"net/http"
87
"os"
98
"time"
109

10+
"github.com/containerd/containerd/server"
1111
"github.com/pkg/errors"
12-
1312
"github.com/urfave/cli"
1413
)
1514

@@ -25,7 +24,7 @@ var pprofCommand = cli.Command{
2524
cli.StringFlag{
2625
Name: "debug-socket, d",
2726
Usage: "socket path for containerd's debug server",
28-
Value: "/run/containerd/debug.sock",
27+
Value: server.DefaultDebugAddress,
2928
},
3029
},
3130
Subcommands: []cli.Command{
@@ -143,13 +142,8 @@ var pprofThreadcreateCommand = cli.Command{
143142
},
144143
}
145144

146-
func (d *pprofDialer) pprofDial(proto, addr string) (conn net.Conn, err error) {
147-
return net.Dial(d.proto, d.addr)
148-
}
149-
150145
func getPProfClient(context *cli.Context) *http.Client {
151-
addr := context.GlobalString("debug-socket")
152-
dialer := pprofDialer{"unix", addr}
146+
dialer := getPProfDialer(context.GlobalString("debug-socket"))
153147

154148
tr := &http.Transport{
155149
Dial: dialer.pprofDial,

cmd/ctr/pprof_unix.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// +build !windows
2+
3+
package main
4+
5+
import "net"
6+
7+
func (d *pprofDialer) pprofDial(proto, addr string) (conn net.Conn, err error) {
8+
return net.Dial(d.proto, d.addr)
9+
}
10+
11+
func getPProfDialer(addr string) *pprofDialer {
12+
return &pprofDialer{"unix", addr}
13+
}

cmd/ctr/pprof_windows.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import (
4+
"net"
5+
6+
winio "github.com/Microsoft/go-winio"
7+
)
8+
9+
func (d *pprofDialer) pprofDial(proto, addr string) (conn net.Conn, err error) {
10+
return winio.DialPipe(d.addr, nil)
11+
}
12+
13+
func getPProfDialer(addr string) *pprofDialer {
14+
return &pprofDialer{"winpipe", addr}
15+
}

cmd/ctr/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func withEnv(context *cli.Context) containerd.SpecOpts {
2525
return func(s *specs.Spec) error {
2626
env := context.StringSlice("env")
2727
if len(env) > 0 {
28-
s.Process.Env = append(s.Process.Env, env...)
28+
s.Process.Env = replaceOrAppendEnvValues(s.Process.Env, env)
2929
}
3030
return nil
3131
}

cmd/ctr/run_windows.go

Lines changed: 35 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -2,149 +2,37 @@ package main
22

33
import (
44
gocontext "context"
5-
"encoding/json"
6-
"fmt"
7-
"io/ioutil"
85
"time"
96

107
"github.com/Sirupsen/logrus"
118
"github.com/containerd/console"
129
"github.com/containerd/containerd"
13-
"github.com/containerd/containerd/api/services/tasks/v1"
10+
"github.com/containerd/containerd/errdefs"
1411
"github.com/containerd/containerd/log"
15-
"github.com/containerd/containerd/mount"
16-
"github.com/containerd/containerd/windows"
17-
"github.com/containerd/containerd/windows/hcs"
1812
digest "github.com/opencontainers/go-digest"
19-
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2013
specs "github.com/opencontainers/runtime-spec/specs-go"
14+
"github.com/pkg/errors"
2115
"github.com/urfave/cli"
2216
)
2317

2418
const pipeRoot = `\\.\pipe`
2519

2620
func init() {
2721
runCommand.Flags = append(runCommand.Flags, cli.StringSliceFlag{
28-
Name: "layers",
22+
Name: "layer",
2923
Usage: "HCSSHIM Layers to be used",
3024
})
3125
}
3226

33-
func spec(id string, config *ocispec.ImageConfig, context *cli.Context) *specs.Spec {
34-
cmd := config.Cmd
35-
if a := context.Args().First(); a != "" {
36-
cmd = context.Args()
37-
}
38-
39-
var (
40-
// TODO: support overriding entrypoint
41-
args = append(config.Entrypoint, cmd...)
42-
tty = context.Bool("tty")
43-
cwd = config.WorkingDir
44-
)
45-
46-
if cwd == "" {
47-
cwd = `C:\`
48-
}
49-
50-
// Some sane defaults for console
51-
w := 80
52-
h := 20
53-
54-
if tty {
55-
con := console.Current()
56-
size, err := con.Size()
57-
if err == nil {
58-
w = int(size.Width)
59-
h = int(size.Height)
27+
func withLayers(context *cli.Context) containerd.SpecOpts {
28+
return func(s *specs.Spec) error {
29+
l := context.StringSlice("layer")
30+
if l == nil {
31+
return errors.Wrap(errdefs.ErrInvalidArgument, "base layers must be specified with `--layer`")
6032
}
33+
s.Windows.LayerFolders = l
34+
return nil
6135
}
62-
63-
env := replaceOrAppendEnvValues(config.Env, context.StringSlice("env"))
64-
65-
return &specs.Spec{
66-
Version: specs.Version,
67-
Root: &specs.Root{
68-
Readonly: context.Bool("readonly"),
69-
},
70-
Process: &specs.Process{
71-
Args: args,
72-
Terminal: tty,
73-
Cwd: cwd,
74-
Env: env,
75-
User: specs.User{
76-
Username: config.User,
77-
},
78-
ConsoleSize: &specs.Box{
79-
Height: uint(w),
80-
Width: uint(h),
81-
},
82-
},
83-
Hostname: id,
84-
}
85-
}
86-
87-
func customSpec(context *cli.Context, configPath, rootfs string) (*specs.Spec, error) {
88-
b, err := ioutil.ReadFile(configPath)
89-
if err != nil {
90-
return nil, err
91-
}
92-
93-
var s specs.Spec
94-
if err := json.Unmarshal(b, &s); err != nil {
95-
return nil, err
96-
}
97-
98-
if rootfs != "" && s.Root.Path != rootfs {
99-
logrus.Warnf("ignoring config Root.Path %q, setting %q forcibly", s.Root.Path, rootfs)
100-
s.Root.Path = rootfs
101-
}
102-
return &s, nil
103-
}
104-
105-
func getConfig(context *cli.Context, imageConfig *ocispec.ImageConfig, rootfs string) (*specs.Spec, error) {
106-
if config := context.String("runtime-config"); config != "" {
107-
return customSpec(context, config, rootfs)
108-
}
109-
110-
s := spec(context.String("id"), imageConfig, context)
111-
if rootfs != "" {
112-
s.Root.Path = rootfs
113-
}
114-
115-
return s, nil
116-
}
117-
118-
func newContainerSpec(context *cli.Context, config *ocispec.ImageConfig, imageRef string) ([]byte, error) {
119-
spec, err := getConfig(context, config, context.String("rootfs"))
120-
if err != nil {
121-
return nil, err
122-
}
123-
if spec.Annotations == nil {
124-
spec.Annotations = make(map[string]string)
125-
}
126-
spec.Annotations["image"] = imageRef
127-
rtSpec := windows.RuntimeSpec{
128-
OCISpec: *spec,
129-
Configuration: hcs.Configuration{
130-
Layers: context.StringSlice("layers"),
131-
IgnoreFlushesDuringBoot: true,
132-
AllowUnqualifiedDNSQuery: true},
133-
}
134-
return json.Marshal(rtSpec)
135-
}
136-
137-
func newCreateTaskRequest(context *cli.Context, id, tmpDir string, checkpoint *ocispec.Descriptor, mounts []mount.Mount) (*tasks.CreateTaskRequest, error) {
138-
create := &tasks.CreateTaskRequest{
139-
ContainerID: id,
140-
Terminal: context.Bool("tty"),
141-
Stdin: fmt.Sprintf(`%s\ctr-%s-stdin`, pipeRoot, id),
142-
Stdout: fmt.Sprintf(`%s\ctr-%s-stdout`, pipeRoot, id),
143-
}
144-
if !create.Terminal {
145-
create.Stderr = fmt.Sprintf(`%s\ctr-%s-stderr`, pipeRoot, id)
146-
}
147-
return create, nil
14836
}
14937

15038
func handleConsoleResize(ctx gocontext.Context, task resizer, con console.Console) error {
@@ -175,7 +63,14 @@ func handleConsoleResize(ctx gocontext.Context, task resizer, con console.Consol
17563
return nil
17664
}
17765

178-
func withTTY() containerd.SpecOpts {
66+
func withTTY(terminal bool) containerd.SpecOpts {
67+
if !terminal {
68+
return func(s *specs.Spec) error {
69+
s.Process.Terminal = false
70+
return nil
71+
}
72+
}
73+
17974
con := console.Current()
18075
size, err := con.Size()
18176
if err != nil {
@@ -192,43 +87,37 @@ func newContainer(ctx gocontext.Context, client *containerd.Client, context *cli
19287
var (
19388
err error
19489

195-
ref = context.Args().First()
196-
id = context.Args().Get(1)
197-
args = context.Args()[2:]
198-
tty = context.Bool("tty")
90+
// ref = context.Args().First()
91+
id = context.Args().Get(1)
92+
args = context.Args()[2:]
93+
tty = context.Bool("tty")
94+
labelStrings = context.StringSlice("label")
19995
)
200-
image, err := client.GetImage(ctx, ref)
201-
if err != nil {
202-
return nil, err
203-
}
96+
97+
labels := labelArgs(labelStrings)
98+
99+
// TODO(mlaventure): get base image once we have a snapshotter
100+
204101
opts := []containerd.SpecOpts{
205-
containerd.WithImageConfig(ctx, image),
102+
// TODO(mlaventure): use containerd.WithImageConfig once we have a snapshotter
103+
withLayers(context),
206104
withEnv(context),
207105
withMounts(context),
106+
withTTY(tty),
208107
}
209108
if len(args) > 0 {
210109
opts = append(opts, containerd.WithProcessArgs(args...))
211110
}
212-
if tty {
213-
opts = append(opts, withTTY())
214-
}
215-
if context.Bool("net-host") {
216-
opts = append(opts, setHostNetworking())
217-
}
111+
218112
spec, err := containerd.GenerateSpec(opts...)
219113
if err != nil {
220114
return nil, err
221115
}
222-
var rootfs containerd.NewContainerOpts
223-
if context.Bool("readonly") {
224-
rootfs = containerd.WithNewReadonlyRootFS(id, image)
225-
} else {
226-
rootfs = containerd.WithNewRootFS(id, image)
227-
}
116+
228117
return client.NewContainer(ctx, id,
229118
containerd.WithSpec(spec),
230-
containerd.WithImage(image),
231-
rootfs,
119+
containerd.WithContainerLabels(labels),
120+
// TODO(mlaventure): containerd.WithImage(image),
232121
)
233122
}
234123

0 commit comments

Comments
 (0)
X Tutup