2014-07-31 17:26:25 -04:00
|
|
|
package daemon
|
2014-07-29 22:04:39 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-08-03 22:02:39 -04:00
|
|
|
"sort"
|
2014-06-02 15:08:39 -04:00
|
|
|
"strconv"
|
2014-07-29 22:04:39 -04:00
|
|
|
"strings"
|
|
|
|
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/container"
|
2015-08-20 03:57:15 -04:00
|
|
|
"github.com/docker/docker/image"
|
2016-02-03 17:46:01 -05:00
|
|
|
"github.com/docker/docker/volume"
|
2015-12-18 12:58:48 -05:00
|
|
|
"github.com/docker/go-connections/nat"
|
2017-07-19 10:20:13 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2014-07-29 22:04:39 -04:00
|
|
|
)
|
|
|
|
|
2016-01-04 03:37:01 -05:00
|
|
|
var acceptedVolumeFilterTags = map[string]bool{
|
|
|
|
"dangling": true,
|
2016-03-21 03:39:48 -04:00
|
|
|
"name": true,
|
|
|
|
"driver": true,
|
2016-03-28 04:43:41 -04:00
|
|
|
"label": true,
|
2016-01-04 03:37:01 -05:00
|
|
|
}
|
|
|
|
|
2016-03-14 23:54:12 -04:00
|
|
|
var acceptedPsFilterTags = map[string]bool{
|
|
|
|
"ancestor": true,
|
|
|
|
"before": true,
|
|
|
|
"exited": true,
|
|
|
|
"id": true,
|
|
|
|
"isolation": true,
|
|
|
|
"label": true,
|
|
|
|
"name": true,
|
|
|
|
"status": true,
|
2016-07-15 14:21:19 -04:00
|
|
|
"health": true,
|
2016-03-14 23:54:12 -04:00
|
|
|
"since": true,
|
|
|
|
"volume": true,
|
2016-05-27 15:20:31 -04:00
|
|
|
"network": true,
|
2016-09-28 06:45:30 -04:00
|
|
|
"is-task": true,
|
2016-10-15 18:53:06 -04:00
|
|
|
"publish": true,
|
|
|
|
"expose": true,
|
2016-03-14 23:54:12 -04:00
|
|
|
}
|
|
|
|
|
2015-08-26 04:16:53 -04:00
|
|
|
// iterationAction represents possible outcomes happening during the container iteration.
|
|
|
|
type iterationAction int
|
|
|
|
|
|
|
|
// containerReducer represents a reducer for a container.
|
|
|
|
// Returns the object to serialize by the api.
|
2017-02-22 18:01:46 -05:00
|
|
|
type containerReducer func(*container.Snapshot, *listContext) (*types.Container, error)
|
2015-08-26 04:16:53 -04:00
|
|
|
|
|
|
|
const (
|
|
|
|
// includeContainer is the action to include a container in the reducer.
|
|
|
|
includeContainer iterationAction = iota
|
|
|
|
// excludeContainer is the action to exclude a container in the reducer.
|
|
|
|
excludeContainer
|
|
|
|
// stopIteration is the action to stop iterating over the list of containers.
|
|
|
|
stopIteration
|
|
|
|
)
|
|
|
|
|
|
|
|
// errStopIteration makes the iterator to stop without returning an error.
|
|
|
|
var errStopIteration = errors.New("container list iteration stopped")
|
|
|
|
|
2014-07-31 17:26:25 -04:00
|
|
|
// List returns an array of all containers registered in the daemon.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (daemon *Daemon) List() []*container.Container {
|
2014-07-31 17:26:25 -04:00
|
|
|
return daemon.containers.List()
|
|
|
|
}
|
|
|
|
|
2015-08-26 04:16:53 -04:00
|
|
|
// listContext is the daemon generated filtering to iterate over containers.
|
2016-01-27 17:09:42 -05:00
|
|
|
// This is created based on the user specification from types.ContainerListOptions.
|
2015-08-26 04:16:53 -04:00
|
|
|
type listContext struct {
|
|
|
|
// idx is the container iteration index for this context
|
|
|
|
idx int
|
|
|
|
// ancestorFilter tells whether it should check ancestors or not
|
|
|
|
ancestorFilter bool
|
|
|
|
// names is a list of container names to filter with
|
|
|
|
names map[string][]string
|
|
|
|
// images is a list of images to filter with
|
2015-11-18 17:20:54 -05:00
|
|
|
images map[image.ID]bool
|
2015-08-26 04:16:53 -04:00
|
|
|
// filters is a collection of arguments to filter with, specified by the user
|
|
|
|
filters filters.Args
|
|
|
|
// exitAllowed is a list of exit codes allowed to filter with
|
|
|
|
exitAllowed []int
|
2016-02-09 03:06:29 -05:00
|
|
|
|
2015-11-05 02:08:00 -05:00
|
|
|
// beforeFilter is a filter to ignore containers that appear before the one given
|
2017-02-22 18:01:46 -05:00
|
|
|
beforeFilter *container.Snapshot
|
2015-11-05 02:08:00 -05:00
|
|
|
// sinceFilter is a filter to stop the filtering when the iterator arrive to the given container
|
2017-02-22 18:01:46 -05:00
|
|
|
sinceFilter *container.Snapshot
|
2016-09-28 06:45:30 -04:00
|
|
|
|
|
|
|
// taskFilter tells if we should filter based on wether a container is part of a task
|
|
|
|
taskFilter bool
|
|
|
|
// isTask tells us if the we should filter container that are a task (true) or not (false)
|
|
|
|
isTask bool
|
2016-10-15 18:53:06 -04:00
|
|
|
|
|
|
|
// publish is a list of published ports to filter with
|
|
|
|
publish map[nat.Port]bool
|
|
|
|
// expose is a list of exposed ports to filter with
|
|
|
|
expose map[nat.Port]bool
|
|
|
|
|
2016-01-27 17:09:42 -05:00
|
|
|
// ContainerListOptions is the filters set by the user
|
|
|
|
*types.ContainerListOptions
|
2015-08-26 04:16:53 -04:00
|
|
|
}
|
|
|
|
|
2017-03-27 13:18:53 -04:00
|
|
|
// byCreatedDescending is a temporary type used to sort a list of containers by creation time.
|
|
|
|
type byCreatedDescending []container.Snapshot
|
2016-08-03 22:02:39 -04:00
|
|
|
|
2017-03-27 13:18:53 -04:00
|
|
|
func (r byCreatedDescending) Len() int { return len(r) }
|
|
|
|
func (r byCreatedDescending) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
|
|
|
func (r byCreatedDescending) Less(i, j int) bool {
|
|
|
|
return r[j].CreatedAt.UnixNano() < r[i].CreatedAt.UnixNano()
|
2016-08-03 22:02:39 -04:00
|
|
|
}
|
|
|
|
|
2015-08-26 04:16:53 -04:00
|
|
|
// Containers returns the list of containers to show given the user's filtering.
|
2016-01-27 17:09:42 -05:00
|
|
|
func (daemon *Daemon) Containers(config *types.ContainerListOptions) ([]*types.Container, error) {
|
2017-03-27 13:18:53 -04:00
|
|
|
return daemon.reduceContainers(config, daemon.refreshImage)
|
2015-08-26 04:16:53 -04:00
|
|
|
}
|
|
|
|
|
2017-02-23 18:12:18 -05:00
|
|
|
func (daemon *Daemon) filterByNameIDMatches(view container.View, ctx *listContext) ([]container.Snapshot, error) {
|
2016-05-28 23:31:30 -04:00
|
|
|
idSearch := false
|
|
|
|
names := ctx.filters.Get("name")
|
|
|
|
ids := ctx.filters.Get("id")
|
|
|
|
if len(names)+len(ids) == 0 {
|
|
|
|
// if name or ID filters are not in use, return to
|
|
|
|
// standard behavior of walking the entire container
|
|
|
|
// list from the daemon's in-memory store
|
2017-02-22 18:01:46 -05:00
|
|
|
all, err := view.All()
|
2017-03-27 13:18:53 -04:00
|
|
|
sort.Sort(byCreatedDescending(all))
|
2017-02-22 18:01:46 -05:00
|
|
|
return all, err
|
2016-05-28 23:31:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// idSearch will determine if we limit name matching to the IDs
|
|
|
|
// matched from any IDs which were specified as filters
|
|
|
|
if len(ids) > 0 {
|
|
|
|
idSearch = true
|
|
|
|
}
|
|
|
|
|
|
|
|
matches := make(map[string]bool)
|
|
|
|
// find ID matches; errors represent "not found" and can be ignored
|
|
|
|
for _, id := range ids {
|
|
|
|
if fullID, err := daemon.idIndex.Get(id); err == nil {
|
|
|
|
matches[fullID] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// look for name matches; if ID filtering was used, then limit the
|
|
|
|
// search space to the matches map only; errors represent "not found"
|
|
|
|
// and can be ignored
|
|
|
|
if len(names) > 0 {
|
|
|
|
for id, idNames := range ctx.names {
|
|
|
|
// if ID filters were used and no matches on that ID were
|
|
|
|
// found, continue to next ID in the list
|
|
|
|
if idSearch && !matches[id] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, eachName := range idNames {
|
|
|
|
if ctx.filters.Match("name", eachName) {
|
|
|
|
matches[id] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-22 18:01:46 -05:00
|
|
|
cntrs := make([]container.Snapshot, 0, len(matches))
|
2016-05-28 23:31:30 -04:00
|
|
|
for id := range matches {
|
2017-02-22 18:01:46 -05:00
|
|
|
c, err := view.Get(id)
|
2017-06-29 18:25:17 -04:00
|
|
|
switch err.(type) {
|
|
|
|
case nil:
|
2017-02-22 18:01:46 -05:00
|
|
|
cntrs = append(cntrs, *c)
|
2017-06-29 18:25:17 -04:00
|
|
|
case container.NoSuchContainerError:
|
|
|
|
// ignore error
|
|
|
|
default:
|
|
|
|
return nil, err
|
2016-08-26 13:02:10 -04:00
|
|
|
}
|
2016-05-28 23:31:30 -04:00
|
|
|
}
|
2016-08-03 22:02:39 -04:00
|
|
|
|
|
|
|
// Restore sort-order after filtering
|
|
|
|
// Created gives us nanosec resolution for sorting
|
2017-03-27 13:18:53 -04:00
|
|
|
sort.Sort(byCreatedDescending(cntrs))
|
2016-08-03 22:02:39 -04:00
|
|
|
|
2017-02-22 18:01:46 -05:00
|
|
|
return cntrs, nil
|
2016-05-28 23:31:30 -04:00
|
|
|
}
|
|
|
|
|
2015-12-30 12:20:41 -05:00
|
|
|
// reduceContainers parses the user's filtering options and generates the list of containers to return based on a reducer.
|
2016-01-27 17:09:42 -05:00
|
|
|
func (daemon *Daemon) reduceContainers(config *types.ContainerListOptions, reducer containerReducer) ([]*types.Container, error) {
|
2016-07-20 19:11:28 -04:00
|
|
|
var (
|
2017-06-29 21:56:22 -04:00
|
|
|
view = daemon.containersReplica.Snapshot()
|
2016-07-20 19:11:28 -04:00
|
|
|
containers = []*types.Container{}
|
|
|
|
)
|
2015-08-26 04:16:53 -04:00
|
|
|
|
2017-02-22 18:01:46 -05:00
|
|
|
ctx, err := daemon.foldFilter(view, config)
|
2015-08-26 04:16:53 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-28 23:31:30 -04:00
|
|
|
// fastpath to only look at a subset of containers if specific name
|
|
|
|
// or ID matches were provided by the user--otherwise we potentially
|
2017-02-22 18:01:46 -05:00
|
|
|
// end up querying many more containers than intended
|
|
|
|
containerList, err := daemon.filterByNameIDMatches(view, ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-05-28 23:31:30 -04:00
|
|
|
|
2017-02-22 18:01:46 -05:00
|
|
|
for i := range containerList {
|
|
|
|
t, err := daemon.reducePsContainer(&containerList[i], ctx, reducer)
|
2015-08-26 04:16:53 -04:00
|
|
|
if err != nil {
|
|
|
|
if err != errStopIteration {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if t != nil {
|
|
|
|
containers = append(containers, t)
|
2015-09-29 13:51:40 -04:00
|
|
|
ctx.idx++
|
2015-08-26 04:16:53 -04:00
|
|
|
}
|
|
|
|
}
|
2016-07-20 19:11:28 -04:00
|
|
|
|
2015-08-26 04:16:53 -04:00
|
|
|
return containers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// reducePsContainer is the basic representation for a container as expected by the ps command.
|
2017-02-22 18:01:46 -05:00
|
|
|
func (daemon *Daemon) reducePsContainer(container *container.Snapshot, ctx *listContext, reducer containerReducer) (*types.Container, error) {
|
2015-08-26 04:16:53 -04:00
|
|
|
// filter containers to return
|
2017-02-22 18:01:46 -05:00
|
|
|
switch includeContainerInList(container, ctx) {
|
2015-08-26 04:16:53 -04:00
|
|
|
case excludeContainer:
|
|
|
|
return nil, nil
|
|
|
|
case stopIteration:
|
|
|
|
return nil, errStopIteration
|
|
|
|
}
|
|
|
|
|
|
|
|
// transform internal container struct into api structs
|
2017-02-18 21:11:48 -05:00
|
|
|
newC, err := reducer(container, ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// release lock because size calculation is slow
|
|
|
|
if ctx.Size {
|
|
|
|
sizeRw, sizeRootFs := daemon.getSize(newC.ID)
|
|
|
|
newC.SizeRw = sizeRw
|
|
|
|
newC.SizeRootFs = sizeRootFs
|
|
|
|
}
|
|
|
|
return newC, nil
|
2015-08-26 04:16:53 -04:00
|
|
|
}
|
|
|
|
|
2015-12-30 12:20:41 -05:00
|
|
|
// foldFilter generates the container filter based on the user's filtering options.
|
2017-02-23 18:12:18 -05:00
|
|
|
func (daemon *Daemon) foldFilter(view container.View, config *types.ContainerListOptions) (*listContext, error) {
|
2016-11-01 10:01:16 -04:00
|
|
|
psFilters := config.Filters
|
2015-08-26 04:16:53 -04:00
|
|
|
|
2016-03-14 23:54:12 -04:00
|
|
|
if err := psFilters.Validate(acceptedPsFilterTags); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-08-26 04:16:53 -04:00
|
|
|
var filtExited []int
|
2016-03-14 23:54:12 -04:00
|
|
|
|
2016-01-27 17:09:42 -05:00
|
|
|
err := psFilters.WalkValues("exited", func(value string) error {
|
2015-11-25 20:27:11 -05:00
|
|
|
code, err := strconv.Atoi(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-06-02 15:08:39 -04:00
|
|
|
}
|
2015-11-25 20:27:11 -05:00
|
|
|
filtExited = append(filtExited, code)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-06-02 15:08:39 -04:00
|
|
|
}
|
|
|
|
|
2015-11-25 20:27:11 -05:00
|
|
|
err = psFilters.WalkValues("status", func(value string) error {
|
2015-11-12 14:55:17 -05:00
|
|
|
if !container.IsValidStateString(value) {
|
2017-07-19 10:20:13 -04:00
|
|
|
return invalidFilter{"status", value}
|
2015-01-08 18:14:04 -05:00
|
|
|
}
|
2015-11-25 20:27:11 -05:00
|
|
|
|
|
|
|
config.All = true
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-01-08 18:14:04 -05:00
|
|
|
}
|
2015-08-20 03:57:15 -04:00
|
|
|
|
2016-09-28 06:45:30 -04:00
|
|
|
var taskFilter, isTask bool
|
2017-09-26 07:39:56 -04:00
|
|
|
if psFilters.Contains("is-task") {
|
2016-09-28 06:45:30 -04:00
|
|
|
if psFilters.ExactMatch("is-task", "true") {
|
|
|
|
taskFilter = true
|
|
|
|
isTask = true
|
|
|
|
} else if psFilters.ExactMatch("is-task", "false") {
|
|
|
|
taskFilter = true
|
|
|
|
isTask = false
|
|
|
|
} else {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, invalidFilter{"is-task", psFilters.Get("is-task")}
|
2016-09-28 06:45:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 14:21:19 -04:00
|
|
|
err = psFilters.WalkValues("health", func(value string) error {
|
|
|
|
if !container.IsValidHealthString(value) {
|
2017-07-19 10:20:13 -04:00
|
|
|
return validationError{errors.Errorf("Unrecognised filter value for health: %s", value)}
|
2016-07-15 14:21:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-02-22 18:01:46 -05:00
|
|
|
var beforeContFilter, sinceContFilter *container.Snapshot
|
2016-02-09 03:06:29 -05:00
|
|
|
|
2015-11-25 20:27:11 -05:00
|
|
|
err = psFilters.WalkValues("before", func(value string) error {
|
2017-02-22 18:01:46 -05:00
|
|
|
beforeContFilter, err = view.Get(value)
|
2015-11-25 20:27:11 -05:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-11-05 02:08:00 -05:00
|
|
|
}
|
|
|
|
|
2015-11-25 20:27:11 -05:00
|
|
|
err = psFilters.WalkValues("since", func(value string) error {
|
2017-02-22 18:01:46 -05:00
|
|
|
sinceContFilter, err = view.Get(value)
|
2015-11-25 20:27:11 -05:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-11-05 02:08:00 -05:00
|
|
|
}
|
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
imagesFilter := map[image.ID]bool{}
|
2015-08-26 04:16:53 -04:00
|
|
|
var ancestorFilter bool
|
2017-09-26 07:39:56 -04:00
|
|
|
if psFilters.Contains("ancestor") {
|
2015-08-20 03:57:15 -04:00
|
|
|
ancestorFilter = true
|
2015-11-25 20:27:11 -05:00
|
|
|
psFilters.WalkValues("ancestor", func(ancestor string) error {
|
2017-08-08 15:43:48 -04:00
|
|
|
id, os, err := daemon.GetImageIDAndOS(ancestor)
|
2015-08-20 03:57:15 -04:00
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Error while looking up for image %v", ancestor)
|
2015-11-25 20:27:11 -05:00
|
|
|
return nil
|
2015-08-20 03:57:15 -04:00
|
|
|
}
|
2015-11-18 17:20:54 -05:00
|
|
|
if imagesFilter[id] {
|
2015-08-20 03:57:15 -04:00
|
|
|
// Already seen this ancestor, skip it
|
2015-11-25 20:27:11 -05:00
|
|
|
return nil
|
2015-08-20 03:57:15 -04:00
|
|
|
}
|
|
|
|
// Then walk down the graph and put the imageIds in imagesFilter
|
2017-08-08 15:43:48 -04:00
|
|
|
populateImageFilterByParents(imagesFilter, id, daemon.stores[os].imageStore.Children)
|
2015-11-25 20:27:11 -05:00
|
|
|
return nil
|
|
|
|
})
|
2015-08-20 03:57:15 -04:00
|
|
|
}
|
|
|
|
|
2016-10-15 18:53:06 -04:00
|
|
|
publishFilter := map[nat.Port]bool{}
|
2017-02-28 05:11:55 -05:00
|
|
|
err = psFilters.WalkValues("publish", portOp("publish", publishFilter))
|
2016-10-15 18:53:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
exposeFilter := map[nat.Port]bool{}
|
2017-02-28 05:11:55 -05:00
|
|
|
err = psFilters.WalkValues("expose", portOp("expose", exposeFilter))
|
2016-10-15 18:53:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-08-26 04:16:53 -04:00
|
|
|
return &listContext{
|
2016-01-27 17:09:42 -05:00
|
|
|
filters: psFilters,
|
|
|
|
ancestorFilter: ancestorFilter,
|
|
|
|
images: imagesFilter,
|
|
|
|
exitAllowed: filtExited,
|
|
|
|
beforeFilter: beforeContFilter,
|
|
|
|
sinceFilter: sinceContFilter,
|
2016-09-28 06:45:30 -04:00
|
|
|
taskFilter: taskFilter,
|
|
|
|
isTask: isTask,
|
2016-10-15 18:53:06 -04:00
|
|
|
publish: publishFilter,
|
|
|
|
expose: exposeFilter,
|
2016-01-27 17:09:42 -05:00
|
|
|
ContainerListOptions: config,
|
2017-06-29 21:56:22 -04:00
|
|
|
names: view.GetAllNames(),
|
2015-08-26 04:16:53 -04:00
|
|
|
}, nil
|
|
|
|
}
|
2017-02-28 05:11:55 -05:00
|
|
|
func portOp(key string, filter map[nat.Port]bool) func(value string) error {
|
|
|
|
return func(value string) error {
|
|
|
|
if strings.Contains(value, ":") {
|
|
|
|
return fmt.Errorf("filter for '%s' should not contain ':': %s", key, value)
|
|
|
|
}
|
|
|
|
//support two formats, original format <portnum>/[<proto>] or <startport-endport>/[<proto>]
|
|
|
|
proto, port := nat.SplitProtoPort(value)
|
|
|
|
start, end, err := nat.ParsePortRange(port)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error while looking up for %s %s: %s", key, value, err)
|
|
|
|
}
|
|
|
|
for i := start; i <= end; i++ {
|
|
|
|
p, err := nat.NewPort(proto, strconv.FormatUint(i, 10))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error while looking up for %s %s: %s", key, value, err)
|
|
|
|
}
|
|
|
|
filter[p] = true
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2014-10-13 02:12:44 -04:00
|
|
|
|
2016-03-17 13:15:32 -04:00
|
|
|
// includeContainerInList decides whether a container should be included in the output or not based in the filter.
|
2015-08-26 04:16:53 -04:00
|
|
|
// It also decides if the iteration should be stopped or not.
|
2017-02-22 18:01:46 -05:00
|
|
|
func includeContainerInList(container *container.Snapshot, ctx *listContext) iterationAction {
|
2016-02-19 01:07:16 -05:00
|
|
|
// Do not include container if it's in the list before the filter container.
|
|
|
|
// Set the filter container to nil to include the rest of containers after this one.
|
|
|
|
if ctx.beforeFilter != nil {
|
|
|
|
if container.ID == ctx.beforeFilter.ID {
|
|
|
|
ctx.beforeFilter = nil
|
|
|
|
}
|
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop iteration when the container arrives to the filter container
|
|
|
|
if ctx.sinceFilter != nil {
|
|
|
|
if container.ID == ctx.sinceFilter.ID {
|
|
|
|
return stopIteration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 04:16:53 -04:00
|
|
|
// Do not include container if it's stopped and we're not filters
|
2016-04-19 01:50:09 -04:00
|
|
|
if !container.Running && !ctx.All && ctx.Limit <= 0 {
|
2015-08-26 04:16:53 -04:00
|
|
|
return excludeContainer
|
|
|
|
}
|
2014-10-13 02:12:44 -04:00
|
|
|
|
2015-08-26 04:16:53 -04:00
|
|
|
// Do not include container if the name doesn't match
|
|
|
|
if !ctx.filters.Match("name", container.Name) {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
2015-01-06 19:04:10 -05:00
|
|
|
|
2015-08-26 04:16:53 -04:00
|
|
|
// Do not include container if the id doesn't match
|
|
|
|
if !ctx.filters.Match("id", container.ID) {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
|
2016-09-28 06:45:30 -04:00
|
|
|
if ctx.taskFilter {
|
|
|
|
if ctx.isTask != container.Managed {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 04:16:53 -04:00
|
|
|
// Do not include container if any of the labels don't match
|
2017-02-22 18:01:46 -05:00
|
|
|
if !ctx.filters.MatchKVList("label", container.Labels) {
|
2015-08-26 04:16:53 -04:00
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
|
2016-02-03 15:07:00 -05:00
|
|
|
// Do not include container if isolation doesn't match
|
2015-10-19 12:33:43 -04:00
|
|
|
if excludeContainer == excludeByIsolation(container, ctx) {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
|
2015-11-05 02:08:00 -05:00
|
|
|
// Stop iteration when the index is over the limit
|
|
|
|
if ctx.Limit > 0 && ctx.idx == ctx.Limit {
|
|
|
|
return stopIteration
|
|
|
|
}
|
|
|
|
|
2015-08-26 04:16:53 -04:00
|
|
|
// Do not include container if its exit code is not in the filter
|
|
|
|
if len(ctx.exitAllowed) > 0 {
|
|
|
|
shouldSkip := true
|
|
|
|
for _, code := range ctx.exitAllowed {
|
2017-02-22 18:01:46 -05:00
|
|
|
if code == container.ExitCode && !container.Running && !container.StartedAt.IsZero() {
|
2015-08-26 04:16:53 -04:00
|
|
|
shouldSkip = false
|
|
|
|
break
|
2014-07-29 22:04:39 -04:00
|
|
|
}
|
|
|
|
}
|
2015-08-26 04:16:53 -04:00
|
|
|
if shouldSkip {
|
|
|
|
return excludeContainer
|
2014-06-02 15:08:39 -04:00
|
|
|
}
|
2015-08-26 04:16:53 -04:00
|
|
|
}
|
2014-10-13 02:12:44 -04:00
|
|
|
|
2015-08-26 04:16:53 -04:00
|
|
|
// Do not include container if its status doesn't match the filter
|
2017-02-22 18:01:46 -05:00
|
|
|
if !ctx.filters.Match("status", container.State) {
|
2015-08-26 04:16:53 -04:00
|
|
|
return excludeContainer
|
|
|
|
}
|
2015-08-20 03:57:15 -04:00
|
|
|
|
2016-07-15 14:21:19 -04:00
|
|
|
// Do not include container if its health doesn't match the filter
|
2017-02-22 18:01:46 -05:00
|
|
|
if !ctx.filters.ExactMatch("health", container.Health) {
|
2016-07-15 14:21:19 -04:00
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
|
2017-09-26 07:39:56 -04:00
|
|
|
if ctx.filters.Contains("volume") {
|
2017-02-22 18:01:46 -05:00
|
|
|
volumesByName := make(map[string]types.MountPoint)
|
|
|
|
for _, m := range container.Mounts {
|
2016-03-16 17:10:16 -04:00
|
|
|
if m.Name != "" {
|
|
|
|
volumesByName[m.Name] = m
|
|
|
|
} else {
|
|
|
|
volumesByName[m.Source] = m
|
|
|
|
}
|
2016-02-03 17:46:01 -05:00
|
|
|
}
|
2017-02-22 18:01:46 -05:00
|
|
|
volumesByDestination := make(map[string]types.MountPoint)
|
|
|
|
for _, m := range container.Mounts {
|
|
|
|
if m.Destination != "" {
|
|
|
|
volumesByDestination[m.Destination] = m
|
|
|
|
}
|
|
|
|
}
|
2016-02-03 17:46:01 -05:00
|
|
|
|
|
|
|
volumeExist := fmt.Errorf("volume mounted in container")
|
|
|
|
err := ctx.filters.WalkValues("volume", func(value string) error {
|
2017-02-22 18:01:46 -05:00
|
|
|
if _, exist := volumesByDestination[value]; exist {
|
2016-02-03 17:46:01 -05:00
|
|
|
return volumeExist
|
|
|
|
}
|
|
|
|
if _, exist := volumesByName[value]; exist {
|
|
|
|
return volumeExist
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != volumeExist {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 04:16:53 -04:00
|
|
|
if ctx.ancestorFilter {
|
|
|
|
if len(ctx.images) == 0 {
|
|
|
|
return excludeContainer
|
2015-08-20 03:57:15 -04:00
|
|
|
}
|
2017-02-22 18:01:46 -05:00
|
|
|
if !ctx.images[image.ID(container.ImageID)] {
|
2015-08-26 04:16:53 -04:00
|
|
|
return excludeContainer
|
2015-04-03 20:39:06 -04:00
|
|
|
}
|
2015-08-26 04:16:53 -04:00
|
|
|
}
|
2015-04-30 21:13:18 -04:00
|
|
|
|
2017-03-27 13:18:53 -04:00
|
|
|
var (
|
|
|
|
networkExist = errors.New("container part of network")
|
|
|
|
noNetworks = errors.New("container is not part of any networks")
|
|
|
|
)
|
2017-09-26 07:39:56 -04:00
|
|
|
if ctx.filters.Contains("network") {
|
2016-05-27 15:20:31 -04:00
|
|
|
err := ctx.filters.WalkValues("network", func(value string) error {
|
2017-03-27 13:18:53 -04:00
|
|
|
if container.NetworkSettings == nil {
|
|
|
|
return noNetworks
|
|
|
|
}
|
2016-06-05 20:04:33 -04:00
|
|
|
if _, ok := container.NetworkSettings.Networks[value]; ok {
|
2016-05-27 15:20:31 -04:00
|
|
|
return networkExist
|
|
|
|
}
|
2016-06-05 20:04:33 -04:00
|
|
|
for _, nw := range container.NetworkSettings.Networks {
|
2017-02-22 18:01:46 -05:00
|
|
|
if nw == nil {
|
2016-08-23 19:50:15 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-11-22 15:02:46 -05:00
|
|
|
if strings.HasPrefix(nw.NetworkID, value) {
|
2016-06-05 20:04:33 -04:00
|
|
|
return networkExist
|
|
|
|
}
|
|
|
|
}
|
2016-05-27 15:20:31 -04:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != networkExist {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-15 18:53:06 -04:00
|
|
|
if len(ctx.publish) > 0 {
|
|
|
|
shouldSkip := true
|
|
|
|
for port := range ctx.publish {
|
2017-03-27 13:18:53 -04:00
|
|
|
if _, ok := container.PortBindings[port]; ok {
|
2016-10-15 18:53:06 -04:00
|
|
|
shouldSkip = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if shouldSkip {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ctx.expose) > 0 {
|
|
|
|
shouldSkip := true
|
|
|
|
for port := range ctx.expose {
|
2017-02-22 18:01:46 -05:00
|
|
|
if _, ok := container.ExposedPorts[port]; ok {
|
2016-10-15 18:53:06 -04:00
|
|
|
shouldSkip = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if shouldSkip {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 04:16:53 -04:00
|
|
|
return includeContainer
|
|
|
|
}
|
2015-04-30 21:13:18 -04:00
|
|
|
|
2017-03-27 13:18:53 -04:00
|
|
|
// refreshImage checks if the Image ref still points to the correct ID, and updates the ref to the actual ID when it doesn't
|
|
|
|
func (daemon *Daemon) refreshImage(s *container.Snapshot, ctx *listContext) (*types.Container, error) {
|
|
|
|
c := s.Container
|
|
|
|
image := s.Image // keep the original ref if still valid (hasn't changed)
|
|
|
|
if image != s.ImageID {
|
2017-08-08 15:43:48 -04:00
|
|
|
id, _, err := daemon.GetImageIDAndOS(image)
|
2017-07-19 10:20:13 -04:00
|
|
|
if _, isDNE := err.(errImageDoesNotExist); err != nil && !isDNE {
|
2015-11-18 17:20:54 -05:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-27 13:18:53 -04:00
|
|
|
if err != nil || id.String() != s.ImageID {
|
|
|
|
// ref changed, we need to use original ID
|
|
|
|
image = s.ImageID
|
2015-11-18 17:20:54 -05:00
|
|
|
}
|
2015-08-26 04:16:53 -04:00
|
|
|
}
|
2017-03-27 13:18:53 -04:00
|
|
|
c.Image = image
|
|
|
|
return &c, nil
|
2014-07-29 22:04:39 -04:00
|
|
|
}
|
2015-06-12 09:25:32 -04:00
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// Volumes lists known volumes, using the filter to restrict the range
|
|
|
|
// of volumes returned.
|
2015-09-23 16:29:14 -04:00
|
|
|
func (daemon *Daemon) Volumes(filter string) ([]*types.Volume, []string, error) {
|
2016-01-04 03:37:01 -05:00
|
|
|
var (
|
2016-03-21 03:39:48 -04:00
|
|
|
volumesOut []*types.Volume
|
2016-01-04 03:37:01 -05:00
|
|
|
)
|
2017-09-26 07:59:45 -04:00
|
|
|
volFilters, err := filters.FromJSON(filter)
|
2015-06-12 09:25:32 -04:00
|
|
|
if err != nil {
|
2015-09-23 16:29:14 -04:00
|
|
|
return nil, nil, err
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
|
|
|
|
2016-01-04 03:37:01 -05:00
|
|
|
if err := volFilters.Validate(acceptedVolumeFilterTags); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-23 16:29:14 -04:00
|
|
|
volumes, warnings, err := daemon.volumes.List()
|
2016-06-04 11:24:14 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-21 03:39:48 -04:00
|
|
|
filterVolumes, err := daemon.filterVolumes(volumes, volFilters)
|
2015-09-23 16:29:14 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2016-03-21 03:39:48 -04:00
|
|
|
for _, v := range filterVolumes {
|
2016-04-12 17:09:55 -04:00
|
|
|
apiV := volumeToAPIType(v)
|
|
|
|
if vv, ok := v.(interface {
|
|
|
|
CachedPath() string
|
|
|
|
}); ok {
|
|
|
|
apiV.Mountpoint = vv.CachedPath()
|
|
|
|
} else {
|
|
|
|
apiV.Mountpoint = v.Path()
|
|
|
|
}
|
|
|
|
volumesOut = append(volumesOut, apiV)
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
2015-09-23 16:29:14 -04:00
|
|
|
return volumesOut, warnings, nil
|
2015-06-12 09:25:32 -04:00
|
|
|
}
|
2015-08-20 03:57:15 -04:00
|
|
|
|
2016-03-21 03:39:48 -04:00
|
|
|
// filterVolumes filters volume list according to user specified filter
|
|
|
|
// and returns user chosen volumes
|
|
|
|
func (daemon *Daemon) filterVolumes(vols []volume.Volume, filter filters.Args) ([]volume.Volume, error) {
|
|
|
|
// if filter is empty, return original volume list
|
|
|
|
if filter.Len() == 0 {
|
|
|
|
return vols, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var retVols []volume.Volume
|
|
|
|
for _, vol := range vols {
|
2017-09-26 07:39:56 -04:00
|
|
|
if filter.Contains("name") {
|
2016-03-21 03:39:48 -04:00
|
|
|
if !filter.Match("name", vol.Name()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2017-09-26 07:39:56 -04:00
|
|
|
if filter.Contains("driver") {
|
2017-01-04 01:43:29 -05:00
|
|
|
if !filter.ExactMatch("driver", vol.DriverName()) {
|
2016-03-21 03:39:48 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2017-09-26 07:39:56 -04:00
|
|
|
if filter.Contains("label") {
|
2016-09-17 15:32:31 -04:00
|
|
|
v, ok := vol.(volume.DetailedVolume)
|
2016-08-11 13:47:01 -04:00
|
|
|
if !ok {
|
|
|
|
continue
|
2016-03-28 04:43:41 -04:00
|
|
|
}
|
2016-08-11 13:47:01 -04:00
|
|
|
if !filter.MatchKVList("label", v.Labels()) {
|
|
|
|
continue
|
2016-03-28 04:43:41 -04:00
|
|
|
}
|
|
|
|
}
|
2016-03-21 03:39:48 -04:00
|
|
|
retVols = append(retVols, vol)
|
|
|
|
}
|
|
|
|
danglingOnly := false
|
2017-09-26 07:39:56 -04:00
|
|
|
if filter.Contains("dangling") {
|
2016-03-21 03:39:48 -04:00
|
|
|
if filter.ExactMatch("dangling", "true") || filter.ExactMatch("dangling", "1") {
|
|
|
|
danglingOnly = true
|
|
|
|
} else if !filter.ExactMatch("dangling", "false") && !filter.ExactMatch("dangling", "0") {
|
2017-07-19 10:20:13 -04:00
|
|
|
return nil, invalidFilter{"dangling", filter.Get("dangling")}
|
2016-03-21 03:39:48 -04:00
|
|
|
}
|
|
|
|
retVols = daemon.volumes.FilterByUsed(retVols, !danglingOnly)
|
|
|
|
}
|
|
|
|
return retVols, nil
|
|
|
|
}
|
|
|
|
|
2015-11-18 17:20:54 -05:00
|
|
|
func populateImageFilterByParents(ancestorMap map[image.ID]bool, imageID image.ID, getChildren func(image.ID) []image.ID) {
|
2015-08-20 03:57:15 -04:00
|
|
|
if !ancestorMap[imageID] {
|
2015-11-18 17:20:54 -05:00
|
|
|
for _, id := range getChildren(imageID) {
|
|
|
|
populateImageFilterByParents(ancestorMap, id, getChildren)
|
2015-08-20 03:57:15 -04:00
|
|
|
}
|
|
|
|
ancestorMap[imageID] = true
|
|
|
|
}
|
|
|
|
}
|