2018-02-05 16:05:59 -05:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2016-08-23 19:25:43 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2016-11-17 00:46:37 -05:00
|
|
|
"fmt"
|
2016-10-18 00:36:52 -04:00
|
|
|
"regexp"
|
2017-04-12 17:04:49 -04:00
|
|
|
"sync/atomic"
|
2016-12-07 17:02:13 -05:00
|
|
|
"time"
|
2016-10-18 00:36:52 -04:00
|
|
|
|
2016-08-23 19:25:43 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2016-11-17 00:46:37 -05:00
|
|
|
"github.com/docker/docker/api/types/filters"
|
2016-12-07 17:02:13 -05:00
|
|
|
timetypes "github.com/docker/docker/api/types/time"
|
2018-03-22 17:11:03 -04:00
|
|
|
"github.com/docker/docker/errdefs"
|
2016-10-18 00:36:52 -04:00
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
"github.com/docker/libnetwork"
|
2018-03-22 17:11:03 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-04-12 17:04:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-05-16 11:37:30 -04:00
|
|
|
// errPruneRunning is returned when a prune request is received while
|
2017-04-12 17:04:49 -04:00
|
|
|
// one is in progress
|
2018-03-22 17:11:03 -04:00
|
|
|
errPruneRunning = errdefs.Conflict(errors.New("a prune operation is already running"))
|
2017-05-05 12:37:06 -04:00
|
|
|
|
|
|
|
containersAcceptedFilters = map[string]bool{
|
|
|
|
"label": true,
|
|
|
|
"label!": true,
|
|
|
|
"until": true,
|
|
|
|
}
|
2018-02-21 17:10:18 -05:00
|
|
|
|
2017-05-05 12:37:06 -04:00
|
|
|
networksAcceptedFilters = map[string]bool{
|
|
|
|
"label": true,
|
|
|
|
"label!": true,
|
|
|
|
"until": true,
|
|
|
|
}
|
2016-08-23 19:25:43 -04:00
|
|
|
)
|
|
|
|
|
2016-10-08 06:38:25 -04:00
|
|
|
// ContainersPrune removes unused containers
|
2017-04-11 15:52:33 -04:00
|
|
|
func (daemon *Daemon) ContainersPrune(ctx context.Context, pruneFilters filters.Args) (*types.ContainersPruneReport, error) {
|
2017-04-12 17:04:49 -04:00
|
|
|
if !atomic.CompareAndSwapInt32(&daemon.pruneRunning, 0, 1) {
|
2017-05-16 11:37:30 -04:00
|
|
|
return nil, errPruneRunning
|
2017-04-12 17:04:49 -04:00
|
|
|
}
|
|
|
|
defer atomic.StoreInt32(&daemon.pruneRunning, 0)
|
|
|
|
|
2016-08-23 19:25:43 -04:00
|
|
|
rep := &types.ContainersPruneReport{}
|
|
|
|
|
2017-05-05 12:37:06 -04:00
|
|
|
// make sure that only accepted filters have been received
|
|
|
|
err := pruneFilters.Validate(containersAcceptedFilters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-07 17:02:13 -05:00
|
|
|
until, err := getUntilFromPruneFilters(pruneFilters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:25:43 -04:00
|
|
|
allContainers := daemon.List()
|
|
|
|
for _, c := range allContainers {
|
2017-04-11 15:52:33 -04:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-07-06 04:47:21 -04:00
|
|
|
logrus.Debugf("ContainersPrune operation cancelled: %#v", *rep)
|
|
|
|
return rep, nil
|
2017-04-11 15:52:33 -04:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-08-23 19:25:43 -04:00
|
|
|
if !c.IsRunning() {
|
2016-12-07 17:02:13 -05:00
|
|
|
if !until.IsZero() && c.Created.After(until) {
|
|
|
|
continue
|
|
|
|
}
|
2017-02-04 12:10:05 -05:00
|
|
|
if !matchLabels(pruneFilters, c.Config.Labels) {
|
|
|
|
continue
|
|
|
|
}
|
2018-02-02 17:18:46 -05:00
|
|
|
cSize, _ := daemon.imageService.GetContainerLayerSize(c.ID)
|
2016-08-23 19:25:43 -04:00
|
|
|
// TODO: sets RmLink to true?
|
|
|
|
err := daemon.ContainerRm(c.ID, &types.ContainerRmConfig{})
|
|
|
|
if err != nil {
|
2016-10-08 06:38:25 -04:00
|
|
|
logrus.Warnf("failed to prune container %s: %v", c.ID, err)
|
2016-08-23 19:25:43 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if cSize > 0 {
|
|
|
|
rep.SpaceReclaimed += uint64(cSize)
|
|
|
|
}
|
|
|
|
rep.ContainersDeleted = append(rep.ContainersDeleted, c.ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rep, nil
|
|
|
|
}
|
|
|
|
|
2016-10-18 00:36:52 -04:00
|
|
|
// localNetworksPrune removes unused local networks
|
2017-04-11 15:52:33 -04:00
|
|
|
func (daemon *Daemon) localNetworksPrune(ctx context.Context, pruneFilters filters.Args) *types.NetworksPruneReport {
|
2016-10-18 00:36:52 -04:00
|
|
|
rep := &types.NetworksPruneReport{}
|
2016-12-07 17:02:13 -05:00
|
|
|
|
2016-12-29 07:09:55 -05:00
|
|
|
until, _ := getUntilFromPruneFilters(pruneFilters)
|
2016-12-07 17:02:13 -05:00
|
|
|
|
2016-10-18 00:36:52 -04:00
|
|
|
// When the function returns true, the walk will stop.
|
|
|
|
l := func(nw libnetwork.Network) bool {
|
2017-04-11 15:52:33 -04:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-07-06 04:47:21 -04:00
|
|
|
// context cancelled
|
2017-04-11 15:52:33 -04:00
|
|
|
return true
|
|
|
|
default:
|
|
|
|
}
|
2017-04-07 14:18:51 -04:00
|
|
|
if nw.Info().ConfigOnly() {
|
|
|
|
return false
|
|
|
|
}
|
2016-12-07 17:02:13 -05:00
|
|
|
if !until.IsZero() && nw.Info().Created().After(until) {
|
|
|
|
return false
|
|
|
|
}
|
2017-02-04 12:10:05 -05:00
|
|
|
if !matchLabels(pruneFilters, nw.Info().Labels()) {
|
|
|
|
return false
|
|
|
|
}
|
2016-10-18 00:36:52 -04:00
|
|
|
nwName := nw.Name()
|
2016-12-29 07:09:55 -05:00
|
|
|
if runconfig.IsPreDefinedNetwork(nwName) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(nw.Endpoints()) > 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if err := daemon.DeleteNetwork(nw.ID()); err != nil {
|
|
|
|
logrus.Warnf("could not remove local network %s: %v", nwName, err)
|
|
|
|
return false
|
2016-10-18 00:36:52 -04:00
|
|
|
}
|
2016-12-29 07:09:55 -05:00
|
|
|
rep.NetworksDeleted = append(rep.NetworksDeleted, nwName)
|
2016-10-18 00:36:52 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
daemon.netController.WalkNetworks(l)
|
2016-12-29 07:09:55 -05:00
|
|
|
return rep
|
2016-10-18 00:36:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// clusterNetworksPrune removes unused cluster networks
|
2017-04-11 15:52:33 -04:00
|
|
|
func (daemon *Daemon) clusterNetworksPrune(ctx context.Context, pruneFilters filters.Args) (*types.NetworksPruneReport, error) {
|
2016-10-18 00:36:52 -04:00
|
|
|
rep := &types.NetworksPruneReport{}
|
2016-12-07 17:02:13 -05:00
|
|
|
|
2016-12-29 07:09:55 -05:00
|
|
|
until, _ := getUntilFromPruneFilters(pruneFilters)
|
2016-12-07 17:02:13 -05:00
|
|
|
|
2016-10-18 00:36:52 -04:00
|
|
|
cluster := daemon.GetCluster()
|
2017-03-28 06:46:42 -04:00
|
|
|
|
|
|
|
if !cluster.IsManager() {
|
|
|
|
return rep, nil
|
|
|
|
}
|
|
|
|
|
2018-05-23 10:03:02 -04:00
|
|
|
networks, err := cluster.GetNetworks(pruneFilters)
|
2016-10-18 00:36:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return rep, err
|
|
|
|
}
|
|
|
|
networkIsInUse := regexp.MustCompile(`network ([[:alnum:]]+) is in use`)
|
|
|
|
for _, nw := range networks {
|
2017-04-11 15:52:33 -04:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-07-06 04:47:21 -04:00
|
|
|
return rep, nil
|
2017-04-11 15:52:33 -04:00
|
|
|
default:
|
|
|
|
if nw.Ingress {
|
|
|
|
// Routing-mesh network removal has to be explicitly invoked by user
|
|
|
|
continue
|
2016-10-18 00:36:52 -04:00
|
|
|
}
|
2017-04-11 15:52:33 -04:00
|
|
|
if !until.IsZero() && nw.Created.After(until) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !matchLabels(pruneFilters, nw.Labels) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// https://github.com/docker/docker/issues/24186
|
|
|
|
// `docker network inspect` unfortunately displays ONLY those containers that are local to that node.
|
|
|
|
// So we try to remove it anyway and check the error
|
|
|
|
err = cluster.RemoveNetwork(nw.ID)
|
|
|
|
if err != nil {
|
|
|
|
// we can safely ignore the "network .. is in use" error
|
|
|
|
match := networkIsInUse.FindStringSubmatch(err.Error())
|
|
|
|
if len(match) != 2 || match[1] != nw.ID {
|
|
|
|
logrus.Warnf("could not remove cluster network %s: %v", nw.Name, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
rep.NetworksDeleted = append(rep.NetworksDeleted, nw.Name)
|
2016-10-18 00:36:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rep, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NetworksPrune removes unused networks
|
2017-04-11 15:52:33 -04:00
|
|
|
func (daemon *Daemon) NetworksPrune(ctx context.Context, pruneFilters filters.Args) (*types.NetworksPruneReport, error) {
|
2017-04-12 17:04:49 -04:00
|
|
|
if !atomic.CompareAndSwapInt32(&daemon.pruneRunning, 0, 1) {
|
2017-05-16 11:37:30 -04:00
|
|
|
return nil, errPruneRunning
|
2017-04-12 17:04:49 -04:00
|
|
|
}
|
|
|
|
defer atomic.StoreInt32(&daemon.pruneRunning, 0)
|
|
|
|
|
2017-05-05 12:37:06 -04:00
|
|
|
// make sure that only accepted filters have been received
|
|
|
|
err := pruneFilters.Validate(networksAcceptedFilters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-12-29 07:09:55 -05:00
|
|
|
if _, err := getUntilFromPruneFilters(pruneFilters); err != nil {
|
|
|
|
return nil, err
|
2016-10-18 00:36:52 -04:00
|
|
|
}
|
2016-12-29 07:09:55 -05:00
|
|
|
|
|
|
|
rep := &types.NetworksPruneReport{}
|
2017-04-11 15:52:33 -04:00
|
|
|
if clusterRep, err := daemon.clusterNetworksPrune(ctx, pruneFilters); err == nil {
|
2017-02-04 12:10:05 -05:00
|
|
|
rep.NetworksDeleted = append(rep.NetworksDeleted, clusterRep.NetworksDeleted...)
|
|
|
|
}
|
2016-12-29 07:09:55 -05:00
|
|
|
|
2017-04-11 15:52:33 -04:00
|
|
|
localRep := daemon.localNetworksPrune(ctx, pruneFilters)
|
2016-12-29 07:09:55 -05:00
|
|
|
rep.NetworksDeleted = append(rep.NetworksDeleted, localRep.NetworksDeleted...)
|
2017-04-11 15:52:33 -04:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-07-06 04:47:21 -04:00
|
|
|
logrus.Debugf("NetworksPrune operation cancelled: %#v", *rep)
|
|
|
|
return rep, nil
|
2017-04-11 15:52:33 -04:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2016-12-29 07:09:55 -05:00
|
|
|
return rep, nil
|
2016-10-18 00:36:52 -04:00
|
|
|
}
|
2016-12-07 17:02:13 -05:00
|
|
|
|
|
|
|
func getUntilFromPruneFilters(pruneFilters filters.Args) (time.Time, error) {
|
|
|
|
until := time.Time{}
|
2017-09-26 07:39:56 -04:00
|
|
|
if !pruneFilters.Contains("until") {
|
2016-12-07 17:02:13 -05:00
|
|
|
return until, nil
|
|
|
|
}
|
|
|
|
untilFilters := pruneFilters.Get("until")
|
|
|
|
if len(untilFilters) > 1 {
|
|
|
|
return until, fmt.Errorf("more than one until filter specified")
|
|
|
|
}
|
|
|
|
ts, err := timetypes.GetTimestamp(untilFilters[0], time.Now())
|
|
|
|
if err != nil {
|
|
|
|
return until, err
|
|
|
|
}
|
|
|
|
seconds, nanoseconds, err := timetypes.ParseTimestamps(ts, 0)
|
|
|
|
if err != nil {
|
|
|
|
return until, err
|
|
|
|
}
|
|
|
|
until = time.Unix(seconds, nanoseconds)
|
|
|
|
return until, nil
|
|
|
|
}
|
2017-02-04 12:10:05 -05:00
|
|
|
|
|
|
|
func matchLabels(pruneFilters filters.Args, labels map[string]string) bool {
|
|
|
|
if !pruneFilters.MatchKVList("label", labels) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// By default MatchKVList will return true if field (like 'label!') does not exist
|
2017-09-26 07:39:56 -04:00
|
|
|
// So we have to add additional Contains("label!") check
|
|
|
|
if pruneFilters.Contains("label!") {
|
2017-02-04 12:10:05 -05:00
|
|
|
if pruneFilters.MatchKVList("label!", labels) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|