Skip to content

Commit

Permalink
provisioner/lvm: ensure only one lvmvg for the vgName
Browse files Browse the repository at this point in the history
    - Because the lvmvg name is generated with the prefix, we need to
      ensure we only have one lvmvg CR for the specific vgName in the
      same node.

Signed-off-by: Vicente Cheng <[email protected]>
(cherry picked from commit ca64541)
  • Loading branch information
Vicente-Cheng committed Oct 28, 2024
1 parent 30e1542 commit f9957d2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
7 changes: 5 additions & 2 deletions pkg/controller/blockdevice/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"reflect"
"sync"
"time"

gocommon "github.com/harvester/go-common"
Expand Down Expand Up @@ -38,7 +39,8 @@ type Controller struct {
BlockdeviceCache ctldiskv1.BlockDeviceCache
BlockInfo block.Info

LVMVgClient ctldiskv1.LVMVolumeGroupController
LVMVgClient ctldiskv1.LVMVolumeGroupController
provisionerLock *sync.Mutex // Lock for some specific provisioner operations, e.g. LVM

scanner *Scanner
semaphore *provisioner.Semaphore
Expand Down Expand Up @@ -81,6 +83,7 @@ func Register(
BlockInfo: block,
scanner: scanner,
semaphore: semaphoreObj,
provisionerLock: &sync.Mutex{},
}

if err := scanner.Start(); err != nil {
Expand Down Expand Up @@ -269,7 +272,7 @@ func (c *Controller) generateLHv1Provisioner(device *diskv1.BlockDevice) (provis

func (c *Controller) generateLVMProvisioner(device *diskv1.BlockDevice) (provisioner.Provisioner, error) {
vgName := device.Spec.Provisioner.LVM.VgName
return provisioner.NewLVMProvisioner(vgName, c.NodeName, c.LVMVgClient, device, c.BlockInfo)
return provisioner.NewLVMProvisioner(vgName, c.NodeName, c.LVMVgClient, device, c.BlockInfo, c.provisionerLock)
}

func (c *Controller) generateLHv2Provisioner(device *diskv1.BlockDevice) (provisioner.Provisioner, error) {
Expand Down
8 changes: 7 additions & 1 deletion pkg/provisioner/lvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package provisioner
import (
"fmt"
"reflect"
"sync"

"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -20,9 +21,10 @@ type LVMProvisioner struct {
vgName string
nodeName string
vgClient ctldiskv1.LVMVolumeGroupController
lock *sync.Mutex
}

func NewLVMProvisioner(vgName, nodeName string, lvmVGs ctldiskv1.LVMVolumeGroupController, device *diskv1.BlockDevice, blockInfo block.Info) (Provisioner, error) {
func NewLVMProvisioner(vgName, nodeName string, lvmVGs ctldiskv1.LVMVolumeGroupController, device *diskv1.BlockDevice, blockInfo block.Info, lock *sync.Mutex) (Provisioner, error) {
baseProvisioner := &provisioner{
name: TypeLVM,
blockInfo: blockInfo,
Expand All @@ -33,6 +35,7 @@ func NewLVMProvisioner(vgName, nodeName string, lvmVGs ctldiskv1.LVMVolumeGroupC
vgName: vgName,
vgClient: lvmVGs,
nodeName: nodeName,
lock: lock,
}, nil
}

Expand Down Expand Up @@ -67,6 +70,9 @@ func (l *LVMProvisioner) UnFormat() (bool, error) {
func (l *LVMProvisioner) Provision() (bool, error) {
logrus.Infof("Provisioning block device %s to vg: %s", l.device.Name, l.vgName)
found := true
// because the LVMVG name is a generated name, we need to lock here to ensure we only have one LVMVG CRD for specific vgName.
l.lock.Lock()
defer l.lock.Unlock()
lvmvg, err := l.getTargetLVMVG()
if err != nil {
if !errors.IsNotFound(err) {
Expand Down

0 comments on commit f9957d2

Please sign in to comment.