Skip to content

Commit ec0e529

Browse files
authored
[occm] Skip External network with IPv6 for FIPs creation (kubernetes#1992)
OpenStack does not support creation of floating IPs with IPv6. However, it's still possible to exist external networks with IPv6 Subnets, which could break the creation of Floating IPs for a load-balancer if CCM uses it. This commit avoids this issue by making sure only external networks with IPv4 Subnets are used when creating a Floating IP.
1 parent ba150cf commit ec0e529

File tree

1 file changed

+24
-32
lines changed

1 file changed

+24
-32
lines changed

pkg/util/openstack/network.go

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ import (
2323
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
2424
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
2525
neutronports "github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
26+
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
2627
"github.com/gophercloud/gophercloud/pagination"
2728

2829
"k8s.io/cloud-provider-openstack/pkg/metrics"
29-
cpoerrors "k8s.io/cloud-provider-openstack/pkg/util/errors"
3030
)
3131

3232
// GetNetworkExtensions returns an extension map.
@@ -85,48 +85,40 @@ func GetFloatingIPByPortID(client *gophercloud.ServiceClient, portID string) (*f
8585

8686
// GetFloatingNetworkID returns a floating network ID.
8787
func GetFloatingNetworkID(client *gophercloud.ServiceClient) (string, error) {
88-
var floatingNetworkIds []string
89-
9088
type NetworkWithExternalExt struct {
9189
networks.Network
9290
external.NetworkExternalExt
9391
}
92+
var externalNetworks []NetworkWithExternalExt
9493

9594
mc := metrics.NewMetricContext("network", "list")
96-
err := networks.List(client, networks.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
97-
var externalNetwork []NetworkWithExternalExt
98-
err := networks.ExtractNetworksInto(page, &externalNetwork)
99-
if err != nil {
100-
return false, err
101-
}
102-
103-
for _, externalNet := range externalNetwork {
104-
if externalNet.External {
105-
floatingNetworkIds = append(floatingNetworkIds, externalNet.ID)
106-
}
107-
}
108-
109-
if len(floatingNetworkIds) > 1 {
110-
return false, cpoerrors.ErrMultipleResults
111-
}
112-
return true, nil
113-
})
95+
page, err := networks.List(client, networks.ListOpts{}).AllPages()
11496
if err != nil {
115-
if cpoerrors.IsNotFound(err) {
116-
return "", mc.ObserveRequest(cpoerrors.ErrNotFound)
117-
}
118-
119-
if err == cpoerrors.ErrMultipleResults {
120-
return floatingNetworkIds[0], mc.ObserveRequest(nil)
121-
}
12297
return "", mc.ObserveRequest(err)
12398
}
12499

125-
if len(floatingNetworkIds) == 0 {
126-
return "", mc.ObserveRequest(cpoerrors.ErrNotFound)
100+
err = networks.ExtractNetworksInto(page, &externalNetworks)
101+
if err != nil {
102+
return "", mc.ObserveRequest(err)
127103
}
128-
129-
return floatingNetworkIds[0], mc.ObserveRequest(nil)
104+
for _, externalNet := range externalNetworks {
105+
if externalNet.External {
106+
mc := metrics.NewMetricContext("subnet", "list")
107+
page, err := subnets.List(client, subnets.ListOpts{NetworkID: externalNet.ID, IPVersion: 4}).AllPages()
108+
if err != nil {
109+
return "", mc.ObserveRequest(err)
110+
}
111+
subnetList, err := subnets.ExtractSubnets(page)
112+
if err != nil {
113+
return "", mc.ObserveRequest(err)
114+
}
115+
if len(subnetList) == 0 {
116+
continue
117+
}
118+
return externalNet.ID, nil
119+
}
120+
}
121+
return "", nil
130122
}
131123

132124
// GetPorts gets all the filtered ports.

0 commit comments

Comments
 (0)