From 0f57302c5ebbd4613ce41a1685666f6bcfa7c272 Mon Sep 17 00:00:00 2001 From: Shuo Wu Date: Fri, 27 Sep 2024 00:30:06 +0800 Subject: [PATCH] feat: add func BdevLvolGetWithFilter Longhorn 9121 Signed-off-by: Shuo Wu --- pkg/spdk/client/basic.go | 17 ++++++++++++++++- pkg/spdk/spdk_test.go | 23 +++++++++++++++-------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/pkg/spdk/client/basic.go b/pkg/spdk/client/basic.go index 4bba39c..96e3520 100644 --- a/pkg/spdk/client/basic.go +++ b/pkg/spdk/client/basic.go @@ -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 /. 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, @@ -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 { diff --git a/pkg/spdk/spdk_test.go b/pkg/spdk/spdk_test.go index 513b43f..a44d5b3 100644 --- a/pkg/spdk/spdk_test.go +++ b/pkg/spdk/spdk_test.go @@ -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(lvolUUID2, 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) @@ -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, "") }