get-kube-binaries.sh 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #!/usr/bin/env bash
  2. # Copyright 2016 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # This script downloads and installs the Kubernetes client and server
  16. # (and optionally test) binaries,
  17. # It is intended to be called from an extracted Kubernetes release tarball.
  18. #
  19. # We automatically choose the correct client binaries to download.
  20. #
  21. # Options:
  22. # Set KUBERNETES_SERVER_ARCH to choose the server (Kubernetes cluster)
  23. # architecture to download:
  24. # * amd64 [default]
  25. # * arm
  26. # * arm64
  27. # * ppc64le
  28. # * s390x
  29. #
  30. # Set KUBERNETES_CLIENT_OS to choose the client OS to download:
  31. # * current OS [default]
  32. # * linux
  33. # * darwin
  34. # * windows
  35. #
  36. # Set KUBERNETES_CLIENT_ARCH to choose the client architecture to download:
  37. # * current architecture [default]
  38. # * amd64
  39. # * arm
  40. # * arm64
  41. # * ppc64le
  42. # * s390x
  43. # * windows
  44. #
  45. # Set KUBERNETES_SKIP_CONFIRM to skip the installation confirmation prompt.
  46. # Set KUBERNETES_RELEASE_URL to choose where to download binaries from.
  47. # (Defaults to https://storage.googleapis.com/kubernetes-release/release).
  48. # Set KUBERNETES_DOWNLOAD_TESTS to additionally download and extract the test
  49. # binaries tarball.
  50. set -o errexit
  51. set -o nounset
  52. set -o pipefail
  53. KUBE_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
  54. KUBERNETES_RELEASE_URL="${KUBERNETES_RELEASE_URL:-https://dl.k8s.io}"
  55. function detect_kube_release() {
  56. if [[ -n "${KUBE_VERSION:-}" ]]; then
  57. return 0 # Allow caller to explicitly set version
  58. fi
  59. if [[ ! -e "${KUBE_ROOT}/version" ]]; then
  60. echo "Can't determine Kubernetes release." >&2
  61. echo "${BASH_SOURCE[0]} should only be run from a prebuilt Kubernetes release." >&2
  62. echo "Did you mean to use get-kube.sh instead?" >&2
  63. exit 1
  64. fi
  65. KUBE_VERSION=$(cat "${KUBE_ROOT}/version")
  66. }
  67. function detect_client_info() {
  68. if [ -n "${KUBERNETES_CLIENT_OS-}" ]; then
  69. CLIENT_PLATFORM="${KUBERNETES_CLIENT_OS}"
  70. else
  71. local kernel
  72. kernel="$(uname -s)"
  73. case "${kernel}" in
  74. Darwin)
  75. CLIENT_PLATFORM="darwin"
  76. ;;
  77. Linux)
  78. CLIENT_PLATFORM="linux"
  79. ;;
  80. *)
  81. echo "Unknown, unsupported platform: ${kernel}." >&2
  82. echo "Supported platforms: Linux, Darwin." >&2
  83. echo "Bailing out." >&2
  84. exit 2
  85. esac
  86. fi
  87. if [ -n "${KUBERNETES_CLIENT_ARCH-}" ]; then
  88. CLIENT_ARCH="${KUBERNETES_CLIENT_ARCH}"
  89. else
  90. # TODO: migrate the kube::util::host_platform function out of hack/lib and
  91. # use it here.
  92. local machine
  93. machine="$(uname -m)"
  94. case "${machine}" in
  95. x86_64*|i?86_64*|amd64*)
  96. CLIENT_ARCH="amd64"
  97. ;;
  98. aarch64*|arm64*)
  99. CLIENT_ARCH="arm64"
  100. ;;
  101. arm*)
  102. CLIENT_ARCH="arm"
  103. ;;
  104. i?86*)
  105. CLIENT_ARCH="386"
  106. ;;
  107. s390x*)
  108. CLIENT_ARCH="s390x"
  109. ;;
  110. *)
  111. echo "Unknown, unsupported architecture (${machine})." >&2
  112. echo "Supported architectures x86_64, i686, arm, arm64, s390x." >&2
  113. echo "Bailing out." >&2
  114. exit 3
  115. ;;
  116. esac
  117. fi
  118. }
  119. function md5sum_file() {
  120. if which md5 >/dev/null 2>&1; then
  121. md5 -q "$1"
  122. else
  123. md5sum "$1" | awk '{ print $1 }'
  124. fi
  125. }
  126. function sha1sum_file() {
  127. if which sha1sum >/dev/null 2>&1; then
  128. sha1sum "$1" | awk '{ print $1 }'
  129. else
  130. shasum -a1 "$1" | awk '{ print $1 }'
  131. fi
  132. }
  133. function download_tarball() {
  134. local -r download_path="$1"
  135. local -r file="$2"
  136. url="${DOWNLOAD_URL_PREFIX}/${file}"
  137. mkdir -p "${download_path}"
  138. if [[ $(which curl) ]]; then
  139. curl -fL --retry 3 --keepalive-time 2 "${url}" -o "${download_path}/${file}"
  140. elif [[ $(which wget) ]]; then
  141. wget "${url}" -O "${download_path}/${file}"
  142. else
  143. echo "Couldn't find curl or wget. Bailing out." >&2
  144. exit 4
  145. fi
  146. echo
  147. local md5sum sha1sum
  148. md5sum=$(md5sum_file "${download_path}/${file}")
  149. echo "md5sum(${file})=${md5sum}"
  150. sha1sum=$(sha1sum_file "${download_path}/${file}")
  151. echo "sha1sum(${file})=${sha1sum}"
  152. echo
  153. # TODO: add actual verification
  154. }
  155. function extract_arch_tarball() {
  156. local -r tarfile="$1"
  157. local -r platform="$2"
  158. local -r arch="$3"
  159. platforms_dir="${KUBE_ROOT}/platforms/${platform}/${arch}"
  160. echo "Extracting ${tarfile} into ${platforms_dir}"
  161. mkdir -p "${platforms_dir}"
  162. # Tarball looks like kubernetes/{client,server,test}/bin/BINARY"
  163. tar -xzf "${tarfile}" --strip-components 3 -C "${platforms_dir}"
  164. }
  165. detect_kube_release
  166. DOWNLOAD_URL_PREFIX="${KUBERNETES_RELEASE_URL}/${KUBE_VERSION}"
  167. SERVER_PLATFORM="linux"
  168. SERVER_ARCH="${KUBERNETES_SERVER_ARCH:-amd64}"
  169. SERVER_TAR="kubernetes-server-${SERVER_PLATFORM}-${SERVER_ARCH}.tar.gz"
  170. if [[ -n "${KUBERNETES_NODE_PLATFORM-}" || -n "${KUBERNETES_NODE_ARCH-}" ]]; then
  171. NODE_PLATFORM="${KUBERNETES_NODE_PLATFORM:-${SERVER_PLATFORM}}"
  172. NODE_ARCH="${KUBERNETES_NODE_ARCH:-${SERVER_ARCH}}"
  173. NODE_TAR="kubernetes-node-${NODE_PLATFORM}-${NODE_ARCH}.tar.gz"
  174. fi
  175. detect_client_info
  176. CLIENT_TAR="kubernetes-client-${CLIENT_PLATFORM}-${CLIENT_ARCH}.tar.gz"
  177. echo "Kubernetes release: ${KUBE_VERSION}"
  178. echo "Server: ${SERVER_PLATFORM}/${SERVER_ARCH} (to override, set KUBERNETES_SERVER_ARCH)"
  179. printf "Client: %s/%s" "${CLIENT_PLATFORM}" "${CLIENT_ARCH}"
  180. if [ -z "${KUBERNETES_CLIENT_OS-}" ] && [ -z "${KUBERNETES_CLIENT_ARCH-}" ]; then
  181. printf " (autodetected)"
  182. fi
  183. echo " (to override, set KUBERNETES_CLIENT_OS and/or KUBERNETES_CLIENT_ARCH)"
  184. echo
  185. echo "Will download ${SERVER_TAR} from ${DOWNLOAD_URL_PREFIX}"
  186. echo "Will download and extract ${CLIENT_TAR} from ${DOWNLOAD_URL_PREFIX}"
  187. DOWNLOAD_NODE_TAR=false
  188. if [[ -n "${NODE_TAR:-}" ]]; then
  189. DOWNLOAD_NODE_TAR=true
  190. echo "Will download and extract ${NODE_TAR} from ${DOWNLOAD_URL_PREFIX}"
  191. fi
  192. DOWNLOAD_TESTS_TAR=false
  193. if [[ -n "${KUBERNETES_DOWNLOAD_TESTS-}" ]]; then
  194. DOWNLOAD_TESTS_TAR=true
  195. echo "Will download and extract kubernetes-test tarball(s) from ${DOWNLOAD_URL_PREFIX}"
  196. fi
  197. if [[ -z "${KUBERNETES_SKIP_CONFIRM-}" ]]; then
  198. echo "Is this ok? [Y]/n"
  199. read -r confirm
  200. if [[ "${confirm}" =~ ^[nN]$ ]]; then
  201. echo "Aborting."
  202. exit 1
  203. fi
  204. fi
  205. download_tarball "${KUBE_ROOT}/server" "${SERVER_TAR}"
  206. if "${DOWNLOAD_NODE_TAR}"; then
  207. download_tarball "${KUBE_ROOT}/node" "${NODE_TAR}"
  208. fi
  209. download_tarball "${KUBE_ROOT}/client" "${CLIENT_TAR}"
  210. extract_arch_tarball "${KUBE_ROOT}/client/${CLIENT_TAR}" "${CLIENT_PLATFORM}" "${CLIENT_ARCH}"
  211. ln -s "${KUBE_ROOT}/platforms/${CLIENT_PLATFORM}/${CLIENT_ARCH}" "${KUBE_ROOT}/client/bin"
  212. echo "Add '${KUBE_ROOT}/client/bin' to your PATH to use newly-installed binaries."
  213. if "${DOWNLOAD_TESTS_TAR}"; then
  214. TESTS_PORTABLE_TAR="kubernetes-test-portable.tar.gz"
  215. download_tarball "${KUBE_ROOT}/test" "${TESTS_PORTABLE_TAR}" || true
  216. if [[ -f "${KUBE_ROOT}/test/${TESTS_PORTABLE_TAR}" ]]; then
  217. echo "Extracting ${TESTS_PORTABLE_TAR} into ${KUBE_ROOT}"
  218. # Strip leading "kubernetes/"
  219. tar -xzf "${KUBE_ROOT}/test/${TESTS_PORTABLE_TAR}" --strip-components 1 -C "${KUBE_ROOT}"
  220. # Next, download platform-specific test tarballs for all relevant platforms
  221. TEST_PLATFORM_TUPLES=(
  222. "${CLIENT_PLATFORM}/${CLIENT_ARCH}"
  223. "${SERVER_PLATFORM}/${SERVER_ARCH}"
  224. )
  225. if [[ -n "${NODE_PLATFORM:-}" && -n "${NODE_ARCH:-}" ]]; then
  226. TEST_PLATFORM_TUPLES+=("${NODE_PLATFORM}/${NODE_ARCH}")
  227. fi
  228. # Loop over only the unique tuples
  229. for TUPLE in $(printf "%s\n" "${TEST_PLATFORM_TUPLES[@]}" | sort -u); do
  230. OS=$(echo "${TUPLE}" | cut -d/ -f1)
  231. ARCH=$(echo "${TUPLE}" | cut -d/ -f2)
  232. TEST_PLATFORM_TAR="kubernetes-test-${OS}-${ARCH}.tar.gz"
  233. download_tarball "${KUBE_ROOT}/test" "${TEST_PLATFORM_TAR}"
  234. extract_arch_tarball "${KUBE_ROOT}/test/${TEST_PLATFORM_TAR}" "${OS}" "${ARCH}"
  235. done
  236. else
  237. echo "Failed to download portable test tarball, falling back to mondo test tarball."
  238. TESTS_MONDO_TAR="kubernetes-test.tar.gz"
  239. download_tarball "${KUBE_ROOT}/test" "${TESTS_MONDO_TAR}"
  240. echo "Extracting ${TESTS_MONDO_TAR} into ${KUBE_ROOT}"
  241. # Strip leading "kubernetes/"
  242. tar -xzf "${KUBE_ROOT}/test/${TESTS_MONDO_TAR}" --strip-components 1 -C "${KUBE_ROOT}"
  243. fi
  244. fi