Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: add size options for volume ls #43548

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/server/router/volume/backend.go
Expand Up @@ -13,7 +13,7 @@ import (
// Backend is the methods that need to be implemented to provide
// volume specific functionality
type Backend interface {
List(ctx context.Context, filter filters.Args) ([]*volume.Volume, []string, error)
List(ctx context.Context, opts ...opts.ListOption) ([]*volume.Volume, []string, error)
Get(ctx context.Context, name string, opts ...opts.GetOption) (*volume.Volume, error)
Create(ctx context.Context, name, driverName string, opts ...opts.CreateOption) (*volume.Volume, error)
Remove(ctx context.Context, name string, opts ...opts.RemoveOption) error
Expand Down
9 changes: 6 additions & 3 deletions api/server/router/volume/volume_routes.go
Expand Up @@ -27,18 +27,21 @@ func (v *volumeRouter) getVolumesList(ctx context.Context, w http.ResponseWriter
return err
}

filters, err := filters.FromJSON(r.Form.Get("filters"))
fltrs, err := filters.FromJSON(r.Form.Get("filters"))
if err != nil {
return errors.Wrap(err, "error reading volume filters")
}
volumes, warnings, err := v.backend.List(ctx, filters)
volumes, warnings, err := v.backend.List(ctx,
opts.WithFilters(fltrs),
opts.WithSize(httputils.BoolValue(r, "size")),
)
if err != nil {
return err
}

version := httputils.VersionFromContext(ctx)
if versions.GreaterThanOrEqualTo(version, clusterVolumesVersion) && v.cluster.IsManager() {
clusterVolumes, swarmErr := v.cluster.GetVolumes(volume.ListOptions{Filters: filters})
clusterVolumes, swarmErr := v.cluster.GetVolumes(volume.ListOptions{Filters: fltrs})
if swarmErr != nil {
// if there is a swarm error, we may not want to error out right
// away. the local list probably worked. instead, let's do what we
Expand Down
3 changes: 3 additions & 0 deletions api/types/volume/options.go
Expand Up @@ -5,4 +5,7 @@ import "github.com/docker/docker/api/types/filters"
// ListOptions holds parameters to list volumes.
type ListOptions struct {
Filters filters.Args

// Size enables calculating the size for each volume.
Size bool
}
3 changes: 3 additions & 0 deletions client/volume_list.go
Expand Up @@ -22,6 +22,9 @@ func (cli *Client) VolumeList(ctx context.Context, options volume.ListOptions) (
}
query.Set("filters", filterJSON)
}
if options.Size {
query.Set("size", "1")
}
resp, err := cli.get(ctx, "/volumes", query, nil)
defer ensureReaderClosed(resp)
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions volume/service/opts/opts.go
@@ -1,5 +1,7 @@
package opts

import "github.com/docker/docker/api/types/filters"

// CreateOption is used to pass options in when creating a volume
type CreateOption func(*CreateConfig)

Expand Down Expand Up @@ -97,3 +99,28 @@ func WithPurgeOnError(b bool) RemoveOption {
o.PurgeOnError = b
}
}

// ListConfig is used by `ListOption` to store config options for listing volumes.
type ListConfig struct {
Filters filters.Args

// Size enables calculating the size for each volume.
Size bool
}

// ListOption is passed to the service `List` add extra details on the get request
type ListOption func(*ListConfig)

// WithFilters applies the given filters to the ListConfig.
func WithFilters(args filters.Args) ListOption {
return func(o *ListConfig) {
o.Filters = args
}
}

// WithSize enables size calculation for the list response.
func WithSize(enabled bool) ListOption {
return func(o *ListConfig) {
o.Size = enabled
}
}
15 changes: 12 additions & 3 deletions volume/service/service.go
Expand Up @@ -256,8 +256,13 @@ func (s *VolumesService) Prune(ctx context.Context, filter filters.Args) (*types

// List gets the list of volumes which match the past in filters
// If filters is nil or empty all volumes are returned.
func (s *VolumesService) List(ctx context.Context, filter filters.Args) (volumesOut []*volumetypes.Volume, warnings []string, err error) {
by, err := filtersToBy(filter, acceptedListFilters)
func (s *VolumesService) List(ctx context.Context, options ...opts.ListOption) (volumesOut []*volumetypes.Volume, warnings []string, err error) {
var cfg opts.ListConfig
for _, o := range options {
o(&cfg)
}

by, err := filtersToBy(cfg.Filters, acceptedListFilters)
if err != nil {
return nil, nil, err
}
Expand All @@ -266,8 +271,12 @@ func (s *VolumesService) List(ctx context.Context, filter filters.Args) (volumes
if err != nil {
return nil, nil, err
}
vOpts := []convertOpt{useCachedPath(true)}
if cfg.Size {
vOpts = append(vOpts, calcSize(true))
}

return s.volumesToAPI(ctx, volumes, useCachedPath(true)), warnings, nil
return s.volumesToAPI(ctx, volumes, vOpts...), warnings, nil
}

// Shutdown shuts down the image service and dependencies
Expand Down