@@ -23,10 +23,10 @@ import (
23
23
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
24
24
"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
25
25
neutronports "github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
26
+ "github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
26
27
"github.com/gophercloud/gophercloud/pagination"
27
28
28
29
"k8s.io/cloud-provider-openstack/pkg/metrics"
29
- cpoerrors "k8s.io/cloud-provider-openstack/pkg/util/errors"
30
30
)
31
31
32
32
// GetNetworkExtensions returns an extension map.
@@ -85,48 +85,40 @@ func GetFloatingIPByPortID(client *gophercloud.ServiceClient, portID string) (*f
85
85
86
86
// GetFloatingNetworkID returns a floating network ID.
87
87
func GetFloatingNetworkID (client * gophercloud.ServiceClient ) (string , error ) {
88
- var floatingNetworkIds []string
89
-
90
88
type NetworkWithExternalExt struct {
91
89
networks.Network
92
90
external.NetworkExternalExt
93
91
}
92
+ var externalNetworks []NetworkWithExternalExt
94
93
95
94
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 ()
114
96
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
- }
122
97
return "" , mc .ObserveRequest (err )
123
98
}
124
99
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 )
127
103
}
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
130
122
}
131
123
132
124
// GetPorts gets all the filtered ports.
0 commit comments