2014-08-05 01:02:51 -04:00
|
|
|
package graph
|
|
|
|
|
|
|
|
import (
|
2015-03-25 03:44:12 -04:00
|
|
|
"fmt"
|
2014-08-05 01:02:51 -04:00
|
|
|
"path"
|
2015-04-03 20:39:06 -04:00
|
|
|
"sort"
|
2014-08-05 01:02:51 -04:00
|
|
|
"strings"
|
|
|
|
|
2015-07-02 10:11:52 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-04-03 20:39:06 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-07-20 13:57:15 -04:00
|
|
|
"github.com/docker/docker/image"
|
2014-08-05 01:02:51 -04:00
|
|
|
"github.com/docker/docker/pkg/parsers/filters"
|
2015-02-26 21:23:50 -05:00
|
|
|
"github.com/docker/docker/utils"
|
2014-08-05 01:02:51 -04:00
|
|
|
)
|
|
|
|
|
2015-01-06 19:04:10 -05:00
|
|
|
var acceptedImageFilterTags = map[string]struct{}{
|
|
|
|
"dangling": {},
|
|
|
|
"label": {},
|
|
|
|
}
|
2015-01-16 16:49:46 -05:00
|
|
|
|
2015-07-29 19:45:47 -04:00
|
|
|
// byCreated is a temporary type used to sort a list of images by creation
|
|
|
|
// time.
|
2015-07-21 12:21:45 -04:00
|
|
|
type byCreated []*types.Image
|
2015-04-03 20:39:06 -04:00
|
|
|
|
2015-07-21 12:21:45 -04:00
|
|
|
func (r byCreated) Len() int { return len(r) }
|
|
|
|
func (r byCreated) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
|
|
|
func (r byCreated) Less(i, j int) bool { return r[i].Created < r[j].Created }
|
2015-04-03 20:39:06 -04:00
|
|
|
|
2015-07-29 19:45:47 -04:00
|
|
|
// Images returns a filtered list of images. filterArgs is a JSON-encoded set
|
|
|
|
// of filter arguments which will be interpreted by pkg/parsers/filters.
|
|
|
|
// filter is a shell glob string applied to repository names. The argument
|
|
|
|
// named all controls whether all images in the graph are filtered, or just
|
|
|
|
// the heads.
|
|
|
|
func (s *TagStore) Images(filterArgs, filter string, all bool) ([]*types.Image, error) {
|
2014-08-05 01:02:51 -04:00
|
|
|
var (
|
2015-07-20 13:57:15 -04:00
|
|
|
allImages map[string]*image.Image
|
2015-03-25 21:40:23 -04:00
|
|
|
err error
|
|
|
|
filtTagged = true
|
|
|
|
filtLabel = false
|
2014-08-05 01:02:51 -04:00
|
|
|
)
|
|
|
|
|
2015-07-29 19:45:47 -04:00
|
|
|
imageFilters, err := filters.FromParam(filterArgs)
|
2014-08-05 01:02:51 -04:00
|
|
|
if err != nil {
|
2015-04-07 21:57:54 -04:00
|
|
|
return nil, err
|
2014-08-05 01:02:51 -04:00
|
|
|
}
|
2015-01-16 16:49:46 -05:00
|
|
|
for name := range imageFilters {
|
|
|
|
if _, ok := acceptedImageFilterTags[name]; !ok {
|
2015-04-07 21:57:54 -04:00
|
|
|
return nil, fmt.Errorf("Invalid filter '%s'", name)
|
2015-01-16 16:49:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 01:02:51 -04:00
|
|
|
if i, ok := imageFilters["dangling"]; ok {
|
|
|
|
for _, value := range i {
|
|
|
|
if strings.ToLower(value) == "true" {
|
2015-03-25 21:40:23 -04:00
|
|
|
filtTagged = false
|
2014-08-05 01:02:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 21:40:23 -04:00
|
|
|
_, filtLabel = imageFilters["label"]
|
2015-01-06 19:04:10 -05:00
|
|
|
|
2015-07-29 19:45:47 -04:00
|
|
|
if all && filtTagged {
|
2015-06-19 11:01:39 -04:00
|
|
|
allImages = s.graph.Map()
|
2014-08-05 01:02:51 -04:00
|
|
|
} else {
|
2015-06-19 11:01:39 -04:00
|
|
|
allImages = s.graph.Heads()
|
2014-08-05 01:02:51 -04:00
|
|
|
}
|
2015-04-03 20:39:06 -04:00
|
|
|
|
|
|
|
lookup := make(map[string]*types.Image)
|
2014-08-05 01:02:51 -04:00
|
|
|
s.Lock()
|
2015-02-26 21:23:50 -05:00
|
|
|
for repoName, repository := range s.Repositories {
|
2015-07-29 19:45:47 -04:00
|
|
|
if filter != "" {
|
|
|
|
if match, _ := path.Match(filter, repoName); !match {
|
2014-08-05 01:02:51 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2015-02-26 21:23:50 -05:00
|
|
|
for ref, id := range repository {
|
|
|
|
imgRef := utils.ImageReference(repoName, ref)
|
2014-08-05 01:02:51 -04:00
|
|
|
image, err := s.graph.Get(id)
|
|
|
|
if err != nil {
|
2015-07-02 10:11:52 -04:00
|
|
|
logrus.Warnf("couldn't load %s from %s: %s", id, imgRef, err)
|
2014-08-05 01:02:51 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-04-03 20:39:06 -04:00
|
|
|
if lImage, exists := lookup[id]; exists {
|
2015-03-25 21:40:23 -04:00
|
|
|
if filtTagged {
|
2015-02-26 21:23:50 -05:00
|
|
|
if utils.DigestReference(ref) {
|
2015-04-03 20:39:06 -04:00
|
|
|
lImage.RepoDigests = append(lImage.RepoDigests, imgRef)
|
2015-02-26 21:23:50 -05:00
|
|
|
} else { // Tag Ref.
|
2015-04-03 20:39:06 -04:00
|
|
|
lImage.RepoTags = append(lImage.RepoTags, imgRef)
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
2014-08-05 01:02:51 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// get the boolean list for if only the untagged images are requested
|
|
|
|
delete(allImages, id)
|
2015-01-06 19:04:10 -05:00
|
|
|
if !imageFilters.MatchKVList("label", image.ContainerConfig.Labels) {
|
|
|
|
continue
|
|
|
|
}
|
2015-03-25 21:40:23 -04:00
|
|
|
if filtTagged {
|
2015-04-03 20:39:06 -04:00
|
|
|
newImage := new(types.Image)
|
2015-07-23 05:40:54 -04:00
|
|
|
newImage.ParentID = image.Parent
|
2015-04-03 20:39:06 -04:00
|
|
|
newImage.ID = image.ID
|
2015-07-23 17:19:58 -04:00
|
|
|
newImage.Created = image.Created.Unix()
|
|
|
|
newImage.Size = image.Size
|
|
|
|
newImage.VirtualSize = s.graph.GetParentsSize(image) + image.Size
|
2015-04-03 20:39:06 -04:00
|
|
|
newImage.Labels = image.ContainerConfig.Labels
|
2015-02-26 21:23:50 -05:00
|
|
|
|
|
|
|
if utils.DigestReference(ref) {
|
2015-04-03 20:39:06 -04:00
|
|
|
newImage.RepoTags = []string{}
|
|
|
|
newImage.RepoDigests = []string{imgRef}
|
2015-02-26 21:23:50 -05:00
|
|
|
} else {
|
2015-04-03 20:39:06 -04:00
|
|
|
newImage.RepoTags = []string{imgRef}
|
|
|
|
newImage.RepoDigests = []string{}
|
2015-02-26 21:23:50 -05:00
|
|
|
}
|
|
|
|
|
2015-04-03 20:39:06 -04:00
|
|
|
lookup[id] = newImage
|
2014-08-05 01:02:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s.Unlock()
|
|
|
|
|
2015-04-03 20:39:06 -04:00
|
|
|
images := []*types.Image{}
|
2014-08-05 01:02:51 -04:00
|
|
|
for _, value := range lookup {
|
2015-04-03 20:39:06 -04:00
|
|
|
images = append(images, value)
|
2014-08-05 01:02:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Display images which aren't part of a repository/tag
|
2015-07-29 19:45:47 -04:00
|
|
|
if filter == "" || filtLabel {
|
2014-08-05 01:02:51 -04:00
|
|
|
for _, image := range allImages {
|
2015-01-06 19:04:10 -05:00
|
|
|
if !imageFilters.MatchKVList("label", image.ContainerConfig.Labels) {
|
|
|
|
continue
|
|
|
|
}
|
2015-04-03 20:39:06 -04:00
|
|
|
newImage := new(types.Image)
|
2015-07-23 05:40:54 -04:00
|
|
|
newImage.ParentID = image.Parent
|
2015-04-03 20:39:06 -04:00
|
|
|
newImage.RepoTags = []string{"<none>:<none>"}
|
|
|
|
newImage.RepoDigests = []string{"<none>@<none>"}
|
|
|
|
newImage.ID = image.ID
|
2015-07-23 17:19:58 -04:00
|
|
|
newImage.Created = image.Created.Unix()
|
|
|
|
newImage.Size = image.Size
|
|
|
|
newImage.VirtualSize = s.graph.GetParentsSize(image) + image.Size
|
2015-04-03 20:39:06 -04:00
|
|
|
newImage.Labels = image.ContainerConfig.Labels
|
|
|
|
|
|
|
|
images = append(images, newImage)
|
2014-08-05 01:02:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 12:21:45 -04:00
|
|
|
sort.Sort(sort.Reverse(byCreated(images)))
|
2015-04-03 20:39:06 -04:00
|
|
|
|
2015-04-07 21:57:54 -04:00
|
|
|
return images, nil
|
2014-08-05 01:02:51 -04:00
|
|
|
}
|