Merge pull request #122 from jhowardmsft/closertoctrcompiling

Getting ctr closer to compiling on Windows
This commit is contained in:
Michael Crosby 2016-02-29 14:20:08 -08:00
commit 70a8c1ec3f
18 changed files with 115 additions and 83 deletions

View file

@ -11,6 +11,7 @@ import (
"github.com/docker/containerd/api/grpc/types" "github.com/docker/containerd/api/grpc/types"
"github.com/docker/containerd/runtime" "github.com/docker/containerd/runtime"
"github.com/docker/containerd/specs"
"github.com/docker/containerd/supervisor" "github.com/docker/containerd/supervisor"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
@ -66,7 +67,7 @@ func (s *apiServer) Signal(ctx context.Context, r *types.SignalRequest) (*types.
} }
func (s *apiServer) AddProcess(ctx context.Context, r *types.AddProcessRequest) (*types.AddProcessResponse, error) { func (s *apiServer) AddProcess(ctx context.Context, r *types.AddProcessRequest) (*types.AddProcessResponse, error) {
process := &runtime.ProcessSpec{ process := &specs.ProcessSpec{
Terminal: r.Terminal, Terminal: r.Terminal,
Args: r.Args, Args: r.Args,
Env: r.Env, Env: r.Env,

View file

@ -10,11 +10,12 @@ import (
"github.com/docker/containerd/api/grpc/types" "github.com/docker/containerd/api/grpc/types"
"github.com/docker/containerd/runtime" "github.com/docker/containerd/runtime"
"github.com/docker/containerd/specs"
"github.com/docker/containerd/supervisor" "github.com/docker/containerd/supervisor"
"github.com/opencontainers/runc/libcontainer" "github.com/opencontainers/runc/libcontainer"
"github.com/opencontainers/runc/libcontainer/cgroups" "github.com/opencontainers/runc/libcontainer/cgroups"
"github.com/opencontainers/runc/libcontainer/system" "github.com/opencontainers/runc/libcontainer/system"
"github.com/opencontainers/specs" ocs "github.com/opencontainers/specs"
"golang.org/x/net/context" "golang.org/x/net/context"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
@ -241,7 +242,7 @@ func (s *apiServer) Stats(ctx context.Context, r *types.StatsRequest) (*types.St
return t, nil return t, nil
} }
func setUserFieldsInProcess(p *types.Process, oldProc runtime.ProcessSpec) { func setUserFieldsInProcess(p *types.Process, oldProc specs.ProcessSpec) {
p.User = &types.User{ p.User = &types.User{
Uid: oldProc.User.UID, Uid: oldProc.User.UID,
Gid: oldProc.User.GID, Gid: oldProc.User.GID,
@ -249,8 +250,8 @@ func setUserFieldsInProcess(p *types.Process, oldProc runtime.ProcessSpec) {
} }
} }
func setPlatformRuntimeProcessSpecUserFields(r *types.User, process *runtime.ProcessSpec) { func setPlatformRuntimeProcessSpecUserFields(r *types.User, process *specs.ProcessSpec) {
process.User = specs.User{ process.User = ocs.User{
UID: r.Uid, UID: r.Uid,
GID: r.Gid, GID: r.Gid,
AdditionalGids: r.AdditionalGids, AdditionalGids: r.AdditionalGids,

View file

@ -4,7 +4,7 @@ import (
"errors" "errors"
"github.com/docker/containerd/api/grpc/types" "github.com/docker/containerd/api/grpc/types"
"github.com/docker/containerd/runtime" "github.com/docker/containerd/specs"
"github.com/docker/containerd/supervisor" "github.com/docker/containerd/supervisor"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
@ -32,8 +32,8 @@ func (s *apiServer) Stats(ctx context.Context, r *types.StatsRequest) (*types.St
return nil, errors.New("Stats() not supported on Windows") return nil, errors.New("Stats() not supported on Windows")
} }
func setUserFieldsInProcess(p *types.Process, oldProc runtime.ProcessSpec) { func setUserFieldsInProcess(p *types.Process, oldProc specs.ProcessSpec) {
} }
func setPlatformRuntimeProcessSpecUserFields(r *types.User, process *runtime.ProcessSpec) { func setPlatformRuntimeProcessSpecUserFields(r *types.User, process *specs.ProcessSpec) {
} }

View file

@ -17,8 +17,8 @@ import (
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"github.com/docker/containerd/api/grpc/types" "github.com/docker/containerd/api/grpc/types"
"github.com/docker/containerd/specs"
"github.com/docker/docker/pkg/term" "github.com/docker/docker/pkg/term"
"github.com/opencontainers/specs"
netcontext "golang.org/x/net/context" netcontext "golang.org/x/net/context"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/grpclog" "google.golang.org/grpc/grpclog"
@ -538,23 +538,3 @@ type stdio struct {
stdout string stdout string
stderr string stderr string
} }
func createStdio() (s stdio, err error) {
tmp, err := ioutil.TempDir("", "ctr-")
if err != nil {
return s, err
}
// create fifo's for the process
for name, fd := range map[string]*string{
"stdin": &s.stdin,
"stdout": &s.stdout,
"stderr": &s.stderr,
} {
path := filepath.Join(tmp, name)
if err := syscall.Mkfifo(path, 0755); err != nil && !os.IsExist(err) {
return s, err
}
*fd = path
}
return s, nil
}

30
ctr/container_unix.go Normal file
View file

@ -0,0 +1,30 @@
// +build !windows
package main
import (
"io/ioutil"
"os"
"path/filepath"
"syscall"
)
func createStdio() (s stdio, err error) {
tmp, err := ioutil.TempDir("", "ctr-")
if err != nil {
return s, err
}
// create fifo's for the process
for name, fd := range map[string]*string{
"stdin": &s.stdin,
"stdout": &s.stdout,
"stderr": &s.stderr,
} {
path := filepath.Join(tmp, name)
if err := syscall.Mkfifo(path, 0755); err != nil && !os.IsExist(err) {
return s, err
}
*fd = path
}
return s, nil
}

6
ctr/container_windows.go Normal file
View file

@ -0,0 +1,6 @@
package main
// TODO Windows: This will have a very different implementation
func createStdio() (s stdio, err error) {
return stdio{}, nil
}

View file

@ -5,9 +5,9 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"syscall"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"github.com/docker/containerd/specs"
) )
type Container interface { type Container interface {
@ -18,7 +18,7 @@ type Container interface {
// Start starts the init process of the container // Start starts the init process of the container
Start(checkpoint string, s Stdio) (Process, error) Start(checkpoint string, s Stdio) (Process, error)
// Exec starts another process in an existing container // Exec starts another process in an existing container
Exec(string, ProcessSpec, Stdio) (Process, error) Exec(string, specs.ProcessSpec, Stdio) (Process, error)
// Delete removes the container's state and any resources // Delete removes the container's state and any resources
Delete() error Delete() error
// Processes returns all the containers processes that have been added // Processes returns all the containers processes that have been added
@ -175,8 +175,8 @@ func (c *container) Labels() []string {
return c.labels return c.labels
} }
func (c *container) readSpec() (*PlatformSpec, error) { func (c *container) readSpec() (*specs.PlatformSpec, error) {
var spec PlatformSpec var spec specs.PlatformSpec
f, err := os.Open(filepath.Join(c.bundle, "config.json")) f, err := os.Open(filepath.Join(c.bundle, "config.json"))
if err != nil { if err != nil {
return nil, err return nil, err
@ -188,18 +188,6 @@ func (c *container) readSpec() (*PlatformSpec, error) {
return &spec, nil return &spec, nil
} }
func (c *container) State() State {
proc := c.processes["init"]
if proc == nil || proc.pid == 0 {
return Stopped
}
err := syscall.Kill(proc.pid, 0)
if err != nil && err == syscall.ESRCH {
return Stopped
}
return Running
}
func (c *container) Delete() error { func (c *container) Delete() error {
return os.RemoveAll(filepath.Join(c.root, c.id)) return os.RemoveAll(filepath.Join(c.root, c.id))
} }

View file

@ -9,17 +9,18 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/docker/containerd/specs"
"github.com/opencontainers/runc/libcontainer" "github.com/opencontainers/runc/libcontainer"
"github.com/opencontainers/specs" ocs "github.com/opencontainers/specs"
) )
func getRootIDs(s *PlatformSpec) (int, int, error) { func getRootIDs(s *specs.PlatformSpec) (int, int, error) {
if s == nil { if s == nil {
return 0, 0, nil return 0, 0, nil
} }
var hasUserns bool var hasUserns bool
for _, ns := range s.Linux.Namespaces { for _, ns := range s.Linux.Namespaces {
if ns.Type == specs.UserNamespace { if ns.Type == ocs.UserNamespace {
hasUserns = true hasUserns = true
break break
} }
@ -32,6 +33,18 @@ func getRootIDs(s *PlatformSpec) (int, int, error) {
return uid, gid, nil return uid, gid, nil
} }
func (c *container) State() State {
proc := c.processes["init"]
if proc == nil || proc.pid == 0 {
return Stopped
}
err := syscall.Kill(proc.pid, 0)
if err != nil && err == syscall.ESRCH {
return Stopped
}
return Running
}
func (c *container) Runtime() string { func (c *container) Runtime() string {
return c.runtime return c.runtime
} }
@ -136,7 +149,7 @@ func (c *container) Start(checkpoint string, s Stdio) (Process, error) {
c: c, c: c,
stdio: s, stdio: s,
spec: spec, spec: spec,
processSpec: ProcessSpec(spec.Process), processSpec: specs.ProcessSpec(spec.Process),
} }
p, err := newProcess(config) p, err := newProcess(config)
if err != nil { if err != nil {
@ -152,7 +165,7 @@ func (c *container) Start(checkpoint string, s Stdio) (Process, error) {
return p, nil return p, nil
} }
func (c *container) Exec(pid string, spec ProcessSpec, s Stdio) (Process, error) { func (c *container) Exec(pid string, spec specs.ProcessSpec, s Stdio) (Process, error) {
processRoot := filepath.Join(c.root, c.id, pid) processRoot := filepath.Join(c.root, c.id, pid)
if err := os.Mkdir(processRoot, 0755); err != nil { if err := os.Mkdir(processRoot, 0755); err != nil {
return nil, err return nil, err
@ -187,14 +200,14 @@ func (c *container) Exec(pid string, spec ProcessSpec, s Stdio) (Process, error)
} }
func (c *container) getLibctContainer() (libcontainer.Container, error) { func (c *container) getLibctContainer() (libcontainer.Container, error) {
f, err := libcontainer.New(specs.LinuxStateDirectory, libcontainer.Cgroupfs) f, err := libcontainer.New(ocs.LinuxStateDirectory, libcontainer.Cgroupfs)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return f.Load(c.id) return f.Load(c.id)
} }
func hostIDFromMap(id uint32, mp []specs.IDMapping) int { func hostIDFromMap(id uint32, mp []ocs.IDMapping) int {
for _, m := range mp { for _, m := range mp {
if (id >= m.ContainerID) && (id <= (m.ContainerID + m.Size - 1)) { if (id >= m.ContainerID) && (id <= (m.ContainerID + m.Size - 1)) {
return int(m.HostID + (id - m.ContainerID)) return int(m.HostID + (id - m.ContainerID))

View file

@ -1,11 +1,20 @@
package runtime package runtime
import "errors" import (
"errors"
func getRootIDs(s *PlatformSpec) (int, int, error) { "github.com/docker/containerd/specs"
)
func getRootIDs(s *specs.PlatformSpec) (int, int, error) {
return 0, 0, nil return 0, 0, nil
} }
// TODO Windows: This will have a different implementation
func (c *container) State() State {
return Running // HACK HACK HACK
}
func (c *container) Runtime() string { func (c *container) Runtime() string {
return "windows" return "windows"
} }
@ -38,7 +47,7 @@ func (c *container) Start(checkpoint string, s Stdio) (Process, error) {
// TODO Windows: Implement me. // TODO Windows: Implement me.
// This will have a very different implementation on Windows. // This will have a very different implementation on Windows.
func (c *container) Exec(pid string, spec ProcessSpec, s Stdio) (Process, error) { func (c *container) Exec(pid string, spec specs.ProcessSpec, s Stdio) (Process, error) {
return nil, errors.New("Exec not yet implemented on Windows") return nil, errors.New("Exec not yet implemented on Windows")
} }

View file

@ -8,6 +8,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"github.com/docker/containerd/specs"
) )
type Process interface { type Process interface {
@ -25,7 +27,7 @@ type Process interface {
// has not exited // has not exited
ExitStatus() (int, error) ExitStatus() (int, error)
// Spec returns the process spec that created the process // Spec returns the process spec that created the process
Spec() ProcessSpec Spec() specs.ProcessSpec
// Signal sends the provided signal to the process // Signal sends the provided signal to the process
Signal(os.Signal) error Signal(os.Signal) error
// Container returns the container that the process belongs to // Container returns the container that the process belongs to
@ -39,8 +41,8 @@ type Process interface {
type processConfig struct { type processConfig struct {
id string id string
root string root string
processSpec ProcessSpec processSpec specs.ProcessSpec
spec *PlatformSpec spec *specs.PlatformSpec
c *container c *container
stdio Stdio stdio Stdio
exec bool exec bool
@ -118,7 +120,7 @@ type process struct {
exitPipe *os.File exitPipe *os.File
controlPipe *os.File controlPipe *os.File
container *container container *container
spec ProcessSpec spec specs.ProcessSpec
stdio Stdio stdio Stdio
} }
@ -163,7 +165,7 @@ func (p *process) ExitStatus() (int, error) {
return strconv.Atoi(string(data)) return strconv.Atoi(string(data))
} }
func (p *process) Spec() ProcessSpec { func (p *process) Spec() specs.ProcessSpec {
return p.spec return p.spec
} }

View file

@ -3,6 +3,8 @@ package runtime
import ( import (
"errors" "errors"
"time" "time"
"github.com/docker/containerd/specs"
) )
var ( var (
@ -46,7 +48,7 @@ type state struct {
} }
type ProcessState struct { type ProcessState struct {
ProcessSpec specs.ProcessSpec
Exec bool `json:"exec"` Exec bool `json:"exec"`
Stdin string `json:"containerdStdin"` Stdin string `json:"containerdStdin"`
Stdout string `json:"containerdStdout"` Stdout string `json:"containerdStdout"`

View file

@ -1,8 +0,0 @@
package runtime
import "github.com/opencontainers/specs"
type (
PlatformSpec specs.LinuxSpec
ProcessSpec specs.Process
)

View file

@ -1,8 +0,0 @@
package runtime
// Temporary Windows version of the spec in lieu of opencontainers/specs having
// Windows support currently.
import "github.com/docker/containerd/specs"
type PlatformSpec specs.WindowsSpec
type ProcessSpec specs.Process

9
specs/spec_linux.go Normal file
View file

@ -0,0 +1,9 @@
package specs
import ocs "github.com/opencontainers/specs"
type (
PlatformSpec ocs.LinuxSpec
ProcessSpec ocs.Process
Spec ocs.Spec
)

View file

@ -1,5 +1,13 @@
package specs package specs
// Temporary Windows version of the spec in lieu of opencontainers/specs having
// Windows support currently.
type (
PlatformSpec WindowsSpec
ProcessSpec Process
)
// This is a temporary module in lieu of opencontainers/specs being compatible // This is a temporary module in lieu of opencontainers/specs being compatible
// currently on Windows. // currently on Windows.

View file

@ -1,3 +0,0 @@
// +build !windows
package specs

View file

@ -4,6 +4,7 @@ import (
"time" "time"
"github.com/docker/containerd/runtime" "github.com/docker/containerd/runtime"
"github.com/docker/containerd/specs"
) )
type AddProcessTask struct { type AddProcessTask struct {
@ -13,7 +14,7 @@ type AddProcessTask struct {
Stdout string Stdout string
Stderr string Stderr string
Stdin string Stdin string
ProcessSpec *runtime.ProcessSpec ProcessSpec *specs.ProcessSpec
StartResponse chan StartResponse StartResponse chan StartResponse
} }

View file

@ -6,6 +6,7 @@ import (
"testing" "testing"
"github.com/docker/containerd/runtime" "github.com/docker/containerd/runtime"
"github.com/docker/containerd/specs"
) )
type testProcess struct { type testProcess struct {
@ -44,8 +45,8 @@ func (p *testProcess) Container() runtime.Container {
return nil return nil
} }
func (p *testProcess) Spec() runtime.ProcessSpec { func (p *testProcess) Spec() specs.ProcessSpec {
return runtime.ProcessSpec{} return specs.ProcessSpec{}
} }
func (p *testProcess) Signal(os.Signal) error { func (p *testProcess) Signal(os.Signal) error {