Skip to content

Commit c275b58

Browse files
authored
🌱 Fix nil pointer in hcloud fake client (#1563)
1 parent 5b9c8aa commit c275b58

File tree

1 file changed

+76
-20
lines changed

1 file changed

+76
-20
lines changed

pkg/services/hcloud/client/fake/hcloud_client.go

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type cacheHCloudClient struct {
4343
placementGroupCache placementGroupCache
4444
loadBalancerCache loadBalancerCache
4545
networkCache networkCache
46-
counterMutex sync.Mutex
46+
mutex sync.RWMutex
4747
serverIDCounter int64
4848
placementGroupIDCounter int64
4949
loadBalancerIDCounter int64
@@ -57,8 +57,8 @@ func (f *cacheHCloudClientFactory) NewClient(string) hcloudclient.Client {
5757

5858
// Reset implements Reset method of hcloud client interface.
5959
func (c *cacheHCloudClient) Reset() {
60-
c.counterMutex.Lock()
61-
defer c.counterMutex.Unlock()
60+
c.mutex.Lock()
61+
defer c.mutex.Unlock()
6262

6363
cacheHCloudClientInstance.serverCache = serverCache{}
6464
cacheHCloudClientInstance.networkCache = networkCache{}
@@ -151,8 +151,8 @@ var defaultImage = hcloud.Image{
151151
}
152152

153153
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()
156156

157157
// cannot have two load balancers with the same name
158158
if _, found := c.loadBalancerCache.nameMap[opts.Name]; found {
@@ -189,16 +189,20 @@ func (c *cacheHCloudClient) CreateLoadBalancer(_ context.Context, opts hcloud.Lo
189189
}
190190

191191
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 {
193196
return hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}
194197
}
195-
lb := c.loadBalancerCache.idMap[id]
196198
delete(c.loadBalancerCache.nameMap, lb.Name)
197199
delete(c.loadBalancerCache.idMap, id)
198200
return nil
199201
}
200202

201203
func (c *cacheHCloudClient) ListLoadBalancers(_ context.Context, opts hcloud.LoadBalancerListOpts) ([]*hcloud.LoadBalancer, error) {
204+
c.mutex.RLock()
205+
defer c.mutex.RUnlock()
202206
lbs := make([]*hcloud.LoadBalancer, 0, len(c.loadBalancerCache.idMap))
203207

204208
labels, err := utils.LabelSelectorToLabels(opts.LabelSelector)
@@ -229,6 +233,8 @@ func (c *cacheHCloudClient) ListLoadBalancers(_ context.Context, opts hcloud.Loa
229233
}
230234

231235
func (c *cacheHCloudClient) AttachLoadBalancerToNetwork(_ context.Context, lb *hcloud.LoadBalancer, opts hcloud.LoadBalancerAttachToNetworkOpts) error {
236+
c.mutex.Lock()
237+
defer c.mutex.Unlock()
232238
// Check if loadBalancer exists
233239
if _, found := c.loadBalancerCache.idMap[lb.ID]; !found {
234240
return hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}
@@ -256,6 +262,8 @@ func (c *cacheHCloudClient) AttachLoadBalancerToNetwork(_ context.Context, lb *h
256262
}
257263

258264
func (c *cacheHCloudClient) ChangeLoadBalancerType(_ context.Context, lb *hcloud.LoadBalancer, opts hcloud.LoadBalancerChangeTypeOpts) error {
265+
c.mutex.Lock()
266+
defer c.mutex.Unlock()
259267
// Check if loadBalancer exists
260268
if _, found := c.loadBalancerCache.idMap[lb.ID]; !found {
261269
return hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}
@@ -267,6 +275,8 @@ func (c *cacheHCloudClient) ChangeLoadBalancerType(_ context.Context, lb *hcloud
267275
}
268276

269277
func (c *cacheHCloudClient) ChangeLoadBalancerAlgorithm(_ context.Context, lb *hcloud.LoadBalancer, opts hcloud.LoadBalancerChangeAlgorithmOpts) error {
278+
c.mutex.Lock()
279+
defer c.mutex.Unlock()
270280
// Check if loadBalancer exists
271281
if _, found := c.loadBalancerCache.idMap[lb.ID]; !found {
272282
return hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}
@@ -278,6 +288,8 @@ func (c *cacheHCloudClient) ChangeLoadBalancerAlgorithm(_ context.Context, lb *h
278288
}
279289

280290
func (c *cacheHCloudClient) UpdateLoadBalancer(_ context.Context, lb *hcloud.LoadBalancer, opts hcloud.LoadBalancerUpdateOpts) (*hcloud.LoadBalancer, error) {
291+
c.mutex.Lock()
292+
defer c.mutex.Unlock()
281293
// Check if loadBalancer exists
282294
if _, found := c.loadBalancerCache.idMap[lb.ID]; !found {
283295
return nil, hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}
@@ -297,6 +309,8 @@ func (c *cacheHCloudClient) UpdateLoadBalancer(_ context.Context, lb *hcloud.Loa
297309
}
298310

299311
func (c *cacheHCloudClient) AddTargetServerToLoadBalancer(_ context.Context, opts hcloud.LoadBalancerAddServerTargetOpts, lb *hcloud.LoadBalancer) error {
312+
c.mutex.Lock()
313+
defer c.mutex.Unlock()
300314
// Check if loadBalancer exists
301315
if _, found := c.loadBalancerCache.idMap[lb.ID]; !found {
302316
return hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}
@@ -321,6 +335,8 @@ func (c *cacheHCloudClient) AddTargetServerToLoadBalancer(_ context.Context, opt
321335
}
322336

323337
func (c *cacheHCloudClient) DeleteTargetServerOfLoadBalancer(_ context.Context, lb *hcloud.LoadBalancer, server *hcloud.Server) error {
338+
c.mutex.Lock()
339+
defer c.mutex.Unlock()
324340
// Check if loadBalancer exists
325341
if _, found := c.loadBalancerCache.idMap[lb.ID]; !found {
326342
return hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}
@@ -339,6 +355,8 @@ func (c *cacheHCloudClient) DeleteTargetServerOfLoadBalancer(_ context.Context,
339355
}
340356

341357
func (c *cacheHCloudClient) AddIPTargetToLoadBalancer(_ context.Context, opts hcloud.LoadBalancerAddIPTargetOpts, lb *hcloud.LoadBalancer) error {
358+
c.mutex.Lock()
359+
defer c.mutex.Unlock()
342360
// Check if loadBalancer exists
343361
if _, found := c.loadBalancerCache.idMap[lb.ID]; !found {
344362
return hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}
@@ -363,6 +381,8 @@ func (c *cacheHCloudClient) AddIPTargetToLoadBalancer(_ context.Context, opts hc
363381
}
364382

365383
func (c *cacheHCloudClient) DeleteIPTargetOfLoadBalancer(_ context.Context, lb *hcloud.LoadBalancer, ip net.IP) error {
384+
c.mutex.Lock()
385+
defer c.mutex.Unlock()
366386
// Check if loadBalancer exists
367387
if _, found := c.loadBalancerCache.idMap[lb.ID]; !found {
368388
return hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}
@@ -381,6 +401,8 @@ func (c *cacheHCloudClient) DeleteIPTargetOfLoadBalancer(_ context.Context, lb *
381401
}
382402

383403
func (c *cacheHCloudClient) AddServiceToLoadBalancer(_ context.Context, lb *hcloud.LoadBalancer, opts hcloud.LoadBalancerAddServiceOpts) error {
404+
c.mutex.Lock()
405+
defer c.mutex.Unlock()
384406
// Check if loadBalancer exists
385407
if _, found := c.loadBalancerCache.idMap[lb.ID]; !found {
386408
return hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}
@@ -402,6 +424,8 @@ func (c *cacheHCloudClient) AddServiceToLoadBalancer(_ context.Context, lb *hclo
402424
}
403425

404426
func (c *cacheHCloudClient) DeleteServiceFromLoadBalancer(_ context.Context, lb *hcloud.LoadBalancer, listenPort int) error {
427+
c.mutex.Lock()
428+
defer c.mutex.Unlock()
405429
// Check if loadBalancer exists
406430
if _, found := c.loadBalancerCache.idMap[lb.ID]; !found {
407431
return hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}
@@ -420,6 +444,8 @@ func (c *cacheHCloudClient) DeleteServiceFromLoadBalancer(_ context.Context, lb
420444
}
421445

422446
func (c *cacheHCloudClient) ListImages(_ context.Context, opts hcloud.ImageListOpts) ([]*hcloud.Image, error) {
447+
c.mutex.RLock()
448+
defer c.mutex.RUnlock()
423449
if opts.Name != "" {
424450
return nil, nil
425451
}
@@ -444,8 +470,8 @@ func (c *cacheHCloudClient) ListImages(_ context.Context, opts hcloud.ImageListO
444470
}
445471

446472
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()
449475

450476
if _, found := c.serverCache.nameMap[opts.Name]; found {
451477
return nil, fmt.Errorf("already exists")
@@ -478,6 +504,8 @@ func (c *cacheHCloudClient) CreateServer(_ context.Context, opts hcloud.ServerCr
478504
}
479505

480506
func (c *cacheHCloudClient) AttachServerToNetwork(_ context.Context, server *hcloud.Server, opts hcloud.ServerAttachToNetworkOpts) error {
507+
c.mutex.Lock()
508+
defer c.mutex.Unlock()
481509
// Check if network exists
482510
if _, found := c.networkCache.idMap[opts.Network.ID]; !found {
483511
return hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}
@@ -501,6 +529,8 @@ func (c *cacheHCloudClient) AttachServerToNetwork(_ context.Context, server *hcl
501529
}
502530

503531
func (c *cacheHCloudClient) ListServers(_ context.Context, opts hcloud.ServerListOpts) ([]*hcloud.Server, error) {
532+
c.mutex.RLock()
533+
defer c.mutex.RUnlock()
504534
servers := make([]*hcloud.Server, 0, len(c.serverCache.idMap))
505535

506536
labels, err := utils.LabelSelectorToLabels(opts.LabelSelector)
@@ -524,10 +554,14 @@ func (c *cacheHCloudClient) ListServers(_ context.Context, opts hcloud.ServerLis
524554
}
525555

526556
func (c *cacheHCloudClient) GetServer(_ context.Context, id int64) (*hcloud.Server, error) {
557+
c.mutex.RLock()
558+
defer c.mutex.RUnlock()
527559
return c.serverCache.idMap[id], nil
528560
}
529561

530562
func (c *cacheHCloudClient) ShutdownServer(_ context.Context, server *hcloud.Server) error {
563+
c.mutex.Lock()
564+
defer c.mutex.Unlock()
531565
if _, found := c.serverCache.idMap[server.ID]; !found {
532566
return hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}
533567
}
@@ -540,6 +574,8 @@ func (c *cacheHCloudClient) RebootServer(_ context.Context, _ *hcloud.Server) er
540574
}
541575

542576
func (c *cacheHCloudClient) PowerOnServer(_ context.Context, server *hcloud.Server) error {
577+
c.mutex.Lock()
578+
defer c.mutex.Unlock()
543579
if _, found := c.serverCache.idMap[server.ID]; !found {
544580
return hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}
545581
}
@@ -548,16 +584,22 @@ func (c *cacheHCloudClient) PowerOnServer(_ context.Context, server *hcloud.Serv
548584
}
549585

550586
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 {
552592
return hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}
553593
}
554-
n := c.serverCache.idMap[server.ID]
594+
555595
delete(c.serverCache.nameMap, n.Name)
556596
delete(c.serverCache.idMap, server.ID)
557597
return nil
558598
}
559599

560600
func (c *cacheHCloudClient) ListServerTypes(_ context.Context) ([]*hcloud.ServerType, error) {
601+
c.mutex.RLock()
602+
defer c.mutex.RUnlock()
561603
return []*hcloud.ServerType{
562604
{
563605
ID: 1,
@@ -584,6 +626,8 @@ func (c *cacheHCloudClient) ListServerTypes(_ context.Context) ([]*hcloud.Server
584626
}
585627

586628
func (c *cacheHCloudClient) GetServerType(_ context.Context, name string) (*hcloud.ServerType, error) {
629+
c.mutex.Lock()
630+
defer c.mutex.Unlock()
587631
serverType := &hcloud.ServerType{
588632
Cores: DefaultCPUCores,
589633
Memory: DefaultMemoryInGB,
@@ -607,8 +651,8 @@ func (c *cacheHCloudClient) GetServerType(_ context.Context, name string) (*hclo
607651
}
608652

609653
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()
612656

613657
if _, found := c.networkCache.nameMap[opts.Name]; found {
614658
return nil, fmt.Errorf("already exists")
@@ -630,6 +674,8 @@ func (c *cacheHCloudClient) CreateNetwork(_ context.Context, opts hcloud.Network
630674
}
631675

632676
func (c *cacheHCloudClient) ListNetworks(_ context.Context, opts hcloud.NetworkListOpts) ([]*hcloud.Network, error) {
677+
c.mutex.RLock()
678+
defer c.mutex.RUnlock()
633679
networks := make([]*hcloud.Network, 0, len(c.networkCache.idMap))
634680

635681
labels, err := utils.LabelSelectorToLabels(opts.LabelSelector)
@@ -654,22 +700,26 @@ func (c *cacheHCloudClient) ListNetworks(_ context.Context, opts hcloud.NetworkL
654700
}
655701

656702
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 {
658707
return hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}
659708
}
660-
n := c.networkCache.idMap[network.ID]
661709
delete(c.networkCache.nameMap, n.Name)
662710
delete(c.networkCache.idMap, network.ID)
663711
return nil
664712
}
665713

666714
func (c *cacheHCloudClient) ListSSHKeys(_ context.Context, _ hcloud.SSHKeyListOpts) ([]*hcloud.SSHKey, error) {
715+
c.mutex.RLock()
716+
defer c.mutex.RUnlock()
667717
return []*hcloud.SSHKey{&defaultSSHKey}, nil
668718
}
669719

670720
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()
673723

674724
if _, found := c.placementGroupCache.nameMap[opts.Name]; found {
675725
return nil, fmt.Errorf("already exists")
@@ -690,18 +740,22 @@ func (c *cacheHCloudClient) CreatePlacementGroup(_ context.Context, opts hcloud.
690740
}
691741

692742
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 {
694748
return hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}
695749
}
696750

697-
n := c.placementGroupCache.idMap[id]
698-
699751
delete(c.placementGroupCache.nameMap, n.Name)
700752
delete(c.placementGroupCache.idMap, id)
701753
return nil
702754
}
703755

704756
func (c *cacheHCloudClient) ListPlacementGroups(_ context.Context, opts hcloud.PlacementGroupListOpts) ([]*hcloud.PlacementGroup, error) {
757+
c.mutex.RLock()
758+
defer c.mutex.RUnlock()
705759
placementGroups := make([]*hcloud.PlacementGroup, 0, len(c.placementGroupCache.idMap))
706760

707761
labels, err := utils.LabelSelectorToLabels(opts.LabelSelector)
@@ -726,6 +780,8 @@ func (c *cacheHCloudClient) ListPlacementGroups(_ context.Context, opts hcloud.P
726780
}
727781

728782
func (c *cacheHCloudClient) AddServerToPlacementGroup(_ context.Context, server *hcloud.Server, pg *hcloud.PlacementGroup) error {
783+
c.mutex.Lock()
784+
defer c.mutex.Unlock()
729785
// Check if placement group exists
730786
if _, found := c.placementGroupCache.idMap[pg.ID]; !found {
731787
return hcloud.Error{Code: hcloud.ErrorCodeNotFound, Message: "not found"}

0 commit comments

Comments
 (0)