Skip to content

Commit

Permalink
Add missing testcases and fix existing testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
Praveenrajmani authored and wlan0 committed Apr 20, 2022
1 parent 3038eb9 commit f5b76b6
Show file tree
Hide file tree
Showing 19 changed files with 1,017 additions and 516 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require (
github.com/imdario/mergo v0.3.7 // indirect
github.com/jedib0t/go-pretty v4.3.0+incompatible
github.com/jedib0t/go-pretty/v6 v6.0.5
github.com/jteeuwen/go-bindata v3.0.7+incompatible // indirect
github.com/kubernetes-csi/csi-lib-utils v0.7.0
github.com/mailru/easyjson v0.7.6 // indirect
github.com/mattn/go-isatty v0.0.13 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,6 @@ github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMW
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jteeuwen/go-bindata v3.0.7+incompatible h1:91Uy4d9SYVr1kyTJ15wJsog+esAZZl7JmEfTkwmhJts=
github.com/jteeuwen/go-bindata v3.0.7+incompatible/go.mod h1:JVvhzYOiGBnFSYRyV00iY8q7/0PThjIYav1p9h5dmKs=
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
Expand Down
14 changes: 14 additions & 0 deletions pkg/apis/direct.csi.min.io/v1beta4/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ const (

// DirectCSIDriveMessageNotFormatted denotes "NotFormatted" drive message.
DirectCSIDriveMessageNotFormatted DirectCSIDriveMessage = "NotFormatted"

// DirectCSIDriveMessageLost denotes "removed" drive message.
DirectCSIDriveMessageLost DirectCSIDriveMessage = "drive is removed"
)

// RequestedFormat denotes drive format request information.
Expand Down Expand Up @@ -339,6 +342,17 @@ const (

// DirectCSIVolumeReasonNotReady denotes "NotReady" volume reason.
DirectCSIVolumeReasonNotReady DirectCSIVolumeReason = "NotReady"

// DirectCSIVolumeReasonDriveLost denotes "DriveLost" volume reason.
DirectCSIVolumeReasonDriveLost DirectCSIVolumeReason = "DriveLost"
)

// DirectCSIVolumeMessage denotes drive message.
type DirectCSIVolumeMessage string

const (
// DirectCSIVolumeMessageDriveLost denotes "DriveLost" drive message.
DirectCSIVolumeMessageDriveLost DirectCSIVolumeMessage = "Drive Lost"
)

// DirectCSIVolumeStatus denotes volume information.
Expand Down
76 changes: 47 additions & 29 deletions pkg/drive/drive.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/minio/directpv/pkg/listener"
"github.com/minio/directpv/pkg/mount"
"github.com/minio/directpv/pkg/sys"
"github.com/minio/directpv/pkg/uevent"
"github.com/minio/directpv/pkg/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand All @@ -44,14 +45,16 @@ var (
)

type driveEventHandler struct {
nodeID string
reflinkSupport bool
getDevice func(major, minor uint32) (string, error)
stat func(name string) (os.FileInfo, error)
mountDevice func(fsUUID, target string, flags []string) error
unmountDevice func(device string) error
makeFS func(ctx context.Context, device, uuid string, force, reflink bool) error
getFreeCapacity func(path string) (uint64, error)
nodeID string
reflinkSupport bool
getDevice func(major, minor uint32) (string, error)
stat func(name string) (os.FileInfo, error)
mountDevice func(device, target string, flags []string) error
unmountDevice func(device string) error
makeFS func(ctx context.Context, device, uuid string, force, reflink bool) error
getFreeCapacity func(path string) (uint64, error)
verifyHostStateForDrive func(drive *directcsi.DirectCSIDrive) error
isMounted func(target string) (bool, error)
}

// StartController starts drive event controller.
Expand All @@ -72,13 +75,15 @@ func StartController(ctx context.Context, nodeID string, reflinkSupport bool) er

