Skip to content

Commit ae7fe54

Browse files
authored
Improve retrival of external Network (kubernetes#2014)
When attempting to fetch an external Network with IPv4 Subnets to create a Floating IP, it's possible that no Subnets from that Network are shared with the tenant, consequently it would not be possible to filter them by IPVersion and no Network would be used. This commit improve the detection of external Network by only allowing usage of Networks that is possible to detect at least one IPV4 Subnet or that is not possible to detect any info about the Subnets.
1 parent 83c02e7 commit ae7fe54

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

pkg/util/openstack/network.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/gophercloud/gophercloud/pagination"
2828

2929
"k8s.io/cloud-provider-openstack/pkg/metrics"
30+
cpoerrors "k8s.io/cloud-provider-openstack/pkg/util/errors"
3031
)
3132

3233
// GetNetworkExtensions returns an extension map.
@@ -89,36 +90,53 @@ func GetFloatingNetworkID(client *gophercloud.ServiceClient) (string, error) {
8990
networks.Network
9091
external.NetworkExternalExt
9192
}
92-
var externalNetworks []NetworkWithExternalExt
93+
var allNetworks []NetworkWithExternalExt
9394

9495
mc := metrics.NewMetricContext("network", "list")
9596
page, err := networks.List(client, networks.ListOpts{}).AllPages()
9697
if err != nil {
9798
return "", mc.ObserveRequest(err)
9899
}
99100

100-
err = networks.ExtractNetworksInto(page, &externalNetworks)
101+
err = networks.ExtractNetworksInto(page, &allNetworks)
101102
if err != nil {
102103
return "", mc.ObserveRequest(err)
103104
}
104-
for _, externalNet := range externalNetworks {
105-
if externalNet.External {
105+
106+
for _, network := range allNetworks {
107+
if network.External && len(network.Subnets) > 0 {
106108
mc := metrics.NewMetricContext("subnet", "list")
107-
page, err := subnets.List(client, subnets.ListOpts{NetworkID: externalNet.ID, IPVersion: 4}).AllPages()
109+
page, err := subnets.List(client, subnets.ListOpts{NetworkID: network.ID}).AllPages()
108110
if err != nil {
109111
return "", mc.ObserveRequest(err)
110112
}
111113
subnetList, err := subnets.ExtractSubnets(page)
112114
if err != nil {
113115
return "", mc.ObserveRequest(err)
114116
}
115-
if len(subnetList) == 0 {
116-
continue
117+
for _, networkSubnet := range network.Subnets {
118+
subnet := getSubnet(networkSubnet, subnetList)
119+
if subnet != nil {
120+
if subnet.IPVersion == 4 {
121+
return network.ID, nil
122+
}
123+
} else {
124+
return network.ID, nil
125+
}
117126
}
118-
return externalNet.ID, nil
119127
}
120128
}
121-
return "", nil
129+
return "", mc.ObserveRequest(cpoerrors.ErrNotFound)
130+
}
131+
132+
// getSubnet checks if a Subnet is present in the list of Subnets the tenant has access to and returns it
133+
func getSubnet(networkSubnet string, subnetList []subnets.Subnet) *subnets.Subnet {
134+
for _, subnet := range subnetList {
135+
if subnet.ID == networkSubnet {
136+
return &subnet
137+
}
138+
}
139+
return nil
122140
}
123141

124142
// GetPorts gets all the filtered ports.

0 commit comments

Comments
 (0)