Merge pull request #570 from lfbear/pr_macos4bootstrap

Adapt karmada-bootstrap.sh for macOS
This commit is contained in:
karmada-bot 2021-08-10 16:04:14 +08:00 committed by GitHub
commit 8502a170e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 107 additions and 43 deletions

View File

@ -3,7 +3,8 @@ set -o errexit
set -o nounset
set -o pipefail
# This script starts a local karmada control plane based on current codebase and with a certain number of clusters joined. REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
# This script starts a local karmada control plane based on current codebase and with a certain number of clusters joined.
# Parameters: [HOST_IPADDRESS](optional) if you want to export clusters' API server port to specific IP address
# This script depends on utils in: ${REPO_ROOT}/hack/util.sh
# 1. used by developer to setup develop environment quickly.
# 2. used by e2e testing to setup test environment automatically.
@ -20,21 +21,47 @@ MEMBER_CLUSTER_KUBECONFIG=${MEMBER_CLUSTER_KUBECONFIG:-"${KUBECONFIG_PATH}/membe
MEMBER_CLUSTER_1_NAME=${MEMBER_CLUSTER_1_NAME:-"member1"}
MEMBER_CLUSTER_2_NAME=${MEMBER_CLUSTER_2_NAME:-"member2"}
PULL_MODE_CLUSTER_NAME=${PULL_MODE_CLUSTER_NAME:-"member3"}
CLUSTER_VERSION=${CLUSTER_VERSION:-"kindest/node:v1.19.1"}
HOST_IPADDRESS=${1:-}
CLUSTER_VERSION=${CLUSTER_VERSION:-"kindest/node:v1.19.1"}
KIND_LOG_FILE=${KIND_LOG_FILE:-"/tmp/karmada"}
#step0: prepare
# Make sure go exists
util::cmd_must_exist "go"
# install kind and kubectl
util::install_tools sigs.k8s.io/kind v0.10.0
# get arch name and os name in bootstrap
BS_ARCH=$(go env GOARCH)
BS_OS=$(go env GOOS)
# check arch and os name before installing
util::install_environment_check "${BS_ARCH}" "${BS_OS}"
# we choose v1.18.0, because in kubectl after versions 1.18 exist a bug which will give wrong output when using jsonpath.
# bug details: https://github.com/kubernetes/kubernetes/pull/98057
util::install_kubectl "v1.18.0" "amd64"
util::install_kubectl "v1.18.0" "${BS_ARCH}" "${BS_OS}"
#step1. create host cluster and member clusters in parallel
util::create_cluster "${HOST_CLUSTER_NAME}" "${MAIN_KUBECONFIG}" "${CLUSTER_VERSION}" "${KIND_LOG_FILE}"
util::create_cluster "${MEMBER_CLUSTER_1_NAME}" "${MEMBER_CLUSTER_KUBECONFIG}" "${CLUSTER_VERSION}" "${KIND_LOG_FILE}" "${REPO_ROOT}/artifacts/kindClusterConfig/member1.yaml"
util::create_cluster "${MEMBER_CLUSTER_2_NAME}" "${MEMBER_CLUSTER_KUBECONFIG}" "${CLUSTER_VERSION}" "${KIND_LOG_FILE}" "${REPO_ROOT}/artifacts/kindClusterConfig/member2.yaml"
# host IP address: script parameter ahead of macOS IP
if [[ -z "${HOST_IPADDRESS}" ]]; then
util::get_macos_ipaddress # Adapt for macOS
HOST_IPADDRESS=${MAC_NIC_IPADDRESS:-}
fi
#prepare for kindClusterConfig
TEMP_PATH=$(mktemp -d)
echo -e "Preparing kindClusterConfig in path: ${TEMP_PATH}"
cp -rf "${REPO_ROOT}"/artifacts/kindClusterConfig/member1.yaml "${TEMP_PATH}"/member1.yaml
cp -rf "${REPO_ROOT}"/artifacts/kindClusterConfig/member2.yaml "${TEMP_PATH}"/member2.yaml
if [[ -n "${HOST_IPADDRESS}" ]]; then # If bind the port of clusters(karmada-host, member1 and member2) to the host IP
cp -rf "${REPO_ROOT}"/artifacts/kindClusterConfig/karmada-host.yaml "${TEMP_PATH}"/karmada-host.yaml
sed -i'' -e "s/{{host_ipaddress}}/${HOST_IPADDRESS}/g" "${TEMP_PATH}"/karmada-host.yaml
sed -i'' -e '/networking:/a\'$'\n'' apiServerAddress: '"${HOST_IPADDRESS}"''$'\n' "${TEMP_PATH}"/member1.yaml
sed -i'' -e '/networking:/a\'$'\n'' apiServerAddress: '"${HOST_IPADDRESS}"''$'\n' "${TEMP_PATH}"/member2.yaml
util::create_cluster "${HOST_CLUSTER_NAME}" "${MAIN_KUBECONFIG}" "${CLUSTER_VERSION}" "${KIND_LOG_FILE}" "${TEMP_PATH}"/karmada-host.yaml
else
util::create_cluster "${HOST_CLUSTER_NAME}" "${MAIN_KUBECONFIG}" "${CLUSTER_VERSION}" "${KIND_LOG_FILE}"
fi
util::create_cluster "${MEMBER_CLUSTER_1_NAME}" "${MEMBER_CLUSTER_KUBECONFIG}" "${CLUSTER_VERSION}" "${KIND_LOG_FILE}" "${TEMP_PATH}"/member1.yaml
util::create_cluster "${MEMBER_CLUSTER_2_NAME}" "${MEMBER_CLUSTER_KUBECONFIG}" "${CLUSTER_VERSION}" "${KIND_LOG_FILE}" "${TEMP_PATH}"/member2.yaml
util::create_cluster "${PULL_MODE_CLUSTER_NAME}" "${MEMBER_CLUSTER_KUBECONFIG}" "${CLUSTER_VERSION}" "${KIND_LOG_FILE}"
#step2. make images and get karmadactl

View File

@ -33,33 +33,7 @@ if [ ! -d "$KUBECONFIG_PATH" ]; then
mkdir -p "$KUBECONFIG_PATH"
fi
# Adapt for macOS
if [[ $(go env GOOS) = "darwin" ]]; then
tmp_ip=$(ipconfig getifaddr en0 || true)
echo ""
echo " Detected that you are installing Karmada on macOS "
echo ""
echo "It needs a Macintosh IP address to bind Karmada API Server(port 5443),"
echo "so that member clusters can access it from docker containers, please"
echo -n "input an available IP, "
if [[ -z ${tmp_ip} ]]; then
echo "you can use the command 'ifconfig' to look for one"
tips_msg="[Enter IP address]:"
else
echo "default IP will be en0 inet addr if exists"
tips_msg="[Enter for default ${tmp_ip}]:"
fi
read -r -p "${tips_msg}" MAC_NIC_IPADDRESS
MAC_NIC_IPADDRESS=${MAC_NIC_IPADDRESS:-$tmp_ip}
if [[ "${MAC_NIC_IPADDRESS}" =~ ^(([1-9]?[0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))\.){3}([1-9]?[0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))$ ]]; then
echo "Using IP address: ${MAC_NIC_IPADDRESS}"
else
echo -e "\nError: you input an invalid IP address"
exit 1
fi
else # non-macOS
MAC_NIC_IPADDRESS=${MAC_NIC_IPADDRESS:-}
fi
util::get_macos_ipaddress # Adapt for macOS
# create a cluster to deploy karmada control plane components.
if [[ -n "${MAC_NIC_IPADDRESS}" ]]; then # install on macOS

View File

@ -65,26 +65,59 @@ function util::cmd_must_exist_cfssl {
fi
}
# util::install_environment_check will check OS and ARCH before installing
# ARCH support list: amd64,arm64
# OS support list: linux,darwin
function util::install_environment_check {
local ARCH=${1:-}
local OS=${2:-}
if [[ "$ARCH" =~ ^(amd64|arm64)$ ]]; then
if [[ "$OS" =~ ^(linux|darwin)$ ]]; then
return 0
fi
fi
echo "Sorry, Karmada installation does not support $ARCH/$OS at the moment"
exit 1
}
# util::install_kubectl will install the given version kubectl
function util::install_kubectl {
local KUBECTL_VERSION=${1}
local ARCH=${2}
echo "Installing 'kubectl ${KUBECTL_VERSION}' for you, may require your root privileges"
curl -sSL --retry 5 https://dl.k8s.io/release/"$KUBECTL_VERSION"/bin/linux/"$ARCH"/kubectl > ./kubectl
chmod +x ./kubectl
sudo rm -rf "$(which kubectl)"
sudo mv ./kubectl /usr/local/bin/kubectl
local OS=${3:-linux}
echo "Installing 'kubectl ${KUBECTL_VERSION}' for you, may require the root privileges"
curl --retry 5 -sSLo ./kubectl -w "%{http_code}" https://dl.k8s.io/release/"$KUBECTL_VERSION"/bin/"$OS"/"$ARCH"/kubectl | grep '200'
ret=$?
if [ ${ret} -eq 0 ]; then
chmod +x ./kubectl
echo "$PATH" | grep '/usr/local/bin' || export PATH=$PATH:/usr/local/bin
sudo rm -rf "$(which kubectl)"
sudo mv ./kubectl /usr/local/bin/kubectl
else
echo "Failed to install kubectl, can not download the binary file at https://dl.k8s.io/release/$KUBECTL_VERSION/bin/$OS/$ARCH/kubectl"
exit 1
fi
}
# util::install_kind will install the given version kind
function util::install_kind {
local kind_version=${1}
echo "Installing 'kind ${kind_version}' for you, may require your root privileges"
echo "Installing 'kind ${kind_version}' for you, may require the root privileges"
local os_name
os_name=$(go env GOOS)
curl --retry 5 -sSLo ./kind "https://kind.sigs.k8s.io/dl/${kind_version}/kind-${os_name:-linux}-amd64"
chmod +x ./kind
sudo rm -f "$(which kind)"
sudo mv ./kind /usr/local/bin/kind
local arch_name
arch_name=$(go env GOARCH)
curl --retry 5 -sSLo ./kind -w "%{http_code}" "https://kind.sigs.k8s.io/dl/${kind_version}/kind-${os_name:-linux}-${arch_name:-amd64}" | grep '200'
ret=$?
if [ ${ret} -eq 0 ]; then
chmod +x ./kind
echo "$PATH" | grep '/usr/local/bin' || export PATH=$PATH:/usr/local/bin
sudo rm -f "$(which kind)"
sudo mv ./kind /usr/local/bin/kind
else
echo "Failed to install kind, can not download the binary file at https://kind.sigs.k8s.io/dl/${kind_version}/kind-${os_name:-linux}-${arch_name:-amd64}"
exit 1
fi
}
# util::create_signing_certkey creates a CA, args are sudo, dest-dir, ca-id, purpose
@ -436,3 +469,33 @@ function util::add_routes() {
unset IFS
}
# util::get_macos_ipaddress will get ip address on macos interactively, store to 'MAC_NIC_IPADDRESS' if available
MAC_NIC_IPADDRESS=''
function util::get_macos_ipaddress() {
if [[ $(go env GOOS) = "darwin" ]]; then
tmp_ip=$(ipconfig getifaddr en0 || true)
echo ""
echo " Detected that you are installing Karmada on macOS "
echo ""
echo "It needs a Macintosh IP address to bind Karmada API Server(port 5443),"
echo "so that member clusters can access it from docker containers, please"
echo -n "input an available IP, "
if [[ -z ${tmp_ip} ]]; then
echo "you can use the command 'ifconfig' to look for one"
tips_msg="[Enter IP address]:"
else
echo "default IP will be en0 inet addr if exists"
tips_msg="[Enter for default ${tmp_ip}]:"
fi
read -r -p "${tips_msg}" MAC_NIC_IPADDRESS
MAC_NIC_IPADDRESS=${MAC_NIC_IPADDRESS:-$tmp_ip}
if [[ "${MAC_NIC_IPADDRESS}" =~ ^(([1-9]?[0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))\.){3}([1-9]?[0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))$ ]]; then
echo "Using IP address: ${MAC_NIC_IPADDRESS}"
else
echo -e "\nError: you input an invalid IP address"
exit 1
fi
else # non-macOS
MAC_NIC_IPADDRESS=${MAC_NIC_IPADDRESS:-}
fi
}