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 {
|
2015-10-16 10:09:32 -04:00
|
|
|
if v := strings.ToLower(value); v == "true" {
|
2015-03-25 21:40:23 -04:00
|
|
|
filtTagged = false
|
2015-10-16 10:09:32 -04:00
|
|
|
} else if v != "false" {
|
|
|
|
return nil, fmt.Errorf("Invalid filter 'dangling=%s'", v)
|
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-11-08 15:30:31 -05: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-08-08 09:58:48 -04:00
|
|
|
filterTagName := ""
|
2015-07-29 19:45:47 -04:00
|
|
|
if filter != "" {
|
2015-08-08 09:58:48 -04:00
|
|
|
filterName := filter
|
|
|
|
// Test if the tag was in there, if yes, get the name
|
|
|
|
if strings.Contains(filterName, ":") {
|
|
|
|
filterWithTag := strings.Split(filter, ":")
|
|
|
|
filterName = filterWithTag[0]
|
|
|
|
filterTagName = filterWithTag[1]
|
|
|
|
}
|
|
|
|
if match, _ := path.Match(filterName, repoName); !match {
|
2014-08-05 01:02:51 -04:00
|
|
|
continue
|
|
|
|
}
|
2015-08-08 09:58:48 -04:00
|
|
|
if filterTagName != "" {
|
|
|
|
if _, ok := repository[filterTagName]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2014-08-05 01:02:51 -04:00
|
|
|
}
|
2015-02-26 21:23:50 -05:00
|
|
|
for ref, id := range repository {
|
|
|
|
imgRef := utils.ImageReference(repoName, ref)
|
2015-08-08 09:58:48 -04:00
|
|
|
if !strings.Contains(imgRef, filterTagName) {
|
|
|
|
continue
|
|
|
|
}
|
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-08-22 08:06:48 -04:00
|
|
|
|
|
|
|
if len(imageFilters["label"]) > 0 {
|
|
|
|
if image.Config == nil {
|
|
|
|
// Very old image that do not have image.Config (or even labels)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// We are now sure image.Config is not nil
|
|
|
|
if !imageFilters.MatchKVList("label", image.Config.Labels) {
|
|
|
|
continue
|
|
|
|
}
|
2015-01-06 19:04:10 -05:00
|
|
|
}
|
2015-03-25 21:40:23 -04:00
|
|
|
if filtTagged {
|
2015-11-08 15:30:31 -05:00
|
|
|
newImage := newImage(image, s.graph.getParentsSize(image))
|
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-08-22 08:06:48 -04:00
|
|
|
if len(imageFilters["label"]) > 0 {
|
|
|
|
if image.Config == nil {
|
|
|
|
// Very old image that do not have image.Config (or even labels)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// We are now sure image.Config is not nil
|
|
|
|
if !imageFilters.MatchKVList("label", image.Config.Labels) {
|
|
|
|
continue
|
|
|
|
}
|
2015-01-06 19:04:10 -05:00
|
|
|
}
|
2015-11-08 15:30:31 -05:00
|
|
|
newImage := newImage(image, s.graph.getParentsSize(image))
|
2015-04-03 20:39:06 -04:00
|
|
|
newImage.RepoTags = []string{"<none>:<none>"}
|
|
|
|
newImage.RepoDigests = []string{"<none>@<none>"}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2015-08-22 08:06:48 -04:00
|
|
|
|
|
|
|
func newImage(image *image.Image, parentSize int64) *types.Image {
|
|
|
|
newImage := new(types.Image)
|
|
|
|
newImage.ParentID = image.Parent
|
|
|
|
newImage.ID = image.ID
|
|
|
|
newImage.Created = image.Created.Unix()
|
|
|
|
newImage.Size = image.Size
|
|
|
|
newImage.VirtualSize = parentSize + image.Size
|
|
|
|
if image.Config != nil {
|
|
|
|
newImage.Labels = image.Config.Labels
|
|
|
|
}
|
|
|
|
return newImage
|
|
|
|
}
|