Skip to content

Commit

Permalink
lists filtered unused volumes in non-force mode
Browse files Browse the repository at this point in the history
Signed-off-by: Achilleas Tzenetopoulos <[email protected]>
  • Loading branch information
Achilleas Tzenetopoulos committed Jan 26, 2021
1 parent 223bdb7 commit c180fd5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
42 changes: 35 additions & 7 deletions cmd/podman/volumes/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,9 @@ func init() {

func prune(cmd *cobra.Command, args []string) error {
var (
pruneOptions = entities.VolumePruneOptions{}
listOptions = entities.VolumeListOptions{}
pruneOptions = entities.VolumePruneOptions{}
listOptions = entities.VolumeListOptions{}
unusedOptions = entities.VolumeListOptions{}
)
// Prompt for confirmation if --force is not set
force, err := cmd.Flags().GetBool("force")
Expand All @@ -60,15 +61,32 @@ func prune(cmd *cobra.Command, args []string) error {
if !force {
reader := bufio.NewReader(os.Stdin)
fmt.Println("WARNING! This will remove all volumes not used by at least one container. The following volumes will be removed:")
pruneOptions.Filters, err = filters.ParseFilterArgumentsIntoFilters(filter)
listOptions.Filter, err = filters.ParseFilterArgumentsIntoFilters(filter)
if err != nil {
return err
}
// filter all the unused (unbound) volumes
unusedOptions.Filter = make(map[string][]string, 1)
unusedOptions.Filter["unused"] = []string{"true"}
if err != nil {
return err
}
unusedVolumes, err := registry.ContainerEngine().VolumeList(context.Background(), unusedOptions)
if err != nil {
return err
}
// filter volumes based on user input
filteredVolumes, err := registry.ContainerEngine().VolumeList(context.Background(), listOptions)
if err != nil {
return err
}
for _, fv := range filteredVolumes {
finalVolumes := getUnion(unusedVolumes, filteredVolumes)
if len(finalVolumes) < 1 {
fmt.Println("No unused volumes found")
return nil
}
for _, fv := range finalVolumes {
fmt.Println(fv.Name)
}
fmt.Print("Are you sure you want to continue? [y/N] ")
Expand All @@ -80,13 +98,23 @@ func prune(cmd *cobra.Command, args []string) error {
return nil
}
}
pruneOptions.Filters, err = filters.ParseFilterArgumentsIntoFilters(filter)
if err != nil {
return err
}
responses, err := registry.ContainerEngine().VolumePrune(context.Background(), pruneOptions)
if err != nil {
return err
}
return utils.PrintVolumePruneResults(responses, false)
}

func getUnion(a, b []*entities.VolumeListReport) []*entities.VolumeListReport {
var intersection []*entities.VolumeListReport
hash := make(map[string]bool, len(a))
for _, aa := range a {
hash[aa.Name] = true
}
for _, bb := range b {
if hash[bb.Name] {
intersection = append(intersection, bb)
}
}
return intersection
}
25 changes: 25 additions & 0 deletions pkg/domain/filters/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,31 @@ func GenerateVolumeFilters(filters url.Values) ([]libpod.VolumeFilter, error) {
}
return dangling
})
case "unused":
invert := false
unusedVal := val
switch strings.ToLower(unusedVal) {
case "true", "1":
// do nth
case "false", "0":
invert = true

default:
return nil, errors.Errorf("%q is not a valid value for the \"unused\" filter - must be true or false", unusedVal)
}
vf = append(vf, func(v *libpod.Volume) bool {
resp, err := v.VolumeInUse()
if err != nil || len(resp) > 0 {
if !invert {
return false
}
return true
}
if !invert {
return true
}
return false
})
default:
return nil, errors.Errorf("%q is in an invalid volume filter", filter)
}
Expand Down

0 comments on commit c180fd5

Please sign in to comment.