Update ctr so it works again on windows

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
This commit is contained in:
Kenfe-Mickael Laventure 2017-07-16 12:37:04 +02:00
parent 61fbd2311c
commit a4aaa09ccc
No known key found for this signature in database
GPG Key ID: 40CF16616B361216
19 changed files with 164 additions and 201 deletions

View File

@ -9,9 +9,6 @@ import (
"time" "time"
) )
// DefaultAddress is the default unix socket address
const DefaultAddress = "/run/containerd/containerd.sock"
func dialer(address string, timeout time.Duration) (net.Conn, error) { func dialer(address string, timeout time.Duration) (net.Conn, error) {
address = strings.TrimPrefix(address, "unix://") address = strings.TrimPrefix(address, "unix://")
return net.DialTimeout("unix", address, timeout) return net.DialTimeout("unix", address, timeout)

View File

@ -7,9 +7,6 @@ import (
winio "github.com/Microsoft/go-winio" 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) { func dialer(address string, timeout time.Duration) (net.Conn, error) {
return winio.DialPipe(address, &timeout) return winio.DialPipe(address, &timeout)
} }

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
"github.com/containerd/containerd"
"github.com/containerd/containerd/server" "github.com/containerd/containerd/server"
) )
@ -9,7 +8,7 @@ func defaultConfig() *server.Config {
return &server.Config{ return &server.Config{
Root: "/var/lib/containerd", Root: "/var/lib/containerd",
GRPC: server.GRPCConfig{ GRPC: server.GRPCConfig{
Address: containerd.DefaultAddress, Address: server.DefaultAddress,
}, },
Debug: server.Debug{ Debug: server.Debug{
Level: "info", Level: "info",

View File

@ -8,7 +8,7 @@ func defaultConfig() *server.Config {
return &server.Config{ return &server.Config{
Root: "/var/lib/containerd", Root: "/var/lib/containerd",
GRPC: server.GRPCConfig{ GRPC: server.GRPCConfig{
Address: "/run/containerd/containerd.sock", Address: server.DefaultAddress,
}, },
Debug: server.Debug{ Debug: server.Debug{
Level: "info", Level: "info",

View File

@ -5,8 +5,8 @@ import (
"os" "os"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces" "github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/server"
"github.com/containerd/containerd/version" "github.com/containerd/containerd/version"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -40,7 +40,7 @@ containerd CLI
cli.StringFlag{ cli.StringFlag{
Name: "address, a", Name: "address, a",
Usage: "address for containerd's GRPC server", Usage: "address for containerd's GRPC server",
Value: containerd.DefaultAddress, Value: server.DefaultAddress,
}, },
cli.DurationFlag{ cli.DurationFlag{
Name: "timeout", Name: "timeout",

View File

@ -3,13 +3,12 @@ package main
import ( import (
"fmt" "fmt"
"io" "io"
"net"
"net/http" "net/http"
"os" "os"
"time" "time"
"github.com/containerd/containerd/server"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -25,7 +24,7 @@ var pprofCommand = cli.Command{
cli.StringFlag{ cli.StringFlag{
Name: "debug-socket, d", Name: "debug-socket, d",
Usage: "socket path for containerd's debug server", Usage: "socket path for containerd's debug server",
Value: "/run/containerd/debug.sock", Value: server.DefaultDebugAddress,
}, },
}, },
Subcommands: []cli.Command{ Subcommands: []cli.Command{
@ -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 { func getPProfClient(context *cli.Context) *http.Client {
addr := context.GlobalString("debug-socket") dialer := getPProfDialer(context.GlobalString("debug-socket"))
dialer := pprofDialer{"unix", addr}
tr := &http.Transport{ tr := &http.Transport{
Dial: dialer.pprofDial, Dial: dialer.pprofDial,

13
cmd/ctr/pprof_unix.go Normal file
View File

@ -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
cmd/ctr/pprof_windows.go Normal file
View File

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

View File

@ -25,7 +25,7 @@ func withEnv(context *cli.Context) containerd.SpecOpts {
return func(s *specs.Spec) error { return func(s *specs.Spec) error {
env := context.StringSlice("env") env := context.StringSlice("env")
if len(env) > 0 { if len(env) > 0 {
s.Process.Env = append(s.Process.Env, env...) s.Process.Env = replaceOrAppendEnvValues(s.Process.Env, env)
} }
return nil return nil
} }

View File

@ -2,22 +2,16 @@ package main
import ( import (
gocontext "context" gocontext "context"
"encoding/json"
"fmt"
"io/ioutil"
"time" "time"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/containerd/console" "github.com/containerd/console"
"github.com/containerd/containerd" "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/log"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/windows"
"github.com/containerd/containerd/windows/hcs"
digest "github.com/opencontainers/go-digest" digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
specs "github.com/opencontainers/runtime-spec/specs-go" specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -25,126 +19,20 @@ const pipeRoot = `\\.\pipe`
func init() { func init() {
runCommand.Flags = append(runCommand.Flags, cli.StringSliceFlag{ runCommand.Flags = append(runCommand.Flags, cli.StringSliceFlag{
Name: "layers", Name: "layer",
Usage: "HCSSHIM Layers to be used", Usage: "HCSSHIM Layers to be used",
}) })
} }
func spec(id string, config *ocispec.ImageConfig, context *cli.Context) *specs.Spec { func withLayers(context *cli.Context) containerd.SpecOpts {
cmd := config.Cmd return func(s *specs.Spec) error {
if a := context.Args().First(); a != "" { l := context.StringSlice("layer")
cmd = context.Args() if l == nil {
} return errors.Wrap(errdefs.ErrInvalidArgument, "base layers must be specified with `--layer`")
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)
} }
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 { 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
return nil 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() con := console.Current()
size, err := con.Size() size, err := con.Size()
if err != nil { if err != nil {
@ -192,43 +87,37 @@ func newContainer(ctx gocontext.Context, client *containerd.Client, context *cli
var ( var (
err error err error
ref = context.Args().First() // ref = context.Args().First()
id = context.Args().Get(1) id = context.Args().Get(1)
args = context.Args()[2:] args = context.Args()[2:]
tty = context.Bool("tty") tty = context.Bool("tty")
labelStrings = context.StringSlice("label")
) )
image, err := client.GetImage(ctx, ref)
if err != nil { labels := labelArgs(labelStrings)
return nil, err
} // TODO(mlaventure): get base image once we have a snapshotter
opts := []containerd.SpecOpts{ opts := []containerd.SpecOpts{
containerd.WithImageConfig(ctx, image), // TODO(mlaventure): use containerd.WithImageConfig once we have a snapshotter
withLayers(context),
withEnv(context), withEnv(context),
withMounts(context), withMounts(context),
withTTY(tty),
} }
if len(args) > 0 { if len(args) > 0 {
opts = append(opts, containerd.WithProcessArgs(args...)) 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...) spec, err := containerd.GenerateSpec(opts...)
if err != nil { if err != nil {
return nil, err 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, return client.NewContainer(ctx, id,
containerd.WithSpec(spec), containerd.WithSpec(spec),
containerd.WithImage(image), containerd.WithContainerLabels(labels),
rootfs, // TODO(mlaventure): containerd.WithImage(image),
) )
} }

View File

@ -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) { func (c *container) NewTask(ctx context.Context, ioCreate IOCreation, opts ...NewTaskOpts) (Task, error) {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
i, err := ioCreate() i, err := ioCreate(c.c.ID)
if err != nil { if err != nil {
return nil, err return nil, err
} }

35
io.go
View File

@ -4,9 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath"
"sync" "sync"
) )
@ -40,7 +38,7 @@ func (i *IO) Close() error {
return i.closer.Close() return i.closer.Close()
} }
type IOCreation func() (*IO, error) type IOCreation func(id string) (*IO, error)
type IOAttach func(*FIFOSet) (*IO, error) type IOAttach func(*FIFOSet) (*IO, error)
@ -49,8 +47,8 @@ func NewIO(stdin io.Reader, stdout, stderr io.Writer) IOCreation {
} }
func NewIOWithTerminal(stdin io.Reader, stdout, stderr io.Writer, terminal bool) IOCreation { func NewIOWithTerminal(stdin io.Reader, stdout, stderr io.Writer, terminal bool) IOCreation {
return func() (*IO, error) { return func(id string) (*IO, error) {
paths, err := NewFifos() paths, err := NewFifos(id)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -72,7 +70,6 @@ func NewIOWithTerminal(stdin io.Reader, stdout, stderr io.Writer, terminal bool)
i.closer = closer i.closer = closer
return i, nil return i, nil
} }
} }
func WithAttach(stdin io.Reader, stdout, stderr io.Writer) IOAttach { func WithAttach(stdin io.Reader, stdout, stderr io.Writer) IOAttach {
@ -102,31 +99,13 @@ func WithAttach(stdin io.Reader, stdout, stderr io.Writer) IOAttach {
// Stdio returns an IO implementation to be used for a task // Stdio returns an IO implementation to be used for a task
// that outputs the container's IO as the current processes Stdio // that outputs the container's IO as the current processes Stdio
func Stdio() (*IO, error) { func Stdio(id string) (*IO, error) {
return NewIO(os.Stdin, os.Stdout, os.Stderr)() return NewIO(os.Stdin, os.Stdout, os.Stderr)(id)
} }
// StdioTerminal will setup the IO for the task to use a terminal // StdioTerminal will setup the IO for the task to use a terminal
func StdioTerminal() (*IO, error) { func StdioTerminal(id string) (*IO, error) {
return NewIOWithTerminal(os.Stdin, os.Stdout, os.Stderr, true)() return NewIOWithTerminal(os.Stdin, os.Stdout, os.Stderr, true)(id)
}
// NewFifos returns a new set of fifos for the task
func NewFifos() (*FIFOSet, error) {
root := filepath.Join(os.TempDir(), "containerd")
if err := os.MkdirAll(root, 0700); err != nil {
return nil, err
}
dir, err := ioutil.TempDir(root, "")
if err != nil {
return nil, err
}
return &FIFOSet{
Dir: dir,
In: filepath.Join(dir, "stdin"),
Out: filepath.Join(dir, "stdout"),
Err: filepath.Join(dir, "stderr"),
}, nil
} }
type FIFOSet struct { type FIFOSet struct {

View File

@ -5,12 +5,33 @@ package containerd
import ( import (
"context" "context"
"io" "io"
"io/ioutil"
"os"
"path/filepath"
"sync" "sync"
"syscall" "syscall"
"github.com/containerd/fifo" "github.com/containerd/fifo"
) )
// NewFifos returns a new set of fifos for the task
func NewFifos(id string) (*FIFOSet, error) {
root := filepath.Join(os.TempDir(), "containerd")
if err := os.MkdirAll(root, 0700); err != nil {
return nil, err
}
dir, err := ioutil.TempDir(root, "")
if err != nil {
return nil, err
}
return &FIFOSet{
Dir: dir,
In: filepath.Join(dir, id+"-stdin"),
Out: filepath.Join(dir, id+"-stdout"),
Err: filepath.Join(dir, id+"-stderr"),
}, nil
}
func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) { func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) {
var ( var (
f io.ReadWriteCloser f io.ReadWriteCloser

View File

@ -1,6 +1,7 @@
package containerd package containerd
import ( import (
"fmt"
"io" "io"
"net" "net"
"sync" "sync"
@ -10,8 +11,22 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
) )
const pipeRoot = `\\.\pipe`
// NewFifos returns a new set of fifos for the task
func NewFifos(id string) (*FIFOSet, error) {
return &FIFOSet{
In: fmt.Sprintf(`%s\ctr-%s-stdin`, pipeRoot, id),
Out: fmt.Sprintf(`%s\ctr-%s-stdout`, pipeRoot, id),
Err: fmt.Sprintf(`%s\ctr-%s-stderr`, pipeRoot, id),
}, nil
}
func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) { func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) {
var wg sync.WaitGroup var (
wg sync.WaitGroup
set []io.Closer
)
if fifos.In != "" { if fifos.In != "" {
l, err := winio.ListenPipe(fifos.In, nil) l, err := winio.ListenPipe(fifos.In, nil)
@ -23,6 +38,7 @@ func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) {
l.Close() l.Close()
} }
}(l) }(l)
set = append(set, l)
go func() { go func() {
c, err := l.Accept() c, err := l.Accept()
@ -46,6 +62,7 @@ func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) {
l.Close() l.Close()
} }
}(l) }(l)
set = append(set, l)
wg.Add(1) wg.Add(1)
go func() { go func() {
@ -71,6 +88,7 @@ func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) {
l.Close() l.Close()
} }
}(l) }(l)
set = append(set, l)
wg.Add(1) wg.Add(1)
go func() { go func() {
@ -89,5 +107,11 @@ func copyIO(fifos *FIFOSet, ioset *ioSet, tty bool) (_ *wgCloser, err error) {
return &wgCloser{ return &wgCloser{
wg: &wg, wg: &wg,
dir: fifos.Dir, dir: fifos.Dir,
set: set,
cancel: func() {
for _, l := range set {
l.Close()
}
},
}, nil }, nil
} }

View File

@ -8,6 +8,13 @@ import (
"github.com/containerd/containerd/sys" "github.com/containerd/containerd/sys"
) )
const (
// DefaultAddress is the default unix socket address
DefaultAddress = "/run/containerd/containerd.sock"
// DefaultDebuggAddress is the default unix socket address for pprof data
DefaultDebugAddress = "/run/containerd/debug.sock"
)
// apply sets config settings on the server process // apply sets config settings on the server process
func apply(ctx context.Context, config *Config) error { func apply(ctx context.Context, config *Config) error {
if config.Subreaper { if config.Subreaper {

View File

@ -1,9 +1,16 @@
// +build !linux // +build !linux,!windows
package server package server
import "context" import "context"
const (
// DefaultAddress is the default unix socket address
DefaultAddress = "/run/containerd/containerd.sock"
// DefaultDebuggAddress is the default unix socket address for pprof data
DefaultDebugAddress = "/run/containerd/debug.sock"
)
func apply(_ context.Context, _ *Config) error { func apply(_ context.Context, _ *Config) error {
return nil return nil
} }

16
server/server_windows.go Normal file
View File

@ -0,0 +1,16 @@
// +build windows
package server
import "context"
const (
// DefaultAddress is the default winpipe address
DefaultAddress = `\\.\pipe\containerd-containerd`
// DefaultDebugAddress is the default winpipe address for pprof data
DefaultDebugAddress = `\\.\pipe\containerd-debug`
)
func apply(_ context.Context, _ *Config) error {
return nil
}

View File

@ -12,18 +12,23 @@ import (
specs "github.com/opencontainers/runtime-spec/specs-go" specs "github.com/opencontainers/runtime-spec/specs-go"
) )
const pipeRoot = `\\.\pipe`
func createDefaultSpec() (*specs.Spec, error) { func createDefaultSpec() (*specs.Spec, error) {
return &specs.Spec{ return &specs.Spec{
Version: specs.Version, Version: specs.Version,
Root: &specs.Root{}, Root: &specs.Root{},
Process: &specs.Process{ Process: &specs.Process{
Cwd: `C:\`,
ConsoleSize: &specs.Box{ ConsoleSize: &specs.Box{
Width: 80, Width: 80,
Height: 20, Height: 20,
}, },
}, },
Windows: &specs.Windows{
IgnoreFlushesDuringBoot: true,
Network: &specs.WindowsNetwork{
AllowUnqualifiedDNSQuery: true,
},
},
}, nil }, nil
} }

View File

@ -204,7 +204,7 @@ func (t *task) Exec(ctx context.Context, id string, spec *specs.Process, ioCreat
if id == "" { if id == "" {
return nil, errors.Wrapf(errdefs.ErrInvalidArgument, "exec id must not be empty") return nil, errors.Wrapf(errdefs.ErrInvalidArgument, "exec id must not be empty")
} }
i, err := ioCreate() i, err := ioCreate(id)
if err != nil { if err != nil {
return nil, err return nil, err
} }