Move runtime and Mounts to container config
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
parent
3551d4c0b9
commit
788e19d251
6 changed files with 56 additions and 72 deletions
35
container.go
35
container.go
|
@ -13,24 +13,17 @@ import (
|
||||||
type ContainerConfig interface {
|
type ContainerConfig interface {
|
||||||
ID() string
|
ID() string
|
||||||
Root() string
|
Root() string
|
||||||
Spec(*Mount) (*specs.Spec, error)
|
Spec() (*specs.Spec, error)
|
||||||
|
Runtime() (Runtime, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type GraphDriver interface {
|
func NewContainer(config ContainerConfig) (*Container, error) {
|
||||||
Mount(id string) (*Mount, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewContainer(config ContainerConfig, graph GraphDriver, exec ExecutionDriver) (*Container, error) {
|
|
||||||
var (
|
var (
|
||||||
id = config.ID()
|
id = config.ID()
|
||||||
root = config.Root()
|
root = config.Root()
|
||||||
path = filepath.Join(root, id)
|
path = filepath.Join(root, id)
|
||||||
)
|
)
|
||||||
mount, err := graph.Mount(id)
|
s, err := config.Spec()
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
s, err := config.Spec(mount)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -48,15 +41,19 @@ func NewContainer(config ContainerConfig, graph GraphDriver, exec ExecutionDrive
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
r, err := config.Runtime()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return &Container{
|
return &Container{
|
||||||
id: id,
|
id: id,
|
||||||
path: path,
|
path: path,
|
||||||
s: s,
|
s: s,
|
||||||
driver: exec,
|
driver: r,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadContainer(config ContainerConfig, exec ExecutionDriver) (*Container, error) {
|
func LoadContainer(config ContainerConfig) (*Container, error) {
|
||||||
var (
|
var (
|
||||||
id = config.ID()
|
id = config.ID()
|
||||||
root = config.Root()
|
root = config.Root()
|
||||||
|
@ -66,7 +63,11 @@ func LoadContainer(config ContainerConfig, exec ExecutionDriver) (*Container, er
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -75,10 +76,10 @@ func LoadContainer(config ContainerConfig, exec ExecutionDriver) (*Container, er
|
||||||
id: id,
|
id: id,
|
||||||
path: path,
|
path: path,
|
||||||
s: spec,
|
s: spec,
|
||||||
driver: exec,
|
driver: r,
|
||||||
init: &Process{
|
init: &Process{
|
||||||
d: process,
|
d: process,
|
||||||
driver: exec,
|
driver: r,
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
@ -103,7 +104,7 @@ type Container struct {
|
||||||
path string
|
path string
|
||||||
s *specs.Spec
|
s *specs.Spec
|
||||||
|
|
||||||
driver ExecutionDriver
|
driver Runtime
|
||||||
|
|
||||||
Stdin io.Reader
|
Stdin io.Reader
|
||||||
Stdout io.Writer
|
Stdout io.Writer
|
||||||
|
|
|
@ -6,23 +6,16 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/docker/containerd/shim"
|
|
||||||
"github.com/docker/containerkit"
|
"github.com/docker/containerkit"
|
||||||
"github.com/docker/containerkit/osutils"
|
"github.com/docker/containerkit/osutils"
|
||||||
specs "github.com/opencontainers/runtime-spec/specs-go"
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
func reloadContainer() error {
|
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{}
|
dockerContainer := &testConfig{}
|
||||||
container, err := containerkit.LoadContainer(dockerContainer, runtime)
|
container, err := containerkit.LoadContainer(dockerContainer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -44,20 +37,10 @@ func reloadContainer() error {
|
||||||
|
|
||||||
func runContainer() error {
|
func runContainer() error {
|
||||||
// create a new runtime runtime that implements the ExecutionDriver interface
|
// 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{}
|
dockerContainer := &testConfig{}
|
||||||
|
|
||||||
// create a new container
|
// create a new container
|
||||||
container, err := containerkit.NewContainer(dockerContainer, NewBindDriver(), runtime)
|
container, err := containerkit.NewContainer(dockerContainer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,35 +5,15 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
|
"github.com/docker/containerd/shim"
|
||||||
"github.com/docker/containerkit"
|
"github.com/docker/containerkit"
|
||||||
specs "github.com/opencontainers/runtime-spec/specs-go"
|
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 (
|
var (
|
||||||
RWM = "rwm"
|
RWM = "rwm"
|
||||||
caps = []string{
|
caps = []string{
|
||||||
|
@ -66,8 +46,32 @@ func (t *testConfig) Root() string {
|
||||||
return "/var/lib/containerkit"
|
return "/var/lib/containerkit"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *testConfig) Spec(m *containerkit.Mount) (*specs.Spec, error) {
|
func (t *testConfig) Runtime() (containerkit.Runtime, error) {
|
||||||
cgpath := filepath.Join("/containerkit", t.ID())
|
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{
|
return &specs.Spec{
|
||||||
Version: specs.Version,
|
Version: specs.Version,
|
||||||
Platform: specs.Platform{
|
Platform: specs.Platform{
|
||||||
|
@ -156,15 +160,15 @@ func (t *testConfig) Spec(m *containerkit.Mount) (*specs.Spec, error) {
|
||||||
},
|
},
|
||||||
Linux: &specs.Linux{
|
Linux: &specs.Linux{
|
||||||
CgroupsPath: &cgpath,
|
CgroupsPath: &cgpath,
|
||||||
Resources: &specs.Resources{
|
Resources: &specs.LinuxResources{
|
||||||
Devices: []specs.DeviceCgroup{
|
Devices: []specs.LinuxDeviceCgroup{
|
||||||
{
|
{
|
||||||
Allow: false,
|
Allow: false,
|
||||||
Access: &RWM,
|
Access: &RWM,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Namespaces: []specs.Namespace{
|
Namespaces: []specs.LinuxNamespace{
|
||||||
{
|
{
|
||||||
Type: "pid",
|
Type: "pid",
|
||||||
},
|
},
|
||||||
|
|
|
@ -2,11 +2,9 @@ package containerkit
|
||||||
|
|
||||||
import "errors"
|
import "errors"
|
||||||
|
|
||||||
var (
|
var ErrProcessSet = errors.New("containerkit: container process is already set")
|
||||||
ErrProcessSet = errors.New("containerkit: container process is already set")
|
|
||||||
)
|
|
||||||
|
|
||||||
type ExecutionDriver interface {
|
type Runtime interface {
|
||||||
Create(*Container) (ProcessDelegate, error)
|
Create(*Container) (ProcessDelegate, error)
|
||||||
Start(*Container) error
|
Start(*Container) error
|
||||||
Delete(*Container) error
|
Delete(*Container) error
|
||||||
|
|
|
@ -8,9 +8,7 @@ import (
|
||||||
specs "github.com/opencontainers/runtime-spec/specs-go"
|
specs "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var ErrNotExecProcess = errors.New("containerkit: process not an exec process")
|
||||||
ErrNotExecProcess = errors.New("containerkit: process not an exec process")
|
|
||||||
)
|
|
||||||
|
|
||||||
type ProcessDelegate interface {
|
type ProcessDelegate interface {
|
||||||
Pid() int
|
Pid() int
|
||||||
|
@ -26,7 +24,7 @@ type Process struct {
|
||||||
exec bool
|
exec bool
|
||||||
s *specs.Process
|
s *specs.Process
|
||||||
|
|
||||||
driver ExecutionDriver
|
driver Runtime
|
||||||
c *Container
|
c *Container
|
||||||
d ProcessDelegate
|
d ProcessDelegate
|
||||||
}
|
}
|
||||||
|
|
|
@ -420,7 +420,7 @@ func getRootIDs(s *specs.Spec) (int, int, error) {
|
||||||
return uid, gid, nil
|
return uid, gid, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func hostIDFromMap(id uint32, mp []specs.IDMapping) int {
|
func hostIDFromMap(id uint32, mp []specs.LinuxIDMapping) 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))
|
||||||
|
|
Loading…
Reference in a new issue