diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2019-04-17 08:55:59 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-17 08:55:59 -0700 |
commit | 799d4667c1c33ac025741e2082739999f5b4563a (patch) | |
tree | 9fe87a7fc5f470314f754b8452163c4f59fd1a2c /cmd/podman/pods_prune.go | |
parent | d0c5e216ca508d195b805d0e48b159cfbff868a9 (diff) | |
parent | 4319552cf89e72925a80c63f427e5ef0a6376046 (diff) | |
download | podman-799d4667c1c33ac025741e2082739999f5b4563a.tar.gz podman-799d4667c1c33ac025741e2082739999f5b4563a.tar.bz2 podman-799d4667c1c33ac025741e2082739999f5b4563a.zip |
Merge pull request #2936 from haircommander/pod-prune
Add podman pod prune
Diffstat (limited to 'cmd/podman/pods_prune.go')
-rw-r--r-- | cmd/podman/pods_prune.go | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/cmd/podman/pods_prune.go b/cmd/podman/pods_prune.go new file mode 100644 index 000000000..89401a98a --- /dev/null +++ b/cmd/podman/pods_prune.go @@ -0,0 +1,83 @@ +package main + +import ( + "context" + + "github.com/containers/libpod/cmd/podman/cliconfig" + "github.com/containers/libpod/cmd/podman/shared" + "github.com/containers/libpod/pkg/adapter" + "github.com/pkg/errors" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +var ( + prunePodsCommand cliconfig.PrunePodsValues + prunePodsDescription = ` + podman pod prune + + Removes all exited pods +` + _prunePodsCommand = &cobra.Command{ + Use: "prune", + Args: noSubArgs, + Short: "Remove all stopped pods", + Long: prunePodsDescription, + RunE: func(cmd *cobra.Command, args []string) error { + prunePodsCommand.InputArgs = args + prunePodsCommand.GlobalFlags = MainGlobalOpts + return prunePodsCmd(&prunePodsCommand) + }, + } +) + +func init() { + prunePodsCommand.Command = _prunePodsCommand + prunePodsCommand.SetHelpTemplate(HelpTemplate()) + prunePodsCommand.SetUsageTemplate(UsageTemplate()) + flags := prunePodsCommand.Flags() + flags.BoolVarP(&prunePodsCommand.Force, "force", "f", false, "Force removal of a running pods. The default is false") +} + +func prunePods(runtime *adapter.LocalRuntime, ctx context.Context, maxWorkers int, force bool) error { + var deleteFuncs []shared.ParallelWorkerInput + + states := []string{shared.PodStateStopped, shared.PodStateExited} + delPods, err := runtime.GetPodsByStatus(states) + if err != nil { + return err + } + if len(delPods) < 1 { + return nil + } + for _, pod := range delPods { + p := pod + f := func() error { + return runtime.RemovePod(ctx, p, force, force) + } + + deleteFuncs = append(deleteFuncs, shared.ParallelWorkerInput{ + ContainerID: p.ID(), + ParallelFunc: f, + }) + } + // Run the parallel funcs + deleteErrors, errCount := shared.ParallelExecuteWorkerPool(maxWorkers, deleteFuncs) + return printParallelOutput(deleteErrors, errCount) +} + +func prunePodsCmd(c *cliconfig.PrunePodsValues) error { + runtime, err := adapter.GetRuntime(&c.PodmanCommand) + if err != nil { + return errors.Wrapf(err, "could not get runtime") + } + defer runtime.Shutdown(false) + + maxWorkers := shared.Parallelize("rm") + if c.GlobalIsSet("max-workers") { + maxWorkers = c.GlobalFlags.MaxWorks + } + logrus.Debugf("Setting maximum workers to %d", maxWorkers) + + return prunePods(runtime, getContext(), maxWorkers, c.Bool("force")) +} |