Skip to content

Commit

Permalink
feat: delete thin pool on cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Santosh Pillai <[email protected]>
  • Loading branch information
sp98 committed Apr 18, 2022
1 parent aaa080a commit a809e90
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
39 changes: 38 additions & 1 deletion pkg/vgmanager/lvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const (
vgRemoveCmd = "/usr/sbin/vgremove"
pvRemoveCmd = "/usr/sbin/pvremove"
lvCreateCmd = "/usr/sbin/lvcreate"
lvRemoveCmd = "/usr/sbin/lvremove"
lvChangeCmd = "/usr/sbin/lvchange"
)

// vgsOutput represents the output of the `vgs --reportformat json` command
Expand Down Expand Up @@ -67,7 +69,7 @@ type lvsOutput struct {
VgName string `json:"vg_name"`
PoolName string `json:"pool_lv"`
LvAttr string `json:"lv_attr"`
} `json:"pv"`
} `json:"lv"`
} `json:"report"`
}

Expand Down Expand Up @@ -256,6 +258,41 @@ func GetLVSOutput(exec internal.Executor, vgName string) (*lvsOutput, error) {
return res, nil
}

// LVExists checks if a logical volume exists in a volume group
func LVExists(exec internal.Executor, lvName, vgName string) (bool, error) {
lvs, err := ListLogicalVolumes(exec, vgName)
if err != nil {
return false, err
}

for _, lv := range lvs {
if lv == lvName {
return true, nil
}
}

return false, nil
}

// DeleteLV deactivates the logical volume and deletes it
func DeleteLV(exec internal.Executor, lvName, vgName string) error {
lv := fmt.Sprintf("%s/%s", vgName, lvName)

// deactivate logical volume
_, err := exec.ExecuteCommandWithOutputAsHost(lvChangeCmd, "-an", lv)
if err != nil {
return fmt.Errorf("failed to deactivate thin pool %q in volume group %q. %v", lvName, vgName, err)
}

// delete logical volume
_, err = exec.ExecuteCommandWithOutputAsHost(lvRemoveCmd, lv)
if err != nil {
return fmt.Errorf("failed to delete logical volume %q in volume group %q. %v", lvName, vgName, err)
}

return nil
}

func execute(exec internal.Executor, v interface{}, args ...string) error {
output, err := exec.ExecuteCommandWithOutputAsHost(lvmCmd, args...)
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions pkg/vgmanager/vgmanager_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,25 @@ func (r *VGReconciler) processDelete(ctx context.Context, volumeGroup *lvmv1alph
return nil
}

// Delete thin pool
if volumeGroup.Spec.ThinPoolConfig != nil {
thinPoolName := volumeGroup.Spec.ThinPoolConfig.Name
lvExists, err := LVExists(r.executor, thinPoolName, volumeGroup.Name)
if err != nil {
return fmt.Errorf("failed to check existence of thin pool %q in volume group %q. %v", thinPoolName, volumeGroup.Name, err)
}

if lvExists {
err := DeleteLV(r.executor, thinPoolName, volumeGroup.Name)
if err != nil {
return fmt.Errorf("failed to delete thin pool %q in volume group %q. %v", thinPoolName, volumeGroup.Name, err)
}
r.Log.Info("thin pool deleted in the volume group.", "VGName", volumeGroup.Name, "ThinPool", thinPoolName)
} else {
r.Log.Info("thin pool not found in the volume group.", "VGName", volumeGroup.Name, "ThinPool", thinPoolName)
}
}

err = vg.Delete(r.executor)
if err != nil {
return fmt.Errorf("failed to delete volume group. %q, %v", volumeGroup.Name, err)
Expand Down

0 comments on commit a809e90

Please sign in to comment.