55 "fmt"
66 "os"
77 "strconv"
8+ "strings"
89 "sync"
910 "time"
1011
@@ -19,37 +20,43 @@ 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
28- func (rc * routeCache ) refreshRoutes (ctx context.Context , client client.Client ) error {
29- rc .Lock ()
30- defer rc .Unlock ()
29+ // RefreshCache checks if cache has expired and updates it accordingly
30+ func (rc * routeCache ) refreshRoutes (ctx context.Context , client client.Client ) {
31+ rc .Mu .Lock ()
32+ defer rc .Mu .Unlock ()
3133
3234 if time .Since (rc .lastUpdate ) < rc .ttl {
33- return nil
35+ return
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+ resp , err := GetVPCIPAddresses (ctx , client , vpcName )
46+ if err != nil {
47+ klog .Errorf ("failed updating cache for VPC %s. Error: %s" , vpcName , err .Error ())
48+ continue
49+ }
50+ for _ , r := range resp {
51+ vpcNodes [r .LinodeID ] = append (vpcNodes [r .LinodeID ], r )
52+ }
4453 }
4554
4655 rc .routes = vpcNodes
4756 rc .lastUpdate = time .Now ()
48- return nil
4957}
5058
5159type routes struct {
52- vpcid int
5360 client client.Client
5461 instances * instances
5562 routeCache * routeCache
@@ -64,13 +71,11 @@ func newRoutes(client client.Client) (cloudprovider.Routes, error) {
6471 }
6572 klog .V (3 ).Infof ("TTL for routeCache set to %d seconds" , timeout )
6673
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 )
74+ if Options .EnableRouteController && Options .VPCNames == "" {
75+ return nil , fmt .Errorf ("cannot enable route controller as vpc-names is empty" )
7076 }
7177
7278 return & routes {
73- vpcid : vpcid ,
7479 client : client ,
7580 instances : newInstances (client ),
7681 routeCache : & routeCache {
@@ -82,8 +87,8 @@ func newRoutes(client client.Client) (cloudprovider.Routes, error) {
8287
8388// instanceRoutesByID returns routes for given instance id
8489func (r * routes ) instanceRoutesByID (id int ) ([]linodego.VPCIP , error ) {
85- r .routeCache .RLock ()
86- defer r .routeCache .RUnlock ()
90+ r .routeCache .Mu . RLock ()
91+ defer r .routeCache .Mu . RUnlock ()
8792 instanceRoutes , ok := r .routeCache .routes [id ]
8893 if ! ok {
8994 return nil , fmt .Errorf ("no routes found for instance %d" , id )
@@ -94,10 +99,7 @@ func (r *routes) instanceRoutesByID(id int) ([]linodego.VPCIP, error) {
9499// getInstanceRoutes returns routes for given instance id
95100// It refreshes routeCache if it has expired
96101func (r * routes ) getInstanceRoutes (ctx context.Context , id int ) ([]linodego.VPCIP , error ) {
97- if err := r .routeCache .refreshRoutes (ctx , r .client ); err != nil {
98- return nil , err
99- }
100-
102+ r .routeCache .refreshRoutes (ctx , r .client )
101103 return r .instanceRoutesByID (id )
102104}
103105
@@ -135,22 +137,25 @@ func (r *routes) CreateRoute(ctx context.Context, clusterName string, nameHint s
135137 // check already configured routes
136138 intfRoutes := []string {}
137139 intfVPCIP := linodego.VPCIP {}
138- for _ , ir := range instanceRoutes {
139- if ir .VPCID != r .vpcid {
140- continue
141- }
142140
143- if ir .Address != nil {
144- intfVPCIP = ir
145- continue
146- }
141+ for _ , vpcid := range GetAllVPCIDs () {
142+ for _ , ir := range instanceRoutes {
143+ if ir .VPCID != vpcid {
144+ continue
145+ }
147146
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- }
147+ if ir .Address != nil {
148+ intfVPCIP = ir
149+ continue
150+ }
152151
153- intfRoutes = append (intfRoutes , * ir .AddressRange )
152+ if ir .AddressRange != nil && * ir .AddressRange == route .DestinationCIDR {
153+ klog .V (4 ).Infof ("Route already exists for node %s" , route .TargetNode )
154+ return nil
155+ }
156+
157+ intfRoutes = append (intfRoutes , * ir .AddressRange )
158+ }
154159 }
155160
156161 if intfVPCIP .Address == nil {
@@ -185,21 +190,24 @@ func (r *routes) DeleteRoute(ctx context.Context, clusterName string, route *clo
185190 // check already configured routes
186191 intfRoutes := []string {}
187192 intfVPCIP := linodego.VPCIP {}
188- for _ , ir := range instanceRoutes {
189- if ir .VPCID != r .vpcid {
190- continue
191- }
192193
193- if ir .Address != nil {
194- intfVPCIP = ir
195- continue
196- }
194+ for _ , vpcid := range GetAllVPCIDs () {
195+ for _ , ir := range instanceRoutes {
196+ if ir .VPCID != vpcid {
197+ continue
198+ }
197199
198- if ir .AddressRange != nil && * ir .AddressRange == route .DestinationCIDR {
199- continue
200- }
200+ if ir .Address != nil {
201+ intfVPCIP = ir
202+ continue
203+ }
204+
205+ if ir .AddressRange != nil && * ir .AddressRange == route .DestinationCIDR {
206+ continue
207+ }
201208
202- intfRoutes = append (intfRoutes , * ir .AddressRange )
209+ intfRoutes = append (intfRoutes , * ir .AddressRange )
210+ }
203211 }
204212
205213 if intfVPCIP .Address == nil {
@@ -234,17 +242,19 @@ func (r *routes) ListRoutes(ctx context.Context, clusterName string) ([]*cloudpr
234242 }
235243
236244 // check for configured routes
237- for _ , ir := range instanceRoutes {
238- if ir .Address != nil || ir .VPCID != r .vpcid {
239- continue
240- }
245+ for _ , vpcid := range GetAllVPCIDs () {
246+ for _ , ir := range instanceRoutes {
247+ if ir .Address != nil || ir .VPCID != vpcid {
248+ continue
249+ }
241250
242- if ir .AddressRange != nil {
243- route := & cloudprovider.Route {
244- TargetNode : types .NodeName (instance .Label ),
245- DestinationCIDR : * ir .AddressRange ,
251+ if ir .AddressRange != nil {
252+ route := & cloudprovider.Route {
253+ TargetNode : types .NodeName (instance .Label ),
254+ DestinationCIDR : * ir .AddressRange ,
255+ }
256+ configuredRoutes = append (configuredRoutes , route )
246257 }
247- configuredRoutes = append (configuredRoutes , route )
248258 }
249259 }
250260 }
0 commit comments