Add --filter until=<timestamp> for docker container/image prune

This fix is a follow up for comment
https://github.com/docker/docker/pull/28535#issuecomment-263215225

This fix provides `--filter until=<timestamp>` for `docker container/image prune`.

This fix adds `--filter until=<timestamp>` to `docker container/image prune`
so that it is possible to specify a timestamp and prune those containers/images
that are earlier than the timestamp.

Related docs has been updated

Several integration tests have been added to cover changes.

This fix fixes #28497.

This fix is related to #28535.

Signed-off-by: Yong Tang <yong.tang.github@outlook.com>
This commit is contained in:
Yong Tang
2016-12-07 14:02:13 -08:00
parent fe181a18d5
commit e2416af013
5 changed files with 49 additions and 35 deletions

View File

@ -6,18 +6,20 @@ import (
"github.com/docker/docker/cli"
"github.com/docker/docker/cli/command"
"github.com/docker/docker/cli/command/prune"
"github.com/docker/docker/opts"
units "github.com/docker/go-units"
"github.com/spf13/cobra"
)
type pruneOptions struct {
force bool
all bool
force bool
all bool
filter opts.FilterOpt
}
// NewPruneCommand creates a new cobra.Command for `docker prune`
func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command {
var opts pruneOptions
opts := pruneOptions{filter: opts.NewFilterOpt()}
cmd := &cobra.Command{
Use: "prune [OPTIONS]",
@ -32,6 +34,7 @@ func NewPruneCommand(dockerCli *command.DockerCli) *cobra.Command {
flags := cmd.Flags()
flags.BoolVarP(&opts.force, "force", "f", false, "Do not prompt for confirmation")
flags.BoolVarP(&opts.all, "all", "a", false, "Remove all unused images not just dangling ones")
flags.Var(&opts.filter, "filter", "Provide filter values (e.g. 'until=<timestamp>')")
return cmd
}
@ -48,27 +51,27 @@ Are you sure you want to continue?`
allImageDesc = `- all images without at least one container associated to them`
)
func runPrune(dockerCli *command.DockerCli, opts pruneOptions) error {
func runPrune(dockerCli *command.DockerCli, options pruneOptions) error {
var message string
if opts.all {
if options.all {
message = fmt.Sprintf(warning, allImageDesc)
} else {
message = fmt.Sprintf(warning, danglingImageDesc)
}
if !opts.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), message) {
if !options.force && !command.PromptForConfirmation(dockerCli.In(), dockerCli.Out(), message) {
return nil
}
var spaceReclaimed uint64
for _, pruneFn := range []func(dockerCli *command.DockerCli) (uint64, string, error){
for _, pruneFn := range []func(dockerCli *command.DockerCli, filter opts.FilterOpt) (uint64, string, error){
prune.RunContainerPrune,
prune.RunVolumePrune,
prune.RunNetworkPrune,
} {
spc, output, err := pruneFn(dockerCli)
spc, output, err := pruneFn(dockerCli, options.filter)
if err != nil {
return err
}
@ -78,7 +81,7 @@ func runPrune(dockerCli *command.DockerCli, opts pruneOptions) error {
}
}
spc, output, err := prune.RunImagePrune(dockerCli, opts.all)
spc, output, err := prune.RunImagePrune(dockerCli, options.all, options.filter)
if err != nil {
return err
}