libnetwork/drivers/bridge: don't convert IP to string and back again

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-07-21 12:28:53 +02:00
parent 8b6203b613
commit ea5f21ceac
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
3 changed files with 26 additions and 23 deletions

View file

@ -1373,9 +1373,7 @@ func (d *driver) link(network *bridgeNetwork, endpoint *bridgeEndpoint, enable b
return err
}
l := newLink(parentEndpoint.addr.IP.String(),
endpoint.addr.IP.String(),
ec.ExposedPorts, network.config.BridgeName)
l := newLink(parentEndpoint.addr.IP, endpoint.addr.IP, ec.ExposedPorts, network.config.BridgeName)
if enable {
err = l.Enable()
if err != nil {
@ -1406,9 +1404,7 @@ func (d *driver) link(network *bridgeNetwork, endpoint *bridgeEndpoint, enable b
continue
}
l := newLink(endpoint.addr.IP.String(),
childEndpoint.addr.IP.String(),
childEndpoint.extConnConfig.ExposedPorts, network.config.BridgeName)
l := newLink(endpoint.addr.IP, childEndpoint.addr.IP, childEndpoint.extConnConfig.ExposedPorts, network.config.BridgeName)
if enable {
err = l.Enable()
if err != nil {

View file

@ -13,8 +13,8 @@ import (
)
type link struct {
parentIP string
childIP string
parentIP net.IP
childIP net.IP
ports []types.TransportPort
bridge string
}
@ -23,7 +23,7 @@ func (l *link) String() string {
return fmt.Sprintf("%s <-> %s [%v] on %s", l.parentIP, l.childIP, l.ports, l.bridge)
}
func newLink(parentIP, childIP string, ports []types.TransportPort, bridge string) *link {
func newLink(parentIP, childIP net.IP, ports []types.TransportPort, bridge string) *link {
return &link{
childIP: childIP,
parentIP: parentIP,
@ -51,19 +51,17 @@ func (l *link) Disable() {
}
}
func linkContainers(action iptables.Action, parentIP, childIP string, ports []types.TransportPort, bridge string, ignoreErrors bool) error {
ip1 := net.ParseIP(parentIP)
if ip1 == nil {
return fmt.Errorf("cannot link to a container with an invalid parent IP address %q", parentIP)
func linkContainers(action iptables.Action, parentIP, childIP net.IP, ports []types.TransportPort, bridge string, ignoreErrors bool) error {
if parentIP == nil {
return fmt.Errorf("cannot link to a container with an empty parent IP address")
}
ip2 := net.ParseIP(childIP)
if ip2 == nil {
return fmt.Errorf("cannot link to a container with an invalid child IP address %q", childIP)
if childIP == nil {
return fmt.Errorf("cannot link to a container with an empty child IP address")
}
chain := iptables.ChainInfo{Name: DockerChain}
for _, port := range ports {
err := chain.Link(action, ip1, ip2, int(port.Port), port.Proto.String(), bridge)
err := chain.Link(action, parentIP, childIP, int(port.Port), port.Proto.String(), bridge)
if !ignoreErrors && err != nil {
return err
}

View file

@ -3,6 +3,7 @@
package bridge
import (
"net"
"testing"
"github.com/docker/docker/libnetwork/types"
@ -19,23 +20,31 @@ func getPorts() []types.TransportPort {
func TestLinkNew(t *testing.T) {
ports := getPorts()
link := newLink("172.0.17.3", "172.0.17.2", ports, "docker0")
const (
pIP = "172.0.17.3"
cIP = "172.0.17.2"
bridgeName = "docker0"
)
if link == nil {
parentIP := net.ParseIP(pIP)
childIP := net.ParseIP(cIP)
l := newLink(parentIP, childIP, ports, bridgeName)
if l == nil {
t.FailNow()
}
if link.parentIP != "172.0.17.3" {
if l.parentIP.String() != pIP {
t.Fail()
}
if link.childIP != "172.0.17.2" {
if l.childIP.String() != cIP {
t.Fail()
}
for i, p := range link.ports {
for i, p := range l.ports {
if p != ports[i] {
t.Fail()
}
}
if link.bridge != "docker0" {
if l.bridge != bridgeName {
t.Fail()
}
}