Skip to content

Commit

Permalink
(feat): Add LvmConsumptionValidator()
Browse files Browse the repository at this point in the history
  • Loading branch information
lasith-kg committed May 20, 2024
1 parent 9bda81e commit 519771a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
42 changes: 18 additions & 24 deletions cmd/ebs-bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,23 @@ func main() {
dae := action.NewDefaultActionExecutor()
le := layer.NewExponentialBackoffLayerExecutor(c, dae, layer.DefaultExponentialBackoffParameters())

// Modify Config
modifiers := []config.Modifier{
config.NewAwsNVMeDriverModifier(ans, lds),
// Validate Config
validators := []config.Validator{
config.NewFileSystemValidator(),
config.NewModeValidator(),
config.NewResizeThresholdValidator(),
config.NewMountPointValidator(),
config.NewMountOptionsValidator(),
config.NewOwnerValidator(uos),
config.NewLvmConsumptionValidator(),
}
for _, m := range modifiers {
checkError(m.Modify(c))
for _, v := range validators {
checkError(v.Validate(c))
}

// NVMe Device Modifier
checkError(config.NewAwsNVMeDriverModifier(ans, lds).Modify(c))

// LVM Layers
lvmLayers := []layer.Layer{
layer.NewCreatePhysicalVolumeLayer(db, lb),
Expand All @@ -60,26 +69,10 @@ func main() {
checkError(le.Execute(lvmLayers))

// LVM Modifiers
lvmModifiers := []config.Modifier{
config.NewLvmModifier(),
}
for _, m := range lvmModifiers {
checkError(m.Modify(c))
}
checkError(config.NewLvmModifier().Modify(c))

// Validate Config
validators := []config.Validator{
config.NewFileSystemValidator(),
config.NewModeValidator(),
config.NewResizeThresholdValidator(),
config.NewDeviceValidator(lds),
config.NewMountPointValidator(),
config.NewMountOptionsValidator(),
config.NewOwnerValidator(uos),
}
for _, v := range validators {
checkError(v.Validate(c))
}
// Device Validator
checkError(config.NewDeviceValidator(lds).Validate(c))

// Layers
layers := []layer.Layer{
Expand All @@ -91,6 +84,7 @@ func main() {
layer.NewChangeOwnerLayer(ub, fb),
layer.NewChangePermissionsLayer(fb),
}

checkError(le.Execute(layers))
log.Println("🟢 Passed all validation checks")
}
Expand Down
25 changes: 25 additions & 0 deletions internal/config/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,28 @@ func (rtv *ResizeThresholdValidator) Validate(c *Config) error {
func (rtv *ResizeThresholdValidator) isValid(rt float64) bool {
return rt >= 0 && rt <= 100
}

type LvmConsumptionValidator struct{}

func NewLvmConsumptionValidator() *LvmConsumptionValidator {
return &LvmConsumptionValidator{}
}

func (lcv *LvmConsumptionValidator) Validate(c *Config) error {
if !lcv.isValid(c.Defaults.LvmConsumption) {
return fmt.Errorf("🔴 '%d' (default) must be an integer between 0 and 100 (inclusive)", c.Defaults.LvmConsumption)
}
if !lcv.isValid(c.overrides.LvmConsumption) {
return fmt.Errorf("🔴 '%d' (-lvm-consumption) must be an integer between 0 and 100 (inclusive)", c.overrides.LvmConsumption)
}
for name, device := range c.Devices {
if !lcv.isValid(device.LvmConsumption) {
return fmt.Errorf("🔴 %s: '%d' must be an integer between 0 and 100 (inclusive)", name, device.LvmConsumption)
}
}
return nil
}

func (lcv *LvmConsumptionValidator) isValid(lc int) bool {
return lc >= 0 && lc <= 100
}

0 comments on commit 519771a

Please sign in to comment.