Skip to content

Commit

Permalink
WIP: add size options for volume ls
Browse files Browse the repository at this point in the history
Need to look what the most reasonable approach is; the backend uses
functional options, whereas other backends use an option-struct :/

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed May 24, 2023
1 parent c5126d1 commit 5b97ddd
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
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

0 comments on commit 5b97ddd

Please sign in to comment.