get-kube.sh 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #!/usr/bin/env bash
  2. # Copyright 2014 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. # Bring up a Kubernetes cluster.
  16. # Usage:
  17. # wget -q -O - https://get.k8s.io | bash
  18. # or
  19. # curl -fsSL https://get.k8s.io | bash
  20. #
  21. # Advanced options
  22. # Set KUBERNETES_PROVIDER to choose between different providers:
  23. # Google Compute Engine [default]
  24. # * export KUBERNETES_PROVIDER=gce; wget -q -O - https://get.k8s.io | bash
  25. #
  26. # Set KUBERNETES_RELEASE to choose a specific release instead of the current
  27. # stable release, (e.g. 'v1.3.7').
  28. # See https://github.com/kubernetes/kubernetes/releases for release options.
  29. # Set KUBERNETES_RELEASE_URL to choose where to download binaries from.
  30. # (Defaults to https://storage.googleapis.com/kubernetes-release/release).
  31. #
  32. # Set KUBERNETES_SERVER_ARCH to choose the server (Kubernetes cluster)
  33. # architecture to download:
  34. # * amd64 [default]
  35. # * arm
  36. # * arm64
  37. #
  38. # Set KUBERNETES_NODE_PLATFORM to choose the platform for which to download
  39. # the node binaries. If none of KUBERNETES_NODE_PLATFORM and
  40. # KUBERNETES_NODE_ARCH is set, no node binaries will be downloaded. If only
  41. # one of the two is set, the other will be defaulted to the
  42. # KUBERNETES_SERVER_PLATFORM/ARCH.
  43. # * linux
  44. # * windows
  45. #
  46. # Set KUBERNETES_NODE_ARCH to choose the node architecture to download the
  47. # node binaries. If none of KUBERNETES_NODE_PLATFORM and
  48. # KUBERNETES_NODE_ARCH is set, no node binaries will be downloaded. If only
  49. # one of the two is set, the other will be defaulted to the
  50. # KUBERNETES_SERVER_PLATFORM/ARCH.
  51. # * amd64 [default]
  52. # * arm
  53. # * arm64
  54. #
  55. # Set KUBERNETES_SKIP_DOWNLOAD to skip downloading a release.
  56. # Set KUBERNETES_SKIP_CONFIRM to skip the installation confirmation prompt.
  57. # Set KUBERNETES_SKIP_CREATE_CLUSTER to skip starting a cluster.
  58. # Set KUBERNETES_SKIP_RELEASE_VALIDATION to skip trying to validate the
  59. # Kubernetes release string. This implies that you know what you're doing
  60. # and have set KUBERNETES_RELEASE and KUBERNETES_RELEASE_URL properly.
  61. set -o errexit
  62. set -o nounset
  63. set -o pipefail
  64. # If KUBERNETES_RELEASE_URL is overridden but KUBERNETES_CI_RELEASE_URL is not then set KUBERNETES_CI_RELEASE_URL to KUBERNETES_RELEASE_URL.
  65. KUBERNETES_CI_RELEASE_URL="${KUBERNETES_CI_RELEASE_URL:-${KUBERNETES_RELEASE_URL:-https://dl.k8s.io/ci}}"
  66. KUBERNETES_RELEASE_URL="${KUBERNETES_RELEASE_URL:-https://dl.k8s.io}"
  67. KUBE_RELEASE_VERSION_REGEX="^v(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-([a-zA-Z0-9]+)\\.(0|[1-9][0-9]*))?$"
  68. KUBE_CI_VERSION_REGEX="^v(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)-([a-zA-Z0-9]+)\\.(0|[1-9][0-9]*)(\\.(0|[1-9][0-9]*)\\+[-0-9a-z]*)?$"
  69. # Sets KUBE_VERSION variable if an explicit version number was provided (e.g. "v1.0.6",
  70. # "v1.2.0-alpha.1.881+376438b69c7612") or resolves the "published" version
  71. # <path>/<version> (e.g. "release/stable",' "ci/latest-1") by reading from GCS.
  72. #
  73. # See the docs on getting builds for more information about version
  74. # publication.
  75. #
  76. # Args:
  77. # $1 version string from command line
  78. # Vars set:
  79. # KUBE_VERSION
  80. function set_binary_version() {
  81. if [[ "${1}" =~ "/" ]]; then
  82. KUBE_VERSION=$(curl -fsSL --retry 5 "https://dl.k8s.io/${1}.txt")
  83. else
  84. KUBE_VERSION=${1}
  85. fi
  86. export KUBE_VERSION
  87. }
  88. # Use the script from inside the Kubernetes tarball to fetch the client and
  89. # server binaries (if not included in kubernetes.tar.gz).
  90. function download_kube_binaries {
  91. (
  92. cd kubernetes
  93. if [[ -x ./cluster/get-kube-binaries.sh ]]; then
  94. # Make sure to use the same download URL in get-kube-binaries.sh
  95. KUBERNETES_RELEASE_URL="${KUBERNETES_RELEASE_URL}" \
  96. ./cluster/get-kube-binaries.sh
  97. fi
  98. )
  99. }
  100. function create_cluster {
  101. if [[ -n "${KUBERNETES_SKIP_CREATE_CLUSTER-}" ]]; then
  102. exit 0
  103. fi
  104. echo "Creating a kubernetes on ${KUBERNETES_PROVIDER:-gce}..."
  105. (
  106. cd kubernetes
  107. ./cluster/kube-up.sh
  108. echo "Kubernetes binaries at ${PWD}/cluster/"
  109. if [[ ":$PATH:" != *":${PWD}/cluster:"* ]]; then
  110. echo "You may want to add this directory to your PATH in \$HOME/.profile"
  111. fi
  112. echo "Installation successful!"
  113. )
  114. }
  115. # Get default service account credentials of the VM.
  116. GCE_METADATA_INTERNAL="http://metadata.google.internal/computeMetadata/v1/instance"
  117. function get-credentials {
  118. curl "${GCE_METADATA_INTERNAL}/service-accounts/default/token" -H "Metadata-Flavor: Google" -s | python -c \
  119. 'import sys; import json; print(json.loads(sys.stdin.read())["access_token"])'
  120. }
  121. function valid-storage-scope {
  122. curl "${GCE_METADATA_INTERNAL}/service-accounts/default/scopes" -H "Metadata-Flavor: Google" -s | grep -E "auth/devstorage|auth/cloud-platform"
  123. }
  124. if [[ -n "${KUBERNETES_SKIP_DOWNLOAD-}" ]]; then
  125. create_cluster
  126. exit 0
  127. fi
  128. if [[ -d "./kubernetes" ]]; then
  129. if [[ -z "${KUBERNETES_SKIP_CONFIRM-}" ]]; then
  130. echo "'kubernetes' directory already exist. Should we skip download step and start to create cluster based on it? [Y]/n"
  131. read -r confirm
  132. if [[ ! "${confirm}" =~ ^[nN]$ ]]; then
  133. echo "Skipping download step."
  134. create_cluster
  135. exit 0
  136. fi
  137. fi
  138. fi
  139. # TODO: remove client checks once kubernetes.tar.gz no longer includes client
  140. # binaries by default.
  141. kernel=$(uname -s)
  142. case "${kernel}" in
  143. Darwin)
  144. ;;
  145. Linux)
  146. ;;
  147. *)
  148. echo "Unknown, unsupported platform: ${kernel}." >&2
  149. echo "Supported platforms: Linux, Darwin." >&2
  150. echo "Bailing out." >&2
  151. exit 2
  152. esac
  153. machine=$(uname -m)
  154. case "${machine}" in
  155. x86_64*|i?86_64*|amd64*)
  156. ;;
  157. aarch64*|arm64*)
  158. ;;
  159. arm*)
  160. ;;
  161. i?86*)
  162. ;;
  163. *)
  164. echo "Unknown, unsupported architecture (${machine})." >&2
  165. echo "Supported architectures x86_64, i686, arm, arm64." >&2
  166. echo "Bailing out." >&2
  167. exit 3
  168. ;;
  169. esac
  170. file=kubernetes.tar.gz
  171. release=${KUBERNETES_RELEASE:-"release/stable"}
  172. # Validate Kubernetes release version.
  173. # Translate a published version <bucket>/<version> (e.g. "release/stable") to version number.
  174. set_binary_version "${release}"
  175. if [[ -z "${KUBERNETES_SKIP_RELEASE_VALIDATION-}" ]]; then
  176. if [[ ${KUBE_VERSION} =~ ${KUBE_CI_VERSION_REGEX} ]]; then
  177. # Override KUBERNETES_RELEASE_URL to point to the CI bucket;
  178. # this will be used by get-kube-binaries.sh.
  179. KUBERNETES_RELEASE_URL="${KUBERNETES_CI_RELEASE_URL}"
  180. elif ! [[ ${KUBE_VERSION} =~ ${KUBE_RELEASE_VERSION_REGEX} ]]; then
  181. echo "Version doesn't match regexp" >&2
  182. exit 1
  183. fi
  184. fi
  185. kubernetes_tar_url="${KUBERNETES_RELEASE_URL}/${KUBE_VERSION}/${file}"
  186. need_download=true
  187. if [[ -r "${PWD}/${file}" ]]; then
  188. downloaded_version=$(tar -xzOf "${PWD}/${file}" kubernetes/version 2>/dev/null || true)
  189. echo "Found preexisting ${file}, release ${downloaded_version}"
  190. if [[ "${downloaded_version}" == "${KUBE_VERSION}" ]]; then
  191. echo "Using preexisting kubernetes.tar.gz"
  192. need_download=false
  193. fi
  194. fi
  195. if "${need_download}"; then
  196. echo "Downloading kubernetes release ${KUBE_VERSION}"
  197. echo " from ${kubernetes_tar_url}"
  198. echo " to ${PWD}/${file}"
  199. fi
  200. if [[ -e "${PWD}/kubernetes" ]]; then
  201. # Let's try not to accidentally nuke something that isn't a kubernetes
  202. # release dir.
  203. if [[ ! -f "${PWD}/kubernetes/version" ]]; then
  204. echo "${PWD}/kubernetes exists but does not look like a Kubernetes release."
  205. echo "Aborting!"
  206. exit 5
  207. fi
  208. echo "Will also delete preexisting 'kubernetes' directory."
  209. fi
  210. if [[ -z "${KUBERNETES_SKIP_CONFIRM-}" ]]; then
  211. echo "Is this ok? [Y]/n"
  212. read -r confirm
  213. if [[ "${confirm}" =~ ^[nN]$ ]]; then
  214. echo "Aborting."
  215. exit 0
  216. fi
  217. fi
  218. if "${need_download}"; then
  219. if [[ $(which curl) ]]; then
  220. # if the url belongs to GCS API we should use oauth2_token in the headers
  221. curl_headers=""
  222. if { [[ "${KUBERNETES_PROVIDER:-gce}" == "gce" ]] || [[ "${KUBERNETES_PROVIDER}" == "gke" ]] ; } &&
  223. [[ "$kubernetes_tar_url" =~ ^https://storage.googleapis.com.* ]] && valid-storage-scope ; then
  224. curl_headers="Authorization: Bearer $(get-credentials)"
  225. fi
  226. curl ${curl_headers:+-H "${curl_headers}"} -fL --retry 3 --keepalive-time 2 "${kubernetes_tar_url}" -o "${file}"
  227. elif [[ $(which wget) ]]; then
  228. wget "${kubernetes_tar_url}"
  229. else
  230. echo "Couldn't find curl or wget. Bailing out."
  231. exit 1
  232. fi
  233. fi
  234. echo "Unpacking kubernetes release ${KUBE_VERSION}"
  235. rm -rf "${PWD}/kubernetes"
  236. tar -xzf ${file}
  237. download_kube_binaries
  238. create_cluster