Skip to content

Commit

Permalink
feat: extend the thinpool
Browse files Browse the repository at this point in the history
Signed-off-by: Nitin Goyal <nigoyal@redhat.com>
  • Loading branch information
iamniting committed Aug 26, 2022
1 parent e6fdc59 commit 500639b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
13 changes: 10 additions & 3 deletions pkg/vgmanager/lvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
vgRemoveCmd = "/usr/sbin/vgremove"
pvRemoveCmd = "/usr/sbin/pvremove"
lvCreateCmd = "/usr/sbin/lvcreate"
lvExtendCmd = "/usr/sbin/lvextend"
lvRemoveCmd = "/usr/sbin/lvremove"
lvChangeCmd = "/usr/sbin/lvchange"
)
Expand All @@ -46,7 +47,8 @@ const (
type vgsOutput struct {
Report []struct {
Vg []struct {
Name string `json:"vg_name"`
Name string `json:"vg_name"`
VgSize string `json:"vg_size"`
} `json:"vg"`
} `json:"report"`
}
Expand All @@ -69,6 +71,7 @@ type lvsOutput struct {
VgName string `json:"vg_name"`
PoolName string `json:"pool_lv"`
LvAttr string `json:"lv_attr"`
LvSize string `json:"lv_size"`
} `json:"lv"`
} `json:"report"`
}
Expand All @@ -78,6 +81,9 @@ type VolumeGroup struct {
// Name is the name of the volume group
Name string `json:"vg_name"`

// VgSize is the size of the volume group
VgSize string `json:"vg_size"`

// PVs is the list of physical volumes associated with the volume group
PVs []string `json:"pvs"`
}
Expand Down Expand Up @@ -148,7 +154,7 @@ func GetVolumeGroup(exec internal.Executor, name string) (*VolumeGroup, error) {

// TODO: Check if `vgs <vg_name> --reportformat json` can be used to filter out the exact volume group name.
args := []string{
"vgs", "--reportformat", "json",
"vgs", "--units", "g", "--reportformat", "json",
}
if err := execute(exec, res, args...); err != nil {
return nil, fmt.Errorf("failed to list volume groups. %v", err)
Expand All @@ -160,6 +166,7 @@ func GetVolumeGroup(exec internal.Executor, name string) (*VolumeGroup, error) {
for _, vg := range report.Vg {
if vg.Name == name {
volumeGroup.Name = vg.Name
volumeGroup.VgSize = vg.VgSize
vgFound = true
break
}
Expand Down Expand Up @@ -249,7 +256,7 @@ func ListLogicalVolumes(exec internal.Executor, vgName string) ([]string, error)
func GetLVSOutput(exec internal.Executor, vgName string) (*lvsOutput, error) {
res := new(lvsOutput)
args := []string{
"lvs", "-S", fmt.Sprintf("vgname=%s", vgName), "--reportformat", "json",
"lvs", "-S", fmt.Sprintf("vgname=%s", vgName), "--units", "g", "--reportformat", "json",
}
if err := execute(exec, res, args...); err != nil {
return nil, err
Expand Down
41 changes: 41 additions & 0 deletions pkg/vgmanager/vgmanager_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -251,6 +252,7 @@ func (r *VGReconciler) addThinPoolToVG(vgName string, config *lvmv1alpha1.ThinPo
if lv.Name == config.Name {
if strings.Contains(lv.LvAttr, "t") {
r.Log.Info("lvm thinpool already exists", "VGName", vgName, "ThinPool", config.Name)
r.extendThinPool(vgName, lv.LvSize, config)
return nil
}

Expand All @@ -269,6 +271,45 @@ func (r *VGReconciler) addThinPoolToVG(vgName string, config *lvmv1alpha1.ThinPo
return nil
}

func (r *VGReconciler) extendThinPool(vgName string, lvSize string, config *lvmv1alpha1.ThinPoolConfig) error {

vg, err := GetVolumeGroup(r.executor, vgName)
if err != nil {
if err != ErrVolumeGroupNotFound {
return fmt.Errorf("failed to get volume group. %q, %v", vgName, err)
}
return nil
}

thinPoolSize, err := strconv.ParseFloat(lvSize[:len(lvSize)-1], 64)
if err != nil {
return fmt.Errorf("failed to parse lvSize. %v", err)
}

vgSize, err := strconv.ParseFloat(vg.VgSize[:len(vg.VgSize)-1], 64)
if err != nil {
return fmt.Errorf("failed to parse vgSize. %v", err)
}

// return if thinPoolSize is as expected
if config.SizePercent == int((thinPoolSize/vgSize)*100) {
return nil
}

r.Log.Info("extending lvm thinpool ", "VGName", vgName, "ThinPool", config.Name)

args := []string{"-l", fmt.Sprintf("%d%%Vg", config.SizePercent), fmt.Sprintf("%s/%s", vgName, config.Name)}

_, err = r.executor.ExecuteCommandWithOutputAsHost(lvExtendCmd, args...)
if err != nil {
return fmt.Errorf("failed to extend thin pool %q in the volume group %q. %v", config.Name, vgName, err)
}

r.Log.Info("successfully extended the thin pool in the volume group ", "thinpool", config.Name, "vgName", vgName)

return nil
}

func (r *VGReconciler) processDelete(ctx context.Context, volumeGroup *lvmv1alpha1.LVMVolumeGroup) error {

// Read the lvmd config file
Expand Down

0 comments on commit 500639b

Please sign in to comment.