Skip to content

Commit c77b45b

Browse files
committed
When checking IsDangling make sure image is not in manifest list
Currently when we run podman image prune or podman images --filter dangling It is pruning images that are in a local manifest. These images are not dangling because they are currently in use by a named manifest list. You can create this situation simply by doing echo "from scratch" > /tmp/Containerfile id=$(podman build /tmp) podman manifest create test $id podman image prune --force podman image exists $id Will return an error since the image was pruned. Now the local manifest test is broken. Signed-off-by: Daniel J Walsh <[email protected]>
1 parent 9c66f45 commit c77b45b

File tree

5 files changed

+31
-12
lines changed

5 files changed

+31
-12
lines changed

libimage/history.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (i *Image) History(ctx context.Context) ([]ImageHistory, error) {
2525
return nil, err
2626
}
2727

28-
layerTree, err := i.runtime.newFreshLayerTree()
28+
layerTree, err := i.runtime.newFreshLayerTree(ctx)
2929
if err != nil {
3030
return nil, err
3131
}

libimage/image.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ func (i *Image) isDangling(ctx context.Context, tree *layerTree) (bool, error) {
206206
if err != nil {
207207
return false, err
208208
}
209-
return len(children) == 0, nil
209+
return (len(children) == 0 && !tree.manifestListDigests[i.Digest()]), nil
210210
}
211211

212212
// IsIntermediate returns true if the image is an intermediate image, that is
@@ -258,7 +258,7 @@ func (i *Image) TopLayer() string {
258258

259259
// Parent returns the parent image or nil if there is none
260260
func (i *Image) Parent(ctx context.Context) (*Image, error) {
261-
tree, err := i.runtime.newFreshLayerTree()
261+
tree, err := i.runtime.newFreshLayerTree(ctx)
262262
if err != nil {
263263
return nil, err
264264
}
@@ -292,7 +292,7 @@ func (i *Image) Children(ctx context.Context) ([]*Image, error) {
292292
// created for this invocation only.
293293
func (i *Image) getChildren(ctx context.Context, all bool, tree *layerTree) ([]*Image, error) {
294294
if tree == nil {
295-
t, err := i.runtime.newFreshLayerTree()
295+
t, err := i.runtime.newFreshLayerTree(ctx)
296296
if err != nil {
297297
return nil, err
298298
}

libimage/image_tree.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package libimage
44

55
import (
6+
"context"
67
"fmt"
78
"strings"
89

@@ -13,7 +14,7 @@ import (
1314
// Tree generates a tree for the specified image and its layers. Use
1415
// `traverseChildren` to traverse the layers of all children. By default, only
1516
// layers of the image are printed.
16-
func (i *Image) Tree(traverseChildren bool) (string, error) {
17+
func (i *Image) Tree(ctx context.Context, traverseChildren bool) (string, error) {
1718
// NOTE: a string builder prevents us from copying to much data around
1819
// and compile the string when and where needed.
1920
sb := &strings.Builder{}
@@ -37,7 +38,7 @@ func (i *Image) Tree(traverseChildren bool) (string, error) {
3738
fmt.Fprintf(sb, "No Image Layers")
3839
}
3940

40-
layerTree, err := i.runtime.newFreshLayerTree()
41+
layerTree, err := i.runtime.newFreshLayerTree(ctx)
4142
if err != nil {
4243
return "", err
4344
}

libimage/layer_tree.go

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/containers/storage"
1010
storageTypes "github.com/containers/storage/types"
11+
digest "github.com/opencontainers/go-digest"
1112
ociv1 "github.com/opencontainers/image-spec/specs-go/v1"
1213
"github.com/sirupsen/logrus"
1314
)
@@ -22,6 +23,10 @@ type layerTree struct {
2223
// emptyImages do not have any top-layer so we cannot create a
2324
// *layerNode for them.
2425
emptyImages []*Image
26+
// manifestList keep track of images based on their digest.
27+
// Library will use this map when checking if a image is dangling.
28+
// If an image is used in a manifestList it is NOT dangling
29+
manifestListDigests map[digest.Digest]bool
2530
}
2631

2732
// node returns a layerNode for the specified layerID.
@@ -90,20 +95,21 @@ func (l *layerNode) repoTags() ([]string, error) {
9095
}
9196

9297
// newFreshLayerTree extracts a layerTree from consistent layers and images in the local storage.
93-
func (r *Runtime) newFreshLayerTree() (*layerTree, error) {
98+
func (r *Runtime) newFreshLayerTree(ctx context.Context) (*layerTree, error) {
9499
images, layers, err := r.getImagesAndLayers()
95100
if err != nil {
96101
return nil, err
97102
}
98-
return r.newLayerTreeFromData(images, layers)
103+
return r.newLayerTreeFromData(ctx, images, layers)
99104
}
100105

101106
// newLayerTreeFromData extracts a layerTree from the given the layers and images.
102107
// The caller is responsible for (layers, images) being consistent.
103-
func (r *Runtime) newLayerTreeFromData(images []*Image, layers []storage.Layer) (*layerTree, error) {
108+
func (r *Runtime) newLayerTreeFromData(ctx context.Context, images []*Image, layers []storage.Layer) (*layerTree, error) {
104109
tree := layerTree{
105-
nodes: make(map[string]*layerNode),
106-
ociCache: make(map[string]*ociv1.Image),
110+
nodes: make(map[string]*layerNode),
111+
ociCache: make(map[string]*ociv1.Image),
112+
manifestListDigests: make(map[digest.Digest]bool),
107113
}
108114

109115
// First build a tree purely based on layer information.
@@ -124,6 +130,18 @@ func (r *Runtime) newLayerTreeFromData(images []*Image, layers []storage.Layer)
124130
topLayer := img.TopLayer()
125131
if topLayer == "" {
126132
tree.emptyImages = append(tree.emptyImages, img)
133+
// When img is a manifest list, cache the lists of
134+
// digests refereenced in manifest list. Digests can
135+
// be used to check for dangling images.
136+
if manifestList, _ := img.IsManifestList(ctx); manifestList {
137+
mlist, err := img.ToManifestList()
138+
if err != nil {
139+
return nil, err
140+
}
141+
for _, digest := range mlist.list.Instances() {
142+
tree.manifestListDigests[digest] = true
143+
}
144+
}
127145
continue
128146
}
129147
node, exists := tree.nodes[topLayer]

libimage/runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ func (r *Runtime) ListImages(ctx context.Context, options *ListImagesOptions) ([
634634

635635
var tree *layerTree
636636
if needsLayerTree {
637-
tree, err = r.newLayerTreeFromData(images, snapshot.Layers)
637+
tree, err = r.newLayerTreeFromData(ctx, images, snapshot.Layers)
638638
if err != nil {
639639
return nil, err
640640
}

0 commit comments

Comments
 (0)