From abee38cb4a107f9798d39b9434bb57374086a387 Mon Sep 17 00:00:00 2001 From: mattjmcnaughton Date: Sun, 24 Sep 2017 17:14:07 -0400 Subject: [PATCH] Dynamically determine default docker machine memory Currently, if using `docker-machine` for k8s docker operations on a Mac, we'll create a VM with 4096MB of memory. The machine's RAM will be the same regardless of the memory available on the local machine. For example, if the user has 16GB on their local machine, the VM will still only have 4GB of RAM. Update the method for defining the `kube-dev` VM with `docker-machine`, so we give it access to 50% of the total RAM. --- build/common.sh | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/build/common.sh b/build/common.sh index 98e53ab9a57..ff726498a4d 100755 --- a/build/common.sh +++ b/build/common.sh @@ -26,7 +26,7 @@ DOCKER_OPTS=${DOCKER_OPTS:-""} DOCKER=(docker ${DOCKER_OPTS}) DOCKER_HOST=${DOCKER_HOST:-""} DOCKER_MACHINE_NAME=${DOCKER_MACHINE_NAME:-"kube-dev"} -readonly DOCKER_MACHINE_DRIVER=${DOCKER_MACHINE_DRIVER:-"virtualbox --virtualbox-memory 4096 --virtualbox-cpu-count -1"} +readonly DOCKER_MACHINE_DRIVER=${DOCKER_MACHINE_DRIVER:-"virtualbox --virtualbox-cpu-count -1"} # This will canonicalize the path KUBE_ROOT=$(cd $(dirname "${BASH_SOURCE}")/.. && pwd -P) @@ -219,16 +219,28 @@ function kube::build::docker_available_on_osx() { function kube::build::prepare_docker_machine() { kube::log::status "docker-machine was found." + + local available_memory_bytes=$(sysctl -n hw.memsize 2>/dev/null) + + local bytes_in_mb=1048576 + + # Give virtualbox 1/2 the system memory. Its necessary to divide by 2, instead + # of multiple by .5, because bash can only multiply by ints. + local memory_divisor=2 + + local virtualbox_memory_mb=$(( ${available_memory_bytes} / (${bytes_in_mb} * ${memory_divisor}) )) + docker-machine inspect "${DOCKER_MACHINE_NAME}" &> /dev/null || { kube::log::status "Creating a machine to build Kubernetes" docker-machine create --driver ${DOCKER_MACHINE_DRIVER} \ + --virtualbox-memory "${virtualbox_memory_mb}" \ --engine-env HTTP_PROXY="${KUBERNETES_HTTP_PROXY:-}" \ --engine-env HTTPS_PROXY="${KUBERNETES_HTTPS_PROXY:-}" \ --engine-env NO_PROXY="${KUBERNETES_NO_PROXY:-127.0.0.1}" \ "${DOCKER_MACHINE_NAME}" > /dev/null || { kube::log::error "Something went wrong creating a machine." kube::log::error "Try the following: " - kube::log::error "docker-machine create -d ${DOCKER_MACHINE_DRIVER} ${DOCKER_MACHINE_NAME}" + kube::log::error "docker-machine create -d ${DOCKER_MACHINE_DRIVER} --virtualbox-memory ${virtualbox_memory_mb} ${DOCKER_MACHINE_NAME}" return 1 } }