Skip to content

Commit

Permalink
Added memory size sanity check
Browse files Browse the repository at this point in the history
Fixed the logic to check parseable size
  • Loading branch information
blueelvis committed Jun 25, 2019
1 parent 4c06cc6 commit 7cf7b54
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
16 changes: 12 additions & 4 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func init() {
startCmd.Flags().Bool(disableDriverMounts, false, "Disables the filesystem mounts provided by the hypervisors (vboxfs, xhyve-9p)")
startCmd.Flags().String(isoURL, constants.DefaultISOURL, "Location of the minikube iso")
startCmd.Flags().String(vmDriver, constants.DefaultVMDriver, fmt.Sprintf("VM driver is one of: %v", constants.SupportedVMDrivers))
startCmd.Flags().Int(memory, constants.DefaultMemory, "Amount of RAM allocated to the minikube VM in MB")
startCmd.Flags().String(memory, constants.DefaultMemorySize, "Amount of RAM allocated to the minikube VM (format: <number>[<unit>], where unit = b, k, m or g)")
startCmd.Flags().Int(cpus, constants.DefaultCPUS, "Number of CPUs allocated to the minikube VM")
startCmd.Flags().String(humanReadableDiskSize, constants.DefaultDiskSize, "Disk size allocated to the minikube VM (format: <number>[<unit>], where unit = b, k, m or g)")
startCmd.Flags().String(hostOnlyCIDR, "192.168.99.1/24", "The CIDR to be used for the minikube VM (only supported with Virtualbox driver)")
Expand Down Expand Up @@ -373,9 +373,9 @@ func validateUser() {

// validateConfig validates the supplied configuration against known bad combinations
func validateConfig() {
diskSizeMB := pkgutil.CalculateDiskSizeInMB(viper.GetString(humanReadableDiskSize))
if diskSizeMB < constants.MinimumDiskSizeMB {
exit.WithCode(exit.Config, "Requested disk size (%dMB) is less than minimum of %dMB", diskSizeMB, constants.MinimumDiskSizeMB)
diskSizeMB := pkgutil.CalculateSizeInMB(viper.GetString(humanReadableDiskSize))
if diskSizeMB < pkgutil.CalculateSizeInMB(constants.MinimumDiskSize) {
exit.WithCode(exit.Config, "Requested disk size (%dMB) is less than minimum of (%dMB)", diskSizeMB, pkgutil.CalculateSizeInMB(constants.MinimumDiskSize))
}

if viper.GetBool(gpu) && viper.GetString(vmDriver) != constants.DriverKvm2 {
Expand All @@ -389,6 +389,14 @@ func validateConfig() {
if err != nil {
glog.Errorf("Error autoSetOptions : %v", err)
}

memorySizeMB := pkgutil.CalculateSizeInMB(viper.GetString(memory))
if memorySizeMB < pkgutil.CalculateSizeInMB(constants.DefaultMemorySize) {
console.OutStyle(console.Notice, "Requested memory allocation (%dMB) is less than the default memory allocation of (%dMB). Beware that Minikube might not work correctly or crash unexpectedly.", memorySizeMB, pkgutil.CalculateSizeInMB(constants.DefaultMemorySize))
}
if memorySizeMB < pkgutil.CalculateSizeInMB(constants.MinimumMemorySize) {
exit.Usage("Requested memory allocation (%dMB) is less than the minimum allowed of %dMB", memorySizeMB, pkgutil.CalculateSizeInMB(constants.MinimumMemorySize))
}

// check that kubeadm extra args contain only whitelisted parameters
for param := range extraOptions.AsMap().Get(kubeadm.Kubeadm) {
Expand Down
14 changes: 8 additions & 6 deletions pkg/minikube/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,16 @@ const (
DefaultKeepContext = false
// SHASuffix is the suffix of a SHA-256 checksum file
SHASuffix = ".sha256"
// DefaultMemory is the default memory of a host, in megabytes
DefaultMemory = 2048
// DefaultMemorySize is the default memory which will be allocated to minikube, in megabytes
DefaultMemorySize = "2000mb"
//MinimumMemorySize is the minimum memory size, in megabytes
MinimumMemorySize = "1024mb"
// DefaultCPUS is the default number of cpus of a host
DefaultCPUS = 2
// DefaultDiskSize is the default disk image size, parseable
DefaultDiskSize = "20g"
// MinimumDiskSizeMB is the minimum disk image size, in megabytes
MinimumDiskSizeMB = 2000
// DefaultDiskSize is the default disk image size, in megabytes
DefaultDiskSize = "20000mb"
// MinimumDiskSize is the minimum disk image size, in megabytes
MinimumDiskSize = "2000mb"
// DefaultVMDriver is the default virtual machine driver name
DefaultVMDriver = DriverVirtualbox
// DefaultStatusFormat is the default format of a host
Expand Down
16 changes: 11 additions & 5 deletions pkg/util/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/golang/glog"
retryablehttp "github.com/hashicorp/go-retryablehttp"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/exit"
)

// ErrPrefix notes an error
Expand All @@ -53,13 +54,18 @@ type RetriableError struct {

func (r RetriableError) Error() string { return "Temporary Error: " + r.Err.Error() }

// CalculateDiskSizeInMB returns the number of MB in the human readable string
func CalculateDiskSizeInMB(humanReadableDiskSize string) int {
diskSize, err := units.FromHumanSize(humanReadableDiskSize)
// CalculateSizeInMB returns the number of MB in the human readable string
func CalculateSizeInMB(humanReadableSize string) int {
_, err := strconv.ParseInt(humanReadableSize, 10, 64)
if err == nil {
humanReadableSize = humanReadableSize + "mb"
}
size, err := units.FromHumanSize(humanReadableSize)
if err != nil {
glog.Errorf("Invalid disk size: %v", err)
exit.WithCode(exit.Config, "Invalid size passed in argument: %v", err)
}
return int(diskSize / units.MB)

return int(size / units.MB)
}

// Until endlessly loops the provided function until a message is received on the done channel.
Expand Down

0 comments on commit 7cf7b54

Please sign in to comment.