ibnetwork/drivers/bridge: newLink: validate before creating
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
5d722b35d9
commit
2aa24519da
3 changed files with 21 additions and 5 deletions
|
@ -1378,7 +1378,10 @@ func (d *driver) link(network *bridgeNetwork, endpoint *bridgeEndpoint, enable b
|
|||
return InvalidEndpointIDError(p)
|
||||
}
|
||||
|
||||
l := newLink(parentEndpoint.addr.IP, endpoint.addr.IP, ec.ExposedPorts, network.config.BridgeName)
|
||||
l, err := newLink(parentEndpoint.addr.IP, endpoint.addr.IP, ec.ExposedPorts, network.config.BridgeName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if enable {
|
||||
if err := l.Enable(); err != nil {
|
||||
return err
|
||||
|
@ -1402,7 +1405,10 @@ func (d *driver) link(network *bridgeNetwork, endpoint *bridgeEndpoint, enable b
|
|||
continue
|
||||
}
|
||||
|
||||
l := newLink(endpoint.addr.IP, childEndpoint.addr.IP, childEndpoint.extConnConfig.ExposedPorts, network.config.BridgeName)
|
||||
l, err := newLink(endpoint.addr.IP, childEndpoint.addr.IP, childEndpoint.extConnConfig.ExposedPorts, network.config.BridgeName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if enable {
|
||||
if err := l.Enable(); err != nil {
|
||||
return err
|
||||
|
|
|
@ -23,13 +23,20 @@ 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 net.IP, ports []types.TransportPort, bridge string) *link {
|
||||
func newLink(parentIP, childIP net.IP, ports []types.TransportPort, bridge string) (*link, error) {
|
||||
if parentIP == nil {
|
||||
return nil, fmt.Errorf("cannot link to a container with an empty parent IP address")
|
||||
}
|
||||
if childIP == nil {
|
||||
return nil, fmt.Errorf("cannot link to a container with an empty child IP address")
|
||||
}
|
||||
|
||||
return &link{
|
||||
childIP: childIP,
|
||||
parentIP: parentIP,
|
||||
ports: ports,
|
||||
bridge: bridge,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (l *link) Enable() error {
|
||||
|
|
|
@ -29,7 +29,10 @@ func TestLinkNew(t *testing.T) {
|
|||
parentIP := net.ParseIP(pIP)
|
||||
childIP := net.ParseIP(cIP)
|
||||
|
||||
l := newLink(parentIP, childIP, ports, bridgeName)
|
||||
l, err := newLink(parentIP, childIP, ports, bridgeName)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error from newlink(): %v", err)
|
||||
}
|
||||
if l == nil {
|
||||
t.FailNow()
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue