Fix usage of oci in other packages.

Signed-off-by: Daniel Nephin <dnephin@gmail.com>
This commit is contained in:
Daniel Nephin 2017-11-22 15:45:47 -05:00
parent 081f8c7ce0
commit cdf62f69a1
13 changed files with 80 additions and 68 deletions

View File

@ -78,7 +78,7 @@ containerd fully supports the OCI runtime specification for running containers.
You can specify options when creating a container about how to modify the specification.
```go
redis, err := client.NewContainer(context, "redis-master", containerd.WithNewSpec(containerd.WithImageConfig(image)))
redis, err := client.NewContainer(context, "redis-master", containerd.WithNewSpec(oci.WithImageConfig(image)))
```
### Root Filesystems
@ -92,7 +92,7 @@ image, err := client.Pull(context, "docker.io/library/redis:latest", containerd.
// allocate a new RW root filesystem for a container based on the image
redis, err := client.NewContainer(context, "redis-master",
containerd.WithNewSnapshot("redis-rootfs", image),
containerd.WithNewSpec(containerd.WithImageConfig(image)),
containerd.WithNewSpec(oci.WithImageConfig(image)),
)
@ -101,7 +101,7 @@ for i := 0; i < 10; i++ {
id := fmt.Sprintf("id-%s", i)
container, err := client.NewContainer(ctx, id,
containerd.WithNewSnapshotView(id, image),
containerd.WithNewSpec(containerd.WithImageConfig(image)),
containerd.WithNewSpec(oci.WithImageConfig(image)),
)
}
```

View File

@ -5,6 +5,7 @@ import (
"testing"
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/oci"
)
func BenchmarkContainerCreate(b *testing.B) {
@ -22,7 +23,7 @@ func BenchmarkContainerCreate(b *testing.B) {
b.Error(err)
return
}
spec, err := GenerateSpec(ctx, client, &containers.Container{ID: b.Name()}, WithImageConfig(image), withTrue())
spec, err := oci.GenerateSpec(ctx, client, &containers.Container{ID: b.Name()}, oci.WithImageConfig(image), withTrue())
if err != nil {
b.Error(err)
return
@ -65,7 +66,7 @@ func BenchmarkContainerStart(b *testing.B) {
b.Error(err)
return
}
spec, err := GenerateSpec(ctx, client, &containers.Container{ID: b.Name()}, WithImageConfig(image), withTrue())
spec, err := oci.GenerateSpec(ctx, client, &containers.Container{ID: b.Name()}, oci.WithImageConfig(image), withTrue())
if err != nil {
b.Error(err)
return

View File

@ -16,6 +16,7 @@ import (
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/oci"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
@ -116,7 +117,10 @@ func test(c config) error {
logrus.Info("starting stress test run...")
for i := 0; i < c.Concurrency; i++ {
wg.Add(1)
spec, err := containerd.GenerateSpec(ctx, client, &containers.Container{ID: ""}, containerd.WithImageConfig(image), containerd.WithProcessArgs("true"))
spec, err := oci.GenerateSpec(ctx, client,
&containers.Container{},
oci.WithImageConfig(image),
oci.WithProcessArgs("true"))
if err != nil {
return err
}

View File

@ -12,14 +12,15 @@ import (
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/cmd/ctr/commands/tasks"
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/oci"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
func withEnv(context *cli.Context) containerd.SpecOpts {
return func(_ gocontext.Context, _ *containerd.Client, _ *containers.Container, s *specs.Spec) error {
func withEnv(context *cli.Context) oci.SpecOpts {
return func(_ gocontext.Context, _ oci.Client, _ *containers.Container, s *specs.Spec) error {
env := context.StringSlice("env")
if len(env) > 0 {
s.Process.Env = replaceOrAppendEnvValues(s.Process.Env, env)
@ -28,8 +29,8 @@ func withEnv(context *cli.Context) containerd.SpecOpts {
}
}
func withMounts(context *cli.Context) containerd.SpecOpts {
return func(_ gocontext.Context, _ *containerd.Client, _ *containers.Container, s *specs.Spec) error {
func withMounts(context *cli.Context) oci.SpecOpts {
return func(_ gocontext.Context, _ oci.Client, _ *containers.Container, s *specs.Spec) error {
for _, mount := range context.StringSlice("mount") {
m, err := parseMountFlag(mount)
if err != nil {

View File

@ -7,6 +7,7 @@ import (
"github.com/containerd/containerd"
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/oci"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/urfave/cli"
)
@ -18,14 +19,6 @@ func init() {
})
}
func withTTY() containerd.SpecOpts {
return containerd.WithTTY
}
func setHostNetworking() containerd.SpecOpts {
return containerd.WithHostNamespace(specs.NetworkNamespace)
}
func newContainer(ctx gocontext.Context, client *containerd.Client, context *cli.Context) (containerd.Container, error) {
var (
ref = context.Args().First()
@ -42,18 +35,18 @@ func newContainer(ctx gocontext.Context, client *containerd.Client, context *cli
}
var (
opts []containerd.SpecOpts
opts []oci.SpecOpts
cOpts []containerd.NewContainerOpts
)
cOpts = append(cOpts, containerd.WithContainerLabels(commands.LabelArgs(context.StringSlice("label"))))
if context.Bool("rootfs") {
opts = append(opts, containerd.WithRootFSPath(ref))
opts = append(opts, oci.WithRootFSPath(ref))
} else {
image, err := client.GetImage(ctx, ref)
if err != nil {
return nil, err
}
opts = append(opts, containerd.WithImageConfig(image))
opts = append(opts, oci.WithImageConfig(image))
cOpts = append(cOpts, containerd.WithImage(image))
cOpts = append(cOpts, containerd.WithSnapshotter(context.String("snapshotter")))
// Even when "readonly" is set, we don't use KindView snapshot here. (#1495)
@ -62,22 +55,22 @@ func newContainer(ctx gocontext.Context, client *containerd.Client, context *cli
cOpts = append(cOpts, containerd.WithNewSnapshot(id, image))
}
if context.Bool("readonly") {
opts = append(opts, containerd.WithRootFSReadonly())
opts = append(opts, oci.WithRootFSReadonly())
}
cOpts = append(cOpts, containerd.WithRuntime(context.String("runtime"), nil))
opts = append(opts, withEnv(context), withMounts(context))
if len(args) > 0 {
opts = append(opts, containerd.WithProcessArgs(args...))
opts = append(opts, oci.WithProcessArgs(args...))
}
if cwd := context.String("cwd"); cwd != "" {
opts = append(opts, containerd.WithProcessCwd(cwd))
opts = append(opts, oci.WithProcessCwd(cwd))
}
if context.Bool("tty") {
opts = append(opts, withTTY())
opts = append(opts, oci.WithTTY)
}
if context.Bool("net-host") {
opts = append(opts, setHostNetworking(), containerd.WithHostHostsFile, containerd.WithHostResolvconf)
opts = append(opts, oci.WithHostNamespace(specs.NetworkNamespace), oci.WithHostHostsFile, oci.WithHostResolvconf)
}
cOpts = append([]containerd.NewContainerOpts{containerd.WithNewSpec(opts...)}, cOpts...)
return client.NewContainer(ctx, id, cOpts...)

View File

@ -8,6 +8,7 @@ import (
"github.com/containerd/containerd/cmd/ctr/commands"
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/oci"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
@ -21,8 +22,8 @@ func init() {
})
}
func withLayers(context *cli.Context) containerd.SpecOpts {
return func(ctx gocontext.Context, client *containerd.Client, c *containers.Container, s *specs.Spec) error {
func withLayers(context *cli.Context) oci.SpecOpts {
return func(ctx gocontext.Context, client oci.Client, c *containers.Container, s *specs.Spec) error {
l := context.StringSlice("layer")
if l == nil {
return errors.Wrap(errdefs.ErrInvalidArgument, "base layers must be specified with `--layer`")
@ -32,9 +33,9 @@ func withLayers(context *cli.Context) containerd.SpecOpts {
}
}
func withTTY(terminal bool) containerd.SpecOpts {
func withTTY(terminal bool) oci.SpecOpts {
if !terminal {
return func(ctx gocontext.Context, client *containerd.Client, c *containers.Container, s *specs.Spec) error {
return func(ctx gocontext.Context, client oci.Client, c *containers.Container, s *specs.Spec) error {
s.Process.Terminal = false
return nil
}
@ -45,7 +46,7 @@ func withTTY(terminal bool) containerd.SpecOpts {
if err != nil {
logrus.WithError(err).Error("console size")
}
return containerd.WithTTY(int(size.Width), int(size.Height))
return oci.WithTTY(int(size.Width), int(size.Height))
}
func newContainer(ctx gocontext.Context, client *containerd.Client, context *cli.Context) (containerd.Container, error) {
@ -61,18 +62,18 @@ func newContainer(ctx gocontext.Context, client *containerd.Client, context *cli
// TODO(mlaventure): get base image once we have a snapshotter
opts := []containerd.SpecOpts{
// TODO(mlaventure): use containerd.WithImageConfig once we have a snapshotter
opts := []oci.SpecOpts{
// TODO(mlaventure): use oci.WithImageConfig once we have a snapshotter
withLayers(context),
withEnv(context),
withMounts(context),
withTTY(tty),
}
if len(args) > 0 {
opts = append(opts, containerd.WithProcessArgs(args...))
opts = append(opts, oci.WithProcessArgs(args...))
}
if cwd := context.String("cwd"); cwd != "" {
opts = append(opts, containerd.WithProcessCwd(cwd))
opts = append(opts, oci.WithProcessCwd(cwd))
}
return client.NewContainer(ctx, id,
containerd.WithNewSpec(opts...),

View File

@ -5,6 +5,8 @@ package containerd
import (
"syscall"
"testing"
"github.com/containerd/containerd/oci"
)
func TestCheckpointRestore(t *testing.T) {
@ -28,7 +30,7 @@ func TestCheckpointRestore(t *testing.T) {
t.Error(err)
return
}
container, err := client.NewContainer(ctx, id, WithNewSpec(WithImageConfig(image), WithProcessArgs("sleep", "100")), WithNewSnapshot(id, image))
container, err := client.NewContainer(ctx, id, WithNewSpec(oci.WithImageConfig(image), oci.WithProcessArgs("sleep", "100")), WithNewSnapshot(id, image))
if err != nil {
t.Error(err)
return
@ -108,7 +110,7 @@ func TestCheckpointRestoreNewContainer(t *testing.T) {
t.Error(err)
return
}
container, err := client.NewContainer(ctx, id, WithNewSpec(WithImageConfig(image), WithProcessArgs("sleep", "100")), WithNewSnapshot(id, image))
container, err := client.NewContainer(ctx, id, WithNewSpec(oci.WithImageConfig(image), oci.WithProcessArgs("sleep", "100")), WithNewSnapshot(id, image))
if err != nil {
t.Error(err)
return
@ -201,7 +203,7 @@ func TestCheckpointLeaveRunning(t *testing.T) {
t.Error(err)
return
}
container, err := client.NewContainer(ctx, id, WithNewSpec(WithImageConfig(image), WithProcessArgs("sleep", "100")), WithNewSnapshot(id, image))
container, err := client.NewContainer(ctx, id, WithNewSpec(oci.WithImageConfig(image), oci.WithProcessArgs("sleep", "100")), WithNewSnapshot(id, image))
if err != nil {
t.Error(err)
return

View File

@ -20,6 +20,7 @@ import (
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/linux/runctypes"
"github.com/containerd/containerd/oci"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
"golang.org/x/sys/unix"
@ -46,13 +47,15 @@ func TestTaskUpdate(t *testing.T) {
return
}
limit := int64(32 * 1024 * 1024)
memory := func(_ context.Context, _ *Client, _ *containers.Container, s *specs.Spec) error {
memory := func(_ context.Context, _ oci.Client, _ *containers.Container, s *specs.Spec) error {
s.Linux.Resources.Memory = &specs.LinuxMemory{
Limit: &limit,
}
return nil
}
container, err := client.NewContainer(ctx, id, WithNewSpec(WithImageConfig(image), withProcessArgs("sleep", "30"), memory), WithNewSnapshot(id, image))
container, err := client.NewContainer(ctx, id,
WithNewSpec(oci.WithImageConfig(image), withProcessArgs("sleep", "30"), memory),
WithNewSnapshot(id, image))
if err != nil {
t.Error(err)
return
@ -131,7 +134,7 @@ func TestShimInCgroup(t *testing.T) {
t.Error(err)
return
}
container, err := client.NewContainer(ctx, id, WithNewSpec(WithImageConfig(image), WithProcessArgs("sleep", "30")), WithNewSnapshot(id, image))
container, err := client.NewContainer(ctx, id, WithNewSpec(oci.WithImageConfig(image), oci.WithProcessArgs("sleep", "30")), WithNewSnapshot(id, image))
if err != nil {
t.Error(err)
return
@ -409,7 +412,7 @@ func TestContainerUsername(t *testing.T) {
// squid user in the alpine image has a uid of 31
container, err := client.NewContainer(ctx, id,
withNewSnapshot(id, image),
WithNewSpec(withImageConfig(image), WithUsername("squid"), WithProcessArgs("id", "-u")),
WithNewSpec(withImageConfig(image), oci.WithUsername("squid"), oci.WithProcessArgs("id", "-u")),
)
if err != nil {
t.Error(err)
@ -618,7 +621,7 @@ func TestContainerUserID(t *testing.T) {
// adm user in the alpine image has a uid of 3 and gid of 4.
container, err := client.NewContainer(ctx, id,
withNewSnapshot(id, image),
WithNewSpec(withImageConfig(image), WithUserID(3), WithProcessArgs("sh", "-c", "echo $(id -u):$(id -g)")),
WithNewSpec(withImageConfig(image), oci.WithUserID(3), oci.WithProcessArgs("sh", "-c", "echo $(id -u):$(id -g)")),
)
if err != nil {
t.Error(err)
@ -679,7 +682,7 @@ func TestContainerKillAll(t *testing.T) {
withNewSnapshot(id, image),
WithNewSpec(withImageConfig(image),
withProcessArgs("sh", "-c", "top"),
WithHostNamespace(specs.PIDNamespace),
oci.WithHostNamespace(specs.PIDNamespace),
),
)
if err != nil {
@ -737,7 +740,7 @@ func TestShimSigkilled(t *testing.T) {
if err != nil {
t.Fatal(err)
}
container, err := client.NewContainer(ctx, id, WithNewSpec(WithImageConfig(image)), withNewSnapshot(id, image))
container, err := client.NewContainer(ctx, id, WithNewSpec(oci.WithImageConfig(image)), withNewSnapshot(id, image))
if err != nil {
t.Fatal(err)
}
@ -801,7 +804,7 @@ func TestDaemonRestartWithRunningShim(t *testing.T) {
t.Error(err)
return
}
container, err := client.NewContainer(ctx, id, WithNewSpec(WithImageConfig(image), WithProcessArgs("sleep", "100")), withNewSnapshot(id, image))
container, err := client.NewContainer(ctx, id, WithNewSpec(oci.WithImageConfig(image), oci.WithProcessArgs("sleep", "100")), withNewSnapshot(id, image))
if err != nil {
t.Fatal(err)
}
@ -931,7 +934,7 @@ func TestContainerKillInitPidHost(t *testing.T) {
withNewSnapshot(id, image),
WithNewSpec(withImageConfig(image),
withProcessArgs("sh", "-c", "sleep 42; echo hi"),
WithHostNamespace(specs.PIDNamespace),
oci.WithHostNamespace(specs.PIDNamespace),
),
)
if err != nil {
@ -1025,7 +1028,7 @@ func testUserNamespaces(t *testing.T, readonlyRootFS bool) {
opts := []NewContainerOpts{WithNewSpec(withImageConfig(image),
withExitStatus(7),
WithUserNamespace(0, 1000, 10000),
oci.WithUserNamespace(0, 1000, 10000),
)}
if readonlyRootFS {
opts = append(opts, WithRemappedSnapshotView(id, image, 1000, 1000))

View File

@ -14,6 +14,7 @@ import (
// Register the typeurl
"github.com/containerd/containerd/cio"
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/oci"
_ "github.com/containerd/containerd/runtime"
"github.com/containerd/typeurl"
@ -621,7 +622,9 @@ func TestContainerNoBinaryExists(t *testing.T) {
}
}
container, err := client.NewContainer(ctx, id, WithNewSpec(withImageConfig(image), WithProcessArgs("nothing")), withNewSnapshot(id, image))
container, err := client.NewContainer(ctx, id,
WithNewSpec(withImageConfig(image), oci.WithProcessArgs("nothing")),
withNewSnapshot(id, image))
if err != nil {
t.Error(err)
return
@ -1044,7 +1047,7 @@ func TestContainerHostname(t *testing.T) {
container, err := client.NewContainer(ctx, id, WithNewSpec(withImageConfig(image),
withProcessArgs("hostname"),
WithHostname(expected),
oci.WithHostname(expected),
),
withNewSnapshot(id, image))
if err != nil {
@ -1265,7 +1268,9 @@ func TestContainerMetrics(t *testing.T) {
return
}
}
container, err := client.NewContainer(ctx, id, WithNewSpec(withImageConfig(image), WithProcessArgs("sleep", "30")), withNewSnapshot(id, image))
container, err := client.NewContainer(ctx, id,
WithNewSpec(withImageConfig(image), oci.WithProcessArgs("sleep", "30")),
withNewSnapshot(id, image))
if err != nil {
t.Error(err)
return

View File

@ -7,15 +7,15 @@ import (
"io/ioutil"
"os"
"github.com/containerd/containerd"
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/oci"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/pkg/errors"
)
// WithProfile sets the provided apparmor profile to the spec
func WithProfile(profile string) containerd.SpecOpts {
return func(_ context.Context, _ *containerd.Client, _ *containers.Container, s *specs.Spec) error {
func WithProfile(profile string) oci.SpecOpts {
return func(_ context.Context, _ oci.Client, _ *containers.Container, s *specs.Spec) error {
s.Process.ApparmorProfile = profile
return nil
}
@ -23,8 +23,8 @@ func WithProfile(profile string) containerd.SpecOpts {
// WithDefaultProfile will generate a default apparmor profile under the provided name
// for the container. It is only generated if a profile under that name does not exist.
func WithDefaultProfile(name string) containerd.SpecOpts {
return func(_ context.Context, _ *containerd.Client, _ *containers.Container, s *specs.Spec) error {
func WithDefaultProfile(name string) oci.SpecOpts {
return func(_ context.Context, _ oci.Client, _ *containers.Container, s *specs.Spec) error {
yes, err := isLoaded(name)
if err != nil {
return err

View File

@ -8,16 +8,16 @@ import (
"fmt"
"io/ioutil"
"github.com/containerd/containerd"
"github.com/containerd/containerd/containers"
"github.com/containerd/containerd/oci"
"github.com/opencontainers/runtime-spec/specs-go"
)
// WithProfile receives the name of a file stored on disk comprising a json
// formated seccomp profile, as specified by the opencontainers/runtime-spec.
// The profile is read from the file, unmarshaled, and set to the spec.
func WithProfile(profile string) containerd.SpecOpts {
return func(_ context.Context, _ *containerd.Client, _ *containers.Container, s *specs.Spec) error {
func WithProfile(profile string) oci.SpecOpts {
return func(_ context.Context, _ oci.Client, _ *containers.Container, s *specs.Spec) error {
s.Linux.Seccomp = &specs.LinuxSeccomp{}
f, err := ioutil.ReadFile(profile)
if err != nil {
@ -32,8 +32,8 @@ func WithProfile(profile string) containerd.SpecOpts {
// WithDefaultProfile sets the default seccomp profile to the spec.
// Note: must follow the setting of process capabilities
func WithDefaultProfile() containerd.SpecOpts {
return func(_ context.Context, _ *containerd.Client, _ *containers.Container, s *specs.Spec) error {
func WithDefaultProfile() oci.SpecOpts {
return func(_ context.Context, _ oci.Client, _ *containers.Container, s *specs.Spec) error {
s.Linux.Seccomp = DefaultProfile(s)
return nil
}

View File

@ -65,20 +65,20 @@ If we want to make a `SpecOpt` to setup a container to monitor the host system w
package monitor
import (
"github.com/containerd/containerd"
"github.com/containerd/containerd/oci"
specs "github.com/opencontainers/runtime-spec/specs-go"
)
// WithHtop configures a container to monitor the host system via `htop`
func WithHtop(s *specs.Spec) error {
// make sure we are in the host pid namespace
if err := containerd.WithHostNamespace(specs.PIDNamespace)(s); err != nil {
if err := oci.WithHostNamespace(specs.PIDNamespace)(s); err != nil {
return err
}
// make sure we set htop as our arg
s.Process.Args = []string{"htop"}
// make sure we have a tty set for htop
if err := containerd.WithTTY(s); err != nil {
if err := oci.WithTTY(s); err != nil {
return err
}
return nil
@ -91,7 +91,7 @@ Adding your new option to spec generation is as easy as importing your new packa
import "github.com/crosbymichael/monitor"
container, err := client.NewContainer(ctx, id,
containerd.WithNewSpec(containerd.WithImageConfig(image), monitor.WithHtop),
containerd.WithNewSpec(oci.WithImageConfig(image), monitor.WithHtop),
)
```

View File

@ -149,7 +149,7 @@ The container will be based off of the image, use the runtime information in the
ctx,
"redis-server",
containerd.WithNewSnapshot("redis-server-snapshot", image),
containerd.WithNewSpec(containerd.WithImageConfig(image)),
containerd.WithNewSpec(oci.WithImageConfig(image)),
)
if err != nil {
return err
@ -173,6 +173,7 @@ import (
"log"
"github.com/containerd/containerd"
"github.com/containerd/containerd/oci"
"github.com/containerd/containerd/namespaces"
)
@ -200,7 +201,7 @@ func redisExample() error {
ctx,
"redis-server",
containerd.WithNewSnapshot("redis-server-snapshot", image),
containerd.WithNewSpec(containerd.WithImageConfig(image)),
containerd.WithNewSpec(oci.WithImageConfig(image)),
)
if err != nil {
return err
@ -317,6 +318,7 @@ import (
"time"
"github.com/containerd/containerd"
"github.com/containerd/containerd/oci"
"github.com/containerd/containerd/namespaces"
)
@ -349,7 +351,7 @@ func redisExample() error {
"redis-server",
containerd.WithImage(image),
containerd.WithNewSnapshot("redis-server-snapshot", image),
containerd.WithNewSpec(containerd.WithImageConfig(image)),
containerd.WithNewSpec(oci.WithImageConfig(image)),
)
if err != nil {
return err