forked from moby/swarmkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle VolumeAssignment from dispatcher
Signed-off-by: Ameya Gawde <agawde@mirantis.com>
- Loading branch information
Showing
5 changed files
with
183 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package csi | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/docker/swarmkit/agent/exec" | ||
"github.com/docker/swarmkit/api" | ||
) | ||
|
||
// volumes is a map that keeps all the currently available volumes to the agent | ||
// mapped by volume ID. | ||
type volumes struct { | ||
mu sync.RWMutex | ||
m map[string]*api.VolumeAssignment | ||
plugin NodePlugin | ||
} | ||
|
||
// NewManager returns a place to store volumes. | ||
func NewManager() exec.VolumesManager { | ||
return &volumes{ | ||
m: make(map[string]*api.VolumeAssignment), | ||
} | ||
} | ||
|
||
// Get returns a volume by ID. If the volume doesn't exist, returns nil. | ||
func (r *volumes) Get(volumesID string) (*api.VolumeAssignment, error) { | ||
r.mu.RLock() | ||
defer r.mu.RUnlock() | ||
if r, ok := r.m[volumesID]; ok { | ||
return r, nil | ||
} | ||
return nil, fmt.Errorf("volume %s not found", volumesID) | ||
} | ||
|
||
// Add adds one or more volumes to the volume map. | ||
func (r *volumes) Add(volumes ...api.VolumeAssignment) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
for _, volume := range volumes { | ||
r.plugin.NodeStageVolume(context.Background(), &volume) | ||
r.m[volume.VolumeID] = volume.Copy() | ||
} | ||
} | ||
|
||
// Remove removes one or more volumes by ID from the volumes map. Succeeds | ||
// whether or not the given IDs are in the map. | ||
func (r *volumes) Remove(volumes []string) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
for _, volume := range volumes { | ||
r.plugin.NodeUnstageVolume(context.Background(), r.m[volume]) | ||
delete(r.m, volume) | ||
} | ||
} | ||
|
||
// Reset removes all the volumes. | ||
func (r *volumes) Reset() { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
r.m = make(map[string]*api.VolumeAssignment) | ||
} |
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