diff --git a/cmd/ebs-bootstrap.go b/cmd/ebs-bootstrap.go index 21c4205..39ad3c0 100644 --- a/cmd/ebs-bootstrap.go +++ b/cmd/ebs-bootstrap.go @@ -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), @@ -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{ @@ -91,6 +84,7 @@ func main() { layer.NewChangeOwnerLayer(ub, fb), layer.NewChangePermissionsLayer(fb), } + checkError(le.Execute(layers)) log.Println("🟢 Passed all validation checks") } diff --git a/internal/config/validator.go b/internal/config/validator.go index 04d674d..7e9f70b 100644 --- a/internal/config/validator.go +++ b/internal/config/validator.go @@ -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 +}