forked from moby/swarmkit
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
6 changed files
with
280 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package csi | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
type MounterInterface interface { | ||
|
||
// Mount will mount source to target | ||
Mount(source string, target string, opts ...string) error | ||
// Unmount will unmount given target | ||
Unmount(target string) error | ||
} | ||
|
||
type Mounter struct{} | ||
|
||
func (m *Mounter) Mount(source, target string, opts ...string) error { | ||
|
||
if source == "" { | ||
return errors.New("mount source voule is not available") | ||
} | ||
|
||
if target == "" { | ||
return errors.New("mount target volume is not available") | ||
} | ||
|
||
// Build mount arguments | ||
var mountArgs []string | ||
if len(opts) > 0 { | ||
mountArgs = append(mountArgs, "-o", strings.Join(opts, ",")) | ||
} | ||
mountArgs = append(mountArgs, source) | ||
mountArgs = append(mountArgs, target) | ||
|
||
// Create the target | ||
err := os.MkdirAll(target, 0750) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
out, err := exec.Command("mount", mountArgs...).CombinedOutput() | ||
if err != nil { | ||
return fmt.Errorf("failure:%v cmd: 'mount %s' output: %q", err, strings.Join(mountArgs, " "), string(out)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (m *Mounter) Unmount(target string) error { | ||
if target == "" { | ||
return errors.New("mount target volume is not available") | ||
} | ||
|
||
out, err := exec.Command("umount", target).CombinedOutput() | ||
if err != nil { | ||
return fmt.Errorf("failure: %v cmd: 'umount %s' output: %q", err, target, string(out)) | ||
} | ||
|
||
return nil | ||
} |
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,75 @@ | ||
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() | ||
var volumeObjects []*api.VolumeAssignment | ||
defer r.mu.Unlock() | ||
for _, volume := range volumes { | ||
v := volume.Copy() | ||
r.m[volume.VolumeID] = v | ||
volumeObjects = append(volumeObjects, v) | ||
} | ||
go r.plugin.NodeStageVolume(context.Background(), volumeObjects) | ||
} | ||
|
||
// 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() | ||
var volumeObjects []*api.VolumeAssignment | ||
defer r.mu.Unlock() | ||
for _, volume := range volumes { | ||
v := r.m[volume] | ||
volumeObjects = append(volumeObjects, v) | ||
delete(r.m, volume) | ||
} | ||
|
||
go r.plugin.NodeUnstageVolume(context.Background(), volumeObjects) | ||
} | ||
|
||
// Reset removes all the volumes. | ||
func (r *volumes) Reset() { | ||
r.mu.Lock() | ||
var volumeObjects []*api.VolumeAssignment | ||
defer r.mu.Unlock() | ||
for _, v := range r.m { | ||
volumeObjects = append(volumeObjects, v) | ||
} | ||
r.m = make(map[string]*api.VolumeAssignment) | ||
go r.plugin.NodeUnstageVolume(context.Background(), volumeObjects) | ||
} |
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