From 84a4bd6159dd12cc2b8a4e14d3aee438f02c4566 Mon Sep 17 00:00:00 2001 From: prateekpandey14 Date: Thu, 7 Nov 2019 20:25:47 +0530 Subject: [PATCH] fix(csi-node): return error for orphaned volume during unpublish event There will be unpublish events request in each ~60s periods for stale/orphaned volume which was causing node driver crash due to NO csiVolume resource exists for the requested volumeID. Signed-off-by: prateekpandey14 --- pkg/service/v1alpha1/node.go | 4 ++-- pkg/utils/v1alpha1/kubernetes.go | 18 +++++++++--------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pkg/service/v1alpha1/node.go b/pkg/service/v1alpha1/node.go index 62ea5df0d..18f752af3 100644 --- a/pkg/service/v1alpha1/node.go +++ b/pkg/service/v1alpha1/node.go @@ -25,7 +25,7 @@ import ( "github.com/container-storage-interface/spec/lib/go/csi" apis "github.com/openebs/cstor-csi/pkg/apis/openebs.io/core/v1alpha1" iscsi "github.com/openebs/cstor-csi/pkg/iscsi/v1alpha1" - "github.com/openebs/cstor-csi/pkg/utils/v1alpha1" + utils "github.com/openebs/cstor-csi/pkg/utils/v1alpha1" csivol "github.com/openebs/cstor-csi/pkg/volume/v1alpha1" "golang.org/x/net/context" "google.golang.org/grpc/codes" @@ -270,7 +270,7 @@ func (ns *node) NodeUnpublishVolume( return nil, status.Error(codes.Internal, err.Error()) } if unmountRequired { - if vol, err = utils.GetCSIVolume(volumeID); (err != nil) || (vol == nil) { + if vol, err = utils.GetCSIVolume(volumeID); err != nil { return nil, status.Error(codes.Internal, err.Error()) } diff --git a/pkg/utils/v1alpha1/kubernetes.go b/pkg/utils/v1alpha1/kubernetes.go index 5e4a1ea47..d9a5ef9c3 100644 --- a/pkg/utils/v1alpha1/kubernetes.go +++ b/pkg/utils/v1alpha1/kubernetes.go @@ -25,7 +25,6 @@ import ( corev1 "k8s.io/api/core/v1" k8serror "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - v1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) const ( @@ -52,7 +51,7 @@ func FetchPVDetails(name string) (*corev1.PersistentVolume, error) { // getVolStatus fetches the current VolumeStatus which specifies if the volume // is ready to serve IOs func getVolStatus(volumeID string) (string, error) { - listOptions := v1.ListOptions{ + listOptions := metav1.ListOptions{ LabelSelector: "openebs.io/persistent-volume=" + volumeID, } @@ -75,7 +74,7 @@ func getVolStatus(volumeID string) (string, error) { // GetVolListForNode fetches the current Published Volume list func GetVolListForNode() (*apis.CSIVolumeList, error) { - listOptions := v1.ListOptions{ + listOptions := metav1.ListOptions{ LabelSelector: NODEID + "=" + NodeIDENV, } @@ -86,10 +85,9 @@ func GetVolListForNode() (*apis.CSIVolumeList, error) { // GetVolList fetches the current Published Volume list func GetVolList(volume string) (*apis.CSIVolumeList, error) { - listOptions := v1.ListOptions{ + listOptions := metav1.ListOptions{ LabelSelector: VOLNAME + "=" + volume, } - return csivolume.NewKubeclient(). WithNamespace(OpenEBSNamespace).List(listOptions) @@ -101,8 +99,10 @@ func GetCSIVolume(volumeID string) (*apis.CSIVolume, error) { if err != nil { return nil, err } + if len(volList.Items) == 0 { - return nil, nil + return nil, k8serror.NewNotFound(apis.Resource("csivolumes"), volumeID) + } if len(volList.Items) != 1 { @@ -114,7 +114,7 @@ func GetCSIVolume(volumeID string) (*apis.CSIVolume, error) { // GetVolumeIP fetches the cstor target IP Address func GetVolumeIP(volumeID string) (string, error) { cstorvolume, err := csv.NewKubeclient(). - WithNamespace(OpenEBSNamespace).Get(volumeID, v1.GetOptions{}) + WithNamespace(OpenEBSNamespace).Get(volumeID, metav1.GetOptions{}) if err != nil { return "", err } @@ -154,7 +154,7 @@ func CreateOrUpdateCSIVolumeCR(csivol *apis.CSIVolume) error { return err } - csivol.OwnerReferences = []v1.OwnerReference{ + csivol.OwnerReferences = []metav1.OwnerReference{ { APIVersion: "v1", Kind: "Node", @@ -175,7 +175,7 @@ func UpdateCSIVolumeCR(csivol *apis.CSIVolume) error { oldcsivol, err := csivolume.NewKubeclient(). WithNamespace(OpenEBSNamespace). - Get(csivol.Name, v1.GetOptions{}) + Get(csivol.Name, metav1.GetOptions{}) if err != nil { return err }