Skip to content

Commit 161848e

Browse files
committed
DeviceManager: Return no error if the device not found while deleting
Discussion on issue intel#413, raised a requirement that PMEM-CSI should handle the DeleteVolume() call for volumes that the underlined device already deleted manually by the admin. So for such a volume we just ignore and return no error in DeviceManager.DeleteVolume(). This also makes DeleteVolume() idempotent.
1 parent ba22729 commit 161848e

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

pkg/pmem-device-manager/pmd-lvm.go

+18-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package pmdmanager
22

33
import (
44
"fmt"
5+
"os"
56
"strconv"
67
"strings"
78
"sync"
89

910
"github.com/intel/pmem-csi/pkg/ndctl"
1011
pmemexec "github.com/intel/pmem-csi/pkg/pmem-exec"
12+
"github.com/pkg/errors"
1113
"k8s.io/klog"
1214
)
1315

@@ -151,11 +153,22 @@ func (lvm *pmemLvm) DeleteDevice(volumeId string, flush bool) error {
151153
lvmMutex.Lock()
152154
defer lvmMutex.Unlock()
153155

154-
device, err := lvm.getDevice(volumeId)
155-
if err != nil {
156+
var err error
157+
var device PmemDeviceInfo
158+
159+
if device, err = lvm.getDevice(volumeId); err != nil {
160+
if os.IsNotExist(errors.Cause(err)) {
161+
return nil
162+
}
156163
return err
157164
}
158-
if err := ClearDevice(device, flush); err != nil {
165+
166+
if err = ClearDevice(device, flush); err != nil {
167+
if os.IsNotExist(errors.Cause(err)) {
168+
// Remove device from cache
169+
delete(lvm.devices, volumeId)
170+
return nil
171+
}
159172
return err
160173
}
161174

@@ -193,7 +206,7 @@ func (lvm *pmemLvm) getDevice(volumeId string) (PmemDeviceInfo, error) {
193206
return dev, nil
194207
}
195208

196-
return PmemDeviceInfo{}, fmt.Errorf("Device not found with name %s", volumeId)
209+
return PmemDeviceInfo{}, errors.Wrapf(os.ErrNotExist, "no device found with id %s", volumeId)
197210
}
198211

199212
func getUncachedDevice(volumeId string, volumeGroup string) (PmemDeviceInfo, error) {
@@ -205,7 +218,7 @@ func getUncachedDevice(volumeId string, volumeGroup string) (PmemDeviceInfo, err
205218
if dev, ok := devices[volumeId]; ok {
206219
return dev, nil
207220
}
208-
return PmemDeviceInfo{}, fmt.Errorf("Device not found with name %s", volumeId)
221+
return PmemDeviceInfo{}, errors.Wrapf(os.ErrNotExist, "no device found with id %s", volumeId)
209222
}
210223

211224
// listDevices Lists available logical devices in given volume groups

pkg/pmem-device-manager/pmd-ndctl.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pmdmanager
22

33
import (
44
"fmt"
5+
"os"
56
"sync"
67

78
"github.com/intel/pmem-csi/pkg/ndctl"
@@ -140,10 +141,16 @@ func (pmem *pmemNdctl) DeleteDevice(volumeId string, flush bool) error {
140141

141142
device, err := pmem.getDevice(volumeId)
142143
if err != nil {
144+
if os.IsNotExist(err) {
145+
return nil
146+
}
143147
return err
144148
}
145-
err = ClearDevice(device, flush)
146-
if err != nil {
149+
if err := ClearDevice(device, flush); err != nil {
150+
// Device might have already deleted
151+
if os.IsNotExist(err) {
152+
return nil
153+
}
147154
return err
148155
}
149156
return pmem.ctx.DestroyNamespaceByName(volumeId)

0 commit comments

Comments
 (0)