Add Spec() method to task

Signed-off-by: Michael Crosby <michael@thepasture.io>
This commit is contained in:
Michael Crosby 2020-07-27 21:16:40 -04:00
parent d184a0a343
commit 4318f93f9c
3 changed files with 81 additions and 0 deletions

View File

@ -290,6 +290,7 @@ func (c *container) NewTask(ctx context.Context, ioCreate cio.Creator, opts ...N
client: c.client, client: c.client,
io: i, io: i,
id: c.id, id: c.id,
c: c,
} }
if info.Checkpoint != nil { if info.Checkpoint != nil {
request.Checkpoint = info.Checkpoint request.Checkpoint = info.Checkpoint
@ -407,6 +408,7 @@ func (c *container) loadTask(ctx context.Context, ioAttach cio.Attach) (Task, er
io: i, io: i,
id: response.Process.ID, id: response.Process.ID,
pid: response.Process.Pid, pid: response.Process.Pid,
c: c,
} }
return t, nil return t, nil
} }

View File

@ -1903,3 +1903,73 @@ func TestShimOOMScore(t *testing.T) {
<-statusC <-statusC
} }
func TestTaskSpec(t *testing.T) {
t.Parallel()
client, err := newClient(t, address)
if err != nil {
t.Fatal(err)
}
defer client.Close()
var (
image Image
ctx, cancel = testContext(t)
id = t.Name()
)
defer cancel()
image, err = client.GetImage(ctx, testImage)
if err != nil {
t.Fatal(err)
}
container, err := client.NewContainer(ctx, id, WithNewSnapshot(id, image), WithNewSpec(oci.WithImageConfig(image), longCommand))
if err != nil {
t.Fatal(err)
}
defer container.Delete(ctx, WithSnapshotCleanup)
task, err := container.NewTask(ctx, empty())
if err != nil {
t.Fatal(err)
}
defer task.Delete(ctx)
statusC, err := task.Wait(ctx)
if err != nil {
t.Fatal(err)
}
spec, err := task.Spec(ctx)
if err != nil {
t.Fatal(err)
}
if spec == nil {
t.Fatal("spec from task is nil")
}
direct, err := newDirectIO(ctx, false)
if err != nil {
t.Fatal(err)
}
defer direct.Delete()
lt, err := container.Task(ctx, direct.IOAttach)
if err != nil {
t.Fatal(err)
}
spec, err = lt.Spec(ctx)
if err != nil {
t.Fatal(err)
}
if spec == nil {
t.Fatal("spec from loaded task is nil")
}
if err := task.Kill(ctx, syscall.SIGKILL); err != nil {
t.Fatal(err)
}
<-statusC
}

View File

@ -35,6 +35,7 @@ import (
"github.com/containerd/containerd/errdefs" "github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images" "github.com/containerd/containerd/images"
"github.com/containerd/containerd/mount" "github.com/containerd/containerd/mount"
"github.com/containerd/containerd/oci"
"github.com/containerd/containerd/plugin" "github.com/containerd/containerd/plugin"
"github.com/containerd/containerd/rootfs" "github.com/containerd/containerd/rootfs"
"github.com/containerd/containerd/runtime/linux/runctypes" "github.com/containerd/containerd/runtime/linux/runctypes"
@ -175,18 +176,26 @@ type Task interface {
// For the built in Linux runtime, github.com/containerd/cgroups.Metrics // For the built in Linux runtime, github.com/containerd/cgroups.Metrics
// are returned in protobuf format // are returned in protobuf format
Metrics(context.Context) (*types.Metric, error) Metrics(context.Context) (*types.Metric, error)
// Spec returns the current OCI specification for the task
Spec(context.Context) (*oci.Spec, error)
} }
var _ = (Task)(&task{}) var _ = (Task)(&task{})
type task struct { type task struct {
client *Client client *Client
c Container
io cio.IO io cio.IO
id string id string
pid uint32 pid uint32
} }
// Spec returns the current OCI specification for the task
func (t *task) Spec(ctx context.Context) (*oci.Spec, error) {
return t.c.Spec(ctx)
}
// ID of the task // ID of the task
func (t *task) ID() string { func (t *task) ID() string {
return t.id return t.id