stx tool: Support to allocate cpu and memory resources for minikube

Add the support to allow to define the cpu and memory resources
when we start the minikube.

Story: 2008862
Task: 44079

Signed-off-by: Zhixiong Chi <zhixiong.chi@windriver.com>
Change-Id: I4ca4f7b5bb6d6eb59ceafbb8114b2fd4a856cb0d
This commit is contained in:
Zhixiong Chi 2021-11-25 12:48:16 +08:00
parent ca527ef97a
commit 13350756b5
2 changed files with 57 additions and 0 deletions

View File

@ -19,8 +19,19 @@ if [ x"$FSTYPE" == x"nfs" ]; then
return 1
fi
notice_warn() {
local tty_on tty_off
if [[ -t 2 ]] ; then
tty_on=$'\033[1;33m'
tty_off=$'\033[0m'
fi
echo >&2 "${tty_on}$*${tty_off}"
}
export PRJDIR=$(pwd)
export PATH=$PRJDIR/stx/bin:$PATH
export MINIKUBECPUS=${MINIKUBECPUS:-2}
export MINIKUBEMEMORY=${MINIKUBEMEMORY:-16000}
export MINIKUBENAME=${MINIKUBENAME:-minikube-$USER-upstream}
export KUBECONFIG=$MINIKUBE_HOME/.kube/config
export STX_BUILD_HOME="${STX_BUILD_HOME:-/localdisk/$USER}"
@ -28,3 +39,47 @@ export STX_BUILD_HOME="${STX_BUILD_HOME:-/localdisk/$USER}"
if [ ! -f "stx.conf" ]; then
cp stx.conf.sample stx.conf
fi
number_of_users () {
users | tr ' ' '\n' | sort --uniq | wc -l
}
number_of_cpus () {
/usr/bin/nproc
}
sqrt () {
echo -e "sqrt($1)" | bc -q -i | head -2 | tail -1
}
# Consider many users are just working with code and not actually building.
NUM_USERS=$(sqrt $(number_of_users))
ABSOLUTE_MAX_CPUS=$(($(number_of_cpus)/$NUM_USERS))
MAX_CPUS=$(number_of_cpus)
if [ "$MAX_CPUS" == "" ] || [ "$MAX_CPUS" == "0" ]; then
MAX_CPUS=1
fi
if [ $MAX_CPUS -gt $ABSOLUTE_MAX_CPUS ]; then
MAX_CPUS=$ABSOLUTE_MAX_CPUS
fi
if [ $MINIKUBECPUS -gt $MAX_CPUS ]; then
notice_warn "MINIKUBECPUS setting:$MINIKUBECPUS is more than MAX_CPUS: $MAX_CPUS."
notice_warn "Limit the minikube cluster with MAX_CPUS."
export MINIKUBECPUS=$MAX_CPUS
fi
MAX_MEMORY=`expr $(cat /proc/meminfo |grep MemTotal | awk '{print $2}') / 1024`
if [ "$MAX_MEMORY" == "" ] || [ "$MAX_MEMORY" == "0" ]; then
MAX_MEMORY=2048
fi
if [ $MINIKUBEMEMORY -gt $MAX_MEMORY ]; then
notice_warn "MINIKUBEMEMORY setting:$MINIKUBEMEMORY is more than system MAX_MEMORY: $MAX_MEMORY M."
notice_warn "Limit the minikube cluster with MAX_MEMORY."
export MINIKUBEMEMORY=$MAX_MEMORY
fi

View File

@ -176,6 +176,8 @@ fi
if [[ $WANT_START_MINIKUBE -eq 1 ]] ; then
notice "Starting minikube cluster \`$MINIKUBENAME'"
$MINIKUBE start --driver=docker -p $MINIKUBENAME \
--cpus=$MINIKUBECPUS \
--memory=$MINIKUBEMEMORY \
--mount=true \
--mount-string="$STX_BUILD_HOME:/workspace" \
|| exit 1