2014-08-01 01:26:25 +04:00
|
|
|
package daemon
|
2014-07-30 06:04:39 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-08-04 05:02:39 +03:00
|
|
|
"sort"
|
2014-06-02 23:08:39 +04:00
|
|
|
"strconv"
|
2014-07-30 06:04:39 +04:00
|
|
|
"strings"
|
|
|
|
|
2015-08-20 10:57:15 +03:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-09-06 21:18:12 +03:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/api/types/filters"
|
|
|
|
networktypes "github.com/docker/docker/api/types/network"
|
2015-11-12 22:55:17 +03:00
|
|
|
"github.com/docker/docker/container"
|
2015-08-20 10:57:15 +03:00
|
|
|
"github.com/docker/docker/image"
|
2016-02-04 01:46:01 +03:00
|
|
|
"github.com/docker/docker/volume"
|
2015-12-18 20:58:48 +03:00
|
|
|
"github.com/docker/go-connections/nat"
|
2014-07-30 06:04:39 +04:00
|
|
|
)
|
|
|
|
|
2016-01-04 11:37:01 +03:00
|
|
|
var acceptedVolumeFilterTags = map[string]bool{
|
|
|
|
"dangling": true,
|
2016-03-21 10:39:48 +03:00
|
|
|
"name": true,
|
|
|
|
"driver": true,
|
2016-03-28 11:43:41 +03:00
|
|
|
"label": true,
|
2016-01-04 11:37:01 +03:00
|
|
|
}
|
|
|
|
|
2016-03-15 06:54:12 +03: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 21:21:19 +03:00
|
|
|
"health": true,
|
2016-03-15 06:54:12 +03:00
|
|
|
"since": true,
|
|
|
|
"volume": true,
|
2016-05-27 22:20:31 +03:00
|
|
|
"network": true,
|
2016-09-28 13:45:30 +03:00
|
|
|
"is-task": true,
|
2016-10-16 01:53:06 +03:00
|
|
|
"publish": true,
|
|
|
|
"expose": true,
|
2016-03-15 06:54:12 +03:00
|
|
|
}
|
|
|
|
|
2015-08-26 11:16:53 +03: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.
|
2015-11-12 22:55:17 +03:00
|
|
|
type containerReducer func(*container.Container, *listContext) (*types.Container, error)
|
2015-08-26 11:16:53 +03: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-08-01 01:26:25 +04:00
|
|
|
// List returns an array of all containers registered in the daemon.
|
2015-11-12 22:55:17 +03:00
|
|
|
func (daemon *Daemon) List() []*container.Container {
|
2014-08-01 01:26:25 +04:00
|
|
|
return daemon.containers.List()
|
|
|
|
}
|
|
|
|
|
2015-08-26 11:16:53 +03:00
|
|
|
// listContext is the daemon generated filtering to iterate over containers.
|
2016-01-28 01:09:42 +03:00
|
|
|
// This is created based on the user specification from types.ContainerListOptions.
|
2015-08-26 11:16:53 +03: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-19 01:20:54 +03:00
|
|
|
images map[image.ID]bool
|
2015-08-26 11:16:53 +03: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 11:06:29 +03:00
|
|
|
|
2015-11-05 10:08:00 +03:00
|
|
|
// beforeFilter is a filter to ignore containers that appear before the one given
|
2015-11-12 22:55:17 +03:00
|
|
|
beforeFilter *container.Container
|
2015-11-05 10:08:00 +03:00
|
|
|
// sinceFilter is a filter to stop the filtering when the iterator arrive to the given container
|
2015-11-12 22:55:17 +03:00
|
|
|
sinceFilter *container.Container
|
2016-09-28 13:45:30 +03: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-16 01:53:06 +03: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-28 01:09:42 +03:00
|
|
|
// ContainerListOptions is the filters set by the user
|
|
|
|
*types.ContainerListOptions
|
2015-08-26 11:16:53 +03:00
|
|
|
}
|
|
|
|
|
2016-08-04 05:02:39 +03:00
|
|
|
// byContainerCreated is a temporary type used to sort a list of containers by creation time.
|
|
|
|
type byContainerCreated []*container.Container
|
|
|
|
|
|
|
|
func (r byContainerCreated) Len() int { return len(r) }
|
|
|
|
func (r byContainerCreated) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
|
|
|
|
func (r byContainerCreated) Less(i, j int) bool {
|
|
|
|
return r[i].Created.UnixNano() < r[j].Created.UnixNano()
|
|
|
|
}
|
|
|
|
|
2015-08-26 11:16:53 +03:00
|
|
|
// Containers returns the list of containers to show given the user's filtering.
|
2016-01-28 01:09:42 +03:00
|
|
|
func (daemon *Daemon) Containers(config *types.ContainerListOptions) ([]*types.Container, error) {
|
2015-09-29 20:51:40 +03:00
|
|
|
return daemon.reduceContainers(config, daemon.transformContainer)
|
2015-08-26 11:16:53 +03:00
|
|
|
}
|
|
|
|
|
2016-05-29 06:31:30 +03:00
|
|
|
func (daemon *Daemon) filterByNameIDMatches(ctx *listContext) []*container.Container {
|
|
|
|
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
|
|
|
|
return daemon.List()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cntrs := make([]*container.Container, 0, len(matches))
|
|
|
|
for id := range matches {
|
2016-08-26 20:02:10 +03:00
|
|
|
if c := daemon.containers.Get(id); c != nil {
|
|
|
|
cntrs = append(cntrs, c)
|
|
|
|
}
|
2016-05-29 06:31:30 +03:00
|
|
|
}
|
2016-08-04 05:02:39 +03:00
|
|
|
|
|
|
|
// Restore sort-order after filtering
|
|
|
|
// Created gives us nanosec resolution for sorting
|
|
|
|
sort.Sort(sort.Reverse(byContainerCreated(cntrs)))
|
|
|
|
|
2016-05-29 06:31:30 +03:00
|
|
|
return cntrs
|
|
|
|
}
|
|
|
|
|
2015-12-30 20:20:41 +03:00
|
|
|
// reduceContainers parses the user's filtering options and generates the list of containers to return based on a reducer.
|
2016-01-28 01:09:42 +03:00
|
|
|
func (daemon *Daemon) reduceContainers(config *types.ContainerListOptions, reducer containerReducer) ([]*types.Container, error) {
|
2016-07-21 02:11:28 +03:00
|
|
|
var (
|
|
|
|
containers = []*types.Container{}
|
|
|
|
)
|
2015-08-26 11:16:53 +03:00
|
|
|
|
2015-09-29 20:51:40 +03:00
|
|
|
ctx, err := daemon.foldFilter(config)
|
2015-08-26 11:16:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-05-29 06:31:30 +03:00
|
|
|
// fastpath to only look at a subset of containers if specific name
|
|
|
|
// or ID matches were provided by the user--otherwise we potentially
|
|
|
|
// end up locking and querying many more containers than intended
|
|
|
|
containerList := daemon.filterByNameIDMatches(ctx)
|
|
|
|
|
|
|
|
for _, container := range containerList {
|
2015-09-29 20:51:40 +03:00
|
|
|
t, err := daemon.reducePsContainer(container, ctx, reducer)
|
2015-08-26 11:16:53 +03:00
|
|
|
if err != nil {
|
|
|
|
if err != errStopIteration {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if t != nil {
|
|
|
|
containers = append(containers, t)
|
2015-09-29 20:51:40 +03:00
|
|
|
ctx.idx++
|
2015-08-26 11:16:53 +03:00
|
|
|
}
|
|
|
|
}
|
2016-07-21 02:11:28 +03:00
|
|
|
|
2015-08-26 11:16:53 +03:00
|
|
|
return containers, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// reducePsContainer is the basic representation for a container as expected by the ps command.
|
2015-11-12 22:55:17 +03:00
|
|
|
func (daemon *Daemon) reducePsContainer(container *container.Container, ctx *listContext, reducer containerReducer) (*types.Container, error) {
|
2015-08-26 11:16:53 +03:00
|
|
|
container.Lock()
|
2014-07-30 06:04:39 +04:00
|
|
|
|
2015-08-26 11:16:53 +03:00
|
|
|
// filter containers to return
|
2015-09-29 20:51:40 +03:00
|
|
|
action := includeContainerInList(container, ctx)
|
2015-08-26 11:16:53 +03:00
|
|
|
switch action {
|
|
|
|
case excludeContainer:
|
2017-02-19 05:11:48 +03:00
|
|
|
container.Unlock()
|
2015-08-26 11:16:53 +03:00
|
|
|
return nil, nil
|
|
|
|
case stopIteration:
|
2017-02-19 05:11:48 +03:00
|
|
|
container.Unlock()
|
2015-08-26 11:16:53 +03:00
|
|
|
return nil, errStopIteration
|
|
|
|
}
|
|
|
|
|
|
|
|
// transform internal container struct into api structs
|
2017-02-19 05:11:48 +03:00
|
|
|
newC, err := reducer(container, ctx)
|
|
|
|
container.Unlock()
|
|
|
|
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 11:16:53 +03:00
|
|
|
}
|
|
|
|
|
2015-12-30 20:20:41 +03:00
|
|
|
// foldFilter generates the container filter based on the user's filtering options.
|
2016-01-28 01:09:42 +03:00
|
|
|
func (daemon *Daemon) foldFilter(config *types.ContainerListOptions) (*listContext, error) {
|
2016-11-01 17:01:16 +03:00
|
|
|
psFilters := config.Filters
|
2015-08-26 11:16:53 +03:00
|
|
|
|
2016-03-15 06:54:12 +03:00
|
|
|
if err := psFilters.Validate(acceptedPsFilterTags); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-08-26 11:16:53 +03:00
|
|
|
var filtExited []int
|
2016-03-15 06:54:12 +03:00
|
|
|
|
2016-01-28 01:09:42 +03:00
|
|
|
err := psFilters.WalkValues("exited", func(value string) error {
|
2015-11-26 04:27:11 +03:00
|
|
|
code, err := strconv.Atoi(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-06-02 23:08:39 +04:00
|
|
|
}
|
2015-11-26 04:27:11 +03:00
|
|
|
filtExited = append(filtExited, code)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-06-02 23:08:39 +04:00
|
|
|
}
|
|
|
|
|
2015-11-26 04:27:11 +03:00
|
|
|
err = psFilters.WalkValues("status", func(value string) error {
|
2015-11-12 22:55:17 +03:00
|
|
|
if !container.IsValidStateString(value) {
|
2015-11-26 04:27:11 +03:00
|
|
|
return fmt.Errorf("Unrecognised filter value for status: %s", value)
|
2015-01-09 02:14:04 +03:00
|
|
|
}
|
2015-11-26 04:27:11 +03:00
|
|
|
|
|
|
|
config.All = true
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-01-09 02:14:04 +03:00
|
|
|
}
|
2015-08-20 10:57:15 +03:00
|
|
|
|
2016-09-28 13:45:30 +03:00
|
|
|
var taskFilter, isTask bool
|
|
|
|
if psFilters.Include("is-task") {
|
|
|
|
if psFilters.ExactMatch("is-task", "true") {
|
|
|
|
taskFilter = true
|
|
|
|
isTask = true
|
|
|
|
} else if psFilters.ExactMatch("is-task", "false") {
|
|
|
|
taskFilter = true
|
|
|
|
isTask = false
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("Invalid filter 'is-task=%s'", psFilters.Get("is-task"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 21:21:19 +03:00
|
|
|
err = psFilters.WalkValues("health", func(value string) error {
|
|
|
|
if !container.IsValidHealthString(value) {
|
|
|
|
return fmt.Errorf("Unrecognised filter value for health: %s", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-12 22:55:17 +03:00
|
|
|
var beforeContFilter, sinceContFilter *container.Container
|
2016-02-09 11:06:29 +03:00
|
|
|
|
2015-11-26 04:27:11 +03:00
|
|
|
err = psFilters.WalkValues("before", func(value string) error {
|
2015-12-11 20:39:28 +03:00
|
|
|
beforeContFilter, err = daemon.GetContainer(value)
|
2015-11-26 04:27:11 +03:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-11-05 10:08:00 +03:00
|
|
|
}
|
|
|
|
|
2015-11-26 04:27:11 +03:00
|
|
|
err = psFilters.WalkValues("since", func(value string) error {
|
2015-12-11 20:39:28 +03:00
|
|
|
sinceContFilter, err = daemon.GetContainer(value)
|
2015-11-26 04:27:11 +03:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-11-05 10:08:00 +03:00
|
|
|
}
|
|
|
|
|
2015-11-19 01:20:54 +03:00
|
|
|
imagesFilter := map[image.ID]bool{}
|
2015-08-26 11:16:53 +03:00
|
|
|
var ancestorFilter bool
|
2015-11-26 04:27:11 +03:00
|
|
|
if psFilters.Include("ancestor") {
|
2015-08-20 10:57:15 +03:00
|
|
|
ancestorFilter = true
|
2015-11-26 04:27:11 +03:00
|
|
|
psFilters.WalkValues("ancestor", func(ancestor string) error {
|
2015-11-19 01:20:54 +03:00
|
|
|
id, err := daemon.GetImageID(ancestor)
|
2015-08-20 10:57:15 +03:00
|
|
|
if err != nil {
|
|
|
|
logrus.Warnf("Error while looking up for image %v", ancestor)
|
2015-11-26 04:27:11 +03:00
|
|
|
return nil
|
2015-08-20 10:57:15 +03:00
|
|
|
}
|
2015-11-19 01:20:54 +03:00
|
|
|
if imagesFilter[id] {
|
2015-08-20 10:57:15 +03:00
|
|
|
// Already seen this ancestor, skip it
|
2015-11-26 04:27:11 +03:00
|
|
|
return nil
|
2015-08-20 10:57:15 +03:00
|
|
|
}
|
|
|
|
// Then walk down the graph and put the imageIds in imagesFilter
|
2015-11-19 01:20:54 +03:00
|
|
|
populateImageFilterByParents(imagesFilter, id, daemon.imageStore.Children)
|
2015-11-26 04:27:11 +03:00
|
|
|
return nil
|
|
|
|
})
|
2015-08-20 10:57:15 +03:00
|
|
|
}
|
|
|
|
|
2016-10-16 01:53:06 +03:00
|
|
|
publishFilter := map[nat.Port]bool{}
|
2017-02-28 13:11:55 +03:00
|
|
|
err = psFilters.WalkValues("publish", portOp("publish", publishFilter))
|
2016-10-16 01:53:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
exposeFilter := map[nat.Port]bool{}
|
2017-02-28 13:11:55 +03:00
|
|
|
err = psFilters.WalkValues("expose", portOp("expose", exposeFilter))
|
2016-10-16 01:53:06 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-08-26 11:16:53 +03:00
|
|
|
return &listContext{
|
2016-01-28 01:09:42 +03:00
|
|
|
filters: psFilters,
|
|
|
|
ancestorFilter: ancestorFilter,
|
|
|
|
images: imagesFilter,
|
|
|
|
exitAllowed: filtExited,
|
|
|
|
beforeFilter: beforeContFilter,
|
|
|
|
sinceFilter: sinceContFilter,
|
2016-09-28 13:45:30 +03:00
|
|
|
taskFilter: taskFilter,
|
|
|
|
isTask: isTask,
|
2016-10-16 01:53:06 +03:00
|
|
|
publish: publishFilter,
|
|
|
|
expose: exposeFilter,
|
2016-01-28 01:09:42 +03:00
|
|
|
ContainerListOptions: config,
|
|
|
|
names: daemon.nameIndex.GetAll(),
|
2015-08-26 11:16:53 +03:00
|
|
|
}, nil
|
|
|
|
}
|
2017-02-28 13:11:55 +03: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 10:12:44 +04:00
|
|
|
|
2016-03-17 20:15:32 +03:00
|
|
|
// includeContainerInList decides whether a container should be included in the output or not based in the filter.
|
2015-08-26 11:16:53 +03:00
|
|
|
// It also decides if the iteration should be stopped or not.
|
2015-11-12 22:55:17 +03:00
|
|
|
func includeContainerInList(container *container.Container, ctx *listContext) iterationAction {
|
2016-02-19 09:07:16 +03: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 11:16:53 +03:00
|
|
|
// Do not include container if it's stopped and we're not filters
|
2016-04-19 08:50:09 +03:00
|
|
|
if !container.Running && !ctx.All && ctx.Limit <= 0 {
|
2015-08-26 11:16:53 +03:00
|
|
|
return excludeContainer
|
|
|
|
}
|
2014-10-13 10:12:44 +04:00
|
|
|
|
2015-08-26 11:16:53 +03:00
|
|
|
// Do not include container if the name doesn't match
|
|
|
|
if !ctx.filters.Match("name", container.Name) {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
2015-01-07 03:04:10 +03:00
|
|
|
|
2015-08-26 11:16:53 +03:00
|
|
|
// Do not include container if the id doesn't match
|
|
|
|
if !ctx.filters.Match("id", container.ID) {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
|
2016-09-28 13:45:30 +03:00
|
|
|
if ctx.taskFilter {
|
|
|
|
if ctx.isTask != container.Managed {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 11:16:53 +03:00
|
|
|
// Do not include container if any of the labels don't match
|
|
|
|
if !ctx.filters.MatchKVList("label", container.Config.Labels) {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
|
2016-02-03 23:07:00 +03:00
|
|
|
// Do not include container if isolation doesn't match
|
2015-10-19 19:33:43 +03:00
|
|
|
if excludeContainer == excludeByIsolation(container, ctx) {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
|
2015-11-05 10:08:00 +03:00
|
|
|
// Stop iteration when the index is over the limit
|
|
|
|
if ctx.Limit > 0 && ctx.idx == ctx.Limit {
|
|
|
|
return stopIteration
|
|
|
|
}
|
|
|
|
|
2015-08-26 11:16:53 +03: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 {
|
2016-06-14 21:11:43 +03:00
|
|
|
if code == container.ExitCode() && !container.Running && !container.StartedAt.IsZero() {
|
2015-08-26 11:16:53 +03:00
|
|
|
shouldSkip = false
|
|
|
|
break
|
2014-07-30 06:04:39 +04:00
|
|
|
}
|
|
|
|
}
|
2015-08-26 11:16:53 +03:00
|
|
|
if shouldSkip {
|
|
|
|
return excludeContainer
|
2014-06-02 23:08:39 +04:00
|
|
|
}
|
2015-08-26 11:16:53 +03:00
|
|
|
}
|
2014-10-13 10:12:44 +04:00
|
|
|
|
2015-08-26 11:16:53 +03:00
|
|
|
// Do not include container if its status doesn't match the filter
|
|
|
|
if !ctx.filters.Match("status", container.State.StateString()) {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
2015-08-20 10:57:15 +03:00
|
|
|
|
2016-07-15 21:21:19 +03:00
|
|
|
// Do not include container if its health doesn't match the filter
|
|
|
|
if !ctx.filters.ExactMatch("health", container.State.HealthString()) {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
|
2016-02-04 01:46:01 +03:00
|
|
|
if ctx.filters.Include("volume") {
|
|
|
|
volumesByName := make(map[string]*volume.MountPoint)
|
|
|
|
for _, m := range container.MountPoints {
|
2016-03-17 00:10:16 +03:00
|
|
|
if m.Name != "" {
|
|
|
|
volumesByName[m.Name] = m
|
|
|
|
} else {
|
|
|
|
volumesByName[m.Source] = m
|
|
|
|
}
|
2016-02-04 01:46:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
volumeExist := fmt.Errorf("volume mounted in container")
|
|
|
|
err := ctx.filters.WalkValues("volume", func(value string) error {
|
|
|
|
if _, exist := container.MountPoints[value]; exist {
|
|
|
|
return volumeExist
|
|
|
|
}
|
|
|
|
if _, exist := volumesByName[value]; exist {
|
|
|
|
return volumeExist
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != volumeExist {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 11:16:53 +03:00
|
|
|
if ctx.ancestorFilter {
|
|
|
|
if len(ctx.images) == 0 {
|
|
|
|
return excludeContainer
|
2015-08-20 10:57:15 +03:00
|
|
|
}
|
2015-08-26 11:16:53 +03:00
|
|
|
if !ctx.images[container.ImageID] {
|
|
|
|
return excludeContainer
|
2015-04-04 03:39:06 +03:00
|
|
|
}
|
2015-08-26 11:16:53 +03:00
|
|
|
}
|
2015-05-01 04:13:18 +03:00
|
|
|
|
2016-05-27 22:20:31 +03:00
|
|
|
networkExist := fmt.Errorf("container part of network")
|
|
|
|
if ctx.filters.Include("network") {
|
|
|
|
err := ctx.filters.WalkValues("network", func(value string) error {
|
2016-06-06 03:04:33 +03:00
|
|
|
if _, ok := container.NetworkSettings.Networks[value]; ok {
|
2016-05-27 22:20:31 +03:00
|
|
|
return networkExist
|
|
|
|
}
|
2016-06-06 03:04:33 +03:00
|
|
|
for _, nw := range container.NetworkSettings.Networks {
|
2016-08-24 02:50:15 +03:00
|
|
|
if nw.EndpointSettings == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-06-06 03:04:33 +03:00
|
|
|
if nw.NetworkID == value {
|
|
|
|
return networkExist
|
|
|
|
}
|
|
|
|
}
|
2016-05-27 22:20:31 +03:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != networkExist {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-16 01:53:06 +03:00
|
|
|
if len(ctx.publish) > 0 {
|
|
|
|
shouldSkip := true
|
|
|
|
for port := range ctx.publish {
|
|
|
|
if _, ok := container.HostConfig.PortBindings[port]; ok {
|
|
|
|
shouldSkip = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if shouldSkip {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ctx.expose) > 0 {
|
|
|
|
shouldSkip := true
|
|
|
|
for port := range ctx.expose {
|
|
|
|
if _, ok := container.Config.ExposedPorts[port]; ok {
|
|
|
|
shouldSkip = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if shouldSkip {
|
|
|
|
return excludeContainer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-26 11:16:53 +03:00
|
|
|
return includeContainer
|
|
|
|
}
|
2015-05-01 04:13:18 +03:00
|
|
|
|
2015-08-26 11:16:53 +03:00
|
|
|
// transformContainer generates the container type expected by the docker ps command.
|
2015-11-12 22:55:17 +03:00
|
|
|
func (daemon *Daemon) transformContainer(container *container.Container, ctx *listContext) (*types.Container, error) {
|
2015-08-26 11:16:53 +03:00
|
|
|
newC := &types.Container{
|
2015-10-02 17:48:35 +03:00
|
|
|
ID: container.ID,
|
|
|
|
Names: ctx.names[container.ID],
|
2015-11-19 01:20:54 +03:00
|
|
|
ImageID: container.ImageID.String(),
|
2015-08-26 11:16:53 +03:00
|
|
|
}
|
2015-10-02 23:54:27 +03:00
|
|
|
if newC.Names == nil {
|
2016-03-17 20:15:32 +03:00
|
|
|
// Dead containers will often have no name, so make sure the response isn't null
|
2015-10-02 23:54:27 +03:00
|
|
|
newC.Names = []string{}
|
|
|
|
}
|
2014-07-30 06:04:39 +04:00
|
|
|
|
2015-11-19 01:20:54 +03:00
|
|
|
image := container.Config.Image // if possible keep the original ref
|
|
|
|
if image != container.ImageID.String() {
|
|
|
|
id, err := daemon.GetImageID(image)
|
|
|
|
if _, isDNE := err.(ErrImageDoesNotExist); err != nil && !isDNE {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err != nil || id != container.ImageID {
|
|
|
|
image = container.ImageID.String()
|
|
|
|
}
|
2015-08-26 11:16:53 +03:00
|
|
|
}
|
2015-11-19 01:20:54 +03:00
|
|
|
newC.Image = image
|
2015-04-04 03:39:06 +03:00
|
|
|
|
2015-08-26 11:16:53 +03:00
|
|
|
if len(container.Args) > 0 {
|
|
|
|
args := []string{}
|
|
|
|
for _, arg := range container.Args {
|
|
|
|
if strings.Contains(arg, " ") {
|
|
|
|
args = append(args, fmt.Sprintf("'%s'", arg))
|
|
|
|
} else {
|
|
|
|
args = append(args, arg)
|
2015-04-04 03:39:06 +03:00
|
|
|
}
|
2014-07-30 06:04:39 +04:00
|
|
|
}
|
2015-08-26 11:16:53 +03:00
|
|
|
argsAsString := strings.Join(args, " ")
|
2015-04-04 03:39:06 +03:00
|
|
|
|
2015-08-26 11:16:53 +03:00
|
|
|
newC.Command = fmt.Sprintf("%s %s", container.Path, argsAsString)
|
|
|
|
} else {
|
|
|
|
newC.Command = container.Path
|
2014-07-30 06:04:39 +04:00
|
|
|
}
|
2015-08-26 11:16:53 +03:00
|
|
|
newC.Created = container.Created.Unix()
|
2015-12-29 17:35:37 +03:00
|
|
|
newC.State = container.State.StateString()
|
2015-08-26 11:16:53 +03:00
|
|
|
newC.Status = container.State.String()
|
2015-11-12 22:55:17 +03:00
|
|
|
newC.HostConfig.NetworkMode = string(container.HostConfig.NetworkMode)
|
2016-01-09 00:41:28 +03:00
|
|
|
// copy networks to avoid races
|
|
|
|
networks := make(map[string]*networktypes.EndpointSettings)
|
|
|
|
for name, network := range container.NetworkSettings.Networks {
|
2016-08-24 02:50:15 +03:00
|
|
|
if network == nil || network.EndpointSettings == nil {
|
2016-01-09 00:41:28 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
networks[name] = &networktypes.EndpointSettings{
|
|
|
|
EndpointID: network.EndpointID,
|
|
|
|
Gateway: network.Gateway,
|
|
|
|
IPAddress: network.IPAddress,
|
|
|
|
IPPrefixLen: network.IPPrefixLen,
|
|
|
|
IPv6Gateway: network.IPv6Gateway,
|
|
|
|
GlobalIPv6Address: network.GlobalIPv6Address,
|
|
|
|
GlobalIPv6PrefixLen: network.GlobalIPv6PrefixLen,
|
|
|
|
MacAddress: network.MacAddress,
|
2016-06-16 22:31:23 +03:00
|
|
|
NetworkID: network.NetworkID,
|
2016-01-09 00:41:28 +03:00
|
|
|
}
|
|
|
|
if network.IPAMConfig != nil {
|
|
|
|
networks[name].IPAMConfig = &networktypes.EndpointIPAMConfig{
|
|
|
|
IPv4Address: network.IPAMConfig.IPv4Address,
|
|
|
|
IPv6Address: network.IPAMConfig.IPv6Address,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newC.NetworkSettings = &types.SummaryNetworkSettings{Networks: networks}
|
2014-07-30 06:04:39 +04:00
|
|
|
|
2015-08-26 11:16:53 +03:00
|
|
|
newC.Ports = []types.Port{}
|
|
|
|
for port, bindings := range container.NetworkSettings.Ports {
|
|
|
|
p, err := nat.ParsePort(port.Port())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(bindings) == 0 {
|
|
|
|
newC.Ports = append(newC.Ports, types.Port{
|
2016-10-03 21:49:49 +03:00
|
|
|
PrivatePort: uint16(p),
|
2015-08-26 11:16:53 +03:00
|
|
|
Type: port.Proto(),
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, binding := range bindings {
|
|
|
|
h, err := nat.ParsePort(binding.HostPort)
|
|
|
|
if err != nil {
|
2015-04-07 22:34:30 +03:00
|
|
|
return nil, err
|
2014-07-30 06:04:39 +04:00
|
|
|
}
|
2015-08-26 11:16:53 +03:00
|
|
|
newC.Ports = append(newC.Ports, types.Port{
|
2016-10-03 21:49:49 +03:00
|
|
|
PrivatePort: uint16(p),
|
|
|
|
PublicPort: uint16(h),
|
2015-08-26 11:16:53 +03:00
|
|
|
Type: port.Proto(),
|
|
|
|
IP: binding.HostIP,
|
|
|
|
})
|
2014-07-30 06:04:39 +04:00
|
|
|
}
|
|
|
|
}
|
2015-08-26 11:16:53 +03:00
|
|
|
|
|
|
|
newC.Labels = container.Config.Labels
|
2016-02-04 01:46:01 +03:00
|
|
|
newC.Mounts = addMountPoints(container)
|
2015-08-26 11:16:53 +03:00
|
|
|
|
|
|
|
return newC, nil
|
2014-07-30 06:04:39 +04:00
|
|
|
}
|
2015-06-12 16:25:32 +03:00
|
|
|
|
2015-07-31 00:01:53 +03:00
|
|
|
// Volumes lists known volumes, using the filter to restrict the range
|
|
|
|
// of volumes returned.
|
2015-09-23 23:29:14 +03:00
|
|
|
func (daemon *Daemon) Volumes(filter string) ([]*types.Volume, []string, error) {
|
2016-01-04 11:37:01 +03:00
|
|
|
var (
|
2016-03-21 10:39:48 +03:00
|
|
|
volumesOut []*types.Volume
|
2016-01-04 11:37:01 +03:00
|
|
|
)
|
2015-06-12 16:25:32 +03:00
|
|
|
volFilters, err := filters.FromParam(filter)
|
|
|
|
if err != nil {
|
2015-09-23 23:29:14 +03:00
|
|
|
return nil, nil, err
|
2015-06-12 16:25:32 +03:00
|
|
|
}
|
|
|
|
|
2016-01-04 11:37:01 +03:00
|
|
|
if err := volFilters.Validate(acceptedVolumeFilterTags); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2015-09-23 23:29:14 +03:00
|
|
|
volumes, warnings, err := daemon.volumes.List()
|
2016-06-04 18:24:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2016-03-21 10:39:48 +03:00
|
|
|
filterVolumes, err := daemon.filterVolumes(volumes, volFilters)
|
2015-09-23 23:29:14 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2016-03-21 10:39:48 +03:00
|
|
|
for _, v := range filterVolumes {
|
2016-04-13 00:09:55 +03: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 16:25:32 +03:00
|
|
|
}
|
2015-09-23 23:29:14 +03:00
|
|
|
return volumesOut, warnings, nil
|
2015-06-12 16:25:32 +03:00
|
|
|
}
|
2015-08-20 10:57:15 +03:00
|
|
|
|
2016-03-21 10:39:48 +03: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 {
|
|
|
|
if filter.Include("name") {
|
|
|
|
if !filter.Match("name", vol.Name()) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if filter.Include("driver") {
|
2017-01-04 09:43:29 +03:00
|
|
|
if !filter.ExactMatch("driver", vol.DriverName()) {
|
2016-03-21 10:39:48 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2016-03-28 11:43:41 +03:00
|
|
|
if filter.Include("label") {
|
2016-09-17 22:32:31 +03:00
|
|
|
v, ok := vol.(volume.DetailedVolume)
|
2016-08-11 20:47:01 +03:00
|
|
|
if !ok {
|
|
|
|
continue
|
2016-03-28 11:43:41 +03:00
|
|
|
}
|
2016-08-11 20:47:01 +03:00
|
|
|
if !filter.MatchKVList("label", v.Labels()) {
|
|
|
|
continue
|
2016-03-28 11:43:41 +03:00
|
|
|
}
|
|
|
|
}
|
2016-03-21 10:39:48 +03:00
|
|
|
retVols = append(retVols, vol)
|
|
|
|
}
|
|
|
|
danglingOnly := false
|
|
|
|
if filter.Include("dangling") {
|
|
|
|
if filter.ExactMatch("dangling", "true") || filter.ExactMatch("dangling", "1") {
|
|
|
|
danglingOnly = true
|
|
|
|
} else if !filter.ExactMatch("dangling", "false") && !filter.ExactMatch("dangling", "0") {
|
|
|
|
return nil, fmt.Errorf("Invalid filter 'dangling=%s'", filter.Get("dangling"))
|
|
|
|
}
|
|
|
|
retVols = daemon.volumes.FilterByUsed(retVols, !danglingOnly)
|
|
|
|
}
|
|
|
|
return retVols, nil
|
|
|
|
}
|
|
|
|
|
2015-11-19 01:20:54 +03:00
|
|
|
func populateImageFilterByParents(ancestorMap map[image.ID]bool, imageID image.ID, getChildren func(image.ID) []image.ID) {
|
2015-08-20 10:57:15 +03:00
|
|
|
if !ancestorMap[imageID] {
|
2015-11-19 01:20:54 +03:00
|
|
|
for _, id := range getChildren(imageID) {
|
|
|
|
populateImageFilterByParents(ancestorMap, id, getChildren)
|
2015-08-20 10:57:15 +03:00
|
|
|
}
|
|
|
|
ancestorMap[imageID] = true
|
|
|
|
}
|
|
|
|
}
|