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
19 changed files with 164 additions and 201 deletions

View File

@@ -1,7 +1,6 @@
package main
import (
"github.com/containerd/containerd"
"github.com/containerd/containerd/server"
)
@@ -9,7 +8,7 @@ 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",

View File

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

View File

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

View File

@@ -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"
)
@@ -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{
@@ -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,

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

View File

@@ -2,22 +2,16 @@ 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"
)
@@ -25,126 +19,20 @@ 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 {
@@ -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 {
@@ -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),
)
}