From 78e6c3de0a3fd66d084e03a05bd4185d95879ea7 Mon Sep 17 00:00:00 2001 From: Vicente Cheng Date: Mon, 7 Oct 2024 00:08:16 +0800 Subject: [PATCH] controller: drop `spec.filesystem.provisioned` - Now we only check the `spec.provision` Signed-off-by: Vicente Cheng --- pkg/controller/blockdevice/controller.go | 43 +++++++++++++++--------- pkg/controller/blockdevice/scanner.go | 10 ++++-- pkg/provisioner/longhornv1.go | 2 +- 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/pkg/controller/blockdevice/controller.go b/pkg/controller/blockdevice/controller.go index f06e3c79..8d235e22 100644 --- a/pkg/controller/blockdevice/controller.go +++ b/pkg/controller/blockdevice/controller.go @@ -111,6 +111,10 @@ func (c *Controller) OnBlockDeviceChange(_ string, device *diskv1.BlockDevice) ( logrus.Warnf("Failed to generate provisioner for device %s: %v", device.Name, err) return nil, err } + if !reflect.DeepEqual(device, deviceCpy) { + logrus.Debugf("Update block device %s for new provisioner state", device.Name) + return c.Blockdevices.Update(deviceCpy) + } if provisionerInst == nil { logrus.Infof("Skip device %s as no provisioner found or not configured", device.Name) return nil, nil @@ -207,26 +211,32 @@ func (c *Controller) finalizeBlockDevice(oldBd, newBd *diskv1.BlockDevice, devPa } func (c *Controller) generateProvisioner(device *diskv1.BlockDevice) (provisioner.Provisioner, error) { - if device.Spec.Provisioner == nil && !device.Spec.FileSystem.Provisioned && device.Status.ProvisionPhase != diskv1.ProvisionPhaseProvisioned { + // skip the non-provisioned device + if device.Spec.Provisioner == nil && device.Status.ProvisionPhase != diskv1.ProvisionPhaseProvisioned { + return nil, nil + } + // upgrade case, we need to update some fields + if device.Spec.Provisioner == nil && device.Status.ProvisionPhase == diskv1.ProvisionPhaseProvisioned { + device.Spec.Provision = true + device.Spec.FileSystem.Provisioned = false + provisionerLHV1 := &diskv1.LonghornProvisionerInfo{ + EngineVersion: provisioner.TypeLonghornV1, + } + device.Spec.Provisioner = &diskv1.ProvisionerInfo{ + Longhorn: provisionerLHV1, + } return nil, nil } logrus.Infof("Generate provisioner from device %s, content: %v", device.Name, device.Spec.Provisioner) // set default provisionerType := provisioner.TypeLonghornV1 if device.Spec.Provisioner != nil { - // **TODO**: we should use webhook to validate the provisioner type (and number) - numProvisioner := 0 if device.Spec.Provisioner.Longhorn != nil { - numProvisioner++ provisionerType = device.Spec.Provisioner.Longhorn.EngineVersion } if device.Spec.Provisioner.LVM != nil { - numProvisioner++ provisionerType = provisioner.TypeLVM } - if numProvisioner > 1 { - return nil, fmt.Errorf("multiple provisioner types found for block device %s", device.Name) - } } switch provisionerType { case provisioner.TypeLonghornV1: @@ -315,7 +325,13 @@ func (c *Controller) updateDeviceStatus(device *diskv1.BlockDevice, devPath stri if needAutoProvision { logrus.Infof("Auto provisioning block device %s", device.Name) device.Spec.FileSystem.ForceFormatted = true - device.Spec.FileSystem.Provisioned = true + device.Spec.Provision = true + device.Spec.Provisioner = &diskv1.ProvisionerInfo{ + Longhorn: &diskv1.LonghornProvisionerInfo{ + EngineVersion: provisioner.TypeLonghornV1, + }, + } + } return nil } @@ -402,16 +418,13 @@ func canSkipBlockDeviceChange(device *diskv1.BlockDevice, nodeName string) bool } func needProvisionerUnprovision(device *diskv1.BlockDevice) bool { - return (!device.Spec.FileSystem.Provisioned && !device.Spec.Provision) && - device.Status.ProvisionPhase != diskv1.ProvisionPhaseUnprovisioned + return !device.Spec.Provision && device.Status.ProvisionPhase != diskv1.ProvisionPhaseUnprovisioned } func needProvisionerUpdate(oldBd, newBd *diskv1.BlockDevice) bool { - return oldBd.Status.ProvisionPhase == diskv1.ProvisionPhaseProvisioned && - (newBd.Spec.FileSystem.Provisioned || newBd.Spec.Provision) + return oldBd.Status.ProvisionPhase == diskv1.ProvisionPhaseProvisioned && newBd.Spec.Provision } func needProvisionerProvision(oldBd, newBd *diskv1.BlockDevice) bool { - return oldBd.Status.ProvisionPhase == diskv1.ProvisionPhaseUnprovisioned && - (newBd.Spec.FileSystem.Provisioned || newBd.Spec.Provision) + return oldBd.Status.ProvisionPhase == diskv1.ProvisionPhaseUnprovisioned && newBd.Spec.Provision } diff --git a/pkg/controller/blockdevice/scanner.go b/pkg/controller/blockdevice/scanner.go index bc625c21..33fdbc98 100644 --- a/pkg/controller/blockdevice/scanner.go +++ b/pkg/controller/blockdevice/scanner.go @@ -16,6 +16,7 @@ import ( "github.com/harvester/node-disk-manager/pkg/block" "github.com/harvester/node-disk-manager/pkg/filter" ctldiskv1 "github.com/harvester/node-disk-manager/pkg/generated/controllers/harvesterhci.io/v1beta1" + "github.com/harvester/node-disk-manager/pkg/provisioner" ) type Scanner struct { @@ -291,7 +292,12 @@ func (s *Scanner) SaveBlockDevice(bd *diskv1.BlockDevice, autoProvisioned bool) if errors.IsNotFound(err) { if autoProvisioned { bd.Spec.FileSystem.ForceFormatted = true - bd.Spec.FileSystem.Provisioned = true + bd.Spec.Provision = true + bd.Spec.Provisioner = &diskv1.ProvisionerInfo{ + Longhorn: &diskv1.LonghornProvisionerInfo{ + EngineVersion: provisioner.TypeLonghornV1, + }, + } } logrus.Infof("Add new block device %s with device: %s", bd.Name, bd.Spec.DevPath) return s.Blockdevices.Create(bd) @@ -309,7 +315,7 @@ func (s *Scanner) SaveBlockDevice(bd *diskv1.BlockDevice, autoProvisioned bool) // - disk hasn't yet been force formatted // - disk matches auto-provisioned patterns func (s *Scanner) NeedsAutoProvision(oldBd *diskv1.BlockDevice, autoProvisionPatternMatches bool) bool { - return !oldBd.Spec.FileSystem.Provisioned && !oldBd.Spec.Provision && autoProvisionPatternMatches && oldBd.Status.DeviceStatus.FileSystem.LastFormattedAt == nil + return !oldBd.Spec.Provision && autoProvisionPatternMatches && oldBd.Status.DeviceStatus.FileSystem.LastFormattedAt == nil } // isDevPathChanged returns true if the device path has changed. diff --git a/pkg/provisioner/longhornv1.go b/pkg/provisioner/longhornv1.go index e5b390c6..8dc49fe4 100644 --- a/pkg/provisioner/longhornv1.go +++ b/pkg/provisioner/longhornv1.go @@ -439,7 +439,7 @@ func needUpdateMountPoint(bd *diskv1.BlockDevice, filesystem *block.FileSystemIn return NeedMountUpdateNoOp } - provisioned := bd.Spec.FileSystem.Provisioned || bd.Spec.Provision + provisioned := bd.Spec.Provision logrus.Debugf("Checking mount operation with FS.Provisioned %v, FS.Mountpoint %s", provisioned, filesystem.MountPoint) if provisioned { if filesystem.MountPoint == "" {