From 788ad22aa09319de6971f2f8d40158117bf764fa Mon Sep 17 00:00:00 2001 From: Akhil Mohan Date: Wed, 9 Dec 2020 20:12:35 +0530 Subject: [PATCH] fix(filter): add partition and device mapper check to validity filter (#519) add validation check for blockdevices of type partition and device mapper Signed-off-by: Akhil Mohan --- changelogs/unreleased/519-akhilerm | 1 + .../filter/devicevalidityfilter.go | 24 ++++++ .../filter/devicevalidityfilter_test.go | 78 +++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 changelogs/unreleased/519-akhilerm diff --git a/changelogs/unreleased/519-akhilerm b/changelogs/unreleased/519-akhilerm new file mode 100644 index 000000000..ec6bd7c48 --- /dev/null +++ b/changelogs/unreleased/519-akhilerm @@ -0,0 +1 @@ +add checks for validity of partition and dm device resources created \ No newline at end of file diff --git a/cmd/ndm_daemonset/filter/devicevalidityfilter.go b/cmd/ndm_daemonset/filter/devicevalidityfilter.go index 3319f027b..39de8f80e 100644 --- a/cmd/ndm_daemonset/filter/devicevalidityfilter.go +++ b/cmd/ndm_daemonset/filter/devicevalidityfilter.go @@ -19,6 +19,8 @@ package filter import ( "github.com/openebs/node-disk-manager/blockdevice" "github.com/openebs/node-disk-manager/cmd/ndm_daemonset/controller" + "github.com/openebs/node-disk-manager/pkg/util" + "k8s.io/klog" ) @@ -73,6 +75,8 @@ func (dvf *deviceValidityFilter) Start() { dvf.excludeValidationFuncs = append(dvf.excludeValidationFuncs, isValidDevPath, isValidCapacity, + isValidDMDevice, + isValidPartition, ) } @@ -110,3 +114,23 @@ func isValidCapacity(bd *blockdevice.BlockDevice) bool { } return true } + +// isValidPartition checks if the blockdevice for the partition has a valid partition UUID +func isValidPartition(bd *blockdevice.BlockDevice) bool { + if bd.DeviceAttributes.DeviceType == blockdevice.BlockDeviceTypePartition && + len(bd.PartitionInfo.PartitionEntryUUID) == 0 { + klog.V(4).Infof("device: %s of device-type partition has invalid partition UUID", bd.DevPath) + return false + } + return true +} + +// isValidDMDevice checks if the blockdevice is a valid dm device +func isValidDMDevice(bd *blockdevice.BlockDevice) bool { + if util.Contains(blockdevice.DeviceMapperDeviceTypes, bd.DeviceAttributes.DeviceType) && + len(bd.DMInfo.DMUUID) == 0 { + klog.V(4).Infof("device: %s of device mapper type has invalid DM_UUID", bd.DevPath) + return false + } + return true +} diff --git a/cmd/ndm_daemonset/filter/devicevalidityfilter_test.go b/cmd/ndm_daemonset/filter/devicevalidityfilter_test.go index 8e64b2e7c..a198751fc 100644 --- a/cmd/ndm_daemonset/filter/devicevalidityfilter_test.go +++ b/cmd/ndm_daemonset/filter/devicevalidityfilter_test.go @@ -138,3 +138,81 @@ func Test_isValidCapacity(t *testing.T) { }) } } + +func TestIsValidPartition(t *testing.T) { + tests := map[string]struct { + bd *blockdevice.BlockDevice + want bool + }{ + "valid blockdevice of type partition": { + bd: &blockdevice.BlockDevice{ + Identifier: blockdevice.Identifier{ + DevPath: "/dev/sda1", + }, + DeviceAttributes: blockdevice.DeviceAttribute{ + DeviceType: blockdevice.BlockDeviceTypePartition, + }, + PartitionInfo: blockdevice.PartitionInformation{ + PartitionEntryUUID: "065e2357-05", + }, + }, + want: true, + }, + "invalid blockdevice of type partition": { + bd: &blockdevice.BlockDevice{ + Identifier: blockdevice.Identifier{ + DevPath: "/dev/sda1", + }, + DeviceAttributes: blockdevice.DeviceAttribute{ + DeviceType: blockdevice.BlockDeviceTypePartition, + }, + }, + want: false, + }, + } + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + got := isValidPartition(tt.bd) + assert.Equal(t, tt.want, got) + }) + } +} + +func TestIsValidDMDevice(t *testing.T) { + tests := map[string]struct { + bd *blockdevice.BlockDevice + want bool + }{ + "valid blockdevice of type device mapper": { + bd: &blockdevice.BlockDevice{ + Identifier: blockdevice.Identifier{ + DevPath: "/dev/dm-0", + }, + DeviceAttributes: blockdevice.DeviceAttribute{ + DeviceType: blockdevice.BlockDeviceTypeLVM, + }, + DMInfo: blockdevice.DeviceMapperInformation{ + DMUUID: "LVM-j2xmqvbcVWBQK9Jdttte3CyeVTGgxtVV5VcCi3nxdwihZDxSquMOBaGL5eymBNvk", + }, + }, + want: true, + }, + "invalid blockdevice of type device mapper": { + bd: &blockdevice.BlockDevice{ + Identifier: blockdevice.Identifier{ + DevPath: "/dev/dm-1", + }, + DeviceAttributes: blockdevice.DeviceAttribute{ + DeviceType: blockdevice.BlockDeviceTypeLVM, + }, + }, + want: false, + }, + } + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + got := isValidDMDevice(tt.bd) + assert.Equal(t, tt.want, got) + }) + } +}