@@ -43,7 +43,7 @@ type cacheHCloudClient struct {
43
43
placementGroupCache placementGroupCache
44
44
loadBalancerCache loadBalancerCache
45
45
networkCache networkCache
46
- counterMutex sync.Mutex
46
+ mutex sync.RWMutex
47
47
serverIDCounter int64
48
48
placementGroupIDCounter int64
49
49
loadBalancerIDCounter int64
@@ -57,8 +57,8 @@ func (f *cacheHCloudClientFactory) NewClient(string) hcloudclient.Client {
57
57
58
58
// Reset implements Reset method of hcloud client interface.
59
59
func (c * cacheHCloudClient ) Reset () {
60
- c .counterMutex .Lock ()
61
- defer c .counterMutex .Unlock ()
60
+ c .mutex .Lock ()
61
+ defer c .mutex .Unlock ()
62
62
63
63
cacheHCloudClientInstance .serverCache = serverCache {}
64
64
cacheHCloudClientInstance .networkCache = networkCache {}
@@ -151,8 +151,8 @@ var defaultImage = hcloud.Image{
151
151
}
152
152
153
153
func (c * cacheHCloudClient ) CreateLoadBalancer (_ context.Context , opts hcloud.LoadBalancerCreateOpts ) (* hcloud.LoadBalancer , error ) {
154
- c .counterMutex .Lock ()
155
- defer c .counterMutex .Unlock ()
154
+ c .mutex .Lock ()
155
+ defer c .mutex .Unlock ()
156
156
157
157
// cannot have two load balancers with the same name
158
158
if _ , found := c .loadBalancerCache .nameMap [opts .Name ]; found {
@@ -189,16 +189,20 @@ func (c *cacheHCloudClient) CreateLoadBalancer(_ context.Context, opts hcloud.Lo
189
189
}
190
190
191
191
func (c * cacheHCloudClient ) DeleteLoadBalancer (_ context.Context , id int64 ) error {
192
- if _ , found := c .loadBalancerCache .idMap [id ]; ! found {
192
+ c .mutex .Lock ()
193
+ defer c .mutex .Unlock ()
194
+ lb , found := c .loadBalancerCache .idMap [id ]
195
+ if ! found {
193
196
return hcloud.Error {Code : hcloud .ErrorCodeNotFound , Message : "not found" }
194
197
}
195
- lb := c .loadBalancerCache .idMap [id ]
196
198
delete (c .loadBalancerCache .nameMap , lb .Name )
197
199
delete (c .loadBalancerCache .idMap , id )
198
200
return nil
199
201
}
200
202
201
203
func (c * cacheHCloudClient ) ListLoadBalancers (_ context.Context , opts hcloud.LoadBalancerListOpts ) ([]* hcloud.LoadBalancer , error ) {
204
+ c .mutex .RLock ()
205
+ defer c .mutex .RUnlock ()
202
206
lbs := make ([]* hcloud.LoadBalancer , 0 , len (c .loadBalancerCache .idMap ))
203
207
204
208
labels , err := utils .LabelSelectorToLabels (opts .LabelSelector )
@@ -229,6 +233,8 @@ func (c *cacheHCloudClient) ListLoadBalancers(_ context.Context, opts hcloud.Loa
229
233
}
230
234
231
235
func (c * cacheHCloudClient ) AttachLoadBalancerToNetwork (_ context.Context , lb * hcloud.LoadBalancer , opts hcloud.LoadBalancerAttachToNetworkOpts ) error {
236
+ c .mutex .Lock ()
237
+ defer c .mutex .Unlock ()
232
238
// Check if loadBalancer exists
233
239
if _ , found := c .loadBalancerCache .idMap [lb .ID ]; ! found {
234
240
return hcloud.Error {Code : hcloud .ErrorCodeNotFound , Message : "not found" }
@@ -256,6 +262,8 @@ func (c *cacheHCloudClient) AttachLoadBalancerToNetwork(_ context.Context, lb *h
256
262
}
257
263
258
264
func (c * cacheHCloudClient ) ChangeLoadBalancerType (_ context.Context , lb * hcloud.LoadBalancer , opts hcloud.LoadBalancerChangeTypeOpts ) error {
265
+ c .mutex .Lock ()
266
+ defer c .mutex .Unlock ()
259
267
// Check if loadBalancer exists
260
268
if _ , found := c .loadBalancerCache .idMap [lb .ID ]; ! found {
261
269
return hcloud.Error {Code : hcloud .ErrorCodeNotFound , Message : "not found" }
@@ -267,6 +275,8 @@ func (c *cacheHCloudClient) ChangeLoadBalancerType(_ context.Context, lb *hcloud
267
275
}
268
276
269
277
func (c * cacheHCloudClient ) ChangeLoadBalancerAlgorithm (_ context.Context , lb * hcloud.LoadBalancer , opts hcloud.LoadBalancerChangeAlgorithmOpts ) error {
278
+ c .mutex .Lock ()
279
+ defer c .mutex .Unlock ()
270
280
// Check if loadBalancer exists
271
281
if _ , found := c .loadBalancerCache .idMap [lb .ID ]; ! found {
272
282
return hcloud.Error {Code : hcloud .ErrorCodeNotFound , Message : "not found" }
@@ -278,6 +288,8 @@ func (c *cacheHCloudClient) ChangeLoadBalancerAlgorithm(_ context.Context, lb *h
278
288
}
279
289
280
290
func (c * cacheHCloudClient ) UpdateLoadBalancer (_ context.Context , lb * hcloud.LoadBalancer , opts hcloud.LoadBalancerUpdateOpts ) (* hcloud.LoadBalancer , error ) {
291
+ c .mutex .Lock ()
292
+ defer c .mutex .Unlock ()
281
293
// Check if loadBalancer exists
282
294
if _ , found := c .loadBalancerCache .idMap [lb .ID ]; ! found {
283
295
return nil , hcloud.Error {Code : hcloud .ErrorCodeNotFound , Message : "not found" }
@@ -297,6 +309,8 @@ func (c *cacheHCloudClient) UpdateLoadBalancer(_ context.Context, lb *hcloud.Loa
297
309
}
298
310
299
311
func (c * cacheHCloudClient ) AddTargetServerToLoadBalancer (_ context.Context , opts hcloud.LoadBalancerAddServerTargetOpts , lb * hcloud.LoadBalancer ) error {
312
+ c .mutex .Lock ()
313
+ defer c .mutex .Unlock ()
300
314
// Check if loadBalancer exists
301
315
if _ , found := c .loadBalancerCache .idMap [lb .ID ]; ! found {
302
316
return hcloud.Error {Code : hcloud .ErrorCodeNotFound , Message : "not found" }
@@ -321,6 +335,8 @@ func (c *cacheHCloudClient) AddTargetServerToLoadBalancer(_ context.Context, opt
321
335
}
322
336
323
337
func (c * cacheHCloudClient ) DeleteTargetServerOfLoadBalancer (_ context.Context , lb * hcloud.LoadBalancer , server * hcloud.Server ) error {
338
+ c .mutex .Lock ()
339
+ defer c .mutex .Unlock ()
324
340
// Check if loadBalancer exists
325
341
if _ , found := c .loadBalancerCache .idMap [lb .ID ]; ! found {
326
342
return hcloud.Error {Code : hcloud .ErrorCodeNotFound , Message : "not found" }
@@ -339,6 +355,8 @@ func (c *cacheHCloudClient) DeleteTargetServerOfLoadBalancer(_ context.Context,
339
355
}
340
356
341
357
func (c * cacheHCloudClient ) AddIPTargetToLoadBalancer (_ context.Context , opts hcloud.LoadBalancerAddIPTargetOpts , lb * hcloud.LoadBalancer ) error {
358
+ c .mutex .Lock ()
359
+ defer c .mutex .Unlock ()
342
360
// Check if loadBalancer exists
343
361
if _ , found := c .loadBalancerCache .idMap [lb .ID ]; ! found {
344
362
return hcloud.Error {Code : hcloud .ErrorCodeNotFound , Message : "not found" }
@@ -363,6 +381,8 @@ func (c *cacheHCloudClient) AddIPTargetToLoadBalancer(_ context.Context, opts hc
363
381
}
364
382
365
383
func (c * cacheHCloudClient ) DeleteIPTargetOfLoadBalancer (_ context.Context , lb * hcloud.LoadBalancer , ip net.IP ) error {
384
+ c .mutex .Lock ()
385
+ defer c .mutex .Unlock ()
366
386
// Check if loadBalancer exists
367
387
if _ , found := c .loadBalancerCache .idMap [lb .ID ]; ! found {
368
388
return hcloud.Error {Code : hcloud .ErrorCodeNotFound , Message : "not found" }
@@ -381,6 +401,8 @@ func (c *cacheHCloudClient) DeleteIPTargetOfLoadBalancer(_ context.Context, lb *
381
401
}
382
402
383
403
func (c * cacheHCloudClient ) AddServiceToLoadBalancer (_ context.Context , lb * hcloud.LoadBalancer , opts hcloud.LoadBalancerAddServiceOpts ) error {
404
+ c .mutex .Lock ()
405
+ defer c .mutex .Unlock ()
384
406
// Check if loadBalancer exists
385
407
if _ , found := c .loadBalancerCache .idMap [lb .ID ]; ! found {
386
408
return hcloud.Error {Code : hcloud .ErrorCodeNotFound , Message : "not found" }
@@ -402,6 +424,8 @@ func (c *cacheHCloudClient) AddServiceToLoadBalancer(_ context.Context, lb *hclo
402
424
}
403
425
404
426
func (c * cacheHCloudClient ) DeleteServiceFromLoadBalancer (_ context.Context , lb * hcloud.LoadBalancer , listenPort int ) error {
427
+ c .mutex .Lock ()
428
+ defer c .mutex .Unlock ()
405
429
// Check if loadBalancer exists
406
430
if _ , found := c .loadBalancerCache .idMap [lb .ID ]; ! found {
407
431
return hcloud.Error {Code : hcloud .ErrorCodeNotFound , Message : "not found" }
@@ -420,6 +444,8 @@ func (c *cacheHCloudClient) DeleteServiceFromLoadBalancer(_ context.Context, lb
420
444
}
421
445
422
446
func (c * cacheHCloudClient ) ListImages (_ context.Context , opts hcloud.ImageListOpts ) ([]* hcloud.Image , error ) {
447
+ c .mutex .RLock ()
448
+ defer c .mutex .RUnlock ()
423
449
if opts .Name != "" {
424
450
return nil , nil
425
451
}
@@ -444,8 +470,8 @@ func (c *cacheHCloudClient) ListImages(_ context.Context, opts hcloud.ImageListO
444
470
}
445
471
446
472
func (c * cacheHCloudClient ) CreateServer (_ context.Context , opts hcloud.ServerCreateOpts ) (* hcloud.Server , error ) {
447
- c .counterMutex .Lock ()
448
- defer c .counterMutex .Unlock ()
473
+ c .mutex .Lock ()
474
+ defer c .mutex .Unlock ()
449
475
450
476
if _ , found := c .serverCache .nameMap [opts .Name ]; found {
451
477
return nil , fmt .Errorf ("already exists" )
@@ -478,6 +504,8 @@ func (c *cacheHCloudClient) CreateServer(_ context.Context, opts hcloud.ServerCr
478
504
}
479
505
480
506
func (c * cacheHCloudClient ) AttachServerToNetwork (_ context.Context , server * hcloud.Server , opts hcloud.ServerAttachToNetworkOpts ) error {
507
+ c .mutex .Lock ()
508
+ defer c .mutex .Unlock ()
481
509
// Check if network exists
482
510
if _ , found := c .networkCache .idMap [opts .Network .ID ]; ! found {
483
511
return hcloud.Error {Code : hcloud .ErrorCodeNotFound , Message : "not found" }
@@ -501,6 +529,8 @@ func (c *cacheHCloudClient) AttachServerToNetwork(_ context.Context, server *hcl
501
529
}
502
530
503
531
func (c * cacheHCloudClient ) ListServers (_ context.Context , opts hcloud.ServerListOpts ) ([]* hcloud.Server , error ) {
532
+ c .mutex .RLock ()
533
+ defer c .mutex .RUnlock ()
504
534
servers := make ([]* hcloud.Server , 0 , len (c .serverCache .idMap ))
505
535
506
536
labels , err := utils .LabelSelectorToLabels (opts .LabelSelector )
@@ -524,10 +554,14 @@ func (c *cacheHCloudClient) ListServers(_ context.Context, opts hcloud.ServerLis
524
554
}
525
555
526
556
func (c * cacheHCloudClient ) GetServer (_ context.Context , id int64 ) (* hcloud.Server , error ) {
557
+ c .mutex .RLock ()
558
+ defer c .mutex .RUnlock ()
527
559
return c .serverCache .idMap [id ], nil
528
560
}
529
561
530
562
func (c * cacheHCloudClient ) ShutdownServer (_ context.Context , server * hcloud.Server ) error {
563
+ c .mutex .Lock ()
564
+ defer c .mutex .Unlock ()
531
565
if _ , found := c .serverCache .idMap [server .ID ]; ! found {
532
566
return hcloud.Error {Code : hcloud .ErrorCodeNotFound , Message : "not found" }
533
567
}
@@ -540,6 +574,8 @@ func (c *cacheHCloudClient) RebootServer(_ context.Context, _ *hcloud.Server) er
540
574
}
541
575
542
576
func (c * cacheHCloudClient ) PowerOnServer (_ context.Context , server * hcloud.Server ) error {
577
+ c .mutex .Lock ()
578
+ defer c .mutex .Unlock ()
543
579
if _ , found := c .serverCache .idMap [server .ID ]; ! found {
544
580
return hcloud.Error {Code : hcloud .ErrorCodeNotFound , Message : "not found" }
545
581
}
@@ -548,16 +584,22 @@ func (c *cacheHCloudClient) PowerOnServer(_ context.Context, server *hcloud.Serv
548
584
}
549
585
550
586
func (c * cacheHCloudClient ) DeleteServer (_ context.Context , server * hcloud.Server ) error {
551
- if _ , found := c .serverCache .idMap [server .ID ]; ! found {
587
+ c .mutex .Lock ()
588
+ defer c .mutex .Unlock ()
589
+
590
+ n , found := c .serverCache .idMap [server .ID ]
591
+ if ! found {
552
592
return hcloud.Error {Code : hcloud .ErrorCodeNotFound , Message : "not found" }
553
593
}
554
- n := c . serverCache . idMap [ server . ID ]
594
+
555
595
delete (c .serverCache .nameMap , n .Name )
556
596
delete (c .serverCache .idMap , server .ID )
557
597
return nil
558
598
}
559
599
560
600
func (c * cacheHCloudClient ) ListServerTypes (_ context.Context ) ([]* hcloud.ServerType , error ) {
601
+ c .mutex .RLock ()
602
+ defer c .mutex .RUnlock ()
561
603
return []* hcloud.ServerType {
562
604
{
563
605
ID : 1 ,
@@ -584,6 +626,8 @@ func (c *cacheHCloudClient) ListServerTypes(_ context.Context) ([]*hcloud.Server
584
626
}
585
627
586
628
func (c * cacheHCloudClient ) GetServerType (_ context.Context , name string ) (* hcloud.ServerType , error ) {
629
+ c .mutex .Lock ()
630
+ defer c .mutex .Unlock ()
587
631
serverType := & hcloud.ServerType {
588
632
Cores : DefaultCPUCores ,
589
633
Memory : DefaultMemoryInGB ,
@@ -607,8 +651,8 @@ func (c *cacheHCloudClient) GetServerType(_ context.Context, name string) (*hclo
607
651
}
608
652
609
653
func (c * cacheHCloudClient ) CreateNetwork (_ context.Context , opts hcloud.NetworkCreateOpts ) (* hcloud.Network , error ) {
610
- c .counterMutex .Lock ()
611
- defer c .counterMutex .Unlock ()
654
+ c .mutex .Lock ()
655
+ defer c .mutex .Unlock ()
612
656
613
657
if _ , found := c .networkCache .nameMap [opts .Name ]; found {
614
658
return nil , fmt .Errorf ("already exists" )
@@ -630,6 +674,8 @@ func (c *cacheHCloudClient) CreateNetwork(_ context.Context, opts hcloud.Network
630
674
}
631
675
632
676
func (c * cacheHCloudClient ) ListNetworks (_ context.Context , opts hcloud.NetworkListOpts ) ([]* hcloud.Network , error ) {
677
+ c .mutex .RLock ()
678
+ defer c .mutex .RUnlock ()
633
679
networks := make ([]* hcloud.Network , 0 , len (c .networkCache .idMap ))
634
680
635
681
labels , err := utils .LabelSelectorToLabels (opts .LabelSelector )
@@ -654,22 +700,26 @@ func (c *cacheHCloudClient) ListNetworks(_ context.Context, opts hcloud.NetworkL
654
700
}
655
701
656
702
func (c * cacheHCloudClient ) DeleteNetwork (_ context.Context , network * hcloud.Network ) error {
657
- if _ , found := c .networkCache .idMap [network .ID ]; ! found {
703
+ c .mutex .Lock ()
704
+ defer c .mutex .Unlock ()
705
+ n , found := c .networkCache .idMap [network .ID ]
706
+ if ! found {
658
707
return hcloud.Error {Code : hcloud .ErrorCodeNotFound , Message : "not found" }
659
708
}
660
- n := c .networkCache .idMap [network .ID ]
661
709
delete (c .networkCache .nameMap , n .Name )
662
710
delete (c .networkCache .idMap , network .ID )
663
711
return nil
664
712
}
665
713
666
714
func (c * cacheHCloudClient ) ListSSHKeys (_ context.Context , _ hcloud.SSHKeyListOpts ) ([]* hcloud.SSHKey , error ) {
715
+ c .mutex .RLock ()
716
+ defer c .mutex .RUnlock ()
667
717
return []* hcloud.SSHKey {& defaultSSHKey }, nil
668
718
}
669
719
670
720
func (c * cacheHCloudClient ) CreatePlacementGroup (_ context.Context , opts hcloud.PlacementGroupCreateOpts ) (* hcloud.PlacementGroup , error ) {
671
- c .counterMutex .Lock ()
672
- defer c .counterMutex .Unlock ()
721
+ c .mutex .Lock ()
722
+ defer c .mutex .Unlock ()
673
723
674
724
if _ , found := c .placementGroupCache .nameMap [opts .Name ]; found {
675
725
return nil , fmt .Errorf ("already exists" )
@@ -690,18 +740,22 @@ func (c *cacheHCloudClient) CreatePlacementGroup(_ context.Context, opts hcloud.
690
740
}
691
741
692
742
func (c * cacheHCloudClient ) DeletePlacementGroup (_ context.Context , id int64 ) error {
693
- if _ , found := c .placementGroupCache .idMap [id ]; ! found {
743
+ c .mutex .Lock ()
744
+ defer c .mutex .Unlock ()
745
+
746
+ n , found := c .placementGroupCache .idMap [id ]
747
+ if ! found {
694
748
return hcloud.Error {Code : hcloud .ErrorCodeNotFound , Message : "not found" }
695
749
}
696
750
697
- n := c .placementGroupCache .idMap [id ]
698
-
699
751
delete (c .placementGroupCache .nameMap , n .Name )
700
752
delete (c .placementGroupCache .idMap , id )
701
753
return nil
702
754
}
703
755
704
756
func (c * cacheHCloudClient ) ListPlacementGroups (_ context.Context , opts hcloud.PlacementGroupListOpts ) ([]* hcloud.PlacementGroup , error ) {
757
+ c .mutex .RLock ()
758
+ defer c .mutex .RUnlock ()
705
759
placementGroups := make ([]* hcloud.PlacementGroup , 0 , len (c .placementGroupCache .idMap ))
706
760
707
761
labels , err := utils .LabelSelectorToLabels (opts .LabelSelector )
@@ -726,6 +780,8 @@ func (c *cacheHCloudClient) ListPlacementGroups(_ context.Context, opts hcloud.P
726
780
}
727
781
728
782
func (c * cacheHCloudClient ) AddServerToPlacementGroup (_ context.Context , server * hcloud.Server , pg * hcloud.PlacementGroup ) error {
783
+ c .mutex .Lock ()
784
+ defer c .mutex .Unlock ()
729
785
// Check if placement group exists
730
786
if _ , found := c .placementGroupCache .idMap [pg .ID ]; ! found {
731
787
return hcloud.Error {Code : hcloud .ErrorCodeNotFound , Message : "not found" }
0 commit comments