Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OCPVE-605: Allow recovering from existing volume groups #448

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
suleymanakbas91 marked this conversation as resolved.
Show resolved Hide resolved
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