Skip to content
This repository was archived by the owner on Mar 22, 2018. It is now read-only.

Commit 0ffcf4c

Browse files
author
FengyunPan
committed
Fix conflict about getPortByIp
Currently getPortByIp() get port of instance only based on IP. If there are two instances in diffent network and the CIDR of their subnet are same, getPortByIp() will be conflict. My PR gets port based on IP and Name of instance.
1 parent f67dc2c commit 0ffcf4c

2 files changed

Lines changed: 49 additions & 32 deletions

File tree

pkg/cloudprovider/providers/openstack/openstack_loadbalancer.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -100,34 +100,6 @@ func networkExtensions(client *gophercloud.ServiceClient) (map[string]bool, erro
100100
return seen, err
101101
}
102102

103-
func getPortByIP(client *gophercloud.ServiceClient, ipAddress string) (neutronports.Port, error) {
104-
var targetPort neutronports.Port
105-
var portFound = false
106-
107-
err := neutronports.List(client, neutronports.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
108-
portList, err := neutronports.ExtractPorts(page)
109-
if err != nil {
110-
return false, err
111-
}
112-
113-
for _, port := range portList {
114-
for _, ip := range port.FixedIPs {
115-
if ip.IPAddress == ipAddress {
116-
targetPort = port
117-
portFound = true
118-
return false, nil
119-
}
120-
}
121-
}
122-
123-
return true, nil
124-
})
125-
if err == nil && !portFound {
126-
err = ErrNotFound
127-
}
128-
return targetPort, err
129-
}
130-
131103
func getFloatingIPByPortID(client *gophercloud.ServiceClient, portID string) (*floatingips.FloatingIP, error) {
132104
opts := floatingips.ListOpts{
133105
PortID: portID,

pkg/cloudprovider/providers/openstack/openstack_routes.go

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,12 @@ func (r *Routes) CreateRoute(clusterName string, nameHint string, route *cloudpr
177177
}
178178
defer onFailure.Call(unwind)
179179

180-
port, err := getPortByIP(r.network, addr)
180+
// get the port of addr on target node.
181+
portID, err := getPortIDByIP(r.compute, route.TargetNode, addr)
182+
if err != nil {
183+
return err
184+
}
185+
port, err := getPortByID(r.network, portID)
181186
if err != nil {
182187
return err
183188
}
@@ -195,7 +200,7 @@ func (r *Routes) CreateRoute(clusterName string, nameHint string, route *cloudpr
195200
newPairs := append(port.AllowedAddressPairs, neutronports.AddressPair{
196201
IPAddress: route.DestinationCIDR,
197202
})
198-
unwind, err := updateAllowedAddressPairs(r.network, &port, newPairs)
203+
unwind, err := updateAllowedAddressPairs(r.network, port, newPairs)
199204
if err != nil {
200205
return err
201206
}
@@ -246,7 +251,12 @@ func (r *Routes) DeleteRoute(clusterName string, route *cloudprovider.Route) err
246251
}
247252
defer onFailure.Call(unwind)
248253

249-
port, err := getPortByIP(r.network, addr)
254+
// get the port of addr on target node.
255+
portID, err := getPortIDByIP(r.compute, route.TargetNode, addr)
256+
if err != nil {
257+
return err
258+
}
259+
port, err := getPortByID(r.network, portID)
250260
if err != nil {
251261
return err
252262
}
@@ -265,7 +275,7 @@ func (r *Routes) DeleteRoute(clusterName string, route *cloudprovider.Route) err
265275
addr_pairs[index] = addr_pairs[len(addr_pairs)-1]
266276
addr_pairs = addr_pairs[:len(addr_pairs)-1]
267277

268-
unwind, err := updateAllowedAddressPairs(r.network, &port, addr_pairs)
278+
unwind, err := updateAllowedAddressPairs(r.network, port, addr_pairs)
269279
if err != nil {
270280
return err
271281
}
@@ -276,3 +286,38 @@ func (r *Routes) DeleteRoute(clusterName string, route *cloudprovider.Route) err
276286
onFailure.Disarm()
277287
return nil
278288
}
289+
290+
func getPortIDByIP(compute *gophercloud.ServiceClient, targetNode types.NodeName, ipAddress string) (string, error) {
291+
srv, err := getServerByName(compute, targetNode)
292+
if err != nil {
293+
return "", err
294+
}
295+
296+
interfaces, err := getAttachedInterfacesByID(compute, srv.ID)
297+
if err != nil {
298+
return "", err
299+
}
300+
301+
for _, intf := range interfaces {
302+
for _, fixedIP := range intf.FixedIPs {
303+
if fixedIP.IPAddress == ipAddress {
304+
return intf.PortID, nil
305+
}
306+
}
307+
}
308+
309+
return "", ErrNotFound
310+
}
311+
312+
func getPortByID(client *gophercloud.ServiceClient, portID string) (*neutronports.Port, error) {
313+
targetPort, err := neutronports.Get(client, portID).Extract()
314+
if err != nil {
315+
return nil, err
316+
}
317+
318+
if targetPort == nil {
319+
return nil, ErrNotFound
320+
}
321+
322+
return targetPort, nil
323+
}

0 commit comments

Comments
 (0)