Skip to content

Commit

Permalink
feat: add func BdevLvolGetWithFilter
Browse files Browse the repository at this point in the history
Longhorn 9121

Signed-off-by: Shuo Wu <[email protected]>
  • Loading branch information
shuo-wu committed Sep 27, 2024
1 parent 22115a9 commit 38f51a8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
17 changes: 16 additions & 1 deletion pkg/spdk/client/basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,19 @@ func (c *Client) BdevLvolDelete(name string) (deleted bool, err error) {
//
// "timeout": Optional. 0 by default, meaning the method returns immediately whether the lvol bdev exists or not.
func (c *Client) BdevLvolGet(name string, timeout uint64) (bdevLvolInfoList []spdktypes.BdevInfo, err error) {
return c.BdevLvolGetWithFilter(name, timeout, func(*spdktypes.BdevInfo) bool { return true })
}

// BdevLvolGetWithFilter gets information about some specific lvol bdevs.
//
// "name": Optional. UUID or alias of a logical volume (lvol) bdev.
// The alias of a lvol bdev is <LVSTORE NAME>/<LVOL NAME>. And the name of a lvol bdev is UUID.
// If this is not specified, the function will list all lvol bdevs.
//
// "timeout": Optional. 0 by default, meaning the method returns immediately whether the lvol bdev exists or not.
//
// "filter": Only the lvol bdevs that pass the filter will be returned.
func (c *Client) BdevLvolGetWithFilter(name string, timeout uint64, filter func(*spdktypes.BdevInfo) bool) (bdevLvolInfoList []spdktypes.BdevInfo, err error) {
req := spdktypes.BdevGetBdevsRequest{
Name: name,
Timeout: timeout,
Expand All @@ -269,7 +282,9 @@ func (c *Client) BdevLvolGet(name string, timeout uint64) (bdevLvolInfoList []sp
if spdktypes.GetBdevType(&b) != spdktypes.BdevTypeLvol {
continue
}

if !filter(&b) {
continue
}
b.DriverSpecific.Lvol.Xattrs = make(map[string]string)
user_created, err := c.BdevLvolGetXattr(b.Name, UserCreated)
if err == nil {
Expand Down
23 changes: 15 additions & 8 deletions pkg/spdk/spdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,22 @@ func (s *TestSuite) TestSPDKBasic(c *C) {
c.Assert(deleted, Equals, true)
}()

lvolList, err := spdkCli.BdevLvolGet("", 0)
lvolFilter1 := func(bdev *spdktypes.BdevInfo) bool {
return bdev.DriverSpecific.Lvol != nil && bdev.UUID == lvolUUID1
}
lvolList1, err := spdkCli.BdevLvolGetWithFilter("", 0, lvolFilter1)
c.Assert(err, IsNil)
c.Assert(len(lvolList), Equals, 2)
c.Assert(len(lvolList1), Equals, 1)
c.Assert(lvolList1[0].Aliases[0], Equals, fmt.Sprintf("%s/%s", lvsName, lvolName1))
lvolFilter2 := func(bdev *spdktypes.BdevInfo) bool {
return bdev.DriverSpecific.Lvol != nil && bdev.UUID == lvolUUID2
}
lvolList2, err := spdkCli.BdevLvolGetWithFilter("", 0, lvolFilter2)
c.Assert(err, IsNil)
c.Assert(len(lvolList2), Equals, 1)
c.Assert(lvolList2[0].Aliases[0], Equals, fmt.Sprintf("%s/%s", lvsName, lvolName2))

lvolList := append(lvolList1, lvolList2...)
for _, lvol := range lvolList {
c.Assert(len(lvol.Aliases), Equals, 1)
c.Assert(uint64(lvol.BlockSize)*lvol.NumBlocks, Equals, defaultLvolSizeInMiB*types.MiB)
Expand All @@ -175,12 +188,6 @@ func (s *TestSuite) TestSPDKBasic(c *C) {
c.Assert(lvol.DriverSpecific.Lvol.Snapshot, Equals, false)
c.Assert(lvol.DriverSpecific.Lvol.Clone, Equals, false)
c.Assert(lvol.DriverSpecific.Lvol.LvolStoreUUID, Equals, lvsUUID)
if lvol.UUID == lvolUUID1 {
c.Assert(lvol.Aliases[0], Equals, fmt.Sprintf("%s/%s", lvsName, lvolName1))
}
if lvol.UUID == lvolUUID2 {
c.Assert(lvol.Aliases[0], Equals, fmt.Sprintf("%s/%s", lvsName, lvolName2))
}
c.Assert(lvol.DriverSpecific.Lvol.Xattrs[client.UserCreated], Equals, "true")
c.Assert(lvol.DriverSpecific.Lvol.Xattrs[client.SnapshotTimestamp], Equals, "")
}
Expand Down

0 comments on commit 38f51a8

Please sign in to comment.