Skip to content

Commit

Permalink
libimage: prune: allow for removing external containers
Browse files Browse the repository at this point in the history
Support pruning images in use by external containers (e.g., build
containers).  Requires extending the containers filter, adding a
callback to libpod and a new options for image removal.

Tests will be added to Podman.

Signed-off-by: Valentin Rothberg <[email protected]>
  • Loading branch information
vrothberg committed Sep 24, 2021
1 parent 458d21c commit 9684b33
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 26 deletions.
38 changes: 29 additions & 9 deletions libimage/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ func filterImages(images []*Image, filters []filterFunc) ([]*Image, error) {
// compileImageFilters creates `filterFunc`s for the specified filters. The
// required format is `key=value` with the following supported keys:
// after, since, before, containers, dangling, id, label, readonly, reference, intermediate
func (r *Runtime) compileImageFilters(ctx context.Context, filters []string) ([]filterFunc, error) {
logrus.Tracef("Parsing image filters %s", filters)
func (r *Runtime) compileImageFilters(ctx context.Context, options *ListImagesOptions) ([]filterFunc, error) {
logrus.Tracef("Parsing image filters %s", options.Filters)

filterFuncs := []filterFunc{}
for _, filter := range filters {
for _, filter := range options.Filters {
var key, value string
split := strings.SplitN(filter, "=", 2)
if len(split) != 2 {
Expand All @@ -77,11 +77,16 @@ func (r *Runtime) compileImageFilters(ctx context.Context, filters []string) ([]
filterFuncs = append(filterFuncs, filterBefore(img.Created()))

case "containers":
containers, err := strconv.ParseBool(value)
if err != nil {
return nil, errors.Wrapf(err, "non-boolean value %q for dangling filter", value)
switch value {
case "false", "true":
case "external":
if options.IsExternalContainerFunc == nil {
return nil, fmt.Errorf("libimage error: external containers filter without callback")
}
default:
return nil, fmt.Errorf("unsupported value %q for containers filter", value)
}
filterFuncs = append(filterFuncs, filterContainers(containers))
filterFuncs = append(filterFuncs, filterContainers(value, options.IsExternalContainerFunc))

case "dangling":
dangling, err := strconv.ParseBool(value)
Expand Down Expand Up @@ -190,13 +195,28 @@ func filterReadOnly(value bool) filterFunc {
}

// filterContainers creates a container filter for matching the specified value.
func filterContainers(value bool) filterFunc {
func filterContainers(value string, fn IsExternalContainerFunc) filterFunc {
return func(img *Image) (bool, error) {
ctrs, err := img.Containers()
if err != nil {
return false, err
}
return (len(ctrs) > 0) == value, nil
if value != "external" {
boolValue := value == "true"
return (len(ctrs) > 0) == boolValue, nil
}

// Check whether all associated containers are external ones.
for _, c := range ctrs {
isExternal, err := fn(c)
if err != nil {
return false, fmt.Errorf("checking if %s is an external container in filter: %w", c, err)
}
if !isExternal {
return isExternal, nil
}
}
return true, nil
}
}

Expand Down
41 changes: 29 additions & 12 deletions libimage/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package libimage

import (
"context"
"fmt"
"path/filepath"
"sort"
"strings"
Expand Down Expand Up @@ -232,20 +233,38 @@ func (i *Image) Containers() ([]string, error) {
}

// removeContainers removes all containers using the image.
func (i *Image) removeContainers(fn RemoveContainerFunc) error {
// Execute the custom removal func if specified.
if fn != nil {
logrus.Debugf("Removing containers of image %s with custom removal function", i.ID())
if err := fn(i.ID()); err != nil {
return err
}
func (i *Image) removeContainers(options *RemoveImagesOptions) error {
if !options.Force && !options.ExternalContainers {
// Nothing to do.
return nil
}

containers, err := i.Containers()
if err != nil {
return err
}

if options.Force {
// Execute the custom removal func if specified.
if options.RemoveContainerFunc != nil {
logrus.Debugf("Removing containers of image %s with custom removal function", i.ID())
if err := options.RemoveContainerFunc(i.ID()); err != nil {
return err
}
}
} else if options.ExternalContainers {
// All containers must be external ones,.
for _, cID := range containers {
isExternal, err := options.IsExternalContainerFunc(cID)
if err != nil {
return fmt.Errorf("checking if %s is an external container: %w", cID, err)
}
if !isExternal {
return fmt.Errorf("cannot remove container %s: not an external container", cID)
}
}
}

logrus.Debugf("Removing containers of image %s from the local containers storage", i.ID())
var multiE error
for _, cID := range containers {
Expand Down Expand Up @@ -392,11 +411,9 @@ func (i *Image) removeRecursive(ctx context.Context, rmMap map[string]*RemoveIma
return processedIDs, nil
}

// Perform the actual removal. First, remove containers if needed.
if options.Force {
if err := i.removeContainers(options.RemoveContainerFunc); err != nil {
return processedIDs, err
}
// Perform the container removal, if needed.
if err := i.removeContainers(options); err != nil {
return processedIDs, err
}

// Podman/Docker compat: we only report an image as removed if it has
Expand Down
9 changes: 6 additions & 3 deletions libimage/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ func TestImageFunctions(t *testing.T) {

// Since we have no containers here, we can only smoke test.
require.NoError(t, image.removeContainers(nil))
require.Error(t, image.removeContainers(func(_ string) error {
return errors.New("TEST")
}))
rmOptions := &RemoveImagesOptions{
RemoveContainerFunc: func(_ string) error {
return errors.New("TEST")
},
}
require.Error(t, image.removeContainers(rmOptions))

// Two items since both names are "Named".
namedRepoTags, err := image.NamedRepoTags()
Expand Down
33 changes: 31 additions & 2 deletions libimage/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package libimage

import (
"context"
"fmt"
"os"
"strings"

Expand Down Expand Up @@ -484,17 +485,28 @@ func (r *Runtime) imageReferenceMatchesContext(ref types.ImageReference, options
return true, nil
}

// IsExternalContainerFunc allows for checking whether the specified container
// is an external one. The definition of an external container can be set by
// callers.
type IsExternalContainerFunc func(containerID string) (bool, error)

// ListImagesOptions allow for customizing listing images.
type ListImagesOptions struct {
// Filters to filter the listed images. Supported filters are
// * after,before,since=image
// * containers=true,false,external
// * dangling=true,false
// * intermediate=true,false (useful for pruning images)
// * id=id
// * label=key[=value]
// * readonly=true,false
// * reference=name[:tag] (wildcards allowed)
Filters []string
// IsExternalContainerFunc allows for checking whether the specified
// container is an external one (when containers=external filter is
// used). The definition of an external container can be set by
// callers.
IsExternalContainerFunc IsExternalContainerFunc
}

// ListImages lists images in the local container storage. If names are
Expand Down Expand Up @@ -525,7 +537,7 @@ func (r *Runtime) ListImages(ctx context.Context, names []string, options *ListI

var filters []filterFunc
if len(options.Filters) > 0 {
compiledFilters, err := r.compileImageFilters(ctx, options.Filters)
compiledFilters, err := r.compileImageFilters(ctx, options)
if err != nil {
return nil, err
}
Expand All @@ -550,8 +562,17 @@ type RemoveImagesOptions struct {
// containers using a specific image. By default, all containers in
// the local containers storage will be removed (if Force is set).
RemoveContainerFunc RemoveContainerFunc
// IsExternalContainerFunc allows for checking whether the specified
// container is an external one (when containers=external filter is
// used). The definition of an external container can be set by
// callers.
IsExternalContainerFunc IsExternalContainerFunc
// Remove external containers even when Force is false. Requires
// IsExternalContainerFunc to be specified.
ExternalContainers bool
// Filters to filter the removed images. Supported filters are
// * after,before,since=image
// * containers=true,false,external
// * dangling=true,false
// * intermediate=true,false (useful for pruning images)
// * id=id
Expand Down Expand Up @@ -581,6 +602,10 @@ func (r *Runtime) RemoveImages(ctx context.Context, names []string, options *Rem
options = &RemoveImagesOptions{}
}

if options.ExternalContainers && options.IsExternalContainerFunc == nil {
return nil, []error{fmt.Errorf("libimage error: cannot remove external containers without callback")}
}

// The logic here may require some explanation. Image removal is
// surprisingly complex since it is recursive (intermediate parents are
// removed) and since multiple items in `names` may resolve to the
Expand Down Expand Up @@ -635,7 +660,11 @@ func (r *Runtime) RemoveImages(ctx context.Context, names []string, options *Rem
}

default:
filteredImages, err := r.ListImages(ctx, nil, &ListImagesOptions{Filters: options.Filters})
options := &ListImagesOptions{
IsExternalContainerFunc: options.IsExternalContainerFunc,
Filters: options.Filters,
}
filteredImages, err := r.ListImages(ctx, nil, options)
if err != nil {
appendError(err)
return nil, rmErrors
Expand Down

0 comments on commit 9684b33

Please sign in to comment.