Skip to content

Commit

Permalink
add memory and cpu validation for 'config set' and 'push' commands
Browse files Browse the repository at this point in the history
  • Loading branch information
devang-gaur committed Jul 15, 2020
1 parent d714c6c commit 1a9b183
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
16 changes: 16 additions & 0 deletions pkg/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions pkg/odo/cli/config/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
Expand Down
15 changes: 13 additions & 2 deletions pkg/odo/util/validation/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 1a9b183

Please sign in to comment.