diff --git a/pkg/component/component.go b/pkg/component/component.go index 90d66764f69..6fd5eb62735 100644 --- a/pkg/component/component.go +++ b/pkg/component/component.go @@ -529,9 +529,25 @@ func ValidateComponentCreateRequest(client *occlient.Client, componentSettings c } } + if err := ensureAndLogProperResourceUsage(*componentSettings.MinMemory, *componentSettings.MaxMemory, "memory"); err != nil { + return err + } + + if err := ensureAndLogProperResourceUsage(*componentSettings.MinCPU, *componentSettings.MaxCPU, "cpu"); err != nil { + return err + } + return } +func ensureAndLogProperResourceUsage(resourceMin, resourceMax, resourceName string) error { + klog.V(4).Infof("Validating configured %v values", resourceName) + if (resourceMin == "") != (resourceMax == "") { + return fmt.Errorf("`min%s` should accompany `max%s` or use `odo config set %s` to use same value for both min and max or try not passing any of them\n", resourceName, resourceName, resourceName) + } + return nil +} + // ApplyConfig applies the component config onto component dc // Parameters: // client: occlient instance diff --git a/pkg/odo/cli/config/set.go b/pkg/odo/cli/config/set.go index b95deaaa81d..8a27db65be7 100644 --- a/pkg/odo/cli/config/set.go +++ b/pkg/odo/cli/config/set.go @@ -170,6 +170,9 @@ func isValidArgumentList(args []string) error { } switch param { + case "memory", "minmemory", "maxmemory", "cpu", "mincpu", "maxcpu": + err = validation.NonNegativeValidator(args[1]) + return errors.Errorf("%s is invalid %v", param, err) case "ports", "debugport": err = validation.PortsValidator(args[1]) } diff --git a/pkg/odo/util/validation/validators.go b/pkg/odo/util/validation/validators.go index 36f9bc9d71f..ec3dbf43bff 100644 --- a/pkg/odo/util/validation/validators.go +++ b/pkg/odo/util/validation/validators.go @@ -2,10 +2,11 @@ package validation import ( "fmt" - "github.com/openshift/odo/pkg/util" - "gopkg.in/AlecAivazis/survey.v1" "strconv" "strings" + + "github.com/openshift/odo/pkg/util" + "gopkg.in/AlecAivazis/survey.v1" ) // NameValidator provides a Validator view of the ValidateName function. @@ -40,6 +41,16 @@ func IntegerValidator(ans interface{}) error { return fmt.Errorf("don't know how to convert %v into an integer", ans) } +// NonNegativeValidator validates whether the given value is not negative or not +func NonNegativeValidator(arg interface{}) error { + if s, ok := arg.(string); ok { + if strings.HasPrefix(s, "-") { + return fmt.Errorf("negative value is not acceptable") + } + } + return nil +} + // PathValidator validates whether the given path exists on the file system func PathValidator(path interface{}) error { if s, ok := path.(string); ok {