From 7b56fa8dc073e10f5a35801e5996cc17e509d9ce Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 17 Jun 2024 10:13:47 +0200 Subject: [PATCH 1/2] api/types/container: NetworkMode.NetworkName: use switch - Use a switch instead of if/else for readability and to reduce the risk of duplicates in the checks. - Align order between Windows and Linux implementation for easier comparing of differences in the implementation. - Add a check for `IsHost()` in the Windows implementation which would never occur currently, but is implemented. Signed-off-by: Sebastiaan van Stijn --- api/types/container/hostconfig_unix.go | 24 ++++++++++++----------- api/types/container/hostconfig_windows.go | 19 +++++++++++------- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/api/types/container/hostconfig_unix.go b/api/types/container/hostconfig_unix.go index 4213292378..a42fcc1d1f 100644 --- a/api/types/container/hostconfig_unix.go +++ b/api/types/container/hostconfig_unix.go @@ -11,20 +11,22 @@ func (i Isolation) IsValid() bool { // NetworkName returns the name of the network stack. func (n NetworkMode) NetworkName() string { - if n.IsBridge() { - return network.NetworkBridge - } else if n.IsHost() { - return network.NetworkHost - } else if n.IsContainer() { - return "container" - } else if n.IsNone() { - return network.NetworkNone - } else if n.IsDefault() { + switch { + case n.IsDefault(): return network.NetworkDefault - } else if n.IsUserDefined() { + case n.IsBridge(): + return network.NetworkBridge + case n.IsHost(): + return network.NetworkHost + case n.IsNone(): + return network.NetworkNone + case n.IsContainer(): + return "container" + case n.IsUserDefined(): return n.UserDefined() + default: + return "" } - return "" } // IsBridge indicates whether container uses the bridge network stack diff --git a/api/types/container/hostconfig_windows.go b/api/types/container/hostconfig_windows.go index 154667f4f0..35a03a1313 100644 --- a/api/types/container/hostconfig_windows.go +++ b/api/types/container/hostconfig_windows.go @@ -26,17 +26,22 @@ func (i Isolation) IsValid() bool { // NetworkName returns the name of the network stack. func (n NetworkMode) NetworkName() string { - if n.IsDefault() { + switch { + case n.IsDefault(): return network.NetworkDefault - } else if n.IsBridge() { + case n.IsBridge(): return network.NetworkNat - } else if n.IsNone() { + case n.IsHost(): + // Windows currently doesn't support host network-mode, so + // this would currently never happen.. + return network.NetworkHost + case n.IsNone(): return network.NetworkNone - } else if n.IsContainer() { + case n.IsContainer(): return "container" - } else if n.IsUserDefined() { + case n.IsUserDefined(): return n.UserDefined() + default: + return "" } - - return "" } From 2f45cbf69f33e6a63e1a7395a3a36166878862f9 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 17 Jun 2024 10:16:32 +0200 Subject: [PATCH 2/2] api/types/container: NetworkMode align code between Windows and Linux Change the order of declarations betwen both implementations for easier comparing of differences. Signed-off-by: Sebastiaan van Stijn --- api/types/container/hostconfig_unix.go | 30 +++++++++++------------ api/types/container/hostconfig_windows.go | 10 ++++---- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/api/types/container/hostconfig_unix.go b/api/types/container/hostconfig_unix.go index a42fcc1d1f..cdee49ea3d 100644 --- a/api/types/container/hostconfig_unix.go +++ b/api/types/container/hostconfig_unix.go @@ -9,6 +9,21 @@ func (i Isolation) IsValid() bool { return i.IsDefault() } +// IsBridge indicates whether container uses the bridge network stack +func (n NetworkMode) IsBridge() bool { + return n == network.NetworkBridge +} + +// IsHost indicates whether container uses the host network stack. +func (n NetworkMode) IsHost() bool { + return n == network.NetworkHost +} + +// IsUserDefined indicates user-created network +func (n NetworkMode) IsUserDefined() bool { + return !n.IsDefault() && !n.IsBridge() && !n.IsHost() && !n.IsNone() && !n.IsContainer() +} + // NetworkName returns the name of the network stack. func (n NetworkMode) NetworkName() string { switch { @@ -28,18 +43,3 @@ func (n NetworkMode) NetworkName() string { return "" } } - -// IsBridge indicates whether container uses the bridge network stack -func (n NetworkMode) IsBridge() bool { - return n == network.NetworkBridge -} - -// IsHost indicates whether container uses the host network stack. -func (n NetworkMode) IsHost() bool { - return n == network.NetworkHost -} - -// IsUserDefined indicates user-created network -func (n NetworkMode) IsUserDefined() bool { - return !n.IsDefault() && !n.IsBridge() && !n.IsHost() && !n.IsNone() && !n.IsContainer() -} diff --git a/api/types/container/hostconfig_windows.go b/api/types/container/hostconfig_windows.go index 35a03a1313..f08545542c 100644 --- a/api/types/container/hostconfig_windows.go +++ b/api/types/container/hostconfig_windows.go @@ -2,6 +2,11 @@ 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 +func (i Isolation) IsValid() bool { + return i.IsDefault() || i.IsHyperV() || i.IsProcess() +} + // IsBridge indicates whether container uses the bridge network stack // in windows it is given the name NAT func (n NetworkMode) IsBridge() bool { @@ -19,11 +24,6 @@ func (n NetworkMode) IsUserDefined() bool { return !n.IsDefault() && !n.IsNone() && !n.IsBridge() && !n.IsContainer() } -// IsValid indicates if an isolation technology is valid -func (i Isolation) IsValid() bool { - return i.IsDefault() || i.IsHyperV() || i.IsProcess() -} - // NetworkName returns the name of the network stack. func (n NetworkMode) NetworkName() string { switch {