1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
package image
import (
"context"
"strings"
"time"
"github.com/containers/podman/v2/libpod/events"
"github.com/containers/podman/v2/pkg/timetype"
"github.com/containers/storage"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
func generatePruneFilterFuncs(filter, filterValue string) (ImageFilter, error) {
switch filter {
case "label":
var filterArray = strings.SplitN(filterValue, "=", 2)
var filterKey = filterArray[0]
if len(filterArray) > 1 {
filterValue = filterArray[1]
} else {
filterValue = ""
}
return func(i *Image) bool {
labels, err := i.Labels(context.Background())
if err != nil {
return false
}
for labelKey, labelValue := range labels {
if labelKey == filterKey && ("" == filterValue || labelValue == filterValue) {
return true
}
}
return false
}, nil
case "until":
ts, err := timetype.GetTimestamp(filterValue, time.Now())
if err != nil {
return nil, err
}
seconds, nanoseconds, err := timetype.ParseTimestamps(ts, 0)
if err != nil {
return nil, err
}
until := time.Unix(seconds, nanoseconds)
return func(i *Image) bool {
if !until.IsZero() && i.Created().After((until)) {
return true
}
return false
}, nil
}
return nil, nil
}
// GetPruneImages returns a slice of images that have no names/unused
func (ir *Runtime) GetPruneImages(ctx context.Context, all bool, filterFuncs []ImageFilter) ([]*Image, error) {
var (
pruneImages []*Image
)
allImages, err := ir.GetRWImages()
if err != nil {
return nil, err
}
tree, err := ir.layerTree()
if err != nil {
return nil, err
}
for _, i := range allImages {
// filter the images based on this.
for _, filterFunc := range filterFuncs {
if !filterFunc(i) {
continue
}
}
if all {
containers, err := i.Containers()
if err != nil {
return nil, err
}
if len(containers) < 1 {
pruneImages = append(pruneImages, i)
continue
}
}
// skip the cache (i.e., with parent) and intermediate (i.e.,
// with children) images
intermediate, err := tree.hasChildrenAndParent(ctx, i)
if err != nil {
return nil, err
}
if intermediate {
continue
}
if i.Dangling() {
pruneImages = append(pruneImages, i)
}
}
return pruneImages, nil
}
// PruneImages prunes dangling and optionally all unused images from the local
// image store
func (ir *Runtime) PruneImages(ctx context.Context, all bool, filter []string) ([]string, error) {
filterFuncs := make([]ImageFilter, 0, len(filter))
for _, f := range filter {
filterSplit := strings.SplitN(f, "=", 2)
if len(filterSplit) < 2 {
return nil, errors.Errorf("filter input must be in the form of filter=value: %s is invalid", f)
}
generatedFunc, err := generatePruneFilterFuncs(filterSplit[0], filterSplit[1])
if err != nil {
return nil, errors.Wrapf(err, "invalid filter")
}
filterFuncs = append(filterFuncs, generatedFunc)
}
pruned := []string{}
prev := 0
for {
toPrune, err := ir.GetPruneImages(ctx, all, filterFuncs)
if err != nil {
return nil, errors.Wrap(err, "unable to get images to prune")
}
numImages := len(toPrune)
if numImages == 0 || numImages == prev {
// If there's nothing left to do, return.
break
}
prev = numImages
for _, img := range toPrune {
repotags, err := img.RepoTags()
if err != nil {
return nil, err
}
if err := img.Remove(ctx, false); err != nil {
if errors.Cause(err) == storage.ErrImageUsedByContainer {
logrus.Warnf("Failed to prune image %s as it is in use: %v.\nA container associated with containers/storage (e.g., Buildah, CRI-O, etc.) maybe associated with this image.\nUsing the rmi command with the --force option will remove the container and image, but may cause failures for other dependent systems.", img.ID(), err)
continue
}
return nil, errors.Wrap(err, "failed to prune image")
}
defer img.newImageEvent(events.Prune)
nameOrID := img.ID()
if len(repotags) > 0 {
nameOrID = repotags[0]
}
pruned = append(pruned, nameOrID)
}
}
return pruned, nil
}
|