Skip to content

Commit

Permalink
Merge pull request #23846 from flouthoc/prune-cleancachemount
Browse files Browse the repository at this point in the history
prune: add `--build-cache` to support clearing build cache using `CleanCacheMount`
  • Loading branch information
openshift-merge-bot[bot] authored Sep 5, 2024
2 parents 858b25d + 901f621 commit 094fed7
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 9 deletions.
1 change: 1 addition & 0 deletions cmd/podman/images/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func init() {

flags := pruneCmd.Flags()
flags.BoolVarP(&pruneOpts.All, "all", "a", false, "Remove all images not in use by containers, not just dangling ones")
flags.BoolVarP(&pruneOpts.BuildCache, "build-cache", "", false, "Remove persistent build cache created by --mount=type=cache")
flags.BoolVarP(&pruneOpts.External, "external", "", false, "Remove images even when they are used by external containers (e.g., by build containers)")
flags.BoolVarP(&force, "force", "f", false, "Do not prompt for confirmation")

Expand Down
4 changes: 4 additions & 0 deletions docs/source/markdown/podman-image-prune.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ The image prune command does not prune cache images that only use layers that ar

Remove dangling images and images that have no associated containers.

#### **--build-cache**

Remove persistent build cache create for `--mount=type=cache`.

#### **--external**

Remove images even when they are used by external containers (e.g., build containers).
Expand Down
12 changes: 7 additions & 5 deletions pkg/api/handlers/libpod/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ func PruneImages(w http.ResponseWriter, r *http.Request) {
runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
decoder := r.Context().Value(api.DecoderKey).(*schema.Decoder)
query := struct {
All bool `schema:"all"`
External bool `schema:"external"`
All bool `schema:"all"`
External bool `schema:"external"`
BuildCache bool `schema:"buildcache"`
}{
// override any golang type defaults
}
Expand Down Expand Up @@ -157,9 +158,10 @@ func PruneImages(w http.ResponseWriter, r *http.Request) {
imageEngine := abi.ImageEngine{Libpod: runtime}

pruneOptions := entities.ImagePruneOptions{
All: query.All,
External: query.External,
Filter: libpodFilters,
All: query.All,
External: query.External,
Filter: libpodFilters,
BuildCache: query.BuildCache,
}
imagePruneReports, err := imageEngine.Prune(r.Context(), pruneOptions)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions pkg/api/server/register_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,12 @@ func (s *APIServer) registerImagesHandlers(r *mux.Router) error {
// description: |
// Remove images even when they are used by external containers (e.g, by build containers)
// - in: query
// name: buildcache
// default: false
// type: boolean
// description: |
// Remove persistent build cache created by build instructions such as `--mount=type=cache`.
// - in: query
// name: filters
// type: string
// description: |
Expand Down
2 changes: 2 additions & 0 deletions pkg/bindings/images/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ type PruneOptions struct {
All *bool
// Prune images even when they're used by external containers
External *bool
// Prune persistent build cache
BuildCache *bool
// Filters to apply when pruning images
Filters map[string][]string
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/bindings/images/types_prune_options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions pkg/domain/entities/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,10 @@ type ImageListOptions struct {
}

type ImagePruneOptions struct {
All bool `json:"all" schema:"all"`
External bool `json:"external" schema:"external"`
Filter []string `json:"filter" schema:"filter"`
All bool `json:"all" schema:"all"`
External bool `json:"external" schema:"external"`
BuildCache bool `json:"buildcache" schema:"buildcache"`
Filter []string `json:"filter" schema:"filter"`
}

type ImageTagOptions struct{}
Expand Down
8 changes: 8 additions & 0 deletions pkg/domain/infra/abi/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"time"

bdefine "github.com/containers/buildah/define"
"github.com/containers/buildah/pkg/volumes"
"github.com/containers/common/libimage"
"github.com/containers/common/libimage/filter"
"github.com/containers/common/pkg/config"
Expand Down Expand Up @@ -107,6 +108,13 @@ func (ir *ImageEngine) Prune(ctx context.Context, opts entities.ImagePruneOption
numPreviouslyRemovedImages = numRemovedImages
}

if opts.BuildCache || opts.All {
// Clean build cache if any
if err := volumes.CleanCacheMount(); err != nil {
return nil, err
}
}

return pruneReports, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/infra/tunnel/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (ir *ImageEngine) Prune(ctx context.Context, opts entities.ImagePruneOption
f := strings.Split(filter, "=")
filters[f[0]] = f[1:]
}
options := new(images.PruneOptions).WithAll(opts.All).WithFilters(filters).WithExternal(opts.External)
options := new(images.PruneOptions).WithAll(opts.All).WithFilters(filters).WithExternal(opts.External).WithBuildCache(opts.BuildCache)
reports, err := images.Prune(ir.ClientCtx, options)
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions test/e2e/build/buildkit-mount/Containerfilecacheread
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM alpine
RUN mkdir /test
# use option z if selinux is enabled
RUN --mount=type=cache,target=/test,z cat /test/world
4 changes: 4 additions & 0 deletions test/e2e/build/buildkit-mount/Containerfilecachewrite
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM alpine
RUN mkdir /test
# use option z if selinux is enabled
RUN --mount=type=cache,target=/test,z echo hello > /test/world
31 changes: 31 additions & 0 deletions test/e2e/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,37 @@ var _ = Describe("Podman build", func() {
Expect(session).Should(ExitCleanly())
})

It("podman image prune should clean build cache", Serial, func() {
// try writing something to persistent cache
session := podmanTest.Podman([]string{"build", "-f", "build/buildkit-mount/Containerfilecachewrite"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())

// try reading something from persistent cache
session = podmanTest.Podman([]string{"build", "-f", "build/buildkit-mount/Containerfilecacheread"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
Expect(session.OutputToString()).To(ContainSubstring("hello"))

// Prune build cache
session = podmanTest.Podman([]string{"image", "prune", "-f", "--build-cache"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())

expectedErr := "open '/test/world': No such file or directory"
// try reading something from persistent cache should fail
session = podmanTest.Podman([]string{"build", "-f", "build/buildkit-mount/Containerfilecacheread"})
session.WaitWithDefaultTimeout()
if IsRemote() {
// In the case of podman remote the error from build is not being propogated to `stderr` instead it appears
// on the `stdout` this could be a potential bug in `remote build` which needs to be fixed and visited.
Expect(session.OutputToString()).To(ContainSubstring(expectedErr))
Expect(session).Should(ExitWithError(1, "exit status 1"))
} else {
Expect(session).Should(ExitWithError(1, expectedErr))
}
})

It("podman build and remove basic alpine with TMPDIR as relative", func() {
// preserve TMPDIR if it was originally set
if cacheDir, found := os.LookupEnv("TMPDIR"); found {
Expand Down

1 comment on commit 094fed7

@packit-as-a-service
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

podman-next COPR build failed. @containers/packit-build please check.

Please sign in to comment.