From 06c4c5f4996bb8890fa0546de2d192217341ada8 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Thu, 29 Dec 2016 13:09:55 +0100 Subject: [PATCH] NetworksPrune: do not return warnings as error `NetworksPrune()` is designed to ignore errors encountered during removal of networks, and only print them as warnings. However, the last error encountered was returned by the function, resulting in the prune command to be reported as "failing" wheras it did not. In addition, in situations where a network failed to be removed, the networks that _were_ succesfully removed were not reported back. This patch changes the function to not return the error, and to return the list of networks that were succesfully removed at all times. Signed-off-by: Sebastiaan van Stijn --- daemon/prune.go | 55 +++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/daemon/prune.go b/daemon/prune.go index 0c0e554274..0c1815571a 100644 --- a/daemon/prune.go +++ b/daemon/prune.go @@ -190,13 +190,10 @@ func (daemon *Daemon) ImagesPrune(pruneFilters filters.Args) (*types.ImagesPrune } // localNetworksPrune removes unused local networks -func (daemon *Daemon) localNetworksPrune(pruneFilters filters.Args) (*types.NetworksPruneReport, error) { +func (daemon *Daemon) localNetworksPrune(pruneFilters filters.Args) *types.NetworksPruneReport { rep := &types.NetworksPruneReport{} - until, err := getUntilFromPruneFilters(pruneFilters) - if err != nil { - return rep, err - } + until, _ := getUntilFromPruneFilters(pruneFilters) // When the function returns true, the walk will stop. l := func(nw libnetwork.Network) bool { @@ -204,28 +201,28 @@ func (daemon *Daemon) localNetworksPrune(pruneFilters filters.Args) (*types.Netw return false } nwName := nw.Name() - predefined := runconfig.IsPreDefinedNetwork(nwName) - if !predefined && len(nw.Endpoints()) == 0 { - if err = daemon.DeleteNetwork(nw.ID()); err != nil { - logrus.Warnf("could not remove network %s: %v", nwName, err) - return false - } - rep.NetworksDeleted = append(rep.NetworksDeleted, nwName) + 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 + } + rep.NetworksDeleted = append(rep.NetworksDeleted, nwName) return false } daemon.netController.WalkNetworks(l) - return rep, err + return rep } // clusterNetworksPrune removes unused cluster networks func (daemon *Daemon) clusterNetworksPrune(pruneFilters filters.Args) (*types.NetworksPruneReport, error) { rep := &types.NetworksPruneReport{} - until, err := getUntilFromPruneFilters(pruneFilters) - if err != nil { - return nil, err - } + until, _ := getUntilFromPruneFilters(pruneFilters) cluster := daemon.GetCluster() networks, err := cluster.GetNetworks() @@ -248,7 +245,7 @@ func (daemon *Daemon) clusterNetworksPrune(pruneFilters filters.Args) (*types.Ne // 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 network %s: %v", nw.Name, err) + logrus.Warnf("could not remove cluster network %s: %v", nw.Name, err) } continue } @@ -259,20 +256,20 @@ func (daemon *Daemon) clusterNetworksPrune(pruneFilters filters.Args) (*types.Ne // NetworksPrune removes unused networks func (daemon *Daemon) NetworksPrune(pruneFilters filters.Args) (*types.NetworksPruneReport, error) { - rep := &types.NetworksPruneReport{} + if _, err := getUntilFromPruneFilters(pruneFilters); err != nil { + return nil, err + } + clusterRep, err := daemon.clusterNetworksPrune(pruneFilters) if err != nil { - logrus.Warnf("could not remove cluster networks: %v", err) - } else { - rep.NetworksDeleted = append(rep.NetworksDeleted, clusterRep.NetworksDeleted...) + return nil, fmt.Errorf("could not remove cluster networks: %s", err) } - localRep, err := daemon.localNetworksPrune(pruneFilters) - if err != nil { - logrus.Warnf("could not remove local networks: %v", err) - } else { - rep.NetworksDeleted = append(rep.NetworksDeleted, localRep.NetworksDeleted...) - } - return rep, err + rep := &types.NetworksPruneReport{} + rep.NetworksDeleted = append(rep.NetworksDeleted, clusterRep.NetworksDeleted...) + + localRep := daemon.localNetworksPrune(pruneFilters) + rep.NetworksDeleted = append(rep.NetworksDeleted, localRep.NetworksDeleted...) + return rep, nil } func getUntilFromPruneFilters(pruneFilters filters.Args) (time.Time, error) {