Exemplo n.º 1
0
func (ad *attacherDetacher) generateDetachVolumeFunc(
	volumeToDetach cache.AttachedVolume,
	actualStateOfWorld cache.ActualStateOfWorld) (func() error, error) {
	// Get attacher plugin
	attachableVolumePlugin, err := ad.volumePluginMgr.FindAttachablePluginBySpec(volumeToDetach.VolumeSpec)
	if err != nil || attachableVolumePlugin == nil {
		return nil, fmt.Errorf(
			"failed to get AttachablePlugin from volumeSpec for volume %q err=%v",
			volumeToDetach.VolumeSpec.Name(),
			err)
	}

	deviceName, err := attachableVolumePlugin.GetDeviceName(volumeToDetach.VolumeSpec)
	if err != nil {
		return nil, fmt.Errorf(
			"failed to GetDeviceName from AttachablePlugin for volumeSpec %q err=%v",
			volumeToDetach.VolumeSpec.Name(),
			err)
	}

	volumeDetacher, err := attachableVolumePlugin.NewDetacher()
	if err != nil {
		return nil, fmt.Errorf(
			"failed to get NewDetacher from volumeSpec for volume %q err=%v",
			volumeToDetach.VolumeSpec.Name(),
			err)
	}

	return func() error {
		// Execute detach
		detachErr := volumeDetacher.Detach(deviceName, volumeToDetach.NodeName)

		if detachErr != nil {
			// On failure, just log and exit. The controller will retry
			glog.Errorf(
				"Detach operation for device %q from node %q failed with: %v",
				volumeToDetach.VolumeName, volumeToDetach.NodeName, detachErr)
			return detachErr
		}

		glog.Infof(
			"Successfully detached device %q from node %q. Will update actual state of world.",
			volumeToDetach.VolumeName, volumeToDetach.NodeName)

		// Update actual state of world
		actualStateOfWorld.DeleteVolumeNode(volumeToDetach.VolumeName, volumeToDetach.NodeName)

		return nil
	}, nil
}
Exemplo n.º 2
0
func (ad *attacherDetacher) generateAttachVolumeFunc(
	volumeToAttach cache.VolumeToAttach,
	actualStateOfWorld cache.ActualStateOfWorld) (func() error, error) {
	// Get attacher plugin
	attachableVolumePlugin, err := ad.volumePluginMgr.FindAttachablePluginBySpec(volumeToAttach.VolumeSpec)
	if err != nil || attachableVolumePlugin == nil {
		return nil, fmt.Errorf(
			"failed to get AttachablePlugin from volumeSpec for volume %q err=%v",
			volumeToAttach.VolumeSpec.Name(),
			err)
	}

	volumeAttacher, newAttacherErr := attachableVolumePlugin.NewAttacher()
	if newAttacherErr != nil {
		return nil, fmt.Errorf(
			"failed to get NewAttacher from volumeSpec for volume %q err=%v",
			volumeToAttach.VolumeSpec.Name(),
			newAttacherErr)
	}

	return func() error {
		// Execute attach
		attachErr := volumeAttacher.Attach(volumeToAttach.VolumeSpec, volumeToAttach.NodeName)

		if attachErr != nil {
			// On failure, just log and exit. The controller will retry
			glog.Errorf(
				"Attach operation for device %q to node %q failed with: %v",
				volumeToAttach.VolumeName, volumeToAttach.NodeName, attachErr)
			return attachErr
		}

		glog.Infof(
			"Successfully attached device %q to node %q. Will update actual state of world.",
			volumeToAttach.VolumeName, volumeToAttach.NodeName)

		// Update actual state of world
		_, addVolumeNodeErr := actualStateOfWorld.AddVolumeNode(volumeToAttach.VolumeSpec, volumeToAttach.NodeName)
		if addVolumeNodeErr != nil {
			// On failure, just log and exit. The controller will retry
			glog.Errorf(
				"Attach operation for device %q to node %q succeeded, but updating actualStateOfWorld failed with: %v",
				volumeToAttach.VolumeName, volumeToAttach.NodeName, addVolumeNodeErr)
			return addVolumeNodeErr
		}

		return nil
	}, nil
}