Skip to content
This repository has been archived by the owner on Jun 18, 2022. It is now read-only.

Commit

Permalink
Merge pull request #266 from StrongMonkey/add-capability
Browse files Browse the repository at this point in the history
add capability endpoint to decide flex volume
  • Loading branch information
Craig Jellick authored Jan 31, 2018
2 parents d99c232 + 41ad7c8 commit 589ac50
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
8 changes: 6 additions & 2 deletions core/compute/compute_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ func setupVolumes(config *container.Config, instance model.Instance, hostConfig
rancherManagedVolumeNames := map[string]struct{}{}
if vMounts := instance.VolumesFromDataVolumeMounts; len(vMounts) > 0 {
for _, volume := range vMounts {
if storage.IsRancherVolume(volume) {
if ok, err := storage.IsRancherVolume(volume); err != nil {
return err
} else if ok {
rancherManagedVolumeNames[volume.Name] = struct{}{}
}
}
Expand Down Expand Up @@ -208,7 +210,9 @@ func setupVolumes(config *container.Config, instance model.Instance, hostConfig
for _, vMount := range vMounts {
storagePool := model.StoragePool{}
// volume active == exists, possibly not attached to this host
if !storage.IsRancherVolume(vMount) {
if ok, err := storage.IsRancherVolume(vMount); err != nil {
return err
} else if !ok {
if ok, err := storage.IsVolumeActive(vMount, storagePool, client); !ok && err == nil {
if err := storage.DoVolumeActivate(vMount, storagePool, progress, client); err != nil {
return errors.Wrap(err, constants.SetupVolumesError+"failed to activate volume")
Expand Down
8 changes: 6 additions & 2 deletions core/compute/compute_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,9 @@ func configureDNS(dockerClient *client.Client, containerID string) error {

func setupRancherFlexVolume(instance model.Instance, hostConfig *container.HostConfig) error {
for _, volume := range instance.VolumesFromDataVolumeMounts {
if storage.IsRancherVolume(volume) {
if ok, err := storage.IsRancherVolume(volume); err != nil {
return err
} else if ok {
payload := struct {
Name string
Options map[string]string `json:"Opts,omitempty"`
Expand Down Expand Up @@ -527,7 +529,9 @@ func setupRancherFlexVolume(instance model.Instance, hostConfig *container.HostC

func unmountRancherFlexVolume(instance model.Instance) error {
for _, volume := range instance.VolumesFromDataVolumeMounts {
if storage.IsRancherVolume(volume) {
if ok, err := storage.IsRancherVolume(volume); err != nil {
return err
} else if ok {
payload := struct{ Name string }{Name: volume.Name}
_, err := storage.CallRancherStorageVolumePlugin(volume, storage.Unmount, payload)
if err != nil {
Expand Down
26 changes: 23 additions & 3 deletions core/storage/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,31 @@ func rancherStorageSockPath(volume model.Volume) string {
return filepath.Join(rancherSockDir, volume.Data.Fields.Driver+".sock")
}

func IsRancherVolume(volume model.Volume) bool {
// IsRancherVolume checks if a volume to be considered as a flex volume if it is in rancherDrivers and the capability is flex
// raise an error if its rancher-managed driver but the socket file is not available
func IsRancherVolume(volume model.Volume) (bool, error) {
if _, ok := rancherDrivers[volume.Data.Fields.Driver]; ok {
return true
if _, err := os.Stat(rancherStorageSockPath(volume)); err == nil {
// check if Capabilities is flex
payload := struct {
Name string
Options map[string]string `json:"Opts,omitempty"`
}{
Name: volume.Name,
Options: volume.Data.Fields.DriverOpts,
}
response, err := CallRancherStorageVolumePlugin(volume, Capabilities, payload)
if err != nil {
return false, err
}
if response.Capabilities.Scope == "flex" {
return true, nil
}
return false, nil
}
return false, errors.Errorf("socket file not found at %s", rancherStorageSockPath(volume))
}
return false
return false, nil
}

func IsVolumeRemoved(volume model.Volume, storagePool model.StoragePool, client *engineCli.Client) (bool, error) {
Expand Down
8 changes: 6 additions & 2 deletions handlers/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ func (h *StorageHandler) VolumeActivate(event *revents.Event, cli *client.Ranche
progress := utils.GetProgress(event, cli)

// if its rancher volume, use flexVolume and bypass docker volume plugin
if storage.IsRancherVolume(volume) {
if ok, err := storage.IsRancherVolume(volume); err != nil {
return err
} else if ok {
err := storage.VolumeActivateFlex(volume)
if err != nil {
return err
Expand All @@ -93,7 +95,9 @@ func (h *StorageHandler) VolumeRemove(event *revents.Event, cli *client.RancherC
storagePool := volumeStoragePoolMap.StoragePool
progress := utils.GetProgress(event, cli)

if storage.IsRancherVolume(volume) {
if ok, err := storage.IsRancherVolume(volume); err != nil {
return err
} else if ok {
// we need to make sure the reference is cleaned up in docker, so if it exists in docker then clean up in docker first
if inspect, err := h.dockerClient.VolumeInspect(context.Background(), volume.Name); err != nil && !engineCli.IsErrVolumeNotFound(err) {
return errors.Wrapf(err, constants.VolumeRemoveError+"failed to inspect volume %v", volume.Name)
Expand Down

0 comments on commit 589ac50

Please sign in to comment.