Skip to content

Commit

Permalink
Update ctr so it works again on windows
Browse files Browse the repository at this point in the history
Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
  • Loading branch information
mlaventure committed Jul 21, 2017
1 parent 61fbd23 commit a4aaa09
Show file tree
Hide file tree
Showing 19 changed files with 164 additions and 201 deletions.
3 changes: 0 additions & 3 deletions client_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (
"time"
)

// DefaultAddress is the default unix socket address
const DefaultAddress = "/run/containerd/containerd.sock"

func dialer(address string, timeout time.Duration) (net.Conn, error) {
address = strings.TrimPrefix(address, "unix://")
return net.DialTimeout("unix", address, timeout)
Expand Down
3 changes: 0 additions & 3 deletions client_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (
winio "github.com/Microsoft/go-winio"
)

// DefaultAddress is the default unix socket address
const DefaultAddress = `\\.\pipe\containerd-containerd`

func dialer(address string, timeout time.Duration) (net.Conn, error) {
return winio.DialPipe(address, &timeout)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/containerd/config_linux.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package main

import (
"github.com/containerd/containerd"
"github.com/containerd/containerd/server"
)

func defaultConfig() *server.Config {
return &server.Config{
Root: "/var/lib/containerd",
GRPC: server.GRPCConfig{
Address: containerd.DefaultAddress,
Address: server.DefaultAddress,
},
Debug: server.Debug{
Level: "info",
Expand Down
2 changes: 1 addition & 1 deletion cmd/containerd/config_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ func defaultConfig() *server.Config {
return &server.Config{
Root: "/var/lib/containerd",
GRPC: server.GRPCConfig{
Address: "/run/containerd/containerd.sock",
Address: server.DefaultAddress,
},
Debug: server.Debug{
Level: "info",
Expand Down
4 changes: 2 additions & 2 deletions cmd/ctr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"os"

"github.com/Sirupsen/logrus"
"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/server"
"github.com/containerd/containerd/version"
"github.com/urfave/cli"
)
Expand Down Expand Up @@ -40,7 +40,7 @@ containerd CLI
cli.StringFlag{
Name: "address, a",
Usage: "address for containerd's GRPC server",
Value: containerd.DefaultAddress,
Value: server.DefaultAddress,
},
cli.DurationFlag{
Name: "timeout",
Expand Down
12 changes: 3 additions & 9 deletions cmd/ctr/pprof.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package main
import (
"fmt"
"io"
"net"
"net/http"
"os"
"time"

"github.com/containerd/containerd/server"
"github.com/pkg/errors"

"github.com/urfave/cli"
)

Expand All @@ -25,7 +24,7 @@ var pprofCommand = cli.Command{
cli.StringFlag{
Name: "debug-socket, d",
Usage: "socket path for containerd's debug server",
Value: "/run/containerd/debug.sock",
Value: server.DefaultDebugAddress,
},
},
Subcommands: []cli.Command{
Expand Down Expand Up @@ -143,13 +142,8 @@ var pprofThreadcreateCommand = cli.Command{
},
}

func (d *pprofDialer) pprofDial(proto, addr string) (conn net.Conn, err error) {
return net.Dial(d.proto, d.addr)
}

func getPProfClient(context *cli.Context) *http.Client {
addr := context.GlobalString("debug-socket")
dialer := pprofDialer{"unix", addr}
dialer := getPProfDialer(context.GlobalString("debug-socket"))

tr := &http.Transport{
Dial: dialer.pprofDial,
Expand Down
13 changes: 13 additions & 0 deletions cmd/ctr/pprof_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build !windows

package main

import "net"

func (d *pprofDialer) pprofDial(proto, addr string) (conn net.Conn, err error) {
return net.Dial(d.proto, d.addr)
}

func getPProfDialer(addr string) *pprofDialer {
return &pprofDialer{"unix", addr}
}
15 changes: 15 additions & 0 deletions cmd/ctr/pprof_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"net"

winio "github.com/Microsoft/go-winio"
)

func (d *pprofDialer) pprofDial(proto, addr string) (conn net.Conn, err error) {
return winio.DialPipe(d.addr, nil)
}

func getPProfDialer(addr string) *pprofDialer {
return &pprofDialer{"winpipe", addr}
}
2 changes: 1 addition & 1 deletion cmd/ctr/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func withEnv(context *cli.Context) containerd.SpecOpts {
return func(s *specs.Spec) error {
env := context.StringSlice("env")
if len(env) > 0 {
s.Process.Env = append(s.Process.Env, env...)
s.Process.Env = replaceOrAppendEnvValues(s.Process.Env, env)
}
return nil
}
Expand Down
181 changes: 35 additions & 146 deletions cmd/ctr/run_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,149 +2,37 @@ package main

import (
gocontext "context"
"encoding/json"
"fmt"
"io/ioutil"
"time"

"github.com/Sirupsen/logrus"
"github.com/containerd/console"
"github.com/containerd/containerd"
"github.com/containerd/containerd/api/services/tasks/v1"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/windows"
"github.com/containerd/containerd/windows/hcs"
digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/urfave/cli"
)

const pipeRoot = `\\.\pipe`

func init() {
runCommand.Flags = append(runCommand.Flags, cli.StringSliceFlag{
Name: "layers",
Name: "layer",
Usage: "HCSSHIM Layers to be used",
})
}

func spec(id string, config *ocispec.ImageConfig, context *cli.Context) *specs.Spec {
cmd := config.Cmd
if a := context.Args().First(); a != "" {
cmd = context.Args()
}

var (
// TODO: support overriding entrypoint
args = append(config.Entrypoint, cmd...)
tty = context.Bool("tty")
cwd = config.WorkingDir
)

if cwd == "" {
cwd = `C:\`
}

// Some sane defaults for console
w := 80
h := 20

if tty {
con := console.Current()
size, err := con.Size()
if err == nil {
w = int(size.Width)
h = int(size.Height)
func withLayers(context *cli.Context) containerd.SpecOpts {
return func(s *specs.Spec) error {
l := context.StringSlice("layer")
if l == nil {
return errors.Wrap(errdefs.ErrInvalidArgument, "base layers must be specified with `--layer`")
}
s.Windows.LayerFolders = l
return nil
}

env := replaceOrAppendEnvValues(config.Env, context.StringSlice("env"))

return &specs.Spec{
Version: specs.Version,
Root: &specs.Root{
Readonly: context.Bool("readonly"),
},
Process: &specs.Process{
Args: args,
Terminal: tty,
Cwd: cwd,
Env: env,
User: specs.User{
Username: config.User,
},
ConsoleSize: &specs.Box{
Height: uint(w),
Width: uint(h),
},
},
Hostname: id,
}
}

func customSpec(context *cli.Context, configPath, rootfs string) (*specs.Spec, error) {
b, err := ioutil.ReadFile(configPath)
if err != nil {
return nil, err
}

var s specs.Spec
if err := json.Unmarshal(b, &s); err != nil {
return nil, err
}

if rootfs != "" && s.Root.Path != rootfs {
logrus.Warnf("ignoring config Root.Path %q, setting %q forcibly", s.Root.Path, rootfs)
s.Root.Path = rootfs
}
return &s, nil
}

func getConfig(context *cli.Context, imageConfig *ocispec.ImageConfig, rootfs string) (*specs.Spec, error) {
if config := context.String("runtime-config"); config != "" {
return customSpec(context, config, rootfs)
}

s := spec(context.String("id"), imageConfig, context)
if rootfs != "" {
s.Root.Path = rootfs
}

return s, nil
}

func newContainerSpec(context *cli.Context, config *ocispec.ImageConfig, imageRef string) ([]byte, error) {
spec, err := getConfig(context, config, context.String("rootfs"))
if err != nil {
return nil, err
}
if spec.Annotations == nil {
spec.Annotations = make(map[string]string)
}
spec.Annotations["image"] = imageRef
rtSpec := windows.RuntimeSpec{
OCISpec: *spec,
Configuration: hcs.Configuration{
Layers: context.StringSlice("layers"),
IgnoreFlushesDuringBoot: true,
AllowUnqualifiedDNSQuery: true},
}
return json.Marshal(rtSpec)
}

func newCreateTaskRequest(context *cli.Context, id, tmpDir string, checkpoint *ocispec.Descriptor, mounts []mount.Mount) (*tasks.CreateTaskRequest, error) {
create := &tasks.CreateTaskRequest{
ContainerID: id,
Terminal: context.Bool("tty"),
Stdin: fmt.Sprintf(`%s\ctr-%s-stdin`, pipeRoot, id),
Stdout: fmt.Sprintf(`%s\ctr-%s-stdout`, pipeRoot, id),
}
if !create.Terminal {
create.Stderr = fmt.Sprintf(`%s\ctr-%s-stderr`, pipeRoot, id)
}
return create, nil
}

func handleConsoleResize(ctx gocontext.Context, task resizer, con console.Console) error {
Expand Down Expand Up @@ -175,7 +63,14 @@ func handleConsoleResize(ctx gocontext.Context, task resizer, con console.Consol
return nil
}

func withTTY() containerd.SpecOpts {
func withTTY(terminal bool) containerd.SpecOpts {
if !terminal {
return func(s *specs.Spec) error {
s.Process.Terminal = false
return nil
}
}

con := console.Current()
size, err := con.Size()
if err != nil {
Expand All @@ -192,43 +87,37 @@ func newContainer(ctx gocontext.Context, client *containerd.Client, context *cli
var (
err error

ref = context.Args().First()
id = context.Args().Get(1)
args = context.Args()[2:]
tty = context.Bool("tty")
// ref = context.Args().First()
id = context.Args().Get(1)
args = context.Args()[2:]
tty = context.Bool("tty")
labelStrings = context.StringSlice("label")
)
image, err := client.GetImage(ctx, ref)
if err != nil {
return nil, err
}

labels := labelArgs(labelStrings)

// TODO(mlaventure): get base image once we have a snapshotter

opts := []containerd.SpecOpts{
containerd.WithImageConfig(ctx, image),
// TODO(mlaventure): use containerd.WithImageConfig once we have a snapshotter
withLayers(context),
withEnv(context),
withMounts(context),
withTTY(tty),
}
if len(args) > 0 {
opts = append(opts, containerd.WithProcessArgs(args...))
}
if tty {
opts = append(opts, withTTY())
}
if context.Bool("net-host") {
opts = append(opts, setHostNetworking())
}

spec, err := containerd.GenerateSpec(opts...)
if err != nil {
return nil, err
}
var rootfs containerd.NewContainerOpts
if context.Bool("readonly") {
rootfs = containerd.WithNewReadonlyRootFS(id, image)
} else {
rootfs = containerd.WithNewRootFS(id, image)
}

return client.NewContainer(ctx, id,
containerd.WithSpec(spec),
containerd.WithImage(image),
rootfs,
containerd.WithContainerLabels(labels),
// TODO(mlaventure): containerd.WithImage(image),
)
}

Expand Down
2 changes: 1 addition & 1 deletion container.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ type NewTaskOpts func(context.Context, *Client, *TaskInfo) error
func (c *container) NewTask(ctx context.Context, ioCreate IOCreation, opts ...NewTaskOpts) (Task, error) {
c.mu.Lock()
defer c.mu.Unlock()
i, err := ioCreate()
i, err := ioCreate(c.c.ID)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit a4aaa09

Please sign in to comment.