-
Notifications
You must be signed in to change notification settings - Fork 113
/
select.go
88 lines (70 loc) · 2.65 KB
/
select.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
Copyright 2019 The OpenEBS Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package blockdevice
import (
"fmt"
apis "github.com/openebs/node-disk-manager/pkg/apis/openebs/v1alpha1"
)
// Filter selects a single block device from a list of block devices
func (c *Config) Filter(bdList *apis.BlockDeviceList) (*apis.BlockDevice, error) {
if len(bdList.Items) == 0 {
return nil, fmt.Errorf("no blockdevices found")
}
candidateDevices, err := c.getCandidateDevices(bdList)
if err != nil {
return nil, err
}
selectedDevice, err := c.getSelectedDevice(candidateDevices)
if err != nil {
return nil, err
}
return selectedDevice, nil
}
// getCandidateDevices selects a list of blockdevices from a given block device
// list based on criteria specified in the claim spec
func (c *Config) getCandidateDevices(bdList *apis.BlockDeviceList) (*apis.BlockDeviceList, error) {
// filterKeys to be used for filtering, by default active and unclaimed filter is present
filterKeys := []string{FilterActive, FilterUnclaimed}
if c.ManualSelection {
filterKeys = append(filterKeys, FilterBlockDeviceName)
} else {
filterKeys = append(filterKeys,
// Sparse BDs can be claimed only by manual selection. Therefore, all
// sparse BDs will be filtered out in auto mode
FilterOutSparseBlockDevices,
FilterDeviceType,
FilterVolumeMode,
FilterNodeName,
)
}
candidateBD := c.ApplyFilters(bdList, filterKeys...)
if len(candidateBD.Items) == 0 {
return nil, fmt.Errorf("no devices found matching the criteria")
}
return candidateBD, nil
}
// getSelectedDevice selects a single a block device based on the resource requirements
// requested by the claim
func (c *Config) getSelectedDevice(bdList *apis.BlockDeviceList) (*apis.BlockDevice, error) {
if c.ManualSelection {
return &bdList.Items[0], nil
}
// filterKeys for filtering based on resource requirements
filterKeys := []string{FilterResourceStorage}
selectedDevices := c.ApplyFilters(bdList, filterKeys...)
if len(selectedDevices.Items) == 0 {
return nil, fmt.Errorf("could not find a device with matching resource requirements")
}
// will use the first available block device
return &selectedDevices.Items[0], nil
}