diff --git a/pkg/algorithm/nodeselect/v1alpha1/select_node.go b/pkg/algorithm/nodeselect/v1alpha1/select_node.go index eb321c2e73..13af41f570 100644 --- a/pkg/algorithm/nodeselect/v1alpha1/select_node.go +++ b/pkg/algorithm/nodeselect/v1alpha1/select_node.go @@ -256,7 +256,7 @@ func (ac *Config) getBlockDevice() (*ndmapis.BlockDeviceList, error) { } if ProvisioningType(ac.Spc) == ProvisioningTypeAuto { - filterList = append(filterList, blockdevice.FilterNonFSType) + filterList = append(filterList, blockdevice.FilterNonFSType, blockdevice.FilterNonReleasedDevices) } bdl = bdList.Filter(filterList...) diff --git a/pkg/blockdevice/v1alpha1/blockdevice.go b/pkg/blockdevice/v1alpha1/blockdevice.go index 6151b6e147..6107fb1e9f 100644 --- a/pkg/blockdevice/v1alpha1/blockdevice.go +++ b/pkg/blockdevice/v1alpha1/blockdevice.go @@ -34,10 +34,11 @@ const ( FilterInactive = "filterInactive" FilterNonInactive = "filterNonInactive" //FilterNonPartitions = "filterNonPartitions" - FilterNonFSType = "filterNonFSType" - FilterSparseDevices = "filterSparseDevices" - FilterNonSparseDevices = "filterNonSparseDevices" - InActiveStatus = "Inactive" + FilterNonFSType = "filterNonFSType" + FilterSparseDevices = "filterSparseDevices" + FilterNonSparseDevices = "filterNonSparseDevices" + InActiveStatus = "Inactive" + FilterNonReleasedDevices = "filterNonReleasedDevices" ) // DefaultDiskCount is a map containing the default block device count of various raid types. @@ -113,9 +114,10 @@ var filterOptionFuncMap = map[string]filterOptionFunc{ FilterInactive: filterInactive, FilterNonInactive: filterNonInactive, //FilterNonPartitions: filterNonPartitions, - FilterNonFSType: filterNonFSType, - FilterSparseDevices: filterSparseDevices, - FilterNonSparseDevices: filterNonSparseDevices, + FilterNonFSType: filterNonFSType, + FilterSparseDevices: filterSparseDevices, + FilterNonSparseDevices: filterNonSparseDevices, + FilterNonReleasedDevices: filterNonReleasedDevices, } // predicateFailedError returns the predicate error which is provided to this function as an argument @@ -289,6 +291,19 @@ func filterNonSparseDevices(originalList *BlockDeviceList) *BlockDeviceList { return filteredList } +func filterNonReleasedDevices(originalList *BlockDeviceList) *BlockDeviceList { + filteredList := &BlockDeviceList{ + BlockDeviceList: &ndm.BlockDeviceList{}, + errs: nil, + } + for _, device := range originalList.Items { + if !(device.Status.ClaimState == ndm.BlockDeviceReleased) { + filteredList.Items = append(filteredList.Items, device) + } + } + return filteredList +} + // Hasitems checks whether the BlockDeviceList contains BlockDevices func (bdl *BlockDeviceList) Hasitems() (string, bool) { if bdl == nil || bdl.BlockDeviceList == nil || bdl.Items == nil { diff --git a/pkg/blockdevice/v1alpha1/blockdevice_test.go b/pkg/blockdevice/v1alpha1/blockdevice_test.go index 08614604c2..3e4883b68f 100644 --- a/pkg/blockdevice/v1alpha1/blockdevice_test.go +++ b/pkg/blockdevice/v1alpha1/blockdevice_test.go @@ -727,3 +727,85 @@ func TestFilterNonFSType(t *testing.T) { }) } } + +func TestFilterNonReleasedDevices(t *testing.T) { + tests := map[string]struct { + // fakeCasPool holds the fake fakeCasPool object in test cases. + blockDeviceList *BlockDeviceList + // expectedBlockDeviceListLength holds the length of disk list + expectedBlockDeviceCount int + }{ + "EmptyBlockDeviceList1": { + blockDeviceList: &BlockDeviceList{ + BlockDeviceList: &ndm.BlockDeviceList{}, + errs: nil, + }, + expectedBlockDeviceCount: 0, + }, + "blockDeviceList2": { + blockDeviceList: &BlockDeviceList{ + BlockDeviceList: &ndm.BlockDeviceList{ + TypeMeta: metav1.TypeMeta{}, + ListMeta: metav1.ListMeta{}, + Items: []ndm.BlockDevice{ + { + TypeMeta: metav1.TypeMeta{}, + ObjectMeta: metav1.ObjectMeta{}, + Spec: ndm.DeviceSpec{ + Path: "/dev/sda", + }, + Status: ndm.DeviceStatus{ + State: "Active", + ClaimState: ndm.BlockDeviceReleased, + }, + }, + { + TypeMeta: metav1.TypeMeta{}, + ObjectMeta: metav1.ObjectMeta{}, + Spec: ndm.DeviceSpec{ + Path: "/dev/sdb", + }, + Status: ndm.DeviceStatus{ + State: "Active", + ClaimState: ndm.BlockDeviceUnclaimed, + }, + }, + { + TypeMeta: metav1.TypeMeta{}, + ObjectMeta: metav1.ObjectMeta{}, + Spec: ndm.DeviceSpec{ + Path: "/dev/sdb", + }, + Status: ndm.DeviceStatus{ + State: "Active", + ClaimState: ndm.BlockDeviceReleased, + }, + }, + { + TypeMeta: metav1.TypeMeta{}, + ObjectMeta: metav1.ObjectMeta{}, + Spec: ndm.DeviceSpec{ + Path: "/dev/sdb", + Partitioned: "NO", + }, + Status: ndm.DeviceStatus{ + State: "Active", + }, + }, + }, + }, + errs: nil, + }, + expectedBlockDeviceCount: 2, + }, + } + for name, test := range tests { + name, test := name, test + t.Run(name, func(t *testing.T) { + filtteredBlockDeviceList := filterNonReleasedDevices(test.blockDeviceList) + if len(filtteredBlockDeviceList.Items) != test.expectedBlockDeviceCount { + t.Errorf("Test %q failed: expected block device object count %d but got %d", name, test.expectedBlockDeviceCount, len(filtteredBlockDeviceList.Items)) + } + }) + } +}