api: Add consts for predefined networks

Constants for both platform-specific and platform-independent networks
are added to the api/network package.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>
This commit is contained in:
Albin Kerouanton 2023-09-10 13:33:21 +02:00
parent 6ce5aa1cd5
commit a8975c9042
No known key found for this signature in database
GPG key ID: 630B8E1DCBDB1864
11 changed files with 57 additions and 32 deletions

View file

@ -6,6 +6,7 @@ import (
"github.com/docker/docker/api/types/blkiodev" "github.com/docker/docker/api/types/blkiodev"
"github.com/docker/docker/api/types/mount" "github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/strslice" "github.com/docker/docker/api/types/strslice"
"github.com/docker/go-connections/nat" "github.com/docker/go-connections/nat"
units "github.com/docker/go-units" units "github.com/docker/go-units"
@ -133,12 +134,12 @@ type NetworkMode string
// IsNone indicates whether container isn't using a network stack. // IsNone indicates whether container isn't using a network stack.
func (n NetworkMode) IsNone() bool { func (n NetworkMode) IsNone() bool {
return n == "none" return n == network.NetworkNone
} }
// IsDefault indicates whether container uses the default network stack. // IsDefault indicates whether container uses the default network stack.
func (n NetworkMode) IsDefault() bool { func (n NetworkMode) IsDefault() bool {
return n == "default" return n == network.NetworkDefault
} }
// IsPrivate indicates whether container uses its private network stack. // IsPrivate indicates whether container uses its private network stack.

View file

@ -2,6 +2,8 @@
package container // import "github.com/docker/docker/api/types/container" package container // import "github.com/docker/docker/api/types/container"
import "github.com/docker/docker/api/types/network"
// IsValid indicates if an isolation technology is valid // IsValid indicates if an isolation technology is valid
func (i Isolation) IsValid() bool { func (i Isolation) IsValid() bool {
return i.IsDefault() return i.IsDefault()
@ -10,15 +12,15 @@ func (i Isolation) IsValid() bool {
// NetworkName returns the name of the network stack. // NetworkName returns the name of the network stack.
func (n NetworkMode) NetworkName() string { func (n NetworkMode) NetworkName() string {
if n.IsBridge() { if n.IsBridge() {
return "bridge" return network.NetworkBridge
} else if n.IsHost() { } else if n.IsHost() {
return "host" return network.NetworkHost
} else if n.IsContainer() { } else if n.IsContainer() {
return "container" return "container"
} else if n.IsNone() { } else if n.IsNone() {
return "none" return network.NetworkNone
} else if n.IsDefault() { } else if n.IsDefault() {
return "default" return network.NetworkDefault
} else if n.IsUserDefined() { } else if n.IsUserDefined() {
return n.UserDefined() return n.UserDefined()
} }
@ -27,12 +29,12 @@ func (n NetworkMode) NetworkName() string {
// IsBridge indicates whether container uses the bridge network stack // IsBridge indicates whether container uses the bridge network stack
func (n NetworkMode) IsBridge() bool { func (n NetworkMode) IsBridge() bool {
return n == "bridge" return n == network.NetworkBridge
} }
// IsHost indicates whether container uses the host network stack. // IsHost indicates whether container uses the host network stack.
func (n NetworkMode) IsHost() bool { func (n NetworkMode) IsHost() bool {
return n == "host" return n == network.NetworkHost
} }
// IsUserDefined indicates user-created network // IsUserDefined indicates user-created network

View file

@ -1,9 +1,11 @@
package container // import "github.com/docker/docker/api/types/container" package container // import "github.com/docker/docker/api/types/container"
import "github.com/docker/docker/api/types/network"
// IsBridge indicates whether container uses the bridge network stack // IsBridge indicates whether container uses the bridge network stack
// in windows it is given the name NAT // in windows it is given the name NAT
func (n NetworkMode) IsBridge() bool { func (n NetworkMode) IsBridge() bool {
return n == "nat" return n == network.NetworkNat
} }
// IsHost indicates whether container uses the host network stack. // IsHost indicates whether container uses the host network stack.
@ -25,11 +27,11 @@ func (i Isolation) IsValid() bool {
// NetworkName returns the name of the network stack. // NetworkName returns the name of the network stack.
func (n NetworkMode) NetworkName() string { func (n NetworkMode) NetworkName() string {
if n.IsDefault() { if n.IsDefault() {
return "default" return network.NetworkDefault
} else if n.IsBridge() { } else if n.IsBridge() {
return "nat" return network.NetworkNat
} else if n.IsNone() { } else if n.IsNone() {
return "none" return network.NetworkNone
} else if n.IsContainer() { } else if n.IsContainer() {
return "container" return "container"
} else if n.IsUserDefined() { } else if n.IsUserDefined() {

View file

@ -1,8 +1,22 @@
package network // import "github.com/docker/docker/api/types/network" package network // import "github.com/docker/docker/api/types/network"
import ( import (
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
) )
const (
// NetworkDefault is a platform-independent alias to choose the platform-specific default network stack.
NetworkDefault = "default"
// NetworkHost is the name of the predefined network used when the NetworkMode host is selected (only available on Linux)
NetworkHost = "host"
// NetworkNone is the name of the predefined network used when the NetworkMode none is selected (available on both Linux and Windows)
NetworkNone = "none"
// NetworkBridge is the name of the default network on Linux
NetworkBridge = "bridge"
// NetworkNat is the name of the default network on Windows
NetworkNat = "nat"
)
// Address represents an IP address // Address represents an IP address
type Address struct { type Address struct {
Addr string Addr string

View file

@ -23,6 +23,7 @@ import (
"github.com/docker/docker/api/types/blkiodev" "github.com/docker/docker/api/types/blkiodev"
pblkiodev "github.com/docker/docker/api/types/blkiodev" pblkiodev "github.com/docker/docker/api/types/blkiodev"
containertypes "github.com/docker/docker/api/types/container" containertypes "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/container" "github.com/docker/docker/container"
"github.com/docker/docker/daemon/config" "github.com/docker/docker/daemon/config"
"github.com/docker/docker/daemon/initlayer" "github.com/docker/docker/daemon/initlayer"
@ -857,24 +858,24 @@ func (daemon *Daemon) initNetworkController(cfg *config.Config, activeSandboxes
} }
func configureNetworking(controller *libnetwork.Controller, conf *config.Config) error { func configureNetworking(controller *libnetwork.Controller, conf *config.Config) error {
// Initialize default network on "null" // Create predefined network "none"
if n, _ := controller.NetworkByName("none"); n == nil { if n, _ := controller.NetworkByName(network.NetworkNone); n == nil {
if _, err := controller.NewNetwork("null", "none", "", libnetwork.NetworkOptionPersist(true)); err != nil { if _, err := controller.NewNetwork("null", network.NetworkNone, "", libnetwork.NetworkOptionPersist(true)); err != nil {
return errors.Wrap(err, `error creating default "null" network`) return errors.Wrapf(err, `error creating default %q network`, network.NetworkNone)
} }
} }
// Initialize default network on "host" // Create predefined network "host"
if n, _ := controller.NetworkByName("host"); n == nil { if n, _ := controller.NetworkByName(network.NetworkHost); n == nil {
if _, err := controller.NewNetwork("host", "host", "", libnetwork.NetworkOptionPersist(true)); err != nil { if _, err := controller.NewNetwork("host", network.NetworkHost, "", libnetwork.NetworkOptionPersist(true)); err != nil {
return errors.Wrap(err, `error creating default "host" network`) return errors.Wrapf(err, `error creating default %q network`, network.NetworkHost)
} }
} }
// Clear stale bridge network // Clear stale bridge network
if n, err := controller.NetworkByName("bridge"); err == nil { if n, err := controller.NetworkByName(network.NetworkBridge); err == nil {
if err = n.Delete(); err != nil { if err = n.Delete(); err != nil {
return errors.Wrap(err, `could not delete the default "bridge"" network`) return errors.Wrapf(err, `could not delete the default %q network`, network.NetworkBridge)
} }
if len(conf.NetworkConfig.DefaultAddressPools.Value()) > 0 && !conf.LiveRestoreEnabled { if len(conf.NetworkConfig.DefaultAddressPools.Value()) > 0 && !conf.LiveRestoreEnabled {
removeDefaultBridgeInterface() removeDefaultBridgeInterface()
@ -898,7 +899,7 @@ func setHostGatewayIP(controller *libnetwork.Controller, config *config.Config)
if config.HostGatewayIP != nil { if config.HostGatewayIP != nil {
return return
} }
if n, err := controller.NetworkByName("bridge"); err == nil { if n, err := controller.NetworkByName(network.NetworkBridge); err == nil {
v4Info, v6Info := n.IpamInfo() v4Info, v6Info := n.IpamInfo()
if len(v4Info) > 0 { if len(v4Info) > 0 {
config.HostGatewayIP = v4Info[0].Gateway.IP config.HostGatewayIP = v4Info[0].Gateway.IP
@ -1051,13 +1052,13 @@ func initBridgeDriver(controller *libnetwork.Controller, cfg config.BridgeConfig
v6Conf = append(v6Conf, ipamV6Conf) v6Conf = append(v6Conf, ipamV6Conf)
} }
// Initialize default network on "bridge" with the same name // Initialize default network on "bridge" with the same name
_, err = controller.NewNetwork("bridge", "bridge", "", _, err = controller.NewNetwork("bridge", network.NetworkBridge, "",
libnetwork.NetworkOptionEnableIPv6(cfg.EnableIPv6), libnetwork.NetworkOptionEnableIPv6(cfg.EnableIPv6),
libnetwork.NetworkOptionDriverOpts(netOption), libnetwork.NetworkOptionDriverOpts(netOption),
libnetwork.NetworkOptionIpam("default", "", v4Conf, v6Conf, nil), libnetwork.NetworkOptionIpam("default", "", v4Conf, v6Conf, nil),
libnetwork.NetworkOptionDeferIPv6Alloc(deferIPv6Alloc)) libnetwork.NetworkOptionDeferIPv6Alloc(deferIPv6Alloc))
if err != nil { if err != nil {
return fmt.Errorf(`error creating default "bridge" network: %v`, err) return fmt.Errorf(`error creating default %q network: %v`, network.NetworkBridge, err)
} }
return nil return nil
} }

View file

@ -12,6 +12,7 @@ import (
"github.com/Microsoft/hcsshim/osversion" "github.com/Microsoft/hcsshim/osversion"
"github.com/containerd/containerd/log" "github.com/containerd/containerd/log"
containertypes "github.com/docker/docker/api/types/container" containertypes "github.com/docker/docker/api/types/container"
networktypes "github.com/docker/docker/api/types/network"
"github.com/docker/docker/container" "github.com/docker/docker/container"
"github.com/docker/docker/daemon/config" "github.com/docker/docker/daemon/config"
"github.com/docker/docker/libcontainerd/local" "github.com/docker/docker/libcontainerd/local"
@ -261,7 +262,7 @@ func (daemon *Daemon) initNetworkController(daemonCfg *config.Config, activeSand
if !found { if !found {
// non-default nat networks should be re-created if missing from HNS // non-default nat networks should be re-created if missing from HNS
if v.Type() == "nat" && v.Name() != "nat" { if v.Type() == "nat" && v.Name() != networktypes.NetworkNat {
_, _, v4Conf, v6Conf := v.IpamConfig() _, _, v4Conf, v6Conf := v.IpamConfig()
netOption := map[string]string{} netOption := map[string]string{}
for k, v := range v.DriverOptions() { for k, v := range v.DriverOptions() {

View file

@ -270,7 +270,7 @@ func (daemon *Daemon) getBackwardsCompatibleNetworkSettings(settings *network.Se
func (daemon *Daemon) getDefaultNetworkSettings(networks map[string]*network.EndpointSettings) types.DefaultNetworkSettings { func (daemon *Daemon) getDefaultNetworkSettings(networks map[string]*network.EndpointSettings) types.DefaultNetworkSettings {
var settings types.DefaultNetworkSettings var settings types.DefaultNetworkSettings
if defaultNetwork, ok := networks["bridge"]; ok && defaultNetwork.EndpointSettings != nil { if defaultNetwork, ok := networks[networktypes.NetworkBridge]; ok && defaultNetwork.EndpointSettings != nil {
settings.EndpointID = defaultNetwork.EndpointID settings.EndpointID = defaultNetwork.EndpointID
settings.Gateway = defaultNetwork.Gateway settings.Gateway = defaultNetwork.Gateway
settings.GlobalIPv6Address = defaultNetwork.GlobalIPv6Address settings.GlobalIPv6Address = defaultNetwork.GlobalIPv6Address

View file

@ -5,6 +5,7 @@ import (
"strings" "strings"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
) )
// DecodeHostConfig creates a HostConfig based on the specified Reader. // DecodeHostConfig creates a HostConfig based on the specified Reader.
@ -23,7 +24,7 @@ func decodeHostConfig(src io.Reader) (*container.HostConfig, error) {
// docker daemon. // docker daemon.
func SetDefaultNetModeIfBlank(hc *container.HostConfig) { func SetDefaultNetModeIfBlank(hc *container.HostConfig) {
if hc != nil && hc.NetworkMode == "" { if hc != nil && hc.NetworkMode == "" {
hc.NetworkMode = "default" hc.NetworkMode = network.NetworkDefault
} }
} }

View file

@ -7,13 +7,14 @@ import (
"runtime" "runtime"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/pkg/sysinfo" "github.com/docker/docker/pkg/sysinfo"
) )
// DefaultDaemonNetworkMode returns the default network stack the daemon should // DefaultDaemonNetworkMode returns the default network stack the daemon should
// use. // use.
func DefaultDaemonNetworkMode() container.NetworkMode { func DefaultDaemonNetworkMode() container.NetworkMode {
return "bridge" return network.NetworkBridge
} }
// IsPreDefinedNetwork indicates if a network is predefined by the daemon // IsPreDefinedNetwork indicates if a network is predefined by the daemon

View file

@ -4,13 +4,14 @@ import (
"fmt" "fmt"
"github.com/docker/docker/api/types/container" "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/pkg/sysinfo" "github.com/docker/docker/pkg/sysinfo"
) )
// DefaultDaemonNetworkMode returns the default network stack the daemon should // DefaultDaemonNetworkMode returns the default network stack the daemon should
// use. // use.
func DefaultDaemonNetworkMode() container.NetworkMode { func DefaultDaemonNetworkMode() container.NetworkMode {
return "nat" return network.NetworkNat
} }
// IsPreDefinedNetwork indicates if a network is predefined by the daemon // IsPreDefinedNetwork indicates if a network is predefined by the daemon

View file

@ -8,6 +8,7 @@ import (
"github.com/docker/docker/api/types" "github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters" "github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/volume" "github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client" "github.com/docker/docker/client"
"github.com/docker/docker/errdefs" "github.com/docker/docker/errdefs"
@ -150,13 +151,13 @@ func deleteAllNetworks(ctx context.Context, t testing.TB, c client.NetworkAPICli
assert.Check(t, err, "failed to list networks") assert.Check(t, err, "failed to list networks")
for _, n := range networks { for _, n := range networks {
if n.Name == "bridge" || n.Name == "none" || n.Name == "host" { if n.Name == network.NetworkBridge || n.Name == network.NetworkNone || n.Name == network.NetworkHost {
continue continue
} }
if _, ok := protectedNetworks[n.ID]; ok { if _, ok := protectedNetworks[n.ID]; ok {
continue continue
} }
if daemonPlatform == "windows" && strings.ToLower(n.Name) == "nat" { if daemonPlatform == "windows" && strings.ToLower(n.Name) == network.NetworkNat {
// nat is a pre-defined network on Windows and cannot be removed // nat is a pre-defined network on Windows and cannot be removed
continue continue
} }