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 c5daf8c commit 4e778e6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
41 changes: 39 additions & 2 deletions 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 @@ -125,7 +127,7 @@ func (vg VolumeGroup) Extend(exec internal.Executor, pvs []string) error {
// Delete deletes a volume group and the physical volumes associated with it
func (vg VolumeGroup) Delete(exec internal.Executor) error {
// Remove Volume Group
vgArgs := []string{vg.Name}
vgArgs := []string{vg.Name, "--yes"}
_, err := exec.ExecuteCommandWithOutputAsHost(vgRemoveCmd, vgArgs...)
if err != nil {
return fmt.Errorf("failed to remove volume group %q. %v", vg.Name, err)
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 @@ -294,6 +294,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)
}
}

// Check if volume group exists
vg, err := GetVolumeGroup(r.executor, volumeGroup.Name)
if err != nil {
Expand Down

0 comments on commit 4e778e6

Please sign in to comment.