From b044ef475f95faa1489ad0f19a03b539d9ed39a4 Mon Sep 17 00:00:00 2001 From: lfbear Date: Thu, 29 Jul 2021 16:03:37 +0800 Subject: [PATCH 1/2] dapt karmada-bootstrap.sh for macOS Signed-off-by: lfbear --- hack/karmada-bootstrap.sh | 37 +++++++++++++++++++++++++++++++------ hack/local-up-karmada.sh | 28 +--------------------------- hack/util.sh | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 66 insertions(+), 34 deletions(-) diff --git a/hack/karmada-bootstrap.sh b/hack/karmada-bootstrap.sh index fb78dca7a..905a7ffea 100755 --- a/hack/karmada-bootstrap.sh +++ b/hack/karmada-bootstrap.sh @@ -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,45 @@ 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) # 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 diff --git a/hack/local-up-karmada.sh b/hack/local-up-karmada.sh index 8823eb4d8..671a0219e 100755 --- a/hack/local-up-karmada.sh +++ b/hack/local-up-karmada.sh @@ -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 diff --git a/hack/util.sh b/hack/util.sh index d31be7853..19b1b22a3 100755 --- a/hack/util.sh +++ b/hack/util.sh @@ -67,16 +67,19 @@ function util::cmd_must_exist_cfssl { # util::install_kubectl will install the given version kubectl function util::install_kubectl { + echo "$PATH" | grep '/usr/local/bin' || export PATH=$PATH:/usr/local/bin local KUBECTL_VERSION=${1} local ARCH=${2} + local OS=${3:-linux} 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 + curl -sSL --retry 5 https://dl.k8s.io/release/"$KUBECTL_VERSION"/bin/"$OS"/"$ARCH"/kubectl > ./kubectl chmod +x ./kubectl sudo rm -rf "$(which kubectl)" sudo mv ./kubectl /usr/local/bin/kubectl } function util::install_kind { + echo "$PATH" | grep '/usr/local/bin' || export PATH=$PATH:/usr/local/bin local kind_version=${1} echo "Installing 'kind ${kind_version}' for you, may require your root privileges" local os_name @@ -432,3 +435,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 +} From f85f0c40bd9bf62b07d816d04929f1c7291c70ee Mon Sep 17 00:00:00 2001 From: lfbear Date: Fri, 6 Aug 2021 14:07:21 +0800 Subject: [PATCH 2/2] 1. add os and arch check in karmada-bootstrap.sh 2. give a error message when install kubectl/kind failed Signed-off-by: lfbear --- hack/karmada-bootstrap.sh | 2 ++ hack/util.sh | 54 ++++++++++++++++++++++++++++++--------- 2 files changed, 44 insertions(+), 12 deletions(-) diff --git a/hack/karmada-bootstrap.sh b/hack/karmada-bootstrap.sh index 905a7ffea..2105d86a3 100755 --- a/hack/karmada-bootstrap.sh +++ b/hack/karmada-bootstrap.sh @@ -34,6 +34,8 @@ 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" "${BS_ARCH}" "${BS_OS}" diff --git a/hack/util.sh b/hack/util.sh index 19b1b22a3..2f09d9bab 100755 --- a/hack/util.sh +++ b/hack/util.sh @@ -65,29 +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 { - echo "$PATH" | grep '/usr/local/bin' || export PATH=$PATH:/usr/local/bin local KUBECTL_VERSION=${1} local ARCH=${2} local OS=${3:-linux} - echo "Installing 'kubectl ${KUBECTL_VERSION}' for you, may require your root privileges" - curl -sSL --retry 5 https://dl.k8s.io/release/"$KUBECTL_VERSION"/bin/"$OS"/"$ARCH"/kubectl > ./kubectl - chmod +x ./kubectl - sudo rm -rf "$(which kubectl)" - sudo mv ./kubectl /usr/local/bin/kubectl + 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 { - echo "$PATH" | grep '/usr/local/bin' || export PATH=$PATH:/usr/local/bin 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