Skip to content

Commit

Permalink
Merge pull request #11015 from jmguzik/until-list-volume
Browse files Browse the repository at this point in the history
Add until filter to volume ls filters list
  • Loading branch information
openshift-merge-robot authored Jul 22, 2021
2 parents 6370622 + 2d8e837 commit 3657754
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
1 change: 1 addition & 0 deletions docs/source/markdown/podman-volume-ls.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Volumes can be filtered by the following attributes:
- name
- opt
- scope
- until

#### **--format**=*format*

Expand Down
2 changes: 2 additions & 0 deletions pkg/api/server/register_volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (s *APIServer) registerVolumeHandlers(r *mux.Router) error {
// - label=<key> or label=<key>:<value> Matches volumes based on the presence of a label alone or a label and a value.
// - name=<volume-name> Matches all of volume name.
// - opt=<driver-option> Matches a storage driver options
// - `until=<timestamp>` List volumes created before this timestamp. The `<timestamp>` can be Unix timestamps, date formatted timestamps, or Go duration strings (e.g. `10m`, `1h30m`) computed relative to the daemon machine’s time.
// responses:
// '200':
// "$ref": "#/responses/VolumeList"
Expand Down Expand Up @@ -166,6 +167,7 @@ func (s *APIServer) registerVolumeHandlers(r *mux.Router) error {
// - driver=<volume-driver-name> Matches volumes based on their driver.
// - label=<key> or label=<key>:<value> Matches volumes based on the presence of a label alone or a label and a value.
// - name=<volume-name> Matches all of volume name.
// - `until=<timestamp>` List volumes created before this timestamp. The `<timestamp>` can be Unix timestamps, date formatted timestamps, or Go duration strings (e.g. `10m`, `1h30m`) computed relative to the daemon machine’s time.
//
// Note:
// The boolean `dangling` filter is not yet implemented for this endpoint.
Expand Down
28 changes: 21 additions & 7 deletions pkg/domain/filters/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func GenerateVolumeFilters(filters url.Values) ([]libpod.VolumeFilter, error) {
}
return false
})
case "until":
f, err := createUntilFilterVolumeFunction(val)
if err != nil {
return nil, err
}
vf = append(vf, f)
case "dangling":
danglingVal := val
invert := false
Expand Down Expand Up @@ -93,20 +99,28 @@ func GeneratePruneVolumeFilters(filters url.Values) ([]libpod.VolumeFilter, erro
return util.MatchLabelFilters([]string{filterVal}, v.Labels())
})
case "until":
until, err := util.ComputeUntilTimestamp([]string{filterVal})
f, err := createUntilFilterVolumeFunction(filterVal)
if err != nil {
return nil, err
}
vf = append(vf, func(v *libpod.Volume) bool {
if !until.IsZero() && v.CreatedTime().Before(until) {
return true
}
return false
})
vf = append(vf, f)
default:
return nil, errors.Errorf("%q is an invalid volume filter", filter)
}
}
}
return vf, nil
}

func createUntilFilterVolumeFunction(filter string) (libpod.VolumeFilter, error) {
until, err := util.ComputeUntilTimestamp([]string{filter})
if err != nil {
return nil, err
}
return func(v *libpod.Volume) bool {
if !until.IsZero() && v.CreatedTime().Before(until) {
return true
}
return false
}, nil
}
2 changes: 2 additions & 0 deletions test/apiv2/30-volumes.at
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ t POST libpod/volumes/create \
# with date way back in the past, volume should not be deleted (compat api)
t POST volumes/prune?filters='{"until":["500000"]}' 200
t GET libpod/volumes/json?filters='{"label":["testuntilcompat"]}' 200 length=1
t GET libpod/volumes/json?filters='{"until":["500000"]}' 200 length=0
t GET libpod/volumes/json?filters='{"until":["5000000000"]}' 200 length=1

# with date far in the future, volume should be deleted (compat api)
t POST volumes/prune?filters='{"until":["5000000000"]}' 200
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/volume_ls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,22 @@ var _ = Describe("Podman volume ls", func() {
Expect(len(session.OutputToStringArray())).To(Equal(0))
})

It("podman ls volume with --filter until flag", func() {
session := podmanTest.Podman([]string{"volume", "create"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

session = podmanTest.Podman([]string{"volume", "ls", "--filter", "until=5000000000"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
Expect(len(session.OutputToStringArray())).To(Equal(2))

session = podmanTest.Podman([]string{"volume", "ls", "--filter", "until=50000"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
Expect(len(session.OutputToStringArray())).To(Equal(0))
})

It("podman volume ls with --filter dangling", func() {
volName1 := "volume1"
session := podmanTest.Podman([]string{"volume", "create", volName1})
Expand Down

0 comments on commit 3657754

Please sign in to comment.