summaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
authorBaron Lenardson <lenardson.baron@gmail.com>2020-12-21 10:35:21 -0600
committerBaron Lenardson <lenardson.baron@gmail.com>2020-12-21 10:55:39 -0600
commit5923656f321a9ca7b222c81cdb5f3387cc7cd3ad (patch)
tree47dfe20988b8ebf512ed769f42ed581a15d90b66 /pkg
parent5c6b5ef34905f40562b518799c35be8d06694e65 (diff)
downloadpodman-5923656f321a9ca7b222c81cdb5f3387cc7cd3ad.tar.gz
podman-5923656f321a9ca7b222c81cdb5f3387cc7cd3ad.tar.bz2
podman-5923656f321a9ca7b222c81cdb5f3387cc7cd3ad.zip
Add volume filters to system prune
This change was missed in pull/8689. Now that volume pruneing supports filters system pruneing can pass its filters down to the volume pruneing. Additionally this change adds tests for the following components * podman system prune subcommand with `--volumes` & `--filter` options * apiv2 api tests for `/system/` and `/libpod/system` endpoints Relates to #8453, #8672 Signed-off-by: Baron Lenardson <lenardson.baron@gmail.com>
Diffstat (limited to 'pkg')
-rw-r--r--pkg/bindings/test/system_test.go57
-rw-r--r--pkg/domain/entities/system.go6
-rw-r--r--pkg/domain/infra/abi/system.go12
-rw-r--r--pkg/domain/infra/tunnel/system.go2
4 files changed, 71 insertions, 6 deletions
diff --git a/pkg/bindings/test/system_test.go b/pkg/bindings/test/system_test.go
index 5cb0f9125..25fda5575 100644
--- a/pkg/bindings/test/system_test.go
+++ b/pkg/bindings/test/system_test.go
@@ -158,4 +158,61 @@ var _ = Describe("Podman system", func() {
// Volume should be pruned now as flag set true
Expect(len(systemPruneResponse.VolumePruneReport)).To(Equal(1))
})
+
+ It("podman system prune running alpine container volume prune --filter", func() {
+ // Start a pod and leave it running
+ _, err := pods.Start(bt.conn, newpod, nil)
+ Expect(err).To(BeNil())
+
+ // Start and stop a container to enter in exited state.
+ var name = "top"
+ _, err = bt.RunTopContainer(&name, bindings.PFalse, nil)
+ Expect(err).To(BeNil())
+ err = containers.Stop(bt.conn, name, nil)
+ Expect(err).To(BeNil())
+
+ // Start second container and leave in running
+ var name2 = "top2"
+ _, err = bt.RunTopContainer(&name2, bindings.PFalse, nil)
+ Expect(err).To(BeNil())
+
+ // Adding an unused volume should work
+ _, err = volumes.Create(bt.conn, entities.VolumeCreateOptions{}, nil)
+ Expect(err).To(BeNil())
+
+ // Adding an unused volume with label should work
+ _, err = volumes.Create(bt.conn, entities.VolumeCreateOptions{Label: map[string]string{
+ "label1": "value1",
+ }}, nil)
+ Expect(err).To(BeNil())
+
+ f := make(map[string][]string)
+ f["label"] = []string{"label1=idontmatch"}
+
+ options := new(system.PruneOptions).WithAll(true).WithVolumes(true).WithFilters(f)
+ systemPruneResponse, err := system.Prune(bt.conn, options)
+ Expect(err).To(BeNil())
+ Expect(len(systemPruneResponse.PodPruneReport)).To(Equal(0))
+ // TODO fix system filter handling so all components can handle filters
+ // This check **should** be "Equal(0)" since we are passing label
+ // filters however the Prune function doesn't seem to pass filters
+ // to each component.
+ Expect(len(systemPruneResponse.ContainerPruneReport.ID)).To(Equal(1))
+ Expect(len(systemPruneResponse.ImagePruneReport.Report.Id)).
+ To(BeNumerically(">", 0))
+ // Alpine image should not be pruned as used by running container
+ Expect(systemPruneResponse.ImagePruneReport.Report.Id).
+ ToNot(ContainElement("docker.io/library/alpine:latest"))
+ // Volume shouldn't be pruned because the PruneOptions filters doesn't match
+ Expect(len(systemPruneResponse.VolumePruneReport)).To(Equal(0))
+
+ // Fix filter and re prune
+ f["label"] = []string{"label1=value1"}
+ options = new(system.PruneOptions).WithAll(true).WithVolumes(true).WithFilters(f)
+ systemPruneResponse, err = system.Prune(bt.conn, options)
+ Expect(err).To(BeNil())
+
+ // Volume should be pruned because the PruneOptions filters now match
+ Expect(len(systemPruneResponse.VolumePruneReport)).To(Equal(1))
+ })
})
diff --git a/pkg/domain/entities/system.go b/pkg/domain/entities/system.go
index 5266dc4cf..d5118f6a8 100644
--- a/pkg/domain/entities/system.go
+++ b/pkg/domain/entities/system.go
@@ -17,9 +17,9 @@ type ServiceOptions struct {
// SystemPruneOptions provides options to prune system.
type SystemPruneOptions struct {
- All bool
- Volume bool
- ContainerPruneOptions
+ All bool
+ Volume bool
+ Filters map[string][]string `json:"filters" schema:"filters"`
}
// SystemPruneReport provides report after system prune is executed.
diff --git a/pkg/domain/infra/abi/system.go b/pkg/domain/infra/abi/system.go
index 17faa7fff..5f6c95d4f 100644
--- a/pkg/domain/infra/abi/system.go
+++ b/pkg/domain/infra/abi/system.go
@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io/ioutil"
+ "net/url"
"os"
"os/exec"
"path/filepath"
@@ -180,7 +181,12 @@ func (ic *ContainerEngine) SystemPrune(ctx context.Context, options entities.Sys
found = true
}
systemPruneReport.PodPruneReport = append(systemPruneReport.PodPruneReport, podPruneReport...)
- containerPruneReport, err := ic.ContainerPrune(ctx, options.ContainerPruneOptions)
+
+ // TODO: Figure out cleaner way to handle all of the different PruneOptions
+ containerPruneOptions := entities.ContainerPruneOptions{}
+ containerPruneOptions.Filters = (url.Values)(options.Filters)
+
+ containerPruneReport, err := ic.ContainerPrune(ctx, containerPruneOptions)
if err != nil {
return nil, err
}
@@ -217,7 +223,9 @@ func (ic *ContainerEngine) SystemPrune(ctx context.Context, options entities.Sys
systemPruneReport.ImagePruneReport.Report.Id = append(systemPruneReport.ImagePruneReport.Report.Id, results...)
}
if options.Volume {
- volumePruneReport, err := ic.pruneVolumesHelper(ctx, nil)
+ volumePruneOptions := entities.VolumePruneOptions{}
+ volumePruneOptions.Filters = (url.Values)(options.Filters)
+ volumePruneReport, err := ic.VolumePrune(ctx, volumePruneOptions)
if err != nil {
return nil, err
}
diff --git a/pkg/domain/infra/tunnel/system.go b/pkg/domain/infra/tunnel/system.go
index 1e4b2e0b4..9013d44c2 100644
--- a/pkg/domain/infra/tunnel/system.go
+++ b/pkg/domain/infra/tunnel/system.go
@@ -20,7 +20,7 @@ func (ic *ContainerEngine) SetupRootless(_ context.Context, cmd *cobra.Command)
// SystemPrune prunes unused data from the system.
func (ic *ContainerEngine) SystemPrune(ctx context.Context, opts entities.SystemPruneOptions) (*entities.SystemPruneReport, error) {
- options := new(system.PruneOptions).WithAll(opts.All).WithVolumes(opts.Volume).WithFilters(opts.ContainerPruneOptions.Filters)
+ options := new(system.PruneOptions).WithAll(opts.All).WithVolumes(opts.Volume).WithFilters(opts.Filters)
return system.Prune(ic.ClientCxt, options)
}