Skip to content

Commit

Permalink
s/HostIP/HostIPv4/ for com.docker.network.host_ipv4 setting
Browse files Browse the repository at this point in the history
Rename all variables/fields/map keys associated with the
`com.docker.network.host_ipv4` option from `HostIP` to `HostIPv4`.
Rationale:

  * This makes the variable/field name consistent with the option
    name.
  * This makes the code more readable because it is clear that the
    variable/field does not hold an IPv6 address.  This will hopefully
    avoid bugs like <moby#46445> in the
    future.
  * If IPv6 SNAT support is ever added, the names will be symmetric.

Signed-off-by: Richard Hansen <rhansen@rhansen.org>
  • Loading branch information
rhansen committed Oct 14, 2023
1 parent 2a14b6c commit 96f85de
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 17 deletions.
6 changes: 3 additions & 3 deletions libnetwork/drivers/bridge/bridge_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type networkConfiguration struct {
Mtu int
DefaultBindingIP net.IP
DefaultBridge bool
HostIP net.IP
HostIPv4 net.IP
ContainerIfacePrefix string
// Internal fields set after ipam data parsing
AddressIPv4 *net.IPNet
Expand Down Expand Up @@ -266,8 +266,8 @@ func (c *networkConfiguration) fromLabels(labels map[string]string) error {
}
case netlabel.ContainerIfacePrefix:
c.ContainerIfacePrefix = value
case netlabel.HostIP:
if c.HostIP = net.ParseIP(value); c.HostIP == nil {
case netlabel.HostIPv4:
if c.HostIPv4 = net.ParseIP(value); c.HostIPv4 == nil {
return parseErr(label, value, "nil ip")
}
}
Expand Down
10 changes: 5 additions & 5 deletions libnetwork/drivers/bridge/bridge_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ func TestCreateFullOptionsLabels(t *testing.T) {
}

bndIPs := "127.0.0.1"
testHostIP := "1.2.3.4"
testHostIPv4 := "1.2.3.4"
nwV6s := "2001:db8:2600:2700:2800::/80"
gwV6s := "2001:db8:2600:2700:2800::25/80"
nwV6, _ := types.ParseCIDR(nwV6s)
Expand All @@ -310,7 +310,7 @@ func TestCreateFullOptionsLabels(t *testing.T) {
EnableICC: "true",
EnableIPMasquerade: "true",
DefaultBindingIP: bndIPs,
netlabel.HostIP: testHostIP,
netlabel.HostIPv4: testHostIPv4,
}

netOption := make(map[string]interface{})
Expand Down Expand Up @@ -358,9 +358,9 @@ func TestCreateFullOptionsLabels(t *testing.T) {
t.Fatalf("Unexpected: %v", nw.config.DefaultBindingIP)
}

hostIP := net.ParseIP(testHostIP)
if !hostIP.Equal(nw.config.HostIP) {
t.Fatalf("Unexpected: %v", nw.config.HostIP)
hostIP := net.ParseIP(testHostIPv4)
if !hostIP.Equal(nw.config.HostIPv4) {
t.Fatalf("Unexpected: %v", nw.config.HostIPv4)
}

if !types.CompareIPNet(nw.config.AddressIPv6, nwV6) {
Expand Down
6 changes: 4 additions & 2 deletions libnetwork/drivers/bridge/bridge_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ func (ncfg *networkConfiguration) MarshalJSON() ([]byte, error) {
nMap["Internal"] = ncfg.Internal
nMap["DefaultBridge"] = ncfg.DefaultBridge
nMap["DefaultBindingIP"] = ncfg.DefaultBindingIP.String()
nMap["HostIP"] = ncfg.HostIP.String()
// This key is "HostIP" instead of "HostIPv4" to preserve compatibility with the on-disk format.
nMap["HostIP"] = ncfg.HostIPv4.String()
nMap["DefaultGatewayIPv4"] = ncfg.DefaultGatewayIPv4.String()
nMap["DefaultGatewayIPv6"] = ncfg.DefaultGatewayIPv6.String()
nMap["ContainerIfacePrefix"] = ncfg.ContainerIfacePrefix
Expand Down Expand Up @@ -189,8 +190,9 @@ func (ncfg *networkConfiguration) UnmarshalJSON(b []byte) error {
ncfg.ContainerIfacePrefix = v.(string)
}

// This key is "HostIP" instead of "HostIPv4" to preserve compatibility with the on-disk format.
if v, ok := nMap["HostIP"]; ok {
ncfg.HostIP = net.ParseIP(v.(string))
ncfg.HostIPv4 = net.ParseIP(v.(string))
}

ncfg.DefaultBridge = nMap["DefaultBridge"].(bool)
Expand Down
6 changes: 3 additions & 3 deletions libnetwork/drivers/bridge/setup_ip_tables_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ func setupIPTablesInternal(ipVer iptables.IPVersion, config *networkConfiguratio
natArgs []string
hpNatArgs []string
)
// If config.HostIP is set, the user wants IPv4 SNAT with the given address.
if config.HostIP != nil && ipVer == iptables.IPv4 {
hostAddr := config.HostIP.String()
// If config.HostIPv4 is set, the user wants IPv4 SNAT with the given address.
if config.HostIPv4 != nil && ipVer == iptables.IPv4 {
hostAddr := config.HostIPv4.String()
natArgs = []string{"-s", address, "!", "-o", config.BridgeName, "-j", "SNAT", "--to-source", hostAddr}
hpNatArgs = []string{"-m", "addrtype", "--src-type", "LOCAL", "-o", config.BridgeName, "-j", "SNAT", "--to-source", hostAddr}
// Else use MASQUERADE which picks the src-ip based on NH from the route table
Expand Down
4 changes: 2 additions & 2 deletions libnetwork/drivers/bridge/setup_ip_tables_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func assertBridgeConfig(config *networkConfiguration, br *bridgeInterface, d *dr
}

// Regression test for https://github.com/moby/moby/issues/46445
func TestSetupIP6TablesWithHostIP(t *testing.T) {
func TestSetupIP6TablesWithHostIPv4(t *testing.T) {
defer netnsutils.SetupTestOSContext(t)()
d := newDriver()
dc := &configuration{
Expand All @@ -174,7 +174,7 @@ func TestSetupIP6TablesWithHostIP(t *testing.T) {
EnableIPMasquerade: true,
EnableIPv6: true,
AddressIPv6: &net.IPNet{IP: net.ParseIP("2001:db8::1"), Mask: net.CIDRMask(64, 128)},
HostIP: net.ParseIP("192.0.2.2"),
HostIPv4: net.ParseIP("192.0.2.2"),
}
nh, err := netlink.NewHandle()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions libnetwork/netlabel/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ const (
// ContainerIfacePrefix can be used to override the interface prefix used inside the container
ContainerIfacePrefix = Prefix + ".container_iface_prefix"

// HostIP is the Source-IP Address used to SNAT container traffic
HostIP = Prefix + ".host_ipv4"
// HostIPv4 is the Source-IPv4 Address used to SNAT IPv4 container traffic
HostIPv4 = Prefix + ".host_ipv4"

// LocalKVClient constants represents the local kv store client
LocalKVClient = DriverPrivatePrefix + "localkv_client"
Expand Down

0 comments on commit 96f85de

Please sign in to comment.