Skip to content

Commit

Permalink
(feat): Shift dependencies between package.model and package.datastru…
Browse files Browse the repository at this point in the history
…cutres
  • Loading branch information
lasith-kg committed May 23, 2024
1 parent 24592ed commit 1eab515
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 58 deletions.
18 changes: 9 additions & 9 deletions internal/backend/lvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (lb *LinuxLvmBackend) GetVolumeGroups(name string) []*model.VolumeGroup {
if err != nil {
return vgs
}
pvn := lb.lvmGraph.GetParents(node, datastructures.PhysicalVolume)
pvn := lb.lvmGraph.GetParents(node, model.PhysicalVolumeKind)
for _, pv := range pvn {
vgs = append(vgs, &model.VolumeGroup{
Name: node.Name,
Expand All @@ -59,14 +59,14 @@ func (lb *LinuxLvmBackend) GetLogicalVolume(name string, volumeGroup string) (*m
if err != nil {
return nil, err
}
vgs := lb.lvmGraph.GetParents(node, datastructures.VolumeGroup)
vgs := lb.lvmGraph.GetParents(node, model.VolumeGroupKind)
if len(vgs) == 0 {
return nil, fmt.Errorf("🔴 %s: Logical volume has no volume group", node.Name)
}
return &model.LogicalVolume{
Name: node.Name,
VolumeGroup: vgs[0].Name,
State: model.LogicalVolumeState(node.State),
State: node.State,
}, nil
}

Expand All @@ -76,12 +76,12 @@ func (lb *LinuxLvmBackend) SearchLogicalVolumes(volumeGroup string) ([]*model.Lo
if err != nil {
return nil, err
}
lvn := lb.lvmGraph.GetChildren(node, datastructures.LogicalVolume)
lvn := lb.lvmGraph.GetChildren(node, model.LogicalVolumeKind)
for _, lv := range lvn {
lvs = append(lvs, &model.LogicalVolume{
Name: lv.Name,
VolumeGroup: node.Name,
State: model.LogicalVolumeState(lv.State),
State: lv.State,
Size: lv.Size,
})
}
Expand All @@ -93,7 +93,7 @@ func (lb *LinuxLvmBackend) SearchVolumeGroup(physicalVolume string) (*model.Volu
if err != nil {
return nil, err
}
vgn := lb.lvmGraph.GetChildren(node, datastructures.VolumeGroup)
vgn := lb.lvmGraph.GetChildren(node, model.VolumeGroupKind)
if len(vgn) == 0 {
return nil, fmt.Errorf("🔴 %s: Physical volume has no volume group", physicalVolume)
}
Expand Down Expand Up @@ -125,7 +125,7 @@ func (lb *LinuxLvmBackend) ShouldResizePhysicalVolume(name string, threshold flo
if err != nil {
return false, nil
}
dvn := lb.lvmGraph.GetParents(node, datastructures.Device)
dvn := lb.lvmGraph.GetParents(node, model.DeviceKind)
if len(dvn) == 0 {
return false, nil
}
Expand All @@ -143,7 +143,7 @@ func (lb *LinuxLvmBackend) ShouldResizeLogicalVolume(name string, volumeGroup st
if err != nil {
return false, err
}
vgn := lb.lvmGraph.GetParents(node, datastructures.VolumeGroup)
vgn := lb.lvmGraph.GetParents(node, model.VolumeGroupKind)
if len(vgn) == 0 {
return false, fmt.Errorf("🔴 %s: Logical volume has no volume group", name)
}
Expand Down Expand Up @@ -200,7 +200,7 @@ func (db *LinuxLvmBackend) From(config *config.Config) error {
return err
}
for _, lv := range lvs {
err := lvmGraph.AddLogicalVolume(lv.Name, lv.VolumeGroup, datastructures.LvmNodeState(lv.State), lv.Size)
err := lvmGraph.AddLogicalVolume(lv.Name, lv.VolumeGroup, lv.State, lv.Size)
if err != nil {
return err
}
Expand Down
48 changes: 15 additions & 33 deletions internal/datastructures/lvm_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,14 @@ package datastructures

import (
"fmt"
)

type LvmNodeState int32
type LvmNodeCategory int32

const (
DeviceActive LvmNodeState = 0b0000001
PhysicalVolumeActive LvmNodeState = 0b0000010
VolumeGroupInactive LvmNodeState = 0b0000100
VolumeGroupActive LvmNodeState = 0b0001100
LogicalVolumeInactive LvmNodeState = 0b0010000
LogicalVolumeActive LvmNodeState = 0b0110000
LogicalVolumeUnsupported LvmNodeState = 0b1110000
)

const (
Device LvmNodeCategory = 0b0000001
PhysicalVolume LvmNodeCategory = 0b0000010
VolumeGroup LvmNodeCategory = 0b0000100
LogicalVolume LvmNodeCategory = 0b0010000
"github.com/reecetech/ebs-bootstrap/internal/model"
)

type LvmNode struct {
id string
Name string
State LvmNodeState
State model.LvmState
Size uint64
children []*LvmNode
parents []*LvmNode
Expand All @@ -37,7 +19,7 @@ func NewDevice(name string, size uint64) *LvmNode {
return &LvmNode{
id: fmt.Sprintf("device:%s", name),
Name: name,
State: DeviceActive,
State: model.DeviceActive,
Size: size,
children: []*LvmNode{},
parents: []*LvmNode{},
Expand All @@ -48,7 +30,7 @@ func NewPhysicalVolume(name string, size uint64) *LvmNode {
return &LvmNode{
id: fmt.Sprintf("pv:%s", name),
Name: name,
State: PhysicalVolumeActive,
State: model.PhysicalVolumeActive,
Size: size,
children: []*LvmNode{},
parents: []*LvmNode{},
Expand All @@ -59,14 +41,14 @@ func NewVolumeGroup(name string, size uint64) *LvmNode {
return &LvmNode{
id: fmt.Sprintf("vg:%s", name),
Name: name,
State: VolumeGroupInactive,
State: model.VolumeGroupInactive,
Size: size,
children: []*LvmNode{},
parents: []*LvmNode{},
}
}

func NewLogicalVolume(name string, vg string, State LvmNodeState, size uint64) *LvmNode {
func NewLogicalVolume(name string, vg string, State model.LvmState, size uint64) *LvmNode {
return &LvmNode{
id: fmt.Sprintf("lv:%s:vg:%s", name, vg),
Name: name,
Expand Down Expand Up @@ -143,7 +125,7 @@ func (lg *LvmGraph) AddVolumeGroup(name string, pv string, size uint64) error {
return nil
}

func (lg *LvmGraph) AddLogicalVolume(name string, vg string, state LvmNodeState, size uint64) error {
func (lg *LvmGraph) AddLogicalVolume(name string, vg string, state model.LvmState, size uint64) error {
lv := NewLogicalVolume(name, vg, state, size)

_, found := lg.nodes[lv.id]
Expand All @@ -164,8 +146,8 @@ func (lg *LvmGraph) AddLogicalVolume(name string, vg string, state LvmNodeState,
// If at least one of the logical volumes are active, the
// volume group is considered active
for _, lvn := range vgn.children {
if lvn.State == LogicalVolumeActive {
vgn.State = VolumeGroupActive
if lvn.State == model.LogicalVolumeActive {
vgn.State = model.VolumeGroupActive
break
}
}
Expand Down Expand Up @@ -199,22 +181,22 @@ func (lg *LvmGraph) GetLogicalVolume(name string, vg string) (*LvmNode, error) {
return node, nil
}

func (lg *LvmGraph) GetParents(node *LvmNode, state LvmNodeCategory) []*LvmNode {
func (lg *LvmGraph) GetParents(node *LvmNode, kind model.LvmKind) []*LvmNode {
parents := []*LvmNode{}
for _, p := range node.parents {
// Bitmasking to check if the parent nodes is of the desired category
if int32(p.State)&int32(state) > 0 {
// Bitmasking to check if the parent nodes is of the desired kind
if int32(p.State)&int32(kind) > 0 {
parents = append(parents, p)
}
}
return parents
}

func (lg *LvmGraph) GetChildren(node *LvmNode, state LvmNodeCategory) []*LvmNode {
func (lg *LvmGraph) GetChildren(node *LvmNode, kind model.LvmKind) []*LvmNode {
children := []*LvmNode{}
for _, c := range node.children {
// Bitmasking to check if children nodes is of the desired category
if int32(c.State)&int32(state) > 0 {
// Bitmasking to check if children nodes is of the desired kind
if int32(c.State)&int32(kind) > 0 {
children = append(children, c)
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/layer/lv_activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func (cvgl *ActivateLogicalVolumeLayer) Modify(c *config.Config) ([]action.Actio
return nil, err
}

if lv.State == model.Active {
if lv.State == model.LogicalVolumeActive {
continue
}

if lv.State == model.Unsupported {
if lv.State == model.LogicalVolumeUnsupported {
return nil, fmt.Errorf("🔴 %s: Can not activate a logical volume in an unsupported state", lv.Name)
}

Expand Down
31 changes: 21 additions & 10 deletions internal/model/lvm.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
package model

import "github.com/reecetech/ebs-bootstrap/internal/datastructures"
type LvmState int32
type LvmKind int32

const (
DeviceActive LvmState = 0b0000001
PhysicalVolumeActive LvmState = 0b0000010
VolumeGroupInactive LvmState = 0b0000100
VolumeGroupActive LvmState = 0b0001100
LogicalVolumeInactive LvmState = 0b0010000
LogicalVolumeActive LvmState = 0b0110000
LogicalVolumeUnsupported LvmState = 0b1110000
)

const (
DeviceKind LvmKind = 0b0000001
PhysicalVolumeKind LvmKind = 0b0000010
VolumeGroupKind LvmKind = 0b0000100
LogicalVolumeKind LvmKind = 0b0010000
)

type Device struct {
Name string
Expand All @@ -16,19 +34,12 @@ type VolumeGroup struct {
Name string
PhysicalVolume string
Size uint64
State LvmState
}

type LogicalVolumeState int32

const (
Inactive LogicalVolumeState = LogicalVolumeState(datastructures.LogicalVolumeInactive)
Active LogicalVolumeState = LogicalVolumeState(datastructures.LogicalVolumeActive)
Unsupported LogicalVolumeState = LogicalVolumeState(datastructures.LogicalVolumeUnsupported)
)

type LogicalVolume struct {
Name string
VolumeGroup string
State LogicalVolumeState
State LvmState
Size uint64
}
13 changes: 9 additions & 4 deletions internal/service/lvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func (ls *LinuxLvmService) GetVolumeGroups() ([]*model.VolumeGroup, error) {
vgs[i] = &model.VolumeGroup{
Name: vg.Name,
PhysicalVolume: vg.PhysicalVolume,
State: model.VolumeGroupInactive,
Size: size,
}
}
Expand All @@ -152,19 +153,23 @@ func (ls *LinuxLvmService) GetLogicalVolumes() ([]*model.LogicalVolume, error) {
}
lvs := make([]*model.LogicalVolume, len(lr.Report[0].LogicalVolume))
for i, lv := range lr.Report[0].LogicalVolume {
var state model.LogicalVolumeState
// Get Logical Volume State
var state model.LvmState
switch lv.Attributes[4] {
case 'a':
state = model.Active
state = model.LogicalVolumeActive
case '-':
state = model.Inactive
state = model.LogicalVolumeInactive
default:
state = model.Unsupported
state = model.LogicalVolumeUnsupported
}

// Get Logical Volume Size
size, err := strconv.ParseUint(lv.Size, 10, 64)
if err != nil {
return nil, fmt.Errorf("🔴 Failed to cast logical volume size to unsigned 64-bit integer")
}

lvs[i] = &model.LogicalVolume{
Name: lv.Name,
VolumeGroup: lv.VolumeGroup,
Expand Down

0 comments on commit 1eab515

Please sign in to comment.