Skip to content

Commit

Permalink
Updates lease creation to respect existing leases
Browse files Browse the repository at this point in the history
Signed-off-by: Derek McGowan <derek@mcgstyle.net>
  • Loading branch information
dmcgowan committed Nov 7, 2017
1 parent dce27d8 commit 07885f1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
19 changes: 6 additions & 13 deletions client.go
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/containerd/containerd/diff"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/leases"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/platforms"
"github.com/containerd/containerd/plugin"
Expand Down Expand Up @@ -138,13 +137,11 @@ func (c *Client) Containers(ctx context.Context, filters ...string) ([]Container
// NewContainer will create a new container in container with the provided id
// the id must be unique within the namespace
func (c *Client) NewContainer(ctx context.Context, id string, opts ...NewContainerOpts) (Container, error) {
l, err := c.CreateLease(ctx)
ctx, done, err := c.withLease(ctx)
if err != nil {
return nil, err
}
defer l.Delete(ctx)

ctx = leases.WithLease(ctx, l.ID())
defer done()

container := containers.Container{
ID: id,
Expand Down Expand Up @@ -221,13 +218,11 @@ func (c *Client) Pull(ctx context.Context, ref string, opts ...RemoteOpt) (Image
}
store := c.ContentStore()

l, err := c.CreateLease(ctx)
ctx, done, err := c.withLease(ctx)
if err != nil {
return nil, err
}
defer l.Delete(ctx)

ctx = leases.WithLease(ctx, l.ID())
defer done()

name, desc, err := pullCtx.Resolver.Resolve(ctx, ref)
if err != nil {
Expand Down Expand Up @@ -596,13 +591,11 @@ func (c *Client) Import(ctx context.Context, ref string, reader io.Reader, opts
return nil, err
}

l, err := c.CreateLease(ctx)
ctx, done, err := c.withLease(ctx)
if err != nil {
return nil, err
}
defer l.Delete(ctx)

ctx = leases.WithLease(ctx, l.ID())
defer done()

switch iopts.format {
case ociImageFormat:
Expand Down
20 changes: 20 additions & 0 deletions lease.go
Expand Up @@ -5,6 +5,7 @@ import (
"time"

leasesapi "github.com/containerd/containerd/api/services/leases/v1"
"github.com/containerd/containerd/leases"
)

// Lease is used to hold a reference to active resources which have not been
Expand Down Expand Up @@ -50,6 +51,25 @@ func (c *Client) ListLeases(ctx context.Context) ([]Lease, error) {
return leases, nil
}

func (c *Client) withLease(ctx context.Context) (context.Context, func() error, error) {
_, ok := leases.Lease(ctx)
if ok {
return ctx, func() error {
return nil
}, nil
}

l, err := c.CreateLease(ctx)
if err != nil {
return nil, nil, err
}

ctx = leases.WithLease(ctx, l.ID())
return ctx, func() error {
return l.Delete(ctx)
}, nil
}

// ID returns the lease ID
func (l Lease) ID() string {
return l.id
Expand Down
7 changes: 2 additions & 5 deletions task.go
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/containerd/containerd/diff"
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/leases"
"github.com/containerd/containerd/mount"
"github.com/containerd/containerd/plugin"
"github.com/containerd/containerd/rootfs"
Expand Down Expand Up @@ -358,13 +357,11 @@ func (t *task) Resize(ctx context.Context, w, h uint32) error {
}

func (t *task) Checkpoint(ctx context.Context, opts ...CheckpointTaskOpts) (Image, error) {
l, err := t.client.CreateLease(ctx)
ctx, done, err := t.client.withLease(ctx)
if err != nil {
return nil, err
}
defer l.Delete(ctx)

ctx = leases.WithLease(ctx, l.ID())
defer done()

request := &tasks.CheckpointTaskRequest{
ContainerID: t.id,
Expand Down

0 comments on commit 07885f1

Please sign in to comment.