Skip to content

Commit

Permalink
Merge pull request #448 from suleymanakbas91/recover-from-vg
Browse files Browse the repository at this point in the history
OCPVE-605: Allow recovering from existing volume groups
  • Loading branch information
openshift-ci[bot] authored Oct 13, 2023
2 parents 48c4139 + f1f4092 commit 1946d91
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 32 deletions.
76 changes: 45 additions & 31 deletions internal/controllers/vgmanager/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,25 +151,6 @@ func (r *Reconciler) reconcile(
}
}

// Read the lvmd config file
lvmdConfig, err := r.LVMD.Load()
if err != nil {
err = fmt.Errorf("failed to read the lvmd config file: %w", err)
if _, err := r.setVolumeGroupFailedStatus(ctx, volumeGroup, nil, err); err != nil {
logger.Error(err, "failed to set status to failed")
}
return ctrl.Result{}, err
}

lvmdConfigWasMissing := false
if lvmdConfig == nil {
lvmdConfigWasMissing = true
lvmdConfig = &lvmd.Config{
SocketName: constants.DefaultLVMdSocket,
}
}
existingLvmdConfig := *lvmdConfig

blockDevices, err := r.LSBLK.ListBlockDevices()
if err != nil {
return ctrl.Result{}, fmt.Errorf("failed to list block devices: %w", err)
Expand Down Expand Up @@ -204,9 +185,8 @@ func (r *Reconciler) reconcile(
vgExistsInLVM := false
for _, vg := range vgs {
if volumeGroup.Name == vg.Name {
if len(vg.PVs) > 0 {
vgExistsInLVM = true
}
vgExistsInLVM = true
break
}
}

Expand Down Expand Up @@ -237,6 +217,11 @@ func (r *Reconciler) reconcile(

msg := "all the available devices are attached to the volume group"
logger.Info(msg)

if err := r.applyLVMDConfig(ctx, volumeGroup, devices); err != nil {
return reconcileAgain, err
}

if updated, err := r.setVolumeGroupReadyStatus(ctx, volumeGroup, devices); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to set status for volume group %s to ready: %w", volumeGroup.Name, err)
} else if updated {
Expand Down Expand Up @@ -285,6 +270,42 @@ func (r *Reconciler) reconcile(
return ctrl.Result{}, err
}

if err := r.applyLVMDConfig(ctx, volumeGroup, devices); err != nil {
return reconcileAgain, err
}

if updated, err := r.setVolumeGroupReadyStatus(ctx, volumeGroup, devices); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to set status for volume group %s to ready: %w", volumeGroup.Name, err)
} else if updated {
msg := "all the available devices are attached to the volume group"
r.NormalEvent(ctx, volumeGroup, EventReasonVolumeGroupReady, msg)
}

return reconcileAgain, nil
}

func (r *Reconciler) applyLVMDConfig(ctx context.Context, volumeGroup *lvmv1alpha1.LVMVolumeGroup, devices *FilteredBlockDevices) error {
logger := log.FromContext(ctx).WithValues("VGName", volumeGroup.Name)

// Read the lvmd config file
lvmdConfig, err := r.LVMD.Load()
if err != nil {
err = fmt.Errorf("failed to read the lvmd config file: %w", err)
if _, err := r.setVolumeGroupFailedStatus(ctx, volumeGroup, nil, err); err != nil {
logger.Error(err, "failed to set status to failed")
}
return err
}

lvmdConfigWasMissing := false
if lvmdConfig == nil {
lvmdConfigWasMissing = true
lvmdConfig = &lvmd.Config{
SocketName: constants.DefaultLVMdSocket,
}
}
existingLvmdConfig := *lvmdConfig

// Add the volume group to device classes inside lvmd config if not exists
found := false
for _, deviceClass := range lvmdConfig.DeviceClasses {
Expand Down Expand Up @@ -323,21 +344,14 @@ func (r *Reconciler) reconcile(
if _, err := r.setVolumeGroupFailedStatus(ctx, volumeGroup, devices, err); err != nil {
logger.Error(err, "failed to set status to failed")
}
return ctrl.Result{}, err
return err
}
msg := "updated lvmd config with new deviceClasses"
logger.Info(msg)
r.NormalEvent(ctx, volumeGroup, EventReasonLVMDConfigUpdated, msg)
}

if updated, err := r.setVolumeGroupReadyStatus(ctx, volumeGroup, devices); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to set status for volume group %s to ready: %w", volumeGroup.Name, err)
} else if updated {
msg := "all the available devices are attached to the volume group"
r.NormalEvent(ctx, volumeGroup, EventReasonVolumeGroupReady, msg)
}

return reconcileAgain, nil
return nil
}

func (r *Reconciler) processDelete(ctx context.Context, volumeGroup *lvmv1alpha1.LVMVolumeGroup) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/vgmanager/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (r *Reconciler) filterDevices(ctx context.Context, devices []lsblk.BlockDev
for name, filterFunc := range filters {
logger := logger.WithValues("filter.Name", name)
if err := filterFunc(device); err != nil {
logger.Error(err, "excluded")
logger.Info("excluded", "reason", err)
filterErrs = append(filterErrs, err)
}
}
Expand Down

0 comments on commit 1946d91

Please sign in to comment.