Merge pull request #4595 from blueelvis/memory-size-sanity-check

Added validation for start memory size
pull/4649/head
Medya Ghazizadeh 2019-06-30 15:17:28 -07:00 committed by GitHub
commit 449c8781e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 21 deletions

View File

@ -119,7 +119,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)")
@ -380,9 +380,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 {
@ -397,6 +397,14 @@ func validateConfig() {
glog.Errorf("Error autoSetOptions : %v", err)
}
memorySizeMB := pkgutil.CalculateSizeInMB(viper.GetString(memory))
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))
}
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))
}
// check that kubeadm extra args contain only whitelisted parameters
for param := range extraOptions.AsMap().Get(kubeadm.Kubeadm) {
if !pkgutil.ContainsString(kubeadm.KubeadmExtraArgsWhitelist[kubeadm.KubeadmCmdParam], param) &&
@ -510,9 +518,9 @@ func generateConfig(cmd *cobra.Command, k8sVersion string) (cfg.Config, error) {
MachineConfig: cfg.MachineConfig{
KeepContext: viper.GetBool(keepContext),
MinikubeISO: viper.GetString(isoURL),
Memory: viper.GetInt(memory),
Memory: pkgutil.CalculateSizeInMB(viper.GetString(memory)),
CPUs: viper.GetInt(cpus),
DiskSize: pkgutil.CalculateDiskSizeInMB(viper.GetString(humanReadableDiskSize)),
DiskSize: pkgutil.CalculateSizeInMB(viper.GetString(humanReadableDiskSize)),
VMDriver: viper.GetString(vmDriver),
ContainerRuntime: viper.GetString(containerRuntime),
HyperkitVpnKitSock: viper.GetString(vpnkitSock),

View File

@ -77,7 +77,7 @@ func NewDriver(hostName, storePath string) *Driver {
SSHUser: "docker",
},
CommonDriver: &pkgdrivers.CommonDriver{},
DiskSize: commonutil.CalculateDiskSizeInMB(constants.DefaultDiskSize),
DiskSize: commonutil.CalculateSizeInMB(constants.DefaultDiskSize),
}
}

View File

@ -104,8 +104,8 @@ func NewDriver(hostName, storePath string) *Driver {
CommonDriver: &pkgdrivers.CommonDriver{},
Boot2DockerURL: constants.DefaultISOURL,
CPU: constants.DefaultCPUS,
DiskSize: util.CalculateDiskSizeInMB(constants.DefaultDiskSize),
Memory: constants.DefaultMemory,
DiskSize: util.CalculateSizeInMB(constants.DefaultDiskSize),
Memory: util.CalculateSizeInMB(constants.DefaultMemorySize),
PrivateNetwork: defaultPrivateNetworkName,
Network: defaultNetworkName,
DiskPath: filepath.Join(constants.GetMinipath(), "machines", config.GetMachineName(), fmt.Sprintf("%s.rawdisk", config.GetMachineName())),

View File

@ -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

View File

@ -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
@ -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)
if err != nil {
glog.Errorf("Invalid disk size: %v", err)
// 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 += "mb"
}
return int(diskSize / units.MB)
size, err := units.FromHumanSize(humanReadableSize)
if err != nil {
exit.WithCode(exit.Config, "Invalid size passed in argument: %v", err)
}
return int(size / units.MB)
}
// Until endlessly loops the provided function until a message is received on the done channel.