Move runtime and Mounts to container config

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2016-10-28 15:51:31 -07:00
parent 3551d4c0b9
commit 788e19d251
6 changed files with 56 additions and 72 deletions

View File

@ -13,24 +13,17 @@ import (
type ContainerConfig interface {
ID() string
Root() string
Spec(*Mount) (*specs.Spec, error)
Spec() (*specs.Spec, error)
Runtime() (Runtime, error)
}
type GraphDriver interface {
Mount(id string) (*Mount, error)
}
func NewContainer(config ContainerConfig, graph GraphDriver, exec ExecutionDriver) (*Container, error) {
func NewContainer(config ContainerConfig) (*Container, error) {
var (
id = config.ID()
root = config.Root()
path = filepath.Join(root, id)
)
mount, err := graph.Mount(id)
if err != nil {
return nil, err
}
s, err := config.Spec(mount)
s, err := config.Spec()
if err != nil {
return nil, err
}
@ -48,15 +41,19 @@ func NewContainer(config ContainerConfig, graph GraphDriver, exec ExecutionDrive
if err != nil {
return nil, err
}
r, err := config.Runtime()
if err != nil {
return nil, err
}
return &Container{
id: id,
path: path,
s: s,
driver: exec,
driver: r,
}, nil
}
func LoadContainer(config ContainerConfig, exec ExecutionDriver) (*Container, error) {
func LoadContainer(config ContainerConfig) (*Container, error) {
var (
id = config.ID()
root = config.Root()
@ -66,7 +63,11 @@ func LoadContainer(config ContainerConfig, exec ExecutionDriver) (*Container, er
if err != nil {
return nil, err
}
process, err := exec.Load(id)
r, err := config.Runtime()
if err != nil {
return nil, err
}
process, err := r.Load(id)
if err != nil {
return nil, err
}
@ -75,10 +76,10 @@ func LoadContainer(config ContainerConfig, exec ExecutionDriver) (*Container, er
id: id,
path: path,
s: spec,
driver: exec,
driver: r,
init: &Process{
d: process,
driver: exec,
driver: r,
},
}, nil
}
@ -103,7 +104,7 @@ type Container struct {
path string
s *specs.Spec
driver ExecutionDriver
driver Runtime
Stdin io.Reader
Stdout io.Writer

View File

@ -6,23 +6,16 @@ import (
"io"
"os"
"strconv"
"time"
"github.com/Sirupsen/logrus"
"github.com/docker/containerd/shim"
"github.com/docker/containerkit"
"github.com/docker/containerkit/osutils"
specs "github.com/opencontainers/runtime-spec/specs-go"
)
func reloadContainer() error {
// create a new runtime runtime that implements the ExecutionDriver interface
runtime, err := shim.Load("/run/cshim/test")
if err != nil {
return err
}
dockerContainer := &testConfig{}
container, err := containerkit.LoadContainer(dockerContainer, runtime)
container, err := containerkit.LoadContainer(dockerContainer)
if err != nil {
return err
}
@ -44,20 +37,10 @@ func reloadContainer() error {
func runContainer() error {
// create a new runtime runtime that implements the ExecutionDriver interface
runtime, err := shim.New(shim.Opts{
Root: "/run/cshim/test",
Name: "containerd-shim",
RuntimeName: "runc",
RuntimeRoot: "/run/runc",
Timeout: 5 * time.Second,
})
if err != nil {
return err
}
dockerContainer := &testConfig{}
// create a new container
container, err := containerkit.NewContainer(dockerContainer, NewBindDriver(), runtime)
container, err := containerkit.NewContainer(dockerContainer)
if err != nil {
return err
}

View File

@ -5,35 +5,15 @@ import (
"path/filepath"
"runtime"
"syscall"
"time"
"golang.org/x/sys/unix"
"github.com/docker/containerd/shim"
"github.com/docker/containerkit"
specs "github.com/opencontainers/runtime-spec/specs-go"
)
func NewBindDriver() containerkit.GraphDriver {
return &bindDriver{}
}
// this demos how the graph/layer subsystem will create the rootfs and
// provide it to the container, the Mount type ties the execution and
// filesystem layers together
type bindDriver struct {
}
func (b *bindDriver) Mount(id string) (*containerkit.Mount, error) {
return &containerkit.Mount{
Target: "/",
Type: "bind",
Source: "/containers/redis/rootfs",
Options: []string{
"rbind",
"rw",
},
}, nil
}
var (
RWM = "rwm"
caps = []string{
@ -66,8 +46,32 @@ func (t *testConfig) Root() string {
return "/var/lib/containerkit"
}
func (t *testConfig) Spec(m *containerkit.Mount) (*specs.Spec, error) {
cgpath := filepath.Join("/containerkit", t.ID())
func (t *testConfig) Runtime() (containerkit.Runtime, error) {
return shim.New(shim.Opts{
Root: "/run/cshim/test",
Name: "containerd-shim",
RuntimeName: "runc",
RuntimeRoot: "/run/runc",
Timeout: 5 * time.Second,
})
// TODO: support loading of runtime
// create a new runtime runtime that implements the ExecutionDriver interface
return shim.Load("/run/cshim/test")
}
func (t *testConfig) Spec() (*specs.Spec, error) {
var (
cgpath = filepath.Join("/containerkit", t.ID())
m = &containerkit.Mount{
Target: "/",
Type: "bind",
Source: "/containers/redis/rootfs",
Options: []string{
"rbind",
"rw",
},
}
)
return &specs.Spec{
Version: specs.Version,
Platform: specs.Platform{
@ -156,15 +160,15 @@ func (t *testConfig) Spec(m *containerkit.Mount) (*specs.Spec, error) {
},
Linux: &specs.Linux{
CgroupsPath: &cgpath,
Resources: &specs.Resources{
Devices: []specs.DeviceCgroup{
Resources: &specs.LinuxResources{
Devices: []specs.LinuxDeviceCgroup{
{
Allow: false,
Access: &RWM,
},
},
},
Namespaces: []specs.Namespace{
Namespaces: []specs.LinuxNamespace{
{
Type: "pid",
},

View File

@ -2,11 +2,9 @@ package containerkit
import "errors"
var (
ErrProcessSet = errors.New("containerkit: container process is already set")
)
var ErrProcessSet = errors.New("containerkit: container process is already set")
type ExecutionDriver interface {
type Runtime interface {
Create(*Container) (ProcessDelegate, error)
Start(*Container) error
Delete(*Container) error

View File

@ -8,9 +8,7 @@ import (
specs "github.com/opencontainers/runtime-spec/specs-go"
)
var (
ErrNotExecProcess = errors.New("containerkit: process not an exec process")
)
var ErrNotExecProcess = errors.New("containerkit: process not an exec process")
type ProcessDelegate interface {
Pid() int
@ -26,7 +24,7 @@ type Process struct {
exec bool
s *specs.Process
driver ExecutionDriver
driver Runtime
c *Container
d ProcessDelegate
}

View File

@ -420,7 +420,7 @@ func getRootIDs(s *specs.Spec) (int, int, error) {
return uid, gid, nil
}
func hostIDFromMap(id uint32, mp []specs.IDMapping) int {
func hostIDFromMap(id uint32, mp []specs.LinuxIDMapping) int {
for _, m := range mp {
if (id >= m.ContainerID) && (id <= (m.ContainerID + m.Size - 1)) {
return int(m.HostID + (id - m.ContainerID))