forked from openebs-archive/node-disk-manager
/
filters.go
319 lines (277 loc) · 10.6 KB
/
filters.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
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 (
"github.com/openebs/node-disk-manager/blockdevice"
"github.com/openebs/node-disk-manager/cmd/ndm_daemonset/controller"
"github.com/openebs/node-disk-manager/db/kubernetes"
apis "github.com/openebs/node-disk-manager/pkg/apis/openebs/v1alpha1"
"github.com/openebs/node-disk-manager/pkg/select/verify"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/klog"
)
const (
// FilterActive is the filter for getting active BDs
FilterActive = "filterActive"
// FilterUnclaimed is the filter for getting unclaimed BDs
FilterUnclaimed = "filterUnclaimed"
// FilterDeviceType is the filter for matching DeviceType like SSD, HDD, sparse
FilterDeviceType = "filterDeviceType"
// FilterVolumeMode is the filter for filtering based on Volume Mode required
FilterVolumeMode = "filterVolumeMode"
// FilterBlockDeviceName is the filter for getting a BD based on a name
FilterBlockDeviceName = "filterBlockDeviceName"
// FilterResourceStorage is the filter for matching resource storage
FilterResourceStorage = "filterResourceStorage"
// FilterOutSparseBlockDevices is used to filter out sparse BDs
FilterOutSparseBlockDevices = "filterSparseBlockDevice"
// FilterNodeName is used to filter based on nodename
FilterNodeName = "filterNodeName"
// FilterBlockDeviceTag is used to filter out blockdevices having
// openebs.io/blockdevice-tag label
FilterBlockDeviceTag = "filterBlockDeviceTag"
)
// filterFunc is the func type for the filter functions
type filterFunc func(original *apis.BlockDeviceList, spec *apis.DeviceClaimSpec) *apis.BlockDeviceList
var filterFuncMap = map[string]filterFunc{
FilterActive: filterActive,
FilterUnclaimed: filterUnclaimed,
FilterDeviceType: filterDeviceType,
FilterVolumeMode: filterVolumeMode,
FilterBlockDeviceName: filterBlockDeviceName,
FilterResourceStorage: filterResourceStorage,
FilterOutSparseBlockDevices: filterOutSparseBlockDevice,
FilterNodeName: filterNodeName,
FilterBlockDeviceTag: filterBlockDeviceTag,
}
// ApplyFilters apply the filter specified in the filterkeys on the given BD List,
func (c *Config) ApplyFilters(bdList *apis.BlockDeviceList, filterKeys ...string) *apis.BlockDeviceList {
filteredList := bdList
for _, key := range filterKeys {
filteredList = filterFuncMap[key](filteredList, c.ClaimSpec)
}
return filteredList
}
// filterActive filters out active Blockdevices from the BDList
func filterActive(originalBD *apis.BlockDeviceList, spec *apis.DeviceClaimSpec) *apis.BlockDeviceList {
filteredBDList := &apis.BlockDeviceList{
TypeMeta: metav1.TypeMeta{
Kind: "BlockDevice",
APIVersion: "openebs.io/v1alpha1",
},
}
for _, bd := range originalBD.Items {
if bd.Status.State == controller.NDMActive {
filteredBDList.Items = append(filteredBDList.Items, bd)
}
}
return filteredBDList
}
// filterUnclaimed returns only unclaimed devices
func filterUnclaimed(originalBD *apis.BlockDeviceList, spec *apis.DeviceClaimSpec) *apis.BlockDeviceList {
filteredBDList := &apis.BlockDeviceList{
TypeMeta: metav1.TypeMeta{
Kind: "BlockDevice",
APIVersion: "openebs.io/v1alpha1",
},
}
for _, bd := range originalBD.Items {
if bd.Status.ClaimState == apis.BlockDeviceUnclaimed {
filteredBDList.Items = append(filteredBDList.Items, bd)
}
}
return filteredBDList
}
// filterDeviceType returns only BDs which match the device type
func filterDeviceType(originalBD *apis.BlockDeviceList, spec *apis.DeviceClaimSpec) *apis.BlockDeviceList {
// if no device type is specified, filter will not be effective
if spec.DeviceType == "" {
return originalBD
}
filteredBDList := &apis.BlockDeviceList{
TypeMeta: metav1.TypeMeta{
Kind: "BlockDevice",
APIVersion: "openebs.io/v1alpha1",
},
}
for _, bd := range originalBD.Items {
if bd.Spec.Details.DeviceType == spec.DeviceType {
filteredBDList.Items = append(filteredBDList.Items, bd)
}
}
return filteredBDList
}
// filterVolumeMode returns only BDs which matches the specified volume mode
func filterVolumeMode(originalBD *apis.BlockDeviceList, spec *apis.DeviceClaimSpec) *apis.BlockDeviceList {
volumeMode := spec.Details.BlockVolumeMode
// if volume mode is not specified in claim spec, this filter will not be effective
if volumeMode == "" {
return originalBD
}
filteredBDList := &apis.BlockDeviceList{
TypeMeta: metav1.TypeMeta{
Kind: "BlockDevice",
APIVersion: "openebs.io/v1alpha1",
},
}
for _, bd := range originalBD.Items {
switch volumeMode {
case apis.VolumeModeBlock:
// if blockvolume mode, FS and Mountpoint should be empty
if bd.Spec.FileSystem.Type != "" || bd.Spec.FileSystem.Mountpoint != "" {
continue
}
case apis.VolumeModeFileSystem:
// in FSVolume Mode,
// In BD: FS and Mountpoint should not be empty. If empty the BD
// is removed by filter
if bd.Spec.FileSystem.Type == "" || bd.Spec.FileSystem.Mountpoint == "" {
continue
}
// In BDC: If DeviceFormat is specified, then it should match with BD,
// else do not compare FSType. If the check fails, the BD is removed by the filter.
if spec.Details.DeviceFormat != "" && bd.Spec.FileSystem.Type != spec.Details.DeviceFormat {
continue
}
}
filteredBDList.Items = append(filteredBDList.Items, bd)
}
return filteredBDList
}
// filterBlockDeviceName returns a single BD in the list, which matches the BDName
func filterBlockDeviceName(originalBD *apis.BlockDeviceList, spec *apis.DeviceClaimSpec) *apis.BlockDeviceList {
filteredBDList := &apis.BlockDeviceList{
TypeMeta: metav1.TypeMeta{
Kind: "BlockDevice",
APIVersion: "openebs.io/v1alpha1",
},
}
for _, bd := range originalBD.Items {
if bd.Name == spec.BlockDeviceName {
filteredBDList.Items = append(filteredBDList.Items, bd)
break
}
}
return filteredBDList
}
// filterResourceStorage gets the devices which match the storage resource requirement
func filterResourceStorage(originalBD *apis.BlockDeviceList, spec *apis.DeviceClaimSpec) *apis.BlockDeviceList {
filteredBDList := &apis.BlockDeviceList{
TypeMeta: metav1.TypeMeta{
Kind: "BlockDevice",
APIVersion: "openebs.io/v1alpha1",
},
}
capacity, _ := verify.GetRequestedCapacity(spec.Resources.Requests)
for _, bd := range originalBD.Items {
if bd.Spec.Capacity.Storage >= uint64(capacity) {
filteredBDList.Items = append(filteredBDList.Items, bd)
break
}
}
return filteredBDList
}
// filterOutSparseBlockDevice returns only non sparse devices
func filterOutSparseBlockDevice(originalBD *apis.BlockDeviceList, spec *apis.DeviceClaimSpec) *apis.BlockDeviceList {
filteredBDList := &apis.BlockDeviceList{
TypeMeta: metav1.TypeMeta{
Kind: "BlockDevice",
APIVersion: "openebs.io/v1alpha1",
},
}
for _, bd := range originalBD.Items {
if bd.Spec.Details.DeviceType != blockdevice.SparseBlockDeviceType {
filteredBDList.Items = append(filteredBDList.Items, bd)
}
}
return filteredBDList
}
func filterNodeName(originalBD *apis.BlockDeviceList, spec *apis.DeviceClaimSpec) *apis.BlockDeviceList {
// if node name is not given in BDC, this filter will not work
if len(spec.BlockDeviceNodeAttributes.NodeName) == 0 {
return originalBD
}
filteredBDList := &apis.BlockDeviceList{
TypeMeta: metav1.TypeMeta{
Kind: "BlockDevice",
APIVersion: "openebs.io/v1alpha1",
},
}
for _, bd := range originalBD.Items {
if bd.Spec.NodeAttributes.NodeName == spec.BlockDeviceNodeAttributes.NodeName {
filteredBDList.Items = append(filteredBDList.Items, bd)
}
}
return filteredBDList
}
// filterBlockDeviceTag is used to filter out BlockDevices which do not have the
// block-device-tag label. This filter works on a block device list which has
// already been filtered by the given selector.
func filterBlockDeviceTag(originalBD *apis.BlockDeviceList, spec *apis.DeviceClaimSpec) *apis.BlockDeviceList {
// if the block-device-tag label was already included in the selector
// given in the BDC by the user, then this filter is not required. This
// is because it would have already performed the filter operation with the
// label. If the label is not present, a new selector is made to remove
// devices which have that label.
if !isBDTagDoesNotExistSelectorRequired(spec.Selector) {
return originalBD
}
// a DoesNotExist requirement is created to filter out devices which have
// the block-device-tag label
blockDeviceTagRequirement, err := labels.NewRequirement(kubernetes.BlockDeviceTagLabel, selection.DoesNotExist, []string{})
// this error should never occur, because error for DoesNotExist happen
// only when non zero length of values are passed
if err != nil {
klog.Info("could not create requirement for label ", kubernetes.BlockDeviceTagLabel)
return originalBD
}
blockDeviceTagDoesNotExistSelector := labels.NewSelector()
blockDeviceTagDoesNotExistSelector =
blockDeviceTagDoesNotExistSelector.Add(*blockDeviceTagRequirement)
filteredBDList := &apis.BlockDeviceList{
TypeMeta: metav1.TypeMeta{
Kind: "BlockDevice",
APIVersion: "openebs.io/v1alpha1",
},
}
for _, bd := range originalBD.Items {
// if the tag label is not present, the BD will be included in the list
if blockDeviceTagDoesNotExistSelector.Matches(labels.Set(bd.Labels)) {
filteredBDList.Items = append(filteredBDList.Items, bd)
}
}
return filteredBDList
}
// isBDTagDoesNotExistSelectorRequired is used to check whether a selector
// was present on the BDC. It is used to decide whether a `does not exist` selector
// for the block-device-tag label should be applied or not.
//
// all the filters are applied after the List call which uses the selector.
// therefore, we don't need to again apply a label selector.
func isBDTagDoesNotExistSelectorRequired(options *metav1.LabelSelector) bool {
if options == nil {
return true
}
if _, ok := options.MatchLabels[kubernetes.BlockDeviceTagLabel]; ok {
return false
}
requirements := options.MatchExpressions
for _, req := range requirements {
if req.Key == kubernetes.BlockDeviceTagLabel {
return false
}
}
return true
}