@@ -312,6 +312,9 @@ func (r *templateRouter) reloadRouter() error {
312
312
}
313
313
314
314
func (r * templateRouter ) FilterNamespaces (namespaces sets.String ) {
315
+ r .lock .Lock ()
316
+ defer r .lock .Unlock ()
317
+
315
318
if len (namespaces ) == 0 {
316
319
r .state = make (map [string ]ServiceUnit )
317
320
}
@@ -334,34 +337,54 @@ func (r *templateRouter) CreateServiceUnit(id string) {
334
337
EndpointTable : []Endpoint {},
335
338
}
336
339
340
+ r .lock .Lock ()
341
+ defer r .lock .Unlock ()
342
+
337
343
r .state [id ] = service
338
344
}
339
345
340
- // FindServiceUnit finds the service with the given id.
341
- func (r * templateRouter ) FindServiceUnit (id string ) (ServiceUnit , bool ) {
346
+ // findMatchingServiceUnit finds the service with the given id - internal
347
+ // lockless form, caller needs to ensure lock acquisition [and release].
348
+ func (r * templateRouter ) findMatchingServiceUnit (id string ) (ServiceUnit , bool ) {
342
349
v , ok := r .state [id ]
343
350
return v , ok
344
351
}
345
352
353
+ // FindServiceUnit finds the service with the given id.
354
+ func (r * templateRouter ) FindServiceUnit (id string ) (ServiceUnit , bool ) {
355
+ r .lock .Lock ()
356
+ defer r .lock .Unlock ()
357
+
358
+ return r .findMatchingServiceUnit (id )
359
+ }
360
+
346
361
// DeleteServiceUnit deletes the service with the given id.
347
362
func (r * templateRouter ) DeleteServiceUnit (id string ) {
348
- svcUnit , ok := r .FindServiceUnit (id )
363
+ r .lock .Lock ()
364
+ defer r .lock .Unlock ()
365
+
366
+ svcUnit , ok := r .findMatchingServiceUnit (id )
349
367
if ! ok {
350
368
return
351
369
}
352
370
353
371
for _ , cfg := range svcUnit .ServiceAliasConfigs {
354
372
r .cleanUpServiceAliasConfig (& cfg )
355
373
}
374
+
356
375
delete (r .state , id )
357
376
}
358
377
359
378
// DeleteEndpoints deletes the endpoints for the service with the given id.
360
379
func (r * templateRouter ) DeleteEndpoints (id string ) {
361
- service , ok := r .FindServiceUnit (id )
380
+ r .lock .Lock ()
381
+ defer r .lock .Unlock ()
382
+
383
+ service , ok := r .findMatchingServiceUnit (id )
362
384
if ! ok {
363
385
return
364
386
}
387
+
365
388
service .EndpointTable = []Endpoint {}
366
389
367
390
r .state [id ] = service
@@ -391,8 +414,6 @@ func (r *templateRouter) routeKey(route *routeapi.Route) string {
391
414
392
415
// AddRoute adds a route for the given id
393
416
func (r * templateRouter ) AddRoute (id string , route * routeapi.Route , host string ) bool {
394
- frontend , _ := r .FindServiceUnit (id )
395
-
396
417
backendKey := r .routeKey (route )
397
418
398
419
config := ServiceAliasConfig {
@@ -450,6 +471,10 @@ func (r *templateRouter) AddRoute(id string, route *routeapi.Route, host string)
450
471
}
451
472
}
452
473
474
+ r .lock .Lock ()
475
+ defer r .lock .Unlock ()
476
+
477
+ frontend , _ := r .findMatchingServiceUnit (id )
453
478
//create or replace
454
479
frontend .ServiceAliasConfigs [backendKey ] = config
455
480
r .state [id ] = frontend
@@ -478,6 +503,9 @@ func (r *templateRouter) cleanUpdates(frontendKey string, backendKey string) {
478
503
479
504
// RemoveRoute removes the given route for the given id.
480
505
func (r * templateRouter ) RemoveRoute (id string , route * routeapi.Route ) {
506
+ r .lock .Lock ()
507
+ defer r .lock .Unlock ()
508
+
481
509
serviceUnit , ok := r .state [id ]
482
510
if ! ok {
483
511
return
@@ -494,7 +522,9 @@ func (r *templateRouter) RemoveRoute(id string, route *routeapi.Route) {
494
522
495
523
// AddEndpoints adds new Endpoints for the given id.
496
524
func (r * templateRouter ) AddEndpoints (id string , endpoints []Endpoint ) bool {
497
- frontend , _ := r .FindServiceUnit (id )
525
+ r .lock .Lock ()
526
+ defer r .lock .Unlock ()
527
+ frontend , _ := r .findMatchingServiceUnit (id )
498
528
499
529
//only make the change if there is a difference
500
530
if reflect .DeepEqual (frontend .EndpointTable , endpoints ) {
0 commit comments