Browse code

runconfig: deprecate IsPreDefinedNetwork

Move the function internal to the daemon, where it's used. Deliberately
not mentioning the new location, as this function should not be used
externally.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2024/06/17 17:48:38
Showing 9 changed files
... ...
@@ -11,7 +11,6 @@ import (
11 11
 	"github.com/docker/docker/daemon/cluster/convert"
12 12
 	networkSettings "github.com/docker/docker/daemon/network"
13 13
 	"github.com/docker/docker/errdefs"
14
-	"github.com/docker/docker/runconfig"
15 14
 	swarmapi "github.com/moby/swarmkit/v2/api"
16 15
 	"github.com/pkg/errors"
17 16
 )
... ...
@@ -269,7 +268,7 @@ func (c *Cluster) DetachNetwork(target string, containerID string) error {
269 269
 
270 270
 // CreateNetwork creates a new cluster managed network.
271 271
 func (c *Cluster) CreateNetwork(s network.CreateRequest) (string, error) {
272
-	if runconfig.IsPreDefinedNetwork(s.Name) {
272
+	if networkSettings.IsPredefined(s.Name) {
273 273
 		err := notAllowedError(fmt.Sprintf("%s is a pre-defined network and cannot be created", s.Name))
274 274
 		return "", errors.WithStack(err)
275 275
 	}
... ...
@@ -314,7 +313,7 @@ func (c *Cluster) populateNetworkID(ctx context.Context, client swarmapi.Control
314 314
 		apiNetwork, err := getNetwork(ctx, client, nw.Target)
315 315
 		if err != nil {
316 316
 			ln, _ := c.config.Backend.FindNetwork(nw.Target)
317
-			if ln != nil && runconfig.IsPreDefinedNetwork(ln.Name()) {
317
+			if ln != nil && networkSettings.IsPredefined(ln.Name()) {
318 318
 				// Need to retrieve the corresponding predefined swarm network
319 319
 				// and use its id for the request.
320 320
 				apiNetwork, err = getNetwork(ctx, client, ln.Name())
... ...
@@ -31,7 +31,6 @@ import (
31 31
 	lntypes "github.com/docker/docker/libnetwork/types"
32 32
 	"github.com/docker/docker/opts"
33 33
 	"github.com/docker/docker/pkg/plugingetter"
34
-	"github.com/docker/docker/runconfig"
35 34
 	"github.com/docker/go-connections/nat"
36 35
 )
37 36
 
... ...
@@ -290,7 +289,7 @@ func (daemon *Daemon) CreateNetwork(create networktypes.CreateRequest) (*network
290 290
 }
291 291
 
292 292
 func (daemon *Daemon) createNetwork(cfg *config.Config, create networktypes.CreateRequest, id string, agent bool) (*networktypes.CreateResponse, error) {
293
-	if runconfig.IsPreDefinedNetwork(create.Name) {
293
+	if network.IsPredefined(create.Name) {
294 294
 		return nil, PredefinedNetworkError(create.Name)
295 295
 	}
296 296
 
... ...
@@ -543,13 +542,13 @@ func (daemon *Daemon) DeleteNetwork(networkID string) error {
543 543
 }
544 544
 
545 545
 func (daemon *Daemon) deleteNetwork(nw *libnetwork.Network, dynamic bool) error {
546
-	if runconfig.IsPreDefinedNetwork(nw.Name()) && !dynamic {
546
+	if network.IsPredefined(nw.Name()) && !dynamic {
547 547
 		err := fmt.Errorf("%s is a pre-defined network and cannot be removed", nw.Name())
548 548
 		return errdefs.Forbidden(err)
549 549
 	}
550 550
 
551 551
 	if dynamic && !nw.Dynamic() {
552
-		if runconfig.IsPreDefinedNetwork(nw.Name()) {
552
+		if network.IsPredefined(nw.Name()) {
553 553
 			// Predefined networks now support swarm services. Make this
554 554
 			// a no-op when cluster requests to remove the predefined network.
555 555
 			return nil
... ...
@@ -4,7 +4,6 @@ import (
4 4
 	"github.com/docker/docker/api/types/filters"
5 5
 	"github.com/docker/docker/api/types/network"
6 6
 	"github.com/docker/docker/errdefs"
7
-	"github.com/docker/docker/runconfig"
8 7
 	"github.com/pkg/errors"
9 8
 )
10 9
 
... ...
@@ -94,9 +93,9 @@ func filterNetworkByUse(nws []network.Inspect, danglingOnly bool) []network.Insp
94 94
 
95 95
 	filterFunc := func(nw network.Inspect) bool {
96 96
 		if danglingOnly {
97
-			return !runconfig.IsPreDefinedNetwork(nw.Name) && len(nw.Containers) == 0 && len(nw.Services) == 0
97
+			return !IsPredefined(nw.Name) && len(nw.Containers) == 0 && len(nw.Services) == 0
98 98
 		}
99
-		return runconfig.IsPreDefinedNetwork(nw.Name) || len(nw.Containers) > 0 || len(nw.Services) > 0
99
+		return IsPredefined(nw.Name) || len(nw.Containers) > 0 || len(nw.Services) > 0
100 100
 	}
101 101
 
102 102
 	for _, nw := range nws {
... ...
@@ -113,13 +112,13 @@ func filterNetworkByType(nws []network.Inspect, netType string) ([]network.Inspe
113 113
 	switch netType {
114 114
 	case "builtin":
115 115
 		for _, nw := range nws {
116
-			if runconfig.IsPreDefinedNetwork(nw.Name) {
116
+			if IsPredefined(nw.Name) {
117 117
 				retNws = append(retNws, nw)
118 118
 			}
119 119
 		}
120 120
 	case "custom":
121 121
 		for _, nw := range nws {
122
-			if !runconfig.IsPreDefinedNetwork(nw.Name) {
122
+			if !IsPredefined(nw.Name) {
123 123
 				retNws = append(retNws, nw)
124 124
 			}
125 125
 		}
... ...
@@ -5,3 +5,9 @@ package network
5 5
 // ([network.NetworkBridge]), and "nat" ([network.NetworkNat]) for Windows
6 6
 // containers.
7 7
 const DefaultNetwork = defaultNetwork
8
+
9
+// IsPredefined indicates if a network is predefined by the daemon.
10
+func IsPredefined(network string) bool {
11
+	// TODO(thaJeztah): check if we can align the check for both platforms
12
+	return isPreDefined(network)
13
+}
... ...
@@ -2,6 +2,14 @@
2 2
 
3 3
 package network
4 4
 
5
-import "github.com/docker/docker/api/types/network"
5
+import (
6
+	"github.com/docker/docker/api/types/container"
7
+	"github.com/docker/docker/api/types/network"
8
+)
6 9
 
7 10
 const defaultNetwork = network.NetworkBridge
11
+
12
+func isPreDefined(network string) bool {
13
+	n := container.NetworkMode(network)
14
+	return n.IsBridge() || n.IsHost() || n.IsNone() || n.IsDefault()
15
+}
... ...
@@ -1,5 +1,12 @@
1 1
 package network
2 2
 
3
-import "github.com/docker/docker/api/types/network"
3
+import (
4
+	"github.com/docker/docker/api/types/container"
5
+	"github.com/docker/docker/api/types/network"
6
+)
4 7
 
5 8
 const defaultNetwork = network.NetworkNat
9
+
10
+func isPreDefined(network string) bool {
11
+	return !container.NetworkMode(network).IsUserDefined()
12
+}
... ...
@@ -14,9 +14,9 @@ import (
14 14
 	"github.com/docker/docker/api/types/filters"
15 15
 	"github.com/docker/docker/api/types/network"
16 16
 	timetypes "github.com/docker/docker/api/types/time"
17
+	networkSettings "github.com/docker/docker/daemon/network"
17 18
 	"github.com/docker/docker/errdefs"
18 19
 	"github.com/docker/docker/libnetwork"
19
-	"github.com/docker/docker/runconfig"
20 20
 	"github.com/pkg/errors"
21 21
 )
22 22
 
... ...
@@ -121,7 +121,7 @@ func (daemon *Daemon) localNetworksPrune(ctx context.Context, pruneFilters filte
121 121
 			return false
122 122
 		}
123 123
 		nwName := nw.Name()
124
-		if runconfig.IsPreDefinedNetwork(nwName) {
124
+		if networkSettings.IsPredefined(nwName) {
125 125
 			return false
126 126
 		}
127 127
 		if len(nw.Endpoints()) > 0 {
... ...
@@ -20,6 +20,8 @@ func DefaultDaemonNetworkMode() container.NetworkMode {
20 20
 }
21 21
 
22 22
 // IsPreDefinedNetwork indicates if a network is predefined by the daemon
23
+//
24
+// Deprecated: this function is no longer used and will be removed in the next release.
23 25
 func IsPreDefinedNetwork(network string) bool {
24 26
 	n := container.NetworkMode(network)
25 27
 	return n.IsBridge() || n.IsHost() || n.IsNone() || n.IsDefault()
... ...
@@ -16,7 +16,9 @@ func DefaultDaemonNetworkMode() container.NetworkMode {
16 16
 	return network.NetworkNat
17 17
 }
18 18
 
19
-// IsPreDefinedNetwork indicates if a network is predefined by the daemon
19
+// IsPreDefinedNetwork indicates if a network is predefined by the daemon.
20
+//
21
+// Deprecated: this function is no longer used and will be removed in the next release.
20 22
 func IsPreDefinedNetwork(network string) bool {
21 23
 	return !container.NetworkMode(network).IsUserDefined()
22 24
 }