Skip to content

Commit 2798593

Browse files
authored
fix: add mutex to protect spotCache from race condition (#294)
Co-authored-by: rayhan1967 <https://github.com/Rayhan1967>
1 parent 4aad419 commit 2798593

File tree

1 file changed

+6
-0
lines changed
  • pkg/services/providers/eks/aws

1 file changed

+6
-0
lines changed

pkg/services/providers/eks/aws/aws.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"io"
77
"math"
88
"strings"
9+
"sync"
910
"time"
1011

1112
"github.com/aws/aws-sdk-go-v2/aws"
@@ -58,6 +59,7 @@ type Provider struct {
5859
apiNodeLifecycleDiscoveryEnabled bool
5960
registerClusterBuilder RegisterClusterBuilder
6061
spotCache map[string]bool
62+
spotCacheMu sync.RWMutex
6163
}
6264

6365
func (p *Provider) RegisterCluster(ctx context.Context, client castai.Client) (*types.ClusterRegistration, error) {
@@ -87,6 +89,7 @@ func (p *Provider) FilterSpot(ctx context.Context, nodes []*v1.Node) ([]*v1.Node
8789
var instanceIDs []string
8890
nodesByInstanceID := map[string]*v1.Node{}
8991

92+
p.spotCacheMu.RLock()
9093
for _, node := range nodes {
9194
lifecycle := determineLifecycle(node)
9295
if lifecycle == NodeLifecycleSpot {
@@ -110,6 +113,7 @@ func (p *Provider) FilterSpot(ctx context.Context, nodes []*v1.Node) ([]*v1.Node
110113
instanceIDs = append(instanceIDs, instanceID)
111114
nodesByInstanceID[instanceID] = node
112115
}
116+
p.spotCacheMu.RUnlock()
113117

114118
if len(instanceIDs) == 0 || !p.apiNodeLifecycleDiscoveryEnabled {
115119
return ret, nil
@@ -128,7 +132,9 @@ func (p *Provider) FilterSpot(ctx context.Context, nodes []*v1.Node) ([]*v1.Node
128132
ret = append(ret, nodesByInstanceID[instanceID])
129133
}
130134

135+
p.spotCacheMu.Lock()
131136
p.spotCache[instanceID] = isSpot
137+
p.spotCacheMu.Unlock()
132138
}
133139

134140
return ret, nil

0 commit comments

Comments
 (0)