From c1a0aa08e31dec817c5787bb6ab6987b42362db5 Mon Sep 17 00:00:00 2001 From: HirazawaUi <695097494plus@gmail.com> Date: Thu, 31 Aug 2023 00:48:57 +0800 Subject: [PATCH] Add cni plugin auto Arch and OS selection --- hack/local-up-cluster.sh | 77 +++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 24 deletions(-) diff --git a/hack/local-up-cluster.sh b/hack/local-up-cluster.sh index 210b7e00ad4..59550623b13 100755 --- a/hack/local-up-cluster.sh +++ b/hack/local-up-cluster.sh @@ -53,11 +53,11 @@ LIMITED_SWAP=${LIMITED_SWAP:-""} # required for cni installation CNI_CONFIG_DIR=${CNI_CONFIG_DIR:-/etc/cni/net.d} CNI_PLUGINS_VERSION=${CNI_PLUGINS_VERSION:-"v1.3.0"} -CNI_TARGETARCH=${CNI_TARGETARCH:-amd64} -CNI_PLUGINS_TARBALL="${CNI_PLUGINS_VERSION}/cni-plugins-linux-${CNI_TARGETARCH}-${CNI_PLUGINS_VERSION}.tgz" -CNI_PLUGINS_URL="https://github.com/containernetworking/plugins/releases/download/${CNI_PLUGINS_TARBALL}" +# The arch of the CNI binary, if not set, will be fetched based on the value of `uname -m` +CNI_TARGETARCH=${CNI_TARGETARCH:-""} +CNI_PLUGINS_URL="https://github.com/containernetworking/plugins/releases/download" CNI_PLUGINS_AMD64_SHA256SUM=${CNI_PLUGINS_AMD64_SHA256SUM:-"754a71ed60a4bd08726c3af705a7d55ee3df03122b12e389fdba4bea35d7dd7e"} -CNI_PLUGINS_ARM64_SHA256SUM=${CNI_PLUGINS_ARM64_SHA256SUM:-"86c4c866a01a8073ad14f6feec74de1fd63669786850c7be47521433f9570902"} +CNI_PLUGINS_ARM64_SHA256SUM=${CNI_PLUGINS_ARM64_SHA256SUM:-"de7a666fd6ad83a228086bd55756db62ef335a193d1b143d910b69f079e30598"} CNI_PLUGINS_PPC64LE_SHA256SUM=${CNI_PLUGINS_PPC64LE_SHA256SUM:-"8ceff026f4eccf33c261b4153af6911e10784ac169d08c1d86cf6887b9f4e99b"} CNI_PLUGINS_S390X_SHA256SUM=${CNI_PLUGINS_S390X_SHA256SUM:-"2f1f65ac33e961bcdc633e14c376656455824e22cc45d3ca7e31eb2750a7ebc4"} @@ -263,20 +263,8 @@ function test_apiserver_off { fi } -function detect_binary { - # Detect the OS name/arch so that we can find our binary - case "$(uname -s)" in - Darwin) - host_os=darwin - ;; - Linux) - host_os=linux - ;; - *) - echo "Unsupported host OS. Must be Linux or Mac OS X." >&2 - exit 1 - ;; - esac +function detect_arch { + local host_arch case "$(uname -m)" in x86_64*) @@ -312,7 +300,39 @@ function detect_binary { ;; esac - GO_OUT="${KUBE_ROOT}/_output/local/bin/${host_os}/${host_arch}" + if [[ -z "${host_arch}" ]]; then + return + fi + echo -n "${host_arch}" +} + +function detect_os { + local host_os + + case "$(uname -s)" in + Darwin) + host_os=darwin + ;; + Linux) + host_os=linux + ;; + *) + echo "Unsupported host OS. Must be Linux or Mac OS X." >&2 + exit 1 + ;; + esac + + if [[ -z "${host_os}" ]]; then + return + fi + echo -n "${host_os}" +} + +function detect_binary { + host_arch=$(detect_arch) + host_os=$(detect_os) + + GO_OUT="${KUBE_ROOT}/_output/local/bin/${host_os}/${host_arch}" } cleanup() @@ -1146,15 +1166,24 @@ function tolerate_cgroups_v2 { } function install_cni { - cni_plugin_sha=CNI_PLUGINS_${CNI_TARGETARCH^^}_SHA256SUM + if [[ -n "${CNI_TARGETARCH}" ]]; then + host_arch="${CNI_TARGETARCH}" + else + host_arch=$(detect_arch) + fi + + cni_plugin_sha=CNI_PLUGINS_${host_arch^^}_SHA256SUM + cni_plugin_tarball="${CNI_PLUGINS_VERSION}/cni-plugins-linux-${host_arch}-${CNI_PLUGINS_VERSION}.tgz" + cni_plugins_url="${CNI_PLUGINS_URL}/${cni_plugin_tarball}" + echo "Installing CNI plugin binaries ..." \ - && curl -sSL --retry 5 --output "${TMP_DIR}"/cni."${CNI_TARGETARCH}".tgz "${CNI_PLUGINS_URL}" \ - && echo "${!cni_plugin_sha} ${TMP_DIR}/cni.${CNI_TARGETARCH}.tgz" | tee "${TMP_DIR}"/cni.sha256 \ + && curl -sSL --retry 5 --output "${TMP_DIR}"/cni."${host_arch}".tgz "${cni_plugins_url}" \ + && echo "${!cni_plugin_sha} ${TMP_DIR}/cni.${host_arch}.tgz" | tee "${TMP_DIR}"/cni.sha256 \ && sha256sum --ignore-missing -c "${TMP_DIR}"/cni.sha256 \ && rm -f "${TMP_DIR}"/cni.sha256 \ && sudo mkdir -p /opt/cni/bin \ - && sudo tar -C /opt/cni/bin -xzvf "${TMP_DIR}"/cni."${CNI_TARGETARCH}".tgz \ - && rm -rf "${TMP_DIR}"/cni."${CNI_TARGETARCH}".tgz \ + && sudo tar -C /opt/cni/bin -xzvf "${TMP_DIR}"/cni."${host_arch}".tgz \ + && rm -rf "${TMP_DIR}"/cni."${host_arch}".tgz \ && sudo find /opt/cni/bin -type f -not \( \ -iname host-local \ -o -iname bridge \