-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(controller,filter): add blockdevice tag label while claiming (#400)
- add support for new label `openebs.io/block-device-tag` - add a new filter while claiming block devices. If `openebs.io/block-device-tag` is present on a BlockDevice, only BDCs having a matching selector will be able to claim that block device - Fix a bug where selector is accidentally updated on BDC while updating other fields. BDC controller used a pointer reference of the selector. This has been changed to copy the selector and use it. This is done so that an update on the BDC object does not cause the selector to get updated with a different value other than the one which was applied by user. - Add unit tests for new filter Sample BDC Yaml which makes use of `openebs.io/block-device-tag` label ```yaml apiVersion: openebs.io/v1alpha1 kind: BlockDeviceClaim metadata: finalizers: - openebs.io/bdc-protection name: bdc-ss35 namespace: default spec: blockDeviceName: blockdevice-91d422d8517f935431daae722f8fdfa0 resources: requests: storage: 10M selector: matchLabels: openebs.io/block-device-tag: X status: phase: Bound ``` BlockDevice that was claimed by the above BDC ```yaml kind: BlockDevice metadata: labels: kubernetes.io/hostname: gke-akhil-ndm-pool-1-4349c998-vt36 ndm.io/blockdevice-type: blockdevice ndm.io/managed: "true" openebs.io/block-device-tag: X name: blockdevice-91d422d8517f935431daae722f8fdfa0 namespace: default spec: capacity: storage: 53687091200 claimRef: apiVersion: openebs.io/v1alpha1 kind: BlockDeviceClaim name: bdc-ss35 namespace: default resourceVersion: "72629912" uid: f6c7af75-783e-11ea-9bcf-42010a80015a details: compliance: SPC-4 deviceType: disk driveType: HDD firmwareRevision: '1 ' hardwareSectorSize: 512 logicalBlockSize: 512 model: PersistentDisk physicalBlockSize: 4096 serial: akhil-disk-3 vendor: Google devlinks: - kind: by-id links: - /dev/disk/by-id/scsi-0Google_PersistentDisk_akhil-disk-3 - /dev/disk/by-id/google-akhil-disk-3 - kind: by-path links: - /dev/disk/by-path/pci-0000:00:03.0-scsi-0:0:2:0 nodeAttributes: nodeName: gke-akhil-ndm-pool-1-4349c998-vt36 path: /dev/sdc status: claimState: Claimed state: Active ``` Signed-off-by: Akhil Mohan <akhil.mohan@mayadata.io>
- Loading branch information
Showing
5 changed files
with
272 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
package blockdevice | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
controller "github.com/openebs/node-disk-manager/cmd/ndm_daemonset/controller" | ||
apis "github.com/openebs/node-disk-manager/pkg/apis/openebs/v1alpha1" | ||
"github.com/stretchr/testify/assert" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
const ( | ||
TestNoOfBDs = 6 | ||
) | ||
|
||
type BDLabel map[string]string | ||
type BDLabelList []BDLabel | ||
|
||
func TestFilterBlockDeviceTag(t *testing.T) { | ||
|
||
// label list with no additional labels | ||
bdLabelList1 := []BDLabel{ | ||
map[string]string{ | ||
controller.KubernetesHostNameLabel: "host1", | ||
}, | ||
map[string]string{ | ||
controller.KubernetesHostNameLabel: "host2", | ||
}, | ||
map[string]string{ | ||
controller.KubernetesHostNameLabel: "host3", | ||
}, | ||
map[string]string{ | ||
controller.KubernetesHostNameLabel: "host4", | ||
}, | ||
map[string]string{ | ||
controller.KubernetesHostNameLabel: "host5", | ||
}, | ||
map[string]string{ | ||
controller.KubernetesHostNameLabel: "host6", | ||
}, | ||
} | ||
|
||
// label list with all devices having same tag | ||
bdLabelList2 := []BDLabel{ | ||
map[string]string{ | ||
controller.KubernetesHostNameLabel: "host1", | ||
controller.BlockDeviceTagLabel: "X", | ||
}, | ||
map[string]string{ | ||
controller.KubernetesHostNameLabel: "host2", | ||
controller.BlockDeviceTagLabel: "X", | ||
}, | ||
map[string]string{ | ||
controller.KubernetesHostNameLabel: "host3", | ||
controller.BlockDeviceTagLabel: "X", | ||
}, | ||
map[string]string{ | ||
controller.KubernetesHostNameLabel: "host4", | ||
controller.BlockDeviceTagLabel: "X", | ||
}, | ||
map[string]string{ | ||
controller.KubernetesHostNameLabel: "host5", | ||
controller.BlockDeviceTagLabel: "X", | ||
}, | ||
map[string]string{ | ||
controller.KubernetesHostNameLabel: "host6", | ||
controller.BlockDeviceTagLabel: "X", | ||
}, | ||
} | ||
|
||
// label list with some devices having default label and some devices | ||
// with device tag | ||
bdLabelList3 := []BDLabel{ | ||
map[string]string{ | ||
controller.KubernetesHostNameLabel: "host1", | ||
}, | ||
map[string]string{ | ||
controller.KubernetesHostNameLabel: "host2", | ||
}, | ||
map[string]string{ | ||
controller.KubernetesHostNameLabel: "host3", | ||
controller.BlockDeviceTagLabel: "X", | ||
}, | ||
map[string]string{ | ||
controller.KubernetesHostNameLabel: "host4", | ||
controller.BlockDeviceTagLabel: "X", | ||
}, | ||
map[string]string{ | ||
controller.KubernetesHostNameLabel: "host5", | ||
controller.BlockDeviceTagLabel: "Y", | ||
}, | ||
map[string]string{ | ||
controller.KubernetesHostNameLabel: "host6", | ||
controller.BlockDeviceTagLabel: "Y", | ||
}, | ||
} | ||
|
||
type args struct { | ||
bdLabelList BDLabelList | ||
spec *apis.DeviceClaimSpec | ||
} | ||
tests := map[string]struct { | ||
args args | ||
wantedNoofBDs int | ||
}{ | ||
"no labels on any BD and no selector on BDC": { | ||
args: args{ | ||
bdLabelList: bdLabelList1, | ||
spec: &apis.DeviceClaimSpec{}, | ||
}, | ||
wantedNoofBDs: 6, | ||
}, | ||
"all BDs have same device tag label and no selector": { | ||
args: args{ | ||
bdLabelList: bdLabelList2, | ||
spec: &apis.DeviceClaimSpec{}, | ||
}, | ||
wantedNoofBDs: 0, | ||
}, | ||
"all BDs have same device tag label and selector for tag": { | ||
args: args{ | ||
bdLabelList: bdLabelList2, | ||
spec: &apis.DeviceClaimSpec{ | ||
Selector: &v1.LabelSelector{ | ||
MatchLabels: map[string]string{controller.BlockDeviceTagLabel: "X"}, | ||
}, | ||
}, | ||
}, | ||
wantedNoofBDs: 6, | ||
}, | ||
"all BDs have same device tag label and custom label used in selector": { | ||
args: args{ | ||
bdLabelList: bdLabelList2, | ||
spec: &apis.DeviceClaimSpec{ | ||
Selector: &v1.LabelSelector{ | ||
MatchLabels: map[string]string{"ndm.io/test": "test"}, | ||
}, | ||
}, | ||
}, | ||
wantedNoofBDs: 0, | ||
}, | ||
"some BDs with tag and some without tag, combined with no selector": { | ||
args: args{ | ||
bdLabelList: bdLabelList3, | ||
spec: &apis.DeviceClaimSpec{ | ||
Selector: &v1.LabelSelector{}, | ||
}, | ||
}, | ||
wantedNoofBDs: 2, | ||
}, | ||
} | ||
for name, test := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
bdLabelList := test.args.bdLabelList | ||
spec := test.args.spec | ||
wantedNoOfBDs := test.wantedNoofBDs | ||
originalBDList := createFakeBlockDeviceList(bdLabelList, TestNoOfBDs) | ||
got := filterBlockDeviceTag(originalBDList, spec) | ||
assert.Equal(t, wantedNoOfBDs, len(got.Items)) | ||
}) | ||
} | ||
} | ||
|
||
func createFakeBlockDeviceList(labelList BDLabelList, noOfBDs int) *apis.BlockDeviceList { | ||
bdListAPI := &apis.BlockDeviceList{ | ||
TypeMeta: v1.TypeMeta{ | ||
Kind: "BlockDevice", | ||
APIVersion: "openebs.io/v1alpha1", | ||
}, | ||
Items: []apis.BlockDevice{}, | ||
} | ||
for i := 0; i < noOfBDs; i++ { | ||
bdName := fmt.Sprint("bd", i) | ||
bdListAPI.Items = append(bdListAPI.Items, createFakeBlockDevice(bdName, labelList[i])) | ||
} | ||
return bdListAPI | ||
} | ||
|
||
func createFakeBlockDevice(name string, label map[string]string) apis.BlockDevice { | ||
bdAPI := apis.BlockDevice{ | ||
TypeMeta: v1.TypeMeta{ | ||
Kind: "BlockDevice", | ||
APIVersion: "openebs.io/v1alpha1", | ||
}, | ||
} | ||
bdAPI.Name = name | ||
bdAPI.Labels = label | ||
return bdAPI | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters