Skip to content

Commit

Permalink
feat(tests): add script for creating minikube cluster
Browse files Browse the repository at this point in the history
Signed-off-by: Niladri Halder <[email protected]>
  • Loading branch information
niladrih committed Sep 11, 2023
1 parent b9aaaf1 commit 032d02b
Showing 1 changed file with 175 additions and 0 deletions.
175 changes: 175 additions & 0 deletions tests/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
#!/usr/bin/env bash

#set -x

die() {
local _return="${2:-1}"
test "${_PRINT_HELP:-no}" = yes && print_help >&2
echo -e "ERROR: $1" >&2
exit "${_return}"
}

print_help() {
cat <<EOF
Usage: $(basename $0) [OPTIONS]
Options:
-h, --help Display this text.
--kubernetes-version <version> Specify the version of kubernetes.
-i, --install-prerequisites Install containerd before creating minikube cluster.
Examples:
$(basename $0) --kubernetes-version v1.27.2
EOF
}

parse_args() {
while test $# -gt 0; do
arg="$1"
case "$arg" in
--kubernetes-version)
test $# -lt 2 && die "missing value for the optional argument '$arg'."
KUBERNETES_VERSION="${2}"
shift
;;
--kubernetes-version=*)
KUBERNETES_VERSION="${arg#*=}"
;;
-i | --install-prerequisites)
INSTALL_PREREQUISITES="true"
;;
-h | --help)
print_help
exit 0
;;
-h*)
print_help
exit 0
;;
*)
_PRINT_HELP=yes die "unexpected argument '$arg'" 1
;;
esac
shift
done
}

must_succeed_command() {
local error="${2:-command \'${1}\' failed}"
${1} || die "${error}"
}

must_exists_in_path() {
local error="${2:-command ${1} not present in PATH}"
must_succeed_command "command -v ${1}" "${error}"
}

get_latest_version_tag() {
local github_org=${1}
local github_repo=${2}

local tag_name=$(curl -L -H "Accept: application/vnd.github+json" https://api.github.com/repos/${github_org}/${github_repo}/releases | jq '.[0].tag_name' | tr -d " \t\r\n")

return $tag_name
}

get_asset_url_from_latest_github_release() {
local github_org=${1}
local github_repo=${2}
local release_asset_regex=${3}

local url=$(curl -L -H "Accept: application/vnd.github+json" https://api.github.com/repos/${github_org}/${github_repo}/releases | jq ".[0].assets[] | select(.name? | match(\"${release_asset_regex}\")).url" | tr -d " \t\r\n")

return $url
}

get_asset_binary() {
local github_asset_url=${1}
local workspace=${2}
local output_filepath=${3}

cd ${workspace}
curl -L -H "Accept: application/octet-stream" ${github_asset_url} -o ${output_filepath}
cd -
}

install_prerequisites() {
# Check if LINUX.
local os_error="not a GNU/Linux environment"
os=$(must_succeed_command "uname -o" "${os_error}" | tr -d " \t\r\n")
if [ "${os}" != "GNU/Linux" ]; then
die "${os_error}"
fi

# Check if x86_64.
local arch_error="not a x86_64 GNU/Linux environment"
arch=$(must_succeed_command "uname -p" "${arch_error}" | tr -d " \t\r\n")
if [ "${arch}" != "x86_64" ]; then
die "${arch_error}"
fi

# Check if nix-shell prerequisites conntrack, minikube, crictl
# are present in PATH.
nix_shell_prerequisites=("conntrack" "minikube" "crictl" "curl" "jq" "awk" "systemctl")
for bin in "${nix_shell_prerequisites[@]}"; do
must_exists_in_path "${bin}" > /dev/null
done

# Directory to store downloaded files
workspace=$(must_succeed_command "mktemp -d --suffix=-mayastor-extensions" "failed to create temporary directory" | tr -d " \t\r\n")

# Install cri-dockerd, if not installed.
cri_dockerd_path=$(command -v cri-dockerd) || CRI_DOCKERD_ERROR=$?
if [ -n "${CRI_DOCKERD_ERROR}" ]; then
echo "Downloading latest version of cri-dockerd..."

url=$(get_asset_url_from_latest_github_release "Mirantis" "cri-dockerd" "^(cri-dockerd-[0-9]+.[0-9]+.[0-9]+.amd64.tgz)$")
get_asset_binary "${url}" "${workspace}" "cri-dockerd.tgz"
tar -xf ${workspace}/cri-dockerd.tgz -C ${workspace}
mkdir -p /usr/local/bin
install -o root -g root -m 0755 ${workspace}/cri-dockerd/cri-dockerd /usr/local/bin/cri-dockerd
command -v cri-dockerd > /dev/null || export PATH=$PATH:/usr/local/bin
command -v cri-dockerd || die "failed to install cri-dockerd" 1

echo "Downloaded latest version of cri-dockerd"
fi
# Check if cri-docker.socket systemd service is active. While this is extremely
# unlikely if cri-dockerd wasn't already installed, it is possible.
systemctl is-active --quiet cri-dockerd.socket || CRI_DOCKERD_VERSION="v$(cri-dockerd --version 2>&1 | awk '{print $2}')"
# Download systemd service files
systemd_service_files_url="https://raw.githubusercontent.com/Mirantis/cri-dockerd/${CRI_DOCKERD_VERSION}/packaging/systemd"
cd ${workspace}
curl -L ${systemd_service_files_url}/cri-docker.service -o cri-docker.service
curl -L ${systemd_service_files_url}/cri-docker.socket -o cri-docker.socket
cd -
mv -f ${workspace}/cri-docker.{service,socket} /etc/systemd/system
sed -i -e 's,/usr/bin/cri-dockerd,/usr/local/bin/cri-dockerd,' /etc/systemd/system/cri-docker.service
systemctl daemon-reload
systemctl enable --now cri-docker.socket
systemctl is-active --quiet cri-dockerd.socket || die "failed to set up cri-dockerd systemd service" 1
echo "Enabled cri-dockerd.socket"

# Install container-network-plugins, if not installed.
echo "Installing container-network-plugins"
containernetworking_tag=$(get_latest_version_tag "containernetworking" "plugins")
CNI_PLUGIN_VERSION="${containernetworking_tag}"
CNI_PLUGIN_TAR="cni-plugins-linux-amd64-$CNI_PLUGIN_VERSION.tgz" # change arch if not on amd64
CNI_PLUGIN_INSTALL_DIR="/opt/cni/bin"
cd ${workspace}
curl -LO "https://github.com/containernetworking/plugins/releases/download/$CNI_PLUGIN_VERSION/$CNI_PLUGIN_TAR"
mkdir -p "$CNI_PLUGIN_INSTALL_DIR"
tar -xf "$CNI_PLUGIN_TAR" -C "$CNI_PLUGIN_INSTALL_DIR"
cd -
echo "Installed container-network-plugins"
}

KUBERNETES_VERSION="v1.25.11"
INSTALL_PREREQUISITES="false"

parse_args "$@"

verify_prerequisites

test "${INSTALL_PREREQUISITES}" = "true" && install_prerequisites

minikube start --kubernetes-version=${KUBERNETES_VERSION} --cni=calico --driver=none --install-addons=false --force

0 comments on commit 032d02b

Please sign in to comment.