Skip to content

Commit 889d815

Browse files
Fix concurrent access to volumeIDToPvcMap from ListVolumes() and CreateVolume() workflows (#4096)
1 parent bc84923 commit 889d815

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

pkg/csi/service/common/commonco/k8sorchestrator/k8sorchestrator.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,17 @@ func (m *volumeIDToPvcMap) get(volumeHandle string) (string, bool) {
144144
return pvcname, found
145145
}
146146

147+
// Returns a snapshot of all items from volumeIDToPvcMap in a thread safe manner.
148+
func (m *volumeIDToPvcMap) getAll() []string {
149+
m.RLock()
150+
defer m.RUnlock()
151+
volumeIDs := make([]string, 0, len(m.items))
152+
for volumeID := range m.items {
153+
volumeIDs = append(volumeIDs, volumeID)
154+
}
155+
return volumeIDs
156+
}
157+
147158
// Adds an entry to pvcToVolumeIDMap in a thread safe manner.
148159
func (m *pvcToVolumeIDMap) add(pvcName, volumeHandle string) {
149160
m.Lock()
@@ -2114,11 +2125,7 @@ func (c *K8sOrchestrator) GetVolumeAttachment(ctx context.Context, volumeId stri
21142125
// GetAllVolumes returns list of volumes in a bound state for wcp clusters.
21152126
// This will not return VCP-CSI migrated volumes.
21162127
func (c *K8sOrchestrator) GetAllVolumes() []string {
2117-
volumeIDs := make([]string, 0)
2118-
for volumeID := range c.volumeIDToPvcMap.items {
2119-
volumeIDs = append(volumeIDs, volumeID)
2120-
}
2121-
return volumeIDs
2128+
return c.volumeIDToPvcMap.getAll()
21222129
}
21232130

21242131
// AnnotateVolumeSnapshot annotates the volumesnapshot CR in k8s cluster

0 commit comments

Comments
 (0)