55 "fmt"
66 "os"
77 "strconv"
8+ "strings"
89 "sync"
910 "time"
1011
@@ -19,28 +20,40 @@ import (
1920)
2021
2122type routeCache struct {
22- sync.RWMutex
23+ Mu sync.RWMutex
2324 routes map [int ][]linodego.VPCIP
2425 lastUpdate time.Time
2526 ttl time.Duration
2627}
2728
29+ // RefreshCache checks if cache has expired and updates it accordingly
2830func (rc * routeCache ) refreshRoutes (ctx context.Context , client client.Client ) error {
29- rc .Lock ()
30- defer rc .Unlock ()
31+ rc .Mu . Lock ()
32+ defer rc .Mu . Unlock ()
3133
3234 if time .Since (rc .lastUpdate ) < rc .ttl {
3335 return nil
3436 }
3537
3638 vpcNodes := map [int ][]linodego.VPCIP {}
37- vpcID := vpcInfo .getID ()
38- resp , err := client .ListVPCIPAddresses (ctx , vpcID , linodego .NewListOptions (0 , "" ))
39- if err != nil {
40- return err
41- }
42- for _ , r := range resp {
43- vpcNodes [r .LinodeID ] = append (vpcNodes [r .LinodeID ], r )
39+ vpcNames := strings .Split (Options .VPCNames , "," )
40+ for _ , v := range vpcNames {
41+ vpcName := strings .TrimSpace (v )
42+ if vpcName == "" {
43+ continue
44+ }
45+ vpcID , err := GetVPCID (client , strings .TrimSpace (vpcName ))
46+ if err != nil {
47+ klog .Errorf ("failed updating cache for VPC %s. Error: %s" , vpcName , err .Error ())
48+ continue
49+ }
50+ resp , err := client .ListVPCIPAddresses (ctx , vpcID , linodego .NewListOptions (0 , "" ))
51+ if err != nil {
52+ return err
53+ }
54+ for _ , r := range resp {
55+ vpcNodes [r .LinodeID ] = append (vpcNodes [r .LinodeID ], r )
56+ }
4457 }
4558
4659 rc .routes = vpcNodes
@@ -49,7 +62,6 @@ func (rc *routeCache) refreshRoutes(ctx context.Context, client client.Client) e
4962}
5063
5164type routes struct {
52- vpcid int
5365 client client.Client
5466 instances * instances
5567 routeCache * routeCache
@@ -64,13 +76,11 @@ func newRoutes(client client.Client) (cloudprovider.Routes, error) {
6476 }
6577 klog .V (3 ).Infof ("TTL for routeCache set to %d seconds" , timeout )
6678
67- vpcid := vpcInfo .getID ()
68- if Options .EnableRouteController && vpcid == 0 {
69- return nil , fmt .Errorf ("cannot enable route controller as vpc [%s] not found" , Options .VPCName )
79+ if Options .EnableRouteController && Options .VPCNames == "" {
80+ return nil , fmt .Errorf ("cannot enable route controller as vpc-names is empty" )
7081 }
7182
7283 return & routes {
73- vpcid : vpcid ,
7484 client : client ,
7585 instances : newInstances (client ),
7686 routeCache : & routeCache {
@@ -82,8 +92,8 @@ func newRoutes(client client.Client) (cloudprovider.Routes, error) {
8292
8393// instanceRoutesByID returns routes for given instance id
8494func (r * routes ) instanceRoutesByID (id int ) ([]linodego.VPCIP , error ) {
85- r .routeCache .RLock ()
86- defer r .routeCache .RUnlock ()
95+ r .routeCache .Mu . RLock ()
96+ defer r .routeCache .Mu . RUnlock ()
8797 instanceRoutes , ok := r .routeCache .routes [id ]
8898 if ! ok {
8999 return nil , fmt .Errorf ("no routes found for instance %d" , id )
@@ -135,22 +145,25 @@ func (r *routes) CreateRoute(ctx context.Context, clusterName string, nameHint s
135145 // check already configured routes
136146 intfRoutes := []string {}
137147 intfVPCIP := linodego.VPCIP {}
138- for _ , ir := range instanceRoutes {
139- if ir .VPCID != r .vpcid {
140- continue
141- }
142148
143- if ir .Address != nil {
144- intfVPCIP = ir
145- continue
146- }
149+ for _ , vpcid := range GetAllVPCIDs () {
150+ for _ , ir := range instanceRoutes {
151+ if ir .VPCID != vpcid {
152+ continue
153+ }
147154
148- if ir .AddressRange != nil && * ir . AddressRange == route . DestinationCIDR {
149- klog . V ( 4 ). Infof ( "Route already exists for node %s" , route . TargetNode )
150- return nil
151- }
155+ if ir .Address != nil {
156+ intfVPCIP = ir
157+ continue
158+ }
152159
153- intfRoutes = append (intfRoutes , * ir .AddressRange )
160+ if ir .AddressRange != nil && * ir .AddressRange == route .DestinationCIDR {
161+ klog .V (4 ).Infof ("Route already exists for node %s" , route .TargetNode )
162+ return nil
163+ }
164+
165+ intfRoutes = append (intfRoutes , * ir .AddressRange )
166+ }
154167 }
155168
156169 if intfVPCIP .Address == nil {
@@ -185,21 +198,24 @@ func (r *routes) DeleteRoute(ctx context.Context, clusterName string, route *clo
185198 // check already configured routes
186199 intfRoutes := []string {}
187200 intfVPCIP := linodego.VPCIP {}
188- for _ , ir := range instanceRoutes {
189- if ir .VPCID != r .vpcid {
190- continue
191- }
192201
193- if ir .Address != nil {
194- intfVPCIP = ir
195- continue
196- }
202+ for _ , vpcid := range GetAllVPCIDs () {
203+ for _ , ir := range instanceRoutes {
204+ if ir .VPCID != vpcid {
205+ continue
206+ }
197207
198- if ir .AddressRange != nil && * ir .AddressRange == route .DestinationCIDR {
199- continue
200- }
208+ if ir .Address != nil {
209+ intfVPCIP = ir
210+ continue
211+ }
212+
213+ if ir .AddressRange != nil && * ir .AddressRange == route .DestinationCIDR {
214+ continue
215+ }
201216
202- intfRoutes = append (intfRoutes , * ir .AddressRange )
217+ intfRoutes = append (intfRoutes , * ir .AddressRange )
218+ }
203219 }
204220
205221 if intfVPCIP .Address == nil {
@@ -234,17 +250,19 @@ func (r *routes) ListRoutes(ctx context.Context, clusterName string) ([]*cloudpr
234250 }
235251
236252 // check for configured routes
237- for _ , ir := range instanceRoutes {
238- if ir .Address != nil || ir .VPCID != r .vpcid {
239- continue
240- }
253+ for _ , vpcid := range GetAllVPCIDs () {
254+ for _ , ir := range instanceRoutes {
255+ if ir .Address != nil || ir .VPCID != vpcid {
256+ continue
257+ }
241258
242- if ir .AddressRange != nil {
243- route := & cloudprovider.Route {
244- TargetNode : types .NodeName (instance .Label ),
245- DestinationCIDR : * ir .AddressRange ,
259+ if ir .AddressRange != nil {
260+ route := & cloudprovider.Route {
261+ TargetNode : types .NodeName (instance .Label ),
262+ DestinationCIDR : * ir .AddressRange ,
263+ }
264+ configuredRoutes = append (configuredRoutes , route )
246265 }
247- configuredRoutes = append (configuredRoutes , route )
248266 }
249267 }
250268 }
0 commit comments