Browse code

add NewContainerOpts to libcontainerd.Create

Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>

Evan Hazlett authored on 2019/04/11 12:00:32
Showing 5 changed files
... ...
@@ -5,6 +5,9 @@ import (
5 5
 	"runtime"
6 6
 	"time"
7 7
 
8
+	"github.com/containerd/containerd"
9
+	"github.com/containerd/containerd/containers"
10
+	"github.com/docker/distribution/reference"
8 11
 	"github.com/docker/docker/api/types"
9 12
 	containertypes "github.com/docker/docker/api/types/container"
10 13
 	"github.com/docker/docker/container"
... ...
@@ -179,7 +182,12 @@ func (daemon *Daemon) containerStart(container *container.Container, checkpoint
179 179
 
180 180
 	ctx := context.TODO()
181 181
 
182
-	err = daemon.containerd.Create(ctx, container.ID, spec, createOptions)
182
+	imageRef, err := reference.ParseNormalizedNamed(container.Config.Image)
183
+	if err != nil {
184
+		return err
185
+	}
186
+
187
+	err = daemon.containerd.Create(ctx, container.ID, spec, createOptions, withImageName(imageRef.String()))
183 188
 	if err != nil {
184 189
 		if errdefs.IsConflict(err) {
185 190
 			logrus.WithError(err).WithField("container", container.ID).Error("Container not cleaned up from containerd from previous run")
... ...
@@ -188,7 +196,7 @@ func (daemon *Daemon) containerStart(container *container.Container, checkpoint
188 188
 			if err := daemon.containerd.Delete(ctx, container.ID); err != nil && !errdefs.IsNotFound(err) {
189 189
 				logrus.WithError(err).WithField("container", container.ID).Error("Error cleaning up stale containerd container object")
190 190
 			}
191
-			err = daemon.containerd.Create(ctx, container.ID, spec, createOptions)
191
+			err = daemon.containerd.Create(ctx, container.ID, spec, createOptions, withImageName(imageRef.String()))
192 192
 		}
193 193
 		if err != nil {
194 194
 			return translateContainerdStartErr(container.Path, container.SetExitCode, err)
... ...
@@ -265,3 +273,10 @@ func (daemon *Daemon) Cleanup(container *container.Container) {
265 265
 		logrus.Errorf("%s cleanup: failed to delete container from containerd: %v", container.ID, err)
266 266
 	}
267 267
 }
268
+
269
+func withImageName(n string) containerd.NewContainerOpts {
270
+	return func(ctx context.Context, _ *containerd.Client, c *containers.Container) error {
271
+		c.Image = n
272
+		return nil
273
+	}
274
+}
... ...
@@ -28,7 +28,7 @@ func (c *MockContainerdClient) Version(ctx context.Context) (containerd.Version,
28 28
 func (c *MockContainerdClient) Restore(ctx context.Context, containerID string, attachStdio libcontainerdtypes.StdioCallback) (alive bool, pid int, p libcontainerdtypes.Process, err error) {
29 29
 	return false, 0, &mockProcess{}, nil
30 30
 }
31
-func (c *MockContainerdClient) Create(ctx context.Context, containerID string, spec *specs.Spec, runtimeOptions interface{}) error {
31
+func (c *MockContainerdClient) Create(ctx context.Context, containerID string, spec *specs.Spec, runtimeOptions interface{}, opts ...containerd.NewContainerOpts) error {
32 32
 	return nil
33 33
 }
34 34
 func (c *MockContainerdClient) Start(ctx context.Context, containerID, checkpointDir string, withStdin bool, attachStdio libcontainerdtypes.StdioCallback) (pid int, err error) {
... ...
@@ -152,7 +152,7 @@ func (c *client) Version(ctx context.Context) (containerd.Version, error) {
152 152
 //		"ImagePath": "C:\\\\control\\\\windowsfilter\\\\65bf96e5760a09edf1790cb229e2dfb2dbd0fcdc0bf7451bae099106bfbfea0c\\\\UtilityVM"
153 153
 //	},
154 154
 //}
155
-func (c *client) Create(_ context.Context, id string, spec *specs.Spec, runtimeOptions interface{}) error {
155
+func (c *client) Create(_ context.Context, id string, spec *specs.Spec, runtimeOptions interface{}, opts ...containerd.NewContainerOpts) error {
156 156
 	if ctr := c.getContainer(id); ctr != nil {
157 157
 		return errors.WithStack(errdefs.Conflict(errors.New("id already in use")))
158 158
 	}
... ...
@@ -124,15 +124,18 @@ func (c *client) Restore(ctx context.Context, id string, attachStdio libcontaine
124 124
 	}, nil
125 125
 }
126 126
 
127
-func (c *client) Create(ctx context.Context, id string, ociSpec *specs.Spec, runtimeOptions interface{}) error {
127
+func (c *client) Create(ctx context.Context, id string, ociSpec *specs.Spec, runtimeOptions interface{}, opts ...containerd.NewContainerOpts) error {
128 128
 	bdir := c.bundleDir(id)
129 129
 	c.logger.WithField("bundle", bdir).WithField("root", ociSpec.Root.Path).Debug("bundle dir created")
130 130
 
131
-	_, err := c.client.NewContainer(ctx, id,
131
+	newOpts := []containerd.NewContainerOpts{
132 132
 		containerd.WithSpec(ociSpec),
133 133
 		containerd.WithRuntime(runtimeName, runtimeOptions),
134 134
 		WithBundle(bdir, ociSpec),
135
-	)
135
+	}
136
+	opts = append(opts, newOpts...)
137
+
138
+	_, err := c.client.NewContainer(ctx, id, opts...)
136 139
 	if err != nil {
137 140
 		if containerderrors.IsAlreadyExists(err) {
138 141
 			return errors.WithStack(errdefs.Conflict(errors.New("id already in use")))
... ...
@@ -52,7 +52,7 @@ type Client interface {
52 52
 
53 53
 	Restore(ctx context.Context, containerID string, attachStdio StdioCallback) (alive bool, pid int, p Process, err error)
54 54
 
55
-	Create(ctx context.Context, containerID string, spec *specs.Spec, runtimeOptions interface{}) error
55
+	Create(ctx context.Context, containerID string, spec *specs.Spec, runtimeOptions interface{}, opts ...containerd.NewContainerOpts) error
56 56
 	Start(ctx context.Context, containerID, checkpointDir string, withStdin bool, attachStdio StdioCallback) (pid int, err error)
57 57
 	SignalProcess(ctx context.Context, containerID, processID string, signal int) error
58 58
 	Exec(ctx context.Context, containerID, processID string, spec *specs.Process, withStdin bool, attachStdio StdioCallback) (int, error)