Skip to content

Commit

Permalink
chore(publish/unpublish): refactor code for node publish and unpublish (
Browse files Browse the repository at this point in the history
#32)

Signed-off-by: Payes <[email protected]>
  • Loading branch information
payes authored and vishnuitta committed Aug 27, 2019
1 parent 3d7556c commit 9456cd6
Show file tree
Hide file tree
Showing 7 changed files with 396 additions and 367 deletions.
12 changes: 11 additions & 1 deletion pkg/apis/openebs.io/core/v1alpha1/csivolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const (
CSIVolumeStatusMounted CSIVolumeStatus = "Mounted"
// CSIVolumeStatusUnMounted indicated that the volume has been successfuly
// unmounted and logged out of the node
CSIVolumeStatusUnMounted CSIVolumeStatus = "UnMounted"
CSIVolumeStatusUnmounted CSIVolumeStatus = "Unmounted"
// CSIVolumeStatusRaw indicates that the volume is being used in raw format
// by the application, therefore CSI has only performed iSCSI login
// operation on this volume and avoided filesystem creation and mount.
Expand All @@ -142,6 +142,16 @@ const (
// the volume has bben started but failed kubernetes needs to retry sending
// nodepublish
CSIVolumeStatusMountFailed CSIVolumeStatus = "MountFailed"
// CSIVolumeStatusUnmountInProgress indicates that the volume is busy and
// unavailable for use by other goroutines, an unmount operation on volume
// is under progress
CSIVolumeStatusUnmountUnderProgress CSIVolumeStatus = "UnmountUnderProgress"
// CSIVolumeStatusWaitingForCVCBound indicates that the volume components
// are still being created
CSIVolumeStatusWaitingForCVCBound CSIVolumeStatus = "WaitingForCVCBound"
// CSIVolumeStatusWaitingForVolumeToBeReady indicates that the replicas are
// yet to connect to target
CSIVolumeStatusWaitingForVolumeToBeReady CSIVolumeStatus = "WaitingForVolumeToBeReady"
)

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
68 changes: 46 additions & 22 deletions pkg/iscsi/v1alpha1/iscsi_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,16 +407,17 @@ func (util *ISCSIUtil) AttachDisk(b iscsiDiskMounter) (string, error) {
return "", nil
}

if err := os.MkdirAll(mntPath, 0750); err != nil {
glog.Errorf("iscsi: failed to mkdir %s, error", mntPath)
return "", err
}

// Persist iscsi disk config to json file for DetachDisk path
if err := util.persistISCSI(*(b.iscsiDisk), b.targetPath); err != nil {
glog.Errorf("iscsi: failed to save iscsi config with error: %v", err)
return "", err
}
/*
if err := os.MkdirAll(mntPath, 0750); err != nil {
glog.Errorf("iscsi: failed to mkdir %s, error", mntPath)
return "", err
}
// Persist iscsi disk config to json file for DetachDisk path
if err := util.persistISCSI(*(b.iscsiDisk), b.targetPath); err != nil {
glog.Errorf("iscsi: failed to save iscsi config with error: %v", err)
return "", err
}
*/

for _, path := range devicePaths {
// There shouldnt be any empty device paths. However adding this check
Expand Down Expand Up @@ -492,18 +493,23 @@ func (util *ISCSIUtil) DetachDisk(
var volName, iqn, iface, initiatorName string
found := true

// load iscsi disk config from json file
if err := util.loadISCSI(c.iscsiDisk, targetPath); err == nil {
bkpPortal, iqn, iface, volName = c.iscsiDisk.Portals, c.iscsiDisk.Iqn,
c.iscsiDisk.Iface, c.iscsiDisk.VolName
initiatorName = c.iscsiDisk.InitiatorName
} else {
glog.Errorf(
"iscsi detach disk: failed to get iscsi config from path %s Error: %v",
targetPath, err,
)
return err
}
/*
// load iscsi disk config from json file
if err := util.loadISCSI(c.iscsiDisk, targetPath); err == nil {
bkpPortal, iqn, iface, volName = c.iscsiDisk.Portals, c.iscsiDisk.Iqn,
c.iscsiDisk.Iface, c.iscsiDisk.VolName
initiatorName = c.iscsiDisk.InitiatorName
} else {
glog.Errorf(
"iscsi detach disk: failed to get iscsi config from path %s Error: %v",
targetPath, err,
)
return err
}
*/
bkpPortal, iqn, iface, volName = c.iscsiDisk.Portals, c.iscsiDisk.Iqn,
c.iscsiDisk.Iface, c.iscsiDisk.VolName
initiatorName = c.iscsiDisk.InitiatorName
portals := removeDuplicate(bkpPortal)
if len(portals) == 0 {
return fmt.Errorf(
Expand Down Expand Up @@ -660,3 +666,21 @@ func cloneIface(b iscsiDiskMounter, newIface string) error {
}
return lastErr
}

// UnmountDisk logs out of the iSCSI volume and the corresponding path is removed
func (util *ISCSIUtil) UnmountDisk(
c iscsiDiskUnmounter,
targetPath string,
) error {
if pathExists, pathErr := mount.PathExists(targetPath); pathErr != nil {
return fmt.Errorf("Error checking if path exists: %v", pathErr)
} else if !pathExists {
glog.Warningf(
"Warning: Unmount skipped because path does not exist: %v",
targetPath,
)
return nil
}

return c.mounter.Unmount(targetPath)
}
23 changes: 13 additions & 10 deletions pkg/iscsi/v1alpha1/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func UnmountAndDetachDisk(vol *apis.CSIVolume, path string) error {
Portals: []string{vol.Spec.ISCSI.TargetPortal},
Iqn: vol.Spec.ISCSI.Iqn,
lun: vol.Spec.ISCSI.Lun,
Iface: vol.Spec.ISCSI.IscsiInterface,
}

diskUnmounter := &iscsiDiskUnmounter{
Expand All @@ -23,11 +24,7 @@ func UnmountAndDetachDisk(vol *apis.CSIVolume, path string) error {
exec: mount.NewOsExec(),
}
util := &ISCSIUtil{}
err := util.DetachDisk(*diskUnmounter, path)
if err != nil {
return status.Error(codes.Internal, err.Error())
}
return nil
return util.DetachDisk(*diskUnmounter, path)
}

// AttachAndMountDisk logs in to the iSCSI Volume
Expand All @@ -38,14 +35,20 @@ func AttachAndMountDisk(vol *apis.CSIVolume) (string, error) {
}
iscsiInfo, err := getISCSIInfo(vol)
if err != nil {
return "", status.Error(codes.Internal, err.Error())
return "", err
}
diskMounter := getISCSIDiskMounter(iscsiInfo, vol)

util := &ISCSIUtil{}
devicePath, err := util.AttachDisk(*diskMounter)
if err != nil {
return "", status.Error(codes.Internal, err.Error())
return util.AttachDisk(*diskMounter)
}

// Unmount unmounts the path provided
func Unmount(path string) error {
diskUnmounter := &iscsiDiskUnmounter{
mounter: &mount.SafeFormatAndMount{Interface: mount.New(""), Exec: mount.NewOsExec()},
exec: mount.NewOsExec(),
}
return devicePath, err
util := &ISCSIUtil{}
return util.UnmountDisk(*diskUnmounter, path)
}
Loading

0 comments on commit 9456cd6

Please sign in to comment.