func newDriveEventHandler(nodeID string, reflinkSupport bool) *driveEventHandler {
return &driveEventHandler{
nodeID: nodeID,
reflinkSupport: reflinkSupport,
getDevice: getDevice,
stat: os.Stat,
mountDevice: mount.MountXFSDevice,
unmountDevice: mount.UnmountDevice,
makeFS: xfs.MakeFS,
nodeID: nodeID,
reflinkSupport: reflinkSupport,
getDevice: getDevice,
stat: os.Stat,
mountDevice: mount.MountXFSDevice,
unmountDevice: mount.UnmountDevice,
makeFS: xfs.MakeFS,
verifyHostStateForDrive: VerifyHostStateForDrive,
isMounted: mount.IsMounted,
}
}

Expand Down Expand Up @@ -109,11 +114,11 @@ func (handler *driveEventHandler) Handle(ctx context.Context, args listener.Even
}

func (handler *driveEventHandler) handleUpdate(ctx context.Context, drive *directcsi.DirectCSIDrive) error {
err := VerifyHostStateForDrive(drive)
err := handler.verifyHostStateForDrive(drive)
switch err {
case nil:
switch {
case isFormatRequested(drive):
case uevent.IsFormatRequested(drive):
return handler.format(ctx, drive)
case drive.Status.DriveStatus == directcsi.DriveStatusReleased:
return handler.release(ctx, drive)
Expand Down Expand Up @@ -143,7 +148,7 @@ func (handler *driveEventHandler) delete(ctx context.Context, drive *directcsi.D

func (handler *driveEventHandler) format(ctx context.Context, drive *directcsi.DirectCSIDrive) (err error) {
target := filepath.Join(sys.MountRoot, drive.Name)
mounted, err := mount.IsMounted(target)
mounted, err := handler.isMounted(target)
if err != nil {
klog.Error(err)
return err
Expand Down Expand Up @@ -283,12 +288,17 @@ func (handler *driveEventHandler) mountDrive(ctx context.Context, drive *directc

func (handler *driveEventHandler) lost(ctx context.Context, drive *directcsi.DirectCSIDrive) error {
// Set the drive ready condition as false if not set
if !utils.IsConditionStatus(drive.Status.Conditions, string(directcsi.DirectCSIDriveConditionReady), metav1.ConditionFalse) {
if !utils.IsCondition(drive.Status.Conditions,
string(directcsi.DirectCSIDriveConditionReady),
metav1.ConditionFalse,
string(directcsi.DirectCSIDriveReasonLost),
string(directcsi.DirectCSIDriveMessageLost),
) {
utils.UpdateCondition(drive.Status.Conditions,
string(directcsi.DirectCSIDriveConditionReady),
metav1.ConditionFalse,
string(directcsi.DirectCSIDriveReasonLost),
"drive is removed")
string(directcsi.DirectCSIDriveMessageLost))
_, err := client.GetLatestDirectCSIDriveInterface().Update(
ctx, drive, metav1.UpdateOptions{TypeMeta: utils.DirectCSIDriveTypeMeta()},
)
Expand All @@ -310,17 +320,25 @@ func (handler *driveEventHandler) lost(ctx context.Context, drive *directcsi.Dir
if err != nil {
return err
}
utils.UpdateCondition(volume.Status.Conditions,

if !utils.IsCondition(volume.Status.Conditions,
string(directcsi.DirectCSIVolumeConditionReady),
metav1.ConditionFalse,
string(directcsi.DirectCSIVolumeReasonNotReady),
"[DRIVE LOST]",
)
_, err = volumeInterface.Update(
ctx, volume, metav1.UpdateOptions{TypeMeta: utils.DirectCSIVolumeTypeMeta()},
)
if err != nil {
return err
string(directcsi.DirectCSIVolumeReasonDriveLost),
string(directcsi.DirectCSIVolumeMessageDriveLost),
) {
utils.UpdateCondition(volume.Status.Conditions,
string(directcsi.DirectCSIVolumeConditionReady),
metav1.ConditionFalse,
string(directcsi.DirectCSIVolumeReasonDriveLost),
string(directcsi.DirectCSIVolumeMessageDriveLost),
)
_, err = volumeInterface.Update(
ctx, volume, metav1.UpdateOptions{TypeMeta: utils.DirectCSIVolumeTypeMeta()},
)
if err != nil {
return err
}
}
}
}
Expand Down
Loading

0 comments on commit f5b76b6

Please sign in to comment.