Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup fix + enhancement #210

Merged
merged 2 commits into from
Jul 23, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}