generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 959
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
check path existence and lock in NodeUnpublishVolume (#3284)
* check path existence and lock in NodeUnpublishVolume Signed-off-by: wangshulin <[email protected]> * use path level lock and lock for NodePublishVolume Signed-off-by: wangshulin <[email protected]> * small fix Signed-off-by: wangshulin <[email protected]> * return codes.aborted when the lock is not acquired Signed-off-by: wangshulin <[email protected]> * check target path validity Signed-off-by: wangshulin <[email protected]> * adjust order of check path and acquire lock Signed-off-by: wangshulin <[email protected]> --------- Signed-off-by: wangshulin <[email protected]>
- Loading branch information
1 parent
987e6e7
commit 950369e
Showing
3 changed files
with
98 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package utils | ||
|
||
import ( | ||
"sync" | ||
|
||
"k8s.io/apimachinery/pkg/util/sets" | ||
) | ||
|
||
type VolumeLocks struct { | ||
locks sets.String | ||
mutex sync.Mutex | ||
} | ||
|
||
func NewVolumeLocks() *VolumeLocks { | ||
return &VolumeLocks{ | ||
locks: sets.NewString(), | ||
} | ||
} | ||
|
||
// TryAcquire tries to acquire the lock for operating on resourceID and returns true if successful. | ||
// If another operation is already using resourceID, returns false. | ||
func (lock *VolumeLocks) TryAcquire(volumeID string) bool { | ||
lock.mutex.Lock() | ||
defer lock.mutex.Unlock() | ||
if lock.locks.Has(volumeID) { | ||
return false | ||
} | ||
lock.locks.Insert(volumeID) | ||
return true | ||
} | ||
|
||
// Release releases lock in volume level | ||
func (lock *VolumeLocks) Release(volumeID string) { | ||
lock.mutex.Lock() | ||
defer lock.mutex.Unlock() | ||
lock.locks.Delete(volumeID) | ||
} |