1515package utils
1616
1717import (
18+ "strings"
19+ "time"
20+
1821 apis "github.com/openebs/api/v3/pkg/apis/cstor/v1"
19- csv "github.com/openebs/cstor-csi/pkg/cstor/volume"
20- csivolume "github.com/openebs/cstor-csi/pkg/cstor/volumeattachment"
21- node "github.com/openebs/cstor-csi/pkg/kubernetes/node"
22- pv "github.com/openebs/cstor-csi/pkg/kubernetes/persistentvolume"
2322 errors "github.com/pkg/errors"
2423 "github.com/sirupsen/logrus"
2524 corev1 "k8s.io/api/core/v1"
25+ k8serrors "k8s.io/apimachinery/pkg/api/errors"
2626 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
28+ csv "github.com/openebs/cstor-csi/pkg/cstor/volume"
29+ csivolume "github.com/openebs/cstor-csi/pkg/cstor/volumeattachment"
30+ node "github.com/openebs/cstor-csi/pkg/kubernetes/node"
31+ pv "github.com/openebs/cstor-csi/pkg/kubernetes/persistentvolume"
2732)
2833
2934const (
@@ -37,6 +42,12 @@ const (
3742 VOLNAME = "Volname"
3843)
3944
45+ var (
46+ // loopCount is the no of times cStor volume attachment's successful deletion check
47+ // will be performed
48+ loopCount = 5
49+ )
50+
4051// getNodeDetails fetches the nodeInfo for the current node
4152func getNodeDetails (name string ) (* corev1.Node , error ) {
4253 return node .NewKubeClient ().Get (name , metav1.GetOptions {})
@@ -145,7 +156,10 @@ func UpdateCStorVolumeAttachmentCR(csivol *apis.CStorVolumeAttachment) (*apis.CS
145156// gets deleted or replaced or updated
146157
147158// DeleteOldCStorVolumeAttachmentCRs removes the CStorVolumeAttachmentCR for the specified path
148- func DeleteOldCStorVolumeAttachmentCRs (volumeID string ) error {
159+ func DeleteOldCStorVolumeAttachmentCRs (volumeID , nodeID string ) error {
160+ // nodeCVA contains the name of the cStor volume attachment for the current node
161+ var nodeCVA string
162+
149163 csivols , err := GetVolList (volumeID )
150164 if err != nil {
151165 return err
@@ -158,7 +172,35 @@ func DeleteOldCStorVolumeAttachmentCRs(volumeID string) error {
158172 if err != nil {
159173 return err
160174 }
175+
176+ // Extract only the CVA which belongs to the current node
177+ if strings .HasSuffix (csivol .Name , nodeID ) {
178+ nodeCVA = csivol .Name
179+ }
180+ }
181+
182+ if nodeCVA != "" {
183+ var err error
184+ for i := 1 ; i <= loopCount ; i ++ {
185+ _ , err = csivolume .NewKubeclient ().
186+ WithNamespace (OpenEBSNamespace ).Get (nodeCVA , metav1.GetOptions {})
187+ if err != nil {
188+ // If the error is an error of type "not found" then break out
189+ // of the loop since the deletion is succeeded
190+ if k8serrors .IsNotFound (err ) {
191+ err = nil
192+ break
193+ }
194+ }
195+ time .Sleep (1 * time .Second )
196+ }
197+ // If error still exists, simply log that here. Since the deletion of CVA
198+ // will be taken care of its corresponding create request when it happens
199+ if err != nil {
200+ logrus .Infof ("cStor volume attachment: {%v} not deleted. Error: {%v}" , nodeCVA , err )
201+ }
161202 }
203+
162204 return nil
163205}
164206
0 commit comments