Skip to content

Commit c949431

Browse files
committed
fix the race
Signed-off-by: Ryan Zhang <[email protected]>
1 parent 8c1ec11 commit c949431

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

pkg/propertyprovider/azure/provider.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ type PropertyProvider struct {
106106
podControllerName string
107107

108108
// Cache for Kubernetes version information with TTL.
109-
k8sVersionMutex sync.RWMutex
109+
k8sVersionMutex sync.Mutex
110110
cachedK8sVersion string
111111
cachedK8sVersionObservedTime time.Time
112112

@@ -487,9 +487,9 @@ func (p *PropertyProvider) collectK8sVersion(_ context.Context, properties map[c
487487
now := time.Now()
488488

489489
// Check if we have a cached version that is still valid.
490-
p.k8sVersionMutex.RLock()
490+
p.k8sVersionMutex.Lock()
491+
defer p.k8sVersionMutex.Unlock()
491492
if p.cachedK8sVersion != "" && now.Sub(p.cachedK8sVersionObservedTime) < k8sVersionCacheTTL {
492-
defer p.k8sVersionMutex.RUnlock()
493493
// Cache is still valid, use the cached version.
494494
properties[propertyprovider.K8sVersionProperty] = clusterv1beta1.PropertyValue{
495495
Value: p.cachedK8sVersion,
@@ -498,7 +498,6 @@ func (p *PropertyProvider) collectK8sVersion(_ context.Context, properties map[c
498498
klog.V(2).InfoS("Using cached Kubernetes version", "version", p.cachedK8sVersion, "cacheAge", now.Sub(p.cachedK8sVersionObservedTime))
499499
return
500500
}
501-
p.k8sVersionMutex.RUnlock()
502501

503502
// Cache is expired or empty, fetch the version from the discovery client.
504503
klog.V(2).Info("Fetching Kubernetes version from discovery client")
@@ -509,11 +508,8 @@ func (p *PropertyProvider) collectK8sVersion(_ context.Context, properties map[c
509508
}
510509

511510
// Update the cache with the new version.
512-
p.k8sVersionMutex.Lock()
513511
p.cachedK8sVersion = serverVersion.GitVersion
514512
p.cachedK8sVersionObservedTime = now
515-
p.k8sVersionMutex.Unlock()
516-
517513
properties[propertyprovider.K8sVersionProperty] = clusterv1beta1.PropertyValue{
518514
Value: p.cachedK8sVersion,
519515
ObservationTime: metav1.NewTime(now),

0 commit comments

Comments
 (0)