Skip to content

Commit

Permalink
Merge pull request #210 from pohly/concurrent-cleanup
Browse files Browse the repository at this point in the history
cleanup fix + enhancement
  • Loading branch information
k8s-ci-robot authored Jul 23, 2019
2 parents 30a72d0 + 39e311d commit 5876211
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions pkg/sanity/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package sanity
import (
"context"
"log"
"sync"

"github.com/container-storage-interface/spec/lib/go/csi"

Expand All @@ -36,7 +37,7 @@ type VolumeInfo struct {
}

// Cleanup keeps track of resources, in particular volumes, which need
// to be freed when testing is done.
// to be freed when testing is done. All methods can be called concurrently.
type Cleanup struct {
Context *SanityContext
ControllerClient csi.ControllerClient
Expand All @@ -47,11 +48,14 @@ type Cleanup struct {
// Maps from volume name to the node ID for which the volume
// is published and the volume ID.
volumes map[string]VolumeInfo
mutex sync.Mutex
}

// RegisterVolume adds or updates an entry for the volume with the
// given name.
func (cl *Cleanup) RegisterVolume(name string, info VolumeInfo) {
cl.mutex.Lock()
defer cl.mutex.Unlock()
if cl.volumes == nil {
cl.volumes = make(map[string]VolumeInfo)
}
Expand All @@ -69,13 +73,20 @@ func (cl *Cleanup) MaybeRegisterVolume(name string, vol *csi.CreateVolumeRespons
// UnregisterVolume removes the entry for the volume with the
// given name, thus preventing all cleanup operations for it.
func (cl *Cleanup) UnregisterVolume(name string) {
cl.mutex.Lock()
defer cl.mutex.Unlock()
cl.unregisterVolume(name)
}
func (cl *Cleanup) unregisterVolume(name string) {
if cl.volumes != nil {
delete(cl.volumes, name)
}
}

// DeleteVolumes stops using the registered volumes and tries to delete all of them.
func (cl *Cleanup) DeleteVolumes() {
cl.mutex.Lock()
defer cl.mutex.Unlock()
if cl.volumes == nil {
return
}
Expand All @@ -88,7 +99,7 @@ func (cl *Cleanup) DeleteVolumes() {
ctx,
&csi.NodeUnpublishVolumeRequest{
VolumeId: info.VolumeID,
TargetPath: cl.Context.TargetPath,
TargetPath: cl.Context.TargetPath + "/target",
},
); err != nil {
logger.Printf("warning: NodeUnpublishVolume: %s", err)
Expand Down Expand Up @@ -129,6 +140,6 @@ func (cl *Cleanup) DeleteVolumes() {
logger.Printf("error: DeleteVolume: %s", err)
}

cl.UnregisterVolume(name)
cl.unregisterVolume(name)
}
}

0 comments on commit 5876211

Please sign in to comment.