get-kube.sh 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. function valid-storage-scope {
  116. curl "${GCE_METADATA_INTERNAL}/service-accounts/default/scopes" -H "Metadata-Flavor: Google" -s | grep -E "auth/devstorage|auth/cloud-platform"
  117. }
  118. if [[ -n "${KUBERNETES_SKIP_DOWNLOAD-}" ]]; then
  119. create_cluster
  120. exit 0
  121. fi
  122. if [[ -d "./kubernetes" ]]; then
  123. if [[ -z "${KUBERNETES_SKIP_CONFIRM-}" ]]; then
  124. echo "'kubernetes' directory already exist. Should we skip download step and start to create cluster based on it? [Y]/n"
  125. read -r confirm
  126. if [[ ! "${confirm}" =~ ^[nN]$ ]]; then
  127. echo "Skipping download step."
  128. create_cluster
  129. exit 0
  130. fi
  131. fi
  132. fi
  133. # TODO: remove client checks once kubernetes.tar.gz no longer includes client
  134. # binaries by default.
  135. kernel=$(uname -s)
  136. case "${kernel}" in
  137. Darwin)
  138. ;;
  139. Linux)
  140. ;;
  141. *)
  142. echo "Unknown, unsupported platform: ${kernel}." >&2
  143. echo "Supported platforms: Linux, Darwin." >&2
  144. echo "Bailing out." >&2
  145. exit 2
  146. esac
  147. machine=$(uname -m)
  148. case "${machine}" in
  149. x86_64*|i?86_64*|amd64*)
  150. ;;
  151. aarch64*|arm64*)
  152. ;;
  153. arm*)
  154. ;;
  155. i?86*)
  156. ;;
  157. *)
  158. echo "Unknown, unsupported architecture (${machine})." >&2
  159. echo "Supported architectures x86_64, i686, arm, arm64." >&2
  160. echo "Bailing out." >&2
  161. exit 3
  162. ;;
  163. esac
  164. file=kubernetes.tar.gz
  165. release=${KUBERNETES_RELEASE:-"release/stable"}
  166. # Validate Kubernetes release version.
  167. # Translate a published version <bucket>/<version> (e.g. "release/stable") to version number.
  168. set_binary_version "${release}"
  169. if [[ -z "${KUBERNETES_SKIP_RELEASE_VALIDATION-}" ]]; then
  170. if [[ ${KUBE_VERSION} =~ ${KUBE_CI_VERSION_REGEX} ]]; then
  171. # Override KUBERNETES_RELEASE_URL to point to the CI bucket;
  172. # this will be used by get-kube-binaries.sh.
  173. KUBERNETES_RELEASE_URL="${KUBERNETES_CI_RELEASE_URL}"
  174. elif ! [[ ${KUBE_VERSION} =~ ${KUBE_RELEASE_VERSION_REGEX} ]]; then
  175. echo "Version doesn't match regexp" >&2
  176. exit 1
  177. fi
  178. fi
  179. kubernetes_tar_url="${KUBERNETES_RELEASE_URL}/${KUBE_VERSION}/${file}"
  180. need_download=true
  181. if [[ -r "${PWD}/${file}" ]]; then
  182. downloaded_version=$(tar -xzOf "${PWD}/${file}" kubernetes/version 2>/dev/null || true)
  183. echo "Found preexisting ${file}, release ${downloaded_version}"
  184. if [[ "${downloaded_version}" == "${KUBE_VERSION}" ]]; then
  185. echo "Using preexisting kubernetes.tar.gz"
  186. need_download=false
  187. fi
  188. fi
  189. if "${need_download}"; then
  190. echo "Downloading kubernetes release ${KUBE_VERSION}"
  191. echo " from ${kubernetes_tar_url}"
  192. echo " to ${PWD}/${file}"
  193. fi
  194. if [[ -e "${PWD}/kubernetes" ]]; then
  195. # Let's try not to accidentally nuke something that isn't a kubernetes
  196. # release dir.
  197. if [[ ! -f "${PWD}/kubernetes/version" ]]; then
  198. echo "${PWD}/kubernetes exists but does not look like a Kubernetes release."
  199. echo "Aborting!"
  200. exit 5
  201. fi
  202. echo "Will also delete preexisting 'kubernetes' directory."
  203. fi
  204. if [[ -z "${KUBERNETES_SKIP_CONFIRM-}" ]]; then
  205. echo "Is this ok? [Y]/n"
  206. read -r confirm
  207. if [[ "${confirm}" =~ ^[nN]$ ]]; then
  208. echo "Aborting."
  209. exit 0
  210. fi
  211. fi
  212. if "${need_download}"; then
  213. if [[ $(which curl) ]]; then
  214. # if the url belongs to GCS API we should use oauth2_token in the headers
  215. curl_headers=""
  216. if { [[ "${KUBERNETES_PROVIDER:-gce}" == "gce" ]] || [[ "${KUBERNETES_PROVIDER}" == "gke" ]] ; } &&
  217. [[ "$kubernetes_tar_url" =~ ^https://storage.googleapis.com.* ]] ; then
  218. curl_headers="Authorization: Bearer $(gcloud auth print-access-token)"
  219. fi
  220. curl ${curl_headers:+-H "${curl_headers}"} -fL --retry 3 --keepalive-time 2 "${kubernetes_tar_url}" -o "${file}"
  221. elif [[ $(which wget) ]]; then
  222. wget "${kubernetes_tar_url}"
  223. else
  224. echo "Couldn't find curl or wget. Bailing out."
  225. exit 1
  226. fi
  227. fi
  228. echo "Unpacking kubernetes release ${KUBE_VERSION}"
  229. rm -rf "${PWD}/kubernetes"
  230. tar -xzf ${file}
  231. download_kube_binaries
  232. create_cluster