Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
Vicente-Cheng committed Jun 5, 2024
1 parent b41669f commit 7d96e8b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 22 deletions.
20 changes: 15 additions & 5 deletions pkg/controller/blockdevice/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,13 +308,23 @@ func (c *Controller) OnBlockDeviceChange(_ string, device *diskv1.BlockDevice) (
}
case needProvision && device.Status.ProvisionPhase == diskv1.ProvisionPhaseUnprovisioned:
logrus.Infof("Prepare to provision device %s to node %s", device.Name, c.NodeName)
if err := c.provisionDeviceToNode(deviceCpy); err != nil {
err := fmt.Errorf("failed to provision device %s to node %s: %w", device.Name, c.NodeName, err)
logrus.Error(err)
diskv1.DiskAddedToNode.SetError(deviceCpy, "", err)
diskv1.DiskAddedToNode.SetStatusBool(deviceCpy, false)
if requeue, err := provisioner.Provision(); requeue {
if err != nil {
err := fmt.Errorf("failed to provision device %s to node %s: %w", device.Name, c.NodeName, err)
diskv1.DiskAddedToNode.SetError(deviceCpy, "", err)
diskv1.DiskAddedToNode.SetStatusBool(deviceCpy, false)
}
c.Blockdevices.EnqueueAfter(c.Namespace, device.Name, jitterEnqueueDelay())

}
//logrus.Infof("Prepare to provision device %s to node %s", device.Name, c.NodeName)
//if err := c.provisionDeviceToNode(deviceCpy); err != nil {
// err := fmt.Errorf("failed to provision device %s to node %s: %w", device.Name, c.NodeName, err)
// logrus.Error(err)
// diskv1.DiskAddedToNode.SetError(deviceCpy, "", err)
// diskv1.DiskAddedToNode.SetStatusBool(deviceCpy, false)
// c.Blockdevices.EnqueueAfter(c.Namespace, device.Name, jitterEnqueueDelay())
//}
}

if !reflect.DeepEqual(device, deviceCpy) {
Expand Down
22 changes: 16 additions & 6 deletions pkg/provisioner/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@ const (
)

type Provisioner interface {
Provision() error
Format() (bool, error)
UnFormat() (bool, error)
Provision() (bool, error)
UnProvision() (bool, error)
Update()
Update() (bool, error)
GetProvisionerName() string
}

func setCondDiskAddedToNodeSuccess(disk *diskv1.BlockDevice, message string) {
diskv1.DiskAddedToNode.SetError(disk, "", nil)
diskv1.DiskAddedToNode.SetStatusBool(disk, false)
diskv1.DiskAddedToNode.Message(disk, message)
func setCondDiskAddedToNodeFalse(device *diskv1.BlockDevice, message string, targetStatus diskv1.BlockDeviceProvisionPhase) {
device.Status.ProvisionPhase = targetStatus
diskv1.DiskAddedToNode.SetError(device, "", nil)
diskv1.DiskAddedToNode.SetStatusBool(device, false)
diskv1.DiskAddedToNode.Message(device, message)
}

func setCondDiskAddedToNodeSuccess(device *diskv1.BlockDevice, message string, targetStatus diskv1.BlockDeviceProvisionPhase) {
device.Status.ProvisionPhase = targetStatus
diskv1.DiskAddedToNode.SetError(device, "", nil)
diskv1.DiskAddedToNode.SetStatusBool(device, true)
diskv1.DiskAddedToNode.Message(device, message)
}
49 changes: 38 additions & 11 deletions pkg/provisioner/longhornv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package provisioner

import (
"fmt"
"reflect"
"time"

longhornv1 "github.com/longhorn/longhorn-manager/k8s/pkg/apis/longhorn/v1beta2"
Expand Down Expand Up @@ -44,11 +45,11 @@ func (p *LonghornV1Provisioner) GetProvisionerName() string {
return p.name
}

func (p *LonghornV1Provisioner) Provision() error {
func (p *LonghornV1Provisioner) Provision() (bool, error) {
logrus.Infof("%s provisioning Longhorn block device %s", p.name, p.device.Name)
if _, found := p.nodeObj.Spec.Disks[p.device.Name]; found {
// already provisioned, do noop
return nil
return false, nil
}
nodeObjCpy := p.nodeObj.DeepCopy()
diskSpec := longhornv1.DiskSpec{
Expand All @@ -58,11 +59,28 @@ func (p *LonghornV1Provisioner) Provision() error {
StorageReserved: 0,
Tags: p.device.Spec.Tags,
}
nodeObjCpy.Spec.Disks[p.device.Name] = diskSpec
if _, err := p.nodesClient.Update(nodeObjCpy); err != nil {
return err

// checked the case that longhorn node updated but blockdevice CRD is not updated
synced := false
if disk, found := p.nodeObj.Spec.Disks[p.device.Name]; found {
synced = reflect.DeepEqual(disk, diskSpec)
if !synced {
logrus.Warnf("The disk spec should not different between longhorn node and blockdevice CRD, disk: %v, diskSpec: %v", disk, diskSpec)
}
} else {
// not found, just updated!
nodeObjCpy.Spec.Disks[p.device.Name] = diskSpec
if _, err := p.nodesClient.Update(nodeObjCpy); err != nil {
return true, err
}
}
return nil

// This case means that the disk spec is
if synced && !diskv1.DiskAddedToNode.IsTrue(p.device) {
msg := fmt.Sprintf("Added disk %s to longhorn node `%s` as an additional disk", p.device.Name, p.nodeObj.Name)
setCondDiskAddedToNodeSuccess(p.device, msg, diskv1.ProvisionPhaseProvisioned)
}
return false, nil
}

func (p *LonghornV1Provisioner) UnProvision() (bool, error) {
Expand All @@ -71,8 +89,7 @@ func (p *LonghornV1Provisioner) UnProvision() (bool, error) {
// inner functions
updateProvisionPhaseUnprovisioned := func() {
msg := fmt.Sprintf("Disk not in longhorn node `%s`", p.nodeObj.Name)
p.device.Status.ProvisionPhase = diskv1.ProvisionPhaseUnprovisioned
setCondDiskAddedToNodeSuccess(p.device, msg)
setCondDiskAddedToNodeFalse(p.device, msg, diskv1.ProvisionPhaseUnprovisioned)
}

removeDiskFromNode := func() error {
Expand Down Expand Up @@ -135,8 +152,7 @@ func (p *LonghornV1Provisioner) UnProvision() (bool, error) {
return true, err
}
msg := fmt.Sprintf("Stop provisioning device %s to longhorn node `%s`", p.device.Name, p.nodeObj.Name)
p.device.Status.ProvisionPhase = diskv1.ProvisionPhaseUnprovisioning
setCondDiskAddedToNodeSuccess(p.device, msg)
setCondDiskAddedToNodeFalse(p.device, msg, diskv1.ProvisionPhaseUnprovisioning)
}

return false, nil
Expand Down Expand Up @@ -169,8 +185,19 @@ func (p *LonghornV1Provisioner) excludeTheDisk(targetDisk longhornv1.DiskSpec) e
return nil
}

func (p *LonghornV1Provisioner) Update() {
func (p *LonghornV1Provisioner) Update() (bool, error) {
logrus.Infof("%s updating Longhorn block device %s", p.name, p.device.Name)
return false, nil
}

func (p *LonghornV1Provisioner) Format() (bool, error) {
logrus.Infof("%s formatting Longhorn block device %s", p.name, p.device.Name)
return false, nil
}

func (p *LonghornV1Provisioner) UnFormat() (bool, error) {
logrus.Infof("%s unformatting Longhorn block device %s", p.name, p.device.Name)
return false, nil
}

func (p *LonghornV1Provisioner) updateDeviceFileSystem(device *diskv1.BlockDevice, devPath string) error {
Expand Down

0 comments on commit 7d96e8b

Please sign in to comment.