Skip to content

Commit 67b45ae

Browse files
committed
Add WithoutRefreshed metadata
Closes #2566 This provides faster lookups and lists for ctr commands. Signed-off-by: Michael Crosby <[email protected]>
1 parent bd27bef commit 67b45ae

3 files changed

Lines changed: 39 additions & 9 deletions

File tree

cmd/ctr/commands/containers/containers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ var listCommand = cli.Command{
124124
w := tabwriter.NewWriter(os.Stdout, 4, 8, 4, ' ', 0)
125125
fmt.Fprintln(w, "CONTAINER\tIMAGE\tRUNTIME\t")
126126
for _, c := range containers {
127-
info, err := c.Info(ctx)
127+
info, err := c.Info(ctx, containerd.WithoutRefreshedMetadata)
128128
if err != nil {
129129
return err
130130
}
@@ -261,7 +261,7 @@ var infoCommand = cli.Command{
261261
if err != nil {
262262
return err
263263
}
264-
info, err := container.Info(ctx)
264+
info, err := container.Info(ctx, containerd.WithoutRefreshedMetadata)
265265
if err != nil {
266266
return err
267267
}

container.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ type Container interface {
4949
// ID identifies the container
5050
ID() string
5151
// Info returns the underlying container record type
52-
Info(context.Context) (containers.Container, error)
52+
Info(context.Context, ...InfoOpts) (containers.Container, error)
5353
// Delete removes the container
5454
Delete(context.Context, ...DeleteOpts) error
5555
// NewTask creates a new task based on the container metadata
@@ -80,25 +80,41 @@ type Container interface {
8080

8181
func containerFromRecord(client *Client, c containers.Container) *container {
8282
return &container{
83-
client: client,
84-
id: c.ID,
83+
client: client,
84+
id: c.ID,
85+
metadata: c,
8586
}
8687
}
8788

8889
var _ = (Container)(&container{})
8990

9091
type container struct {
91-
client *Client
92-
id string
92+
client *Client
93+
id string
94+
metadata containers.Container
9395
}
9496

9597
// ID returns the container's unique id
9698
func (c *container) ID() string {
9799
return c.id
98100
}
99101

100-
func (c *container) Info(ctx context.Context) (containers.Container, error) {
101-
return c.get(ctx)
102+
func (c *container) Info(ctx context.Context, opts ...InfoOpts) (containers.Container, error) {
103+
i := &InfoConfig{
104+
// default to refreshing the container's local metadata
105+
Refresh: true,
106+
}
107+
for _, o := range opts {
108+
o(i)
109+
}
110+
if i.Refresh {
111+
metadata, err := c.get(ctx)
112+
if err != nil {
113+
return c.metadata, err
114+
}
115+
c.metadata = metadata
116+
}
117+
return c.metadata, nil
102118
}
103119

104120
func (c *container) Extensions(ctx context.Context) (map[string]prototypes.Any, error) {

container_opts.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ type NewContainerOpts func(ctx context.Context, client *Client, c *containers.Co
4141
// UpdateContainerOpts allows the caller to set additional options when updating a container
4242
type UpdateContainerOpts func(ctx context.Context, client *Client, c *containers.Container) error
4343

44+
// InfoOpts controls how container metadata is fetched and returned
45+
type InfoOpts func(*InfoConfig)
46+
47+
// InfoConfig specifies how container metadata is fetched
48+
type InfoConfig struct {
49+
// Refresh will to a fetch of the latest container metadata
50+
Refresh bool
51+
}
52+
4453
// WithRuntime allows a user to specify the runtime name and additional options that should
4554
// be used to create tasks for the container
4655
func WithRuntime(name string, options interface{}) NewContainerOpts {
@@ -235,3 +244,8 @@ func WithSpec(s *oci.Spec, opts ...oci.SpecOpts) NewContainerOpts {
235244
return err
236245
}
237246
}
247+
248+
// WithoutRefreshedMetadata will use the current metadata attached to the container object
249+
func WithoutRefreshedMetadata(i *InfoConfig) {
250+
i.Refresh = false
251+
}

0 commit comments

Comments
 (0)