common.sh 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. #!/bin/bash
  2. # Copyright 2015 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. # Common utilites for kube-up/kube-down
  16. set -o errexit
  17. set -o nounset
  18. set -o pipefail
  19. KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
  20. DEFAULT_KUBECONFIG="${HOME}/.kube/config"
  21. source "${KUBE_ROOT}/cluster/lib/util.sh"
  22. source "${KUBE_ROOT}/cluster/lib/logging.sh"
  23. # KUBE_RELEASE_VERSION_REGEX matches things like "v1.2.3" or "v1.2.3-alpha.4"
  24. #
  25. # NOTE This must match the version_regex in build/common.sh
  26. # kube::release::parse_and_validate_release_version()
  27. KUBE_RELEASE_VERSION_REGEX="^v(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(-(beta|alpha)\\.(0|[1-9][0-9]*))?$"
  28. KUBE_RELEASE_VERSION_DASHED_REGEX="v(0|[1-9][0-9]*)-(0|[1-9][0-9]*)-(0|[1-9][0-9]*)(-(beta|alpha)-(0|[1-9][0-9]*))?"
  29. # KUBE_CI_VERSION_REGEX matches things like "v1.2.3-alpha.4.56+abcdefg" This
  30. #
  31. # NOTE This must match the version_regex in build/common.sh
  32. # kube::release::parse_and_validate_ci_version()
  33. KUBE_CI_VERSION_REGEX="^v(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)-(beta|alpha)\\.(0|[1-9][0-9]*)(\\.(0|[1-9][0-9]*)\\+[-0-9a-z]*)?$"
  34. KUBE_CI_VERSION_DASHED_REGEX="^v(0|[1-9][0-9]*)-(0|[1-9][0-9]*)-(0|[1-9][0-9]*)-(beta|alpha)-(0|[1-9][0-9]*)(-(0|[1-9][0-9]*)\\+[-0-9a-z]*)?"
  35. # Generate kubeconfig data for the created cluster.
  36. # Assumed vars:
  37. # KUBE_USER
  38. # KUBE_PASSWORD
  39. # KUBE_MASTER_IP
  40. # KUBECONFIG
  41. # CONTEXT
  42. #
  43. # If the apiserver supports bearer auth, also provide:
  44. # KUBE_BEARER_TOKEN
  45. #
  46. # If the kubeconfig context being created should NOT be set as the current context
  47. # SECONDARY_KUBECONFIG=true
  48. #
  49. # To explicitly name the context being created, use OVERRIDE_CONTEXT
  50. #
  51. # The following can be omitted for --insecure-skip-tls-verify
  52. # KUBE_CERT
  53. # KUBE_KEY
  54. # CA_CERT
  55. function create-kubeconfig() {
  56. KUBECONFIG=${KUBECONFIG:-$DEFAULT_KUBECONFIG}
  57. local kubectl="${KUBE_ROOT}/cluster/kubectl.sh"
  58. SECONDARY_KUBECONFIG=${SECONDARY_KUBECONFIG:-}
  59. OVERRIDE_CONTEXT=${OVERRIDE_CONTEXT:-}
  60. if [[ "$OVERRIDE_CONTEXT" != "" ]];then
  61. CONTEXT=$OVERRIDE_CONTEXT
  62. fi
  63. # KUBECONFIG determines the file we write to, but it may not exist yet
  64. if [[ ! -e "${KUBECONFIG}" ]]; then
  65. mkdir -p $(dirname "${KUBECONFIG}")
  66. touch "${KUBECONFIG}"
  67. fi
  68. local cluster_args=(
  69. "--server=${KUBE_SERVER:-https://${KUBE_MASTER_IP}}"
  70. )
  71. if [[ -z "${CA_CERT:-}" ]]; then
  72. cluster_args+=("--insecure-skip-tls-verify=true")
  73. else
  74. cluster_args+=(
  75. "--certificate-authority=${CA_CERT}"
  76. "--embed-certs=true"
  77. )
  78. fi
  79. local user_args=()
  80. if [[ ! -z "${KUBE_BEARER_TOKEN:-}" ]]; then
  81. user_args+=(
  82. "--token=${KUBE_BEARER_TOKEN}"
  83. )
  84. elif [[ ! -z "${KUBE_USER:-}" && ! -z "${KUBE_PASSWORD:-}" ]]; then
  85. user_args+=(
  86. "--username=${KUBE_USER}"
  87. "--password=${KUBE_PASSWORD}"
  88. )
  89. fi
  90. if [[ ! -z "${KUBE_CERT:-}" && ! -z "${KUBE_KEY:-}" ]]; then
  91. user_args+=(
  92. "--client-certificate=${KUBE_CERT}"
  93. "--client-key=${KUBE_KEY}"
  94. "--embed-certs=true"
  95. )
  96. fi
  97. KUBECONFIG="${KUBECONFIG}" "${kubectl}" config set-cluster "${CONTEXT}" "${cluster_args[@]}"
  98. if [[ -n "${user_args[@]:-}" ]]; then
  99. KUBECONFIG="${KUBECONFIG}" "${kubectl}" config set-credentials "${CONTEXT}" "${user_args[@]}"
  100. fi
  101. KUBECONFIG="${KUBECONFIG}" "${kubectl}" config set-context "${CONTEXT}" --cluster="${CONTEXT}" --user="${CONTEXT}"
  102. if [[ "${SECONDARY_KUBECONFIG}" != "true" ]];then
  103. KUBECONFIG="${KUBECONFIG}" "${kubectl}" config use-context "${CONTEXT}" --cluster="${CONTEXT}"
  104. fi
  105. # If we have a bearer token, also create a credential entry with basic auth
  106. # so that it is easy to discover the basic auth password for your cluster
  107. # to use in a web browser.
  108. if [[ ! -z "${KUBE_BEARER_TOKEN:-}" && ! -z "${KUBE_USER:-}" && ! -z "${KUBE_PASSWORD:-}" ]]; then
  109. KUBECONFIG="${KUBECONFIG}" "${kubectl}" config set-credentials "${CONTEXT}-basic-auth" "--username=${KUBE_USER}" "--password=${KUBE_PASSWORD}"
  110. fi
  111. echo "Wrote config for ${CONTEXT} to ${KUBECONFIG}"
  112. }
  113. # Clear kubeconfig data for a context
  114. # Assumed vars:
  115. # KUBECONFIG
  116. # CONTEXT
  117. #
  118. # To explicitly name the context being removed, use OVERRIDE_CONTEXT
  119. function clear-kubeconfig() {
  120. export KUBECONFIG=${KUBECONFIG:-$DEFAULT_KUBECONFIG}
  121. OVERRIDE_CONTEXT=${OVERRIDE_CONTEXT:-}
  122. if [[ "$OVERRIDE_CONTEXT" != "" ]];then
  123. CONTEXT=$OVERRIDE_CONTEXT
  124. fi
  125. local kubectl="${KUBE_ROOT}/cluster/kubectl.sh"
  126. "${kubectl}" config unset "clusters.${CONTEXT}"
  127. "${kubectl}" config unset "users.${CONTEXT}"
  128. "${kubectl}" config unset "users.${CONTEXT}-basic-auth"
  129. "${kubectl}" config unset "contexts.${CONTEXT}"
  130. local cc=$("${kubectl}" config view -o jsonpath='{.current-context}')
  131. if [[ "${cc}" == "${CONTEXT}" ]]; then
  132. "${kubectl}" config unset current-context
  133. fi
  134. echo "Cleared config for ${CONTEXT} from ${KUBECONFIG}"
  135. }
  136. # Creates a kubeconfig file with the credentials for only the current-context
  137. # cluster. This is used by federation to create secrets in test setup.
  138. function create-kubeconfig-for-federation() {
  139. if [[ "${FEDERATION:-}" == "true" ]]; then
  140. echo "creating kubeconfig for federation secret"
  141. local kubectl="${KUBE_ROOT}/cluster/kubectl.sh"
  142. local cc=$("${kubectl}" config view -o jsonpath='{.current-context}')
  143. KUBECONFIG_DIR=$(dirname ${KUBECONFIG:-$DEFAULT_KUBECONFIG})
  144. KUBECONFIG_PATH="${KUBECONFIG_DIR}/federation/kubernetes-apiserver/${cc}"
  145. mkdir -p "${KUBECONFIG_PATH}"
  146. "${kubectl}" config view --minify --flatten > "${KUBECONFIG_PATH}/kubeconfig"
  147. fi
  148. }
  149. function tear_down_alive_resources() {
  150. local kubectl="${KUBE_ROOT}/cluster/kubectl.sh"
  151. "${kubectl}" delete rc --all || true
  152. "${kubectl}" delete pods --all || true
  153. "${kubectl}" delete svc --all || true
  154. "${kubectl}" delete pvc --all || true
  155. }
  156. # Gets username, password for the current-context in kubeconfig, if they exist.
  157. # Assumed vars:
  158. # KUBECONFIG # if unset, defaults to global
  159. # KUBE_CONTEXT # if unset, defaults to current-context
  160. #
  161. # Vars set:
  162. # KUBE_USER
  163. # KUBE_PASSWORD
  164. #
  165. # KUBE_USER,KUBE_PASSWORD will be empty if no current-context is set, or
  166. # the current-context user does not exist or contain basicauth entries.
  167. function get-kubeconfig-basicauth() {
  168. export KUBECONFIG=${KUBECONFIG:-$DEFAULT_KUBECONFIG}
  169. local cc=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o jsonpath="{.current-context}")
  170. if [[ ! -z "${KUBE_CONTEXT:-}" ]]; then
  171. cc="${KUBE_CONTEXT}"
  172. fi
  173. local user=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o jsonpath="{.contexts[?(@.name == \"${cc}\")].context.user}")
  174. KUBE_USER=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o jsonpath="{.users[?(@.name == \"${user}\")].user.username}")
  175. KUBE_PASSWORD=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o jsonpath="{.users[?(@.name == \"${user}\")].user.password}")
  176. }
  177. # Generate basic auth user and password.
  178. # Vars set:
  179. # KUBE_USER
  180. # KUBE_PASSWORD
  181. function gen-kube-basicauth() {
  182. KUBE_USER=admin
  183. KUBE_PASSWORD=$(python -c 'import string,random; print("".join(random.SystemRandom().choice(string.ascii_letters + string.digits) for _ in range(16)))')
  184. }
  185. # Get the bearer token for the current-context in kubeconfig if one exists.
  186. # Assumed vars:
  187. # KUBECONFIG # if unset, defaults to global
  188. # KUBE_CONTEXT # if unset, defaults to current-context
  189. #
  190. # Vars set:
  191. # KUBE_BEARER_TOKEN
  192. #
  193. # KUBE_BEARER_TOKEN will be empty if no current-context is set, or the
  194. # current-context user does not exist or contain a bearer token entry.
  195. function get-kubeconfig-bearertoken() {
  196. export KUBECONFIG=${KUBECONFIG:-$DEFAULT_KUBECONFIG}
  197. local cc=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o jsonpath="{.current-context}")
  198. if [[ ! -z "${KUBE_CONTEXT:-}" ]]; then
  199. cc="${KUBE_CONTEXT}"
  200. fi
  201. local user=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o jsonpath="{.contexts[?(@.name == \"${cc}\")].context.user}")
  202. KUBE_BEARER_TOKEN=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o jsonpath="{.users[?(@.name == \"${user}\")].user.token}")
  203. }
  204. # Generate bearer token.
  205. #
  206. # Vars set:
  207. # KUBE_BEARER_TOKEN
  208. function gen-kube-bearertoken() {
  209. KUBE_BEARER_TOKEN=$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64 | tr -d "=+/" | dd bs=32 count=1 2>/dev/null)
  210. }
  211. # Generate uid
  212. # This function only works on systems with python. It generates a time based
  213. # UID instead of a UUID because GCE has a name length limit.
  214. #
  215. # Vars set:
  216. # KUBE_UID
  217. function gen-uid {
  218. KUBE_UID=$(python -c 'import uuid; print(uuid.uuid1().fields[0])')
  219. }
  220. function load-or-gen-kube-basicauth() {
  221. if [[ ! -z "${KUBE_CONTEXT:-}" ]]; then
  222. get-kubeconfig-basicauth
  223. fi
  224. if [[ -z "${KUBE_USER:-}" || -z "${KUBE_PASSWORD:-}" ]]; then
  225. gen-kube-basicauth
  226. fi
  227. # Make sure they don't contain any funny characters.
  228. if ! [[ "${KUBE_USER}" =~ ^[-._@a-zA-Z0-9]+$ ]]; then
  229. echo "Bad KUBE_USER string."
  230. exit 1
  231. fi
  232. if ! [[ "${KUBE_PASSWORD}" =~ ^[-._@#%/a-zA-Z0-9]+$ ]]; then
  233. echo "Bad KUBE_PASSWORD string."
  234. exit 1
  235. fi
  236. }
  237. function load-or-gen-kube-bearertoken() {
  238. if [[ ! -z "${KUBE_CONTEXT:-}" ]]; then
  239. get-kubeconfig-bearertoken
  240. fi
  241. if [[ -z "${KUBE_BEARER_TOKEN:-}" ]]; then
  242. gen-kube-bearertoken
  243. fi
  244. }
  245. # Get the master IP for the current-context in kubeconfig if one exists.
  246. #
  247. # Assumed vars:
  248. # KUBECONFIG # if unset, defaults to global
  249. # KUBE_CONTEXT # if unset, defaults to current-context
  250. #
  251. # Vars set:
  252. # KUBE_MASTER_URL
  253. #
  254. # KUBE_MASTER_URL will be empty if no current-context is set, or the
  255. # current-context user does not exist or contain a server entry.
  256. function detect-master-from-kubeconfig() {
  257. export KUBECONFIG=${KUBECONFIG:-$DEFAULT_KUBECONFIG}
  258. local cc=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o jsonpath="{.current-context}")
  259. if [[ ! -z "${KUBE_CONTEXT:-}" ]]; then
  260. cc="${KUBE_CONTEXT}"
  261. fi
  262. local cluster=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o jsonpath="{.contexts[?(@.name == \"${cc}\")].context.cluster}")
  263. KUBE_MASTER_URL=$("${KUBE_ROOT}/cluster/kubectl.sh" config view -o jsonpath="{.clusters[?(@.name == \"${cluster}\")].cluster.server}")
  264. }
  265. # Sets KUBE_VERSION variable to the proper version number (e.g. "v1.0.6",
  266. # "v1.2.0-alpha.1.881+376438b69c7612") or a version' publication of the form
  267. # <path>/<version> (e.g. "release/stable",' "ci/latest-1").
  268. #
  269. # See the docs on getting builds for more information about version
  270. # publication.
  271. #
  272. # Args:
  273. # $1 version string from command line
  274. # Vars set:
  275. # KUBE_VERSION
  276. function set_binary_version() {
  277. if [[ "${1}" =~ "/" ]]; then
  278. IFS='/' read -a path <<< "${1}"
  279. if [[ "${path[0]}" == "release" ]]; then
  280. KUBE_VERSION=$(gsutil cat "gs://kubernetes-release/${1}.txt")
  281. else
  282. KUBE_VERSION=$(gsutil cat "gs://kubernetes-release-dev/${1}.txt")
  283. fi
  284. else
  285. KUBE_VERSION=${1}
  286. fi
  287. }
  288. # Figure out which binary use on the server and assure it is available.
  289. # If KUBE_VERSION is specified use binaries specified by it, otherwise
  290. # use local dev binaries.
  291. #
  292. # Assumed vars:
  293. # KUBE_VERSION
  294. # KUBE_RELEASE_VERSION_REGEX
  295. # KUBE_CI_VERSION_REGEX
  296. # Vars set:
  297. # KUBE_TAR_HASH
  298. # SERVER_BINARY_TAR_URL
  299. # SERVER_BINARY_TAR_HASH
  300. # SALT_TAR_URL
  301. # SALT_TAR_HASH
  302. function tars_from_version() {
  303. if [[ -z "${KUBE_VERSION-}" ]]; then
  304. find-release-tars
  305. upload-server-tars
  306. elif [[ ${KUBE_VERSION} =~ ${KUBE_RELEASE_VERSION_REGEX} ]]; then
  307. SERVER_BINARY_TAR_URL="https://storage.googleapis.com/kubernetes-release/release/${KUBE_VERSION}/kubernetes-server-linux-amd64.tar.gz"
  308. SALT_TAR_URL="https://storage.googleapis.com/kubernetes-release/release/${KUBE_VERSION}/kubernetes-salt.tar.gz"
  309. # TODO: Clean this up.
  310. KUBE_MANIFESTS_TAR_URL="${SERVER_BINARY_TAR_URL/server-linux-amd64/manifests}"
  311. KUBE_MANIFESTS_TAR_HASH=$(curl ${KUBE_MANIFESTS_TAR_URL} | sha1sum | awk '{print $1}')
  312. elif [[ ${KUBE_VERSION} =~ ${KUBE_CI_VERSION_REGEX} ]]; then
  313. SERVER_BINARY_TAR_URL="https://storage.googleapis.com/kubernetes-release-dev/ci/${KUBE_VERSION}/kubernetes-server-linux-amd64.tar.gz"
  314. SALT_TAR_URL="https://storage.googleapis.com/kubernetes-release-dev/ci/${KUBE_VERSION}/kubernetes-salt.tar.gz"
  315. # TODO: Clean this up.
  316. KUBE_MANIFESTS_TAR_URL="${SERVER_BINARY_TAR_URL/server-linux-amd64/manifests}"
  317. KUBE_MANIFESTS_TAR_HASH=$(curl ${KUBE_MANIFESTS_TAR_URL} | sha1sum | awk '{print $1}')
  318. else
  319. echo "Version doesn't match regexp" >&2
  320. exit 1
  321. fi
  322. if ! SERVER_BINARY_TAR_HASH=$(curl -Ss --fail "${SERVER_BINARY_TAR_URL}.sha1"); then
  323. echo "Failure trying to curl release .sha1"
  324. fi
  325. if ! SALT_TAR_HASH=$(curl -Ss --fail "${SALT_TAR_URL}.sha1"); then
  326. echo "Failure trying to curl Salt tar .sha1"
  327. fi
  328. if ! curl -Ss --head "${SERVER_BINARY_TAR_URL}" >&/dev/null; then
  329. echo "Can't find release at ${SERVER_BINARY_TAR_URL}" >&2
  330. exit 1
  331. fi
  332. if ! curl -Ss --head "${SALT_TAR_URL}" >&/dev/null; then
  333. echo "Can't find Salt tar at ${SALT_TAR_URL}" >&2
  334. exit 1
  335. fi
  336. }
  337. # Verify and find the various tar files that we are going to use on the server.
  338. #
  339. # Assumed vars:
  340. # KUBE_ROOT
  341. # Vars set:
  342. # SERVER_BINARY_TAR
  343. # SALT_TAR
  344. # KUBE_MANIFESTS_TAR
  345. function find-release-tars() {
  346. SERVER_BINARY_TAR="${KUBE_ROOT}/server/kubernetes-server-linux-amd64.tar.gz"
  347. if [[ ! -f "${SERVER_BINARY_TAR}" ]]; then
  348. SERVER_BINARY_TAR="${KUBE_ROOT}/_output/release-tars/kubernetes-server-linux-amd64.tar.gz"
  349. fi
  350. if [[ ! -f "${SERVER_BINARY_TAR}" ]]; then
  351. echo "!!! Cannot find kubernetes-server-linux-amd64.tar.gz" >&2
  352. exit 1
  353. fi
  354. SALT_TAR="${KUBE_ROOT}/server/kubernetes-salt.tar.gz"
  355. if [[ ! -f "${SALT_TAR}" ]]; then
  356. SALT_TAR="${KUBE_ROOT}/_output/release-tars/kubernetes-salt.tar.gz"
  357. fi
  358. if [[ ! -f "${SALT_TAR}" ]]; then
  359. echo "!!! Cannot find kubernetes-salt.tar.gz" >&2
  360. exit 1
  361. fi
  362. # This tarball is used by GCI, Ubuntu Trusty, and CoreOS.
  363. KUBE_MANIFESTS_TAR=
  364. if [[ "${MASTER_OS_DISTRIBUTION:-}" == "trusty" || "${MASTER_OS_DISTRIBUTION:-}" == "gci" || "${MASTER_OS_DISTRIBUTION:-}" == "coreos" ]] || \
  365. [[ "${NODE_OS_DISTRIBUTION:-}" == "trusty" || "${NODE_OS_DISTRIBUTION:-}" == "gci" || "${NODE_OS_DISTRIBUTION:-}" == "coreos" ]] ; then
  366. KUBE_MANIFESTS_TAR="${KUBE_ROOT}/server/kubernetes-manifests.tar.gz"
  367. if [[ ! -f "${KUBE_MANIFESTS_TAR}" ]]; then
  368. KUBE_MANIFESTS_TAR="${KUBE_ROOT}/_output/release-tars/kubernetes-manifests.tar.gz"
  369. fi
  370. if [[ ! -f "${KUBE_MANIFESTS_TAR}" ]]; then
  371. echo "!!! Cannot find kubernetes-manifests.tar.gz" >&2
  372. exit 1
  373. fi
  374. fi
  375. }
  376. # Discover the git version of the current build package
  377. #
  378. # Assumed vars:
  379. # KUBE_ROOT
  380. # Vars set:
  381. # KUBE_GIT_VERSION
  382. function find-release-version() {
  383. KUBE_GIT_VERSION=""
  384. if [[ -f "${KUBE_ROOT}/version" ]]; then
  385. KUBE_GIT_VERSION="$(cat ${KUBE_ROOT}/version)"
  386. fi
  387. if [[ -f "${KUBE_ROOT}/_output/release-stage/full/kubernetes/version" ]]; then
  388. KUBE_GIT_VERSION="$(cat ${KUBE_ROOT}/_output/release-stage/full/kubernetes/version)"
  389. fi
  390. if [[ -z "${KUBE_GIT_VERSION}" ]]; then
  391. echo "!!! Cannot find release version"
  392. exit 1
  393. fi
  394. }
  395. function stage-images() {
  396. find-release-version
  397. find-release-tars
  398. KUBE_IMAGE_TAG="$(echo """${KUBE_GIT_VERSION}""" | sed 's/+/-/g')"
  399. local docker_wrapped_binaries=(
  400. "kube-apiserver"
  401. "kube-controller-manager"
  402. "kube-scheduler"
  403. "kube-proxy"
  404. )
  405. local docker_cmd=("docker")
  406. if [[ "${KUBE_DOCKER_REGISTRY}" == "gcr.io/"* ]]; then
  407. local docker_push_cmd=("gcloud" "docker")
  408. else
  409. local docker_push_cmd=("${docker_cmd[@]}")
  410. fi
  411. local temp_dir="$(mktemp -d -t 'kube-server-XXXX')"
  412. tar xzfv "${SERVER_BINARY_TAR}" -C "${temp_dir}" &> /dev/null
  413. for binary in "${docker_wrapped_binaries[@]}"; do
  414. local docker_tag="$(cat ${temp_dir}/kubernetes/server/bin/${binary}.docker_tag)"
  415. (
  416. "${docker_cmd[@]}" load -i "${temp_dir}/kubernetes/server/bin/${binary}.tar"
  417. "${docker_cmd[@]}" tag -f "gcr.io/google_containers/${binary}:${docker_tag}" "${KUBE_DOCKER_REGISTRY}/${binary}:${KUBE_IMAGE_TAG}"
  418. "${docker_push_cmd[@]}" push "${KUBE_DOCKER_REGISTRY}/${binary}:${KUBE_IMAGE_TAG}"
  419. ) &> "${temp_dir}/${binary}-push.log" &
  420. done
  421. kube::util::wait-for-jobs || {
  422. kube::log::error "unable to push images. see ${temp_dir}/*.log for more info."
  423. return 1
  424. }
  425. rm -rf "${temp_dir}"
  426. return 0
  427. }
  428. # Quote something appropriate for a yaml string.
  429. #
  430. # TODO(zmerlynn): Note that this function doesn't so much "quote" as
  431. # "strip out quotes", and we really should be using a YAML library for
  432. # this, but PyYAML isn't shipped by default, and *rant rant rant ... SIGH*
  433. function yaml-quote {
  434. echo "'$(echo "${@:-}" | sed -e "s/'/''/g")'"
  435. }
  436. # Builds the RUNTIME_CONFIG var from other feature enable options (such as
  437. # features in alpha)
  438. function build-runtime-config() {
  439. # There is nothing to do here for now. Just using this function as a placeholder.
  440. :
  441. }
  442. # Writes the cluster name into a temporary file.
  443. # Assumed vars
  444. # CLUSTER_NAME
  445. function write-cluster-name {
  446. cat >"${KUBE_TEMP}/cluster-name.txt" << EOF
  447. ${CLUSTER_NAME}
  448. EOF
  449. }
  450. function write-master-env {
  451. # If the user requested that the master be part of the cluster, set the
  452. # environment variable to program the master kubelet to register itself.
  453. if [[ "${REGISTER_MASTER_KUBELET:-}" == "true" ]]; then
  454. KUBELET_APISERVER="${MASTER_NAME}"
  455. fi
  456. build-kube-env true "${KUBE_TEMP}/master-kube-env.yaml"
  457. }
  458. function write-node-env {
  459. build-kube-env false "${KUBE_TEMP}/node-kube-env.yaml"
  460. }
  461. # $1: if 'true', we're building a master yaml, else a node
  462. function build-kube-env {
  463. local master=$1
  464. local file=$2
  465. local server_binary_tar_url=$SERVER_BINARY_TAR_URL
  466. local salt_tar_url=$SALT_TAR_URL
  467. local kube_manifests_tar_url="${KUBE_MANIFESTS_TAR_URL:-}"
  468. if [[ "${master}" == "true" && "${MASTER_OS_DISTRIBUTION}" == "coreos" ]] || \
  469. [[ "${master}" == "false" && "${NODE_OS_DISTRIBUTION}" == "coreos" ]] ; then
  470. # TODO: Support fallback .tar.gz settings on CoreOS
  471. server_binary_tar_url=$(split_csv "${SERVER_BINARY_TAR_URL}")
  472. salt_tar_url=$(split_csv "${SALT_TAR_URL}")
  473. kube_manifests_tar_url=$(split_csv "${KUBE_MANIFESTS_TAR_URL}")
  474. fi
  475. build-runtime-config
  476. gen-uid
  477. rm -f ${file}
  478. cat >$file <<EOF
  479. ENV_TIMESTAMP: $(yaml-quote $(date -u +%Y-%m-%dT%T%z))
  480. INSTANCE_PREFIX: $(yaml-quote ${INSTANCE_PREFIX})
  481. NODE_INSTANCE_PREFIX: $(yaml-quote ${NODE_INSTANCE_PREFIX})
  482. NODE_TAGS: $(yaml-quote ${NODE_TAGS:-})
  483. CLUSTER_IP_RANGE: $(yaml-quote ${CLUSTER_IP_RANGE:-10.244.0.0/16})
  484. SERVER_BINARY_TAR_URL: $(yaml-quote ${server_binary_tar_url})
  485. SERVER_BINARY_TAR_HASH: $(yaml-quote ${SERVER_BINARY_TAR_HASH})
  486. SALT_TAR_URL: $(yaml-quote ${salt_tar_url})
  487. SALT_TAR_HASH: $(yaml-quote ${SALT_TAR_HASH})
  488. SERVICE_CLUSTER_IP_RANGE: $(yaml-quote ${SERVICE_CLUSTER_IP_RANGE})
  489. KUBERNETES_MASTER_NAME: $(yaml-quote ${MASTER_NAME})
  490. ALLOCATE_NODE_CIDRS: $(yaml-quote ${ALLOCATE_NODE_CIDRS:-false})
  491. ENABLE_CLUSTER_MONITORING: $(yaml-quote ${ENABLE_CLUSTER_MONITORING:-none})
  492. DOCKER_REGISTRY_MIRROR_URL: $(yaml-quote ${DOCKER_REGISTRY_MIRROR_URL:-})
  493. ENABLE_L7_LOADBALANCING: $(yaml-quote ${ENABLE_L7_LOADBALANCING:-none})
  494. ENABLE_CLUSTER_LOGGING: $(yaml-quote ${ENABLE_CLUSTER_LOGGING:-false})
  495. ENABLE_CLUSTER_UI: $(yaml-quote ${ENABLE_CLUSTER_UI:-false})
  496. ENABLE_NODE_PROBLEM_DETECTOR: $(yaml-quote ${ENABLE_NODE_PROBLEM_DETECTOR:-false})
  497. ENABLE_NODE_LOGGING: $(yaml-quote ${ENABLE_NODE_LOGGING:-false})
  498. ENABLE_RESCHEDULER: $(yaml-quote ${ENABLE_RESCHEDULER:-false})
  499. LOGGING_DESTINATION: $(yaml-quote ${LOGGING_DESTINATION:-})
  500. ELASTICSEARCH_LOGGING_REPLICAS: $(yaml-quote ${ELASTICSEARCH_LOGGING_REPLICAS:-})
  501. ENABLE_CLUSTER_DNS: $(yaml-quote ${ENABLE_CLUSTER_DNS:-false})
  502. ENABLE_CLUSTER_REGISTRY: $(yaml-quote ${ENABLE_CLUSTER_REGISTRY:-false})
  503. CLUSTER_REGISTRY_DISK: $(yaml-quote ${CLUSTER_REGISTRY_DISK:-})
  504. CLUSTER_REGISTRY_DISK_SIZE: $(yaml-quote ${CLUSTER_REGISTRY_DISK_SIZE:-})
  505. DNS_REPLICAS: $(yaml-quote ${DNS_REPLICAS:-})
  506. DNS_SERVER_IP: $(yaml-quote ${DNS_SERVER_IP:-})
  507. DNS_DOMAIN: $(yaml-quote ${DNS_DOMAIN:-})
  508. KUBELET_TOKEN: $(yaml-quote ${KUBELET_TOKEN:-})
  509. KUBE_PROXY_TOKEN: $(yaml-quote ${KUBE_PROXY_TOKEN:-})
  510. ADMISSION_CONTROL: $(yaml-quote ${ADMISSION_CONTROL:-})
  511. MASTER_IP_RANGE: $(yaml-quote ${MASTER_IP_RANGE})
  512. RUNTIME_CONFIG: $(yaml-quote ${RUNTIME_CONFIG})
  513. CA_CERT: $(yaml-quote ${CA_CERT_BASE64:-})
  514. KUBELET_CERT: $(yaml-quote ${KUBELET_CERT_BASE64:-})
  515. KUBELET_KEY: $(yaml-quote ${KUBELET_KEY_BASE64:-})
  516. NETWORK_PROVIDER: $(yaml-quote ${NETWORK_PROVIDER:-})
  517. NETWORK_POLICY_PROVIDER: $(yaml-quote ${NETWORK_POLICY_PROVIDER:-})
  518. PREPULL_E2E_IMAGES: $(yaml-quote ${PREPULL_E2E_IMAGES:-})
  519. HAIRPIN_MODE: $(yaml-quote ${HAIRPIN_MODE:-})
  520. OPENCONTRAIL_TAG: $(yaml-quote ${OPENCONTRAIL_TAG:-})
  521. OPENCONTRAIL_KUBERNETES_TAG: $(yaml-quote ${OPENCONTRAIL_KUBERNETES_TAG:-})
  522. OPENCONTRAIL_PUBLIC_SUBNET: $(yaml-quote ${OPENCONTRAIL_PUBLIC_SUBNET:-})
  523. E2E_STORAGE_TEST_ENVIRONMENT: $(yaml-quote ${E2E_STORAGE_TEST_ENVIRONMENT:-})
  524. KUBE_IMAGE_TAG: $(yaml-quote ${KUBE_IMAGE_TAG:-})
  525. KUBE_DOCKER_REGISTRY: $(yaml-quote ${KUBE_DOCKER_REGISTRY:-})
  526. KUBE_ADDON_REGISTRY: $(yaml-quote ${KUBE_ADDON_REGISTRY:-})
  527. MULTIZONE: $(yaml-quote ${MULTIZONE:-})
  528. NON_MASQUERADE_CIDR: $(yaml-quote ${NON_MASQUERADE_CIDR:-})
  529. KUBE_UID: $(yaml-quote ${KUBE_UID:-})
  530. EOF
  531. if [ -n "${KUBELET_PORT:-}" ]; then
  532. cat >>$file <<EOF
  533. KUBELET_PORT: $(yaml-quote ${KUBELET_PORT})
  534. EOF
  535. fi
  536. if [ -n "${KUBE_APISERVER_REQUEST_TIMEOUT:-}" ]; then
  537. cat >>$file <<EOF
  538. KUBE_APISERVER_REQUEST_TIMEOUT: $(yaml-quote ${KUBE_APISERVER_REQUEST_TIMEOUT})
  539. EOF
  540. fi
  541. if [ -n "${TERMINATED_POD_GC_THRESHOLD:-}" ]; then
  542. cat >>$file <<EOF
  543. TERMINATED_POD_GC_THRESHOLD: $(yaml-quote ${TERMINATED_POD_GC_THRESHOLD})
  544. EOF
  545. fi
  546. if [[ "${master}" == "true" && ("${MASTER_OS_DISTRIBUTION}" == "trusty" || "${MASTER_OS_DISTRIBUTION}" == "gci" || "${MASTER_OS_DISTRIBUTION}" == "coreos") ]] || \
  547. [[ "${master}" == "false" && ("${NODE_OS_DISTRIBUTION}" == "trusty" || "${NODE_OS_DISTRIBUTION}" == "gci" || "${NODE_OS_DISTRIBUTION}" == "coreos") ]] ; then
  548. cat >>$file <<EOF
  549. KUBE_MANIFESTS_TAR_URL: $(yaml-quote ${kube_manifests_tar_url})
  550. KUBE_MANIFESTS_TAR_HASH: $(yaml-quote ${KUBE_MANIFESTS_TAR_HASH})
  551. EOF
  552. fi
  553. if [ -n "${TEST_CLUSTER:-}" ]; then
  554. cat >>$file <<EOF
  555. TEST_CLUSTER: $(yaml-quote ${TEST_CLUSTER})
  556. EOF
  557. fi
  558. if [ -n "${KUBELET_TEST_ARGS:-}" ]; then
  559. cat >>$file <<EOF
  560. KUBELET_TEST_ARGS: $(yaml-quote ${KUBELET_TEST_ARGS})
  561. EOF
  562. fi
  563. if [ -n "${KUBELET_TEST_LOG_LEVEL:-}" ]; then
  564. cat >>$file <<EOF
  565. KUBELET_TEST_LOG_LEVEL: $(yaml-quote ${KUBELET_TEST_LOG_LEVEL})
  566. EOF
  567. fi
  568. if [ -n "${DOCKER_TEST_LOG_LEVEL:-}" ]; then
  569. cat >>$file <<EOF
  570. DOCKER_TEST_LOG_LEVEL: $(yaml-quote ${DOCKER_TEST_LOG_LEVEL})
  571. EOF
  572. fi
  573. if [ -n "${ENABLE_CUSTOM_METRICS:-}" ]; then
  574. cat >>$file <<EOF
  575. ENABLE_CUSTOM_METRICS: $(yaml-quote ${ENABLE_CUSTOM_METRICS})
  576. EOF
  577. fi
  578. if [ -n "${FEATURE_GATES:-}" ]; then
  579. cat >>$file <<EOF
  580. FEATURE_GATES: $(yaml-quote ${FEATURE_GATES})
  581. EOF
  582. fi
  583. if [[ "${master}" == "true" ]]; then
  584. # Master-only env vars.
  585. cat >>$file <<EOF
  586. KUBERNETES_MASTER: $(yaml-quote "true")
  587. KUBE_USER: $(yaml-quote ${KUBE_USER})
  588. KUBE_PASSWORD: $(yaml-quote ${KUBE_PASSWORD})
  589. KUBE_BEARER_TOKEN: $(yaml-quote ${KUBE_BEARER_TOKEN})
  590. MASTER_CERT: $(yaml-quote ${MASTER_CERT_BASE64:-})
  591. MASTER_KEY: $(yaml-quote ${MASTER_KEY_BASE64:-})
  592. KUBECFG_CERT: $(yaml-quote ${KUBECFG_CERT_BASE64:-})
  593. KUBECFG_KEY: $(yaml-quote ${KUBECFG_KEY_BASE64:-})
  594. KUBELET_APISERVER: $(yaml-quote ${KUBELET_APISERVER:-})
  595. ENABLE_MANIFEST_URL: $(yaml-quote ${ENABLE_MANIFEST_URL:-false})
  596. MANIFEST_URL: $(yaml-quote ${MANIFEST_URL:-})
  597. MANIFEST_URL_HEADER: $(yaml-quote ${MANIFEST_URL_HEADER:-})
  598. NUM_NODES: $(yaml-quote ${NUM_NODES})
  599. STORAGE_BACKEND: $(yaml-quote ${STORAGE_BACKEND:-})
  600. ENABLE_GARBAGE_COLLECTOR: $(yaml-quote ${ENABLE_GARBAGE_COLLECTOR:-})
  601. EOF
  602. if [ -n "${TEST_ETCD_VERSION:-}" ]; then
  603. cat >>$file <<EOF
  604. TEST_ETCD_VERSION: $(yaml-quote ${TEST_ETCD_VERSION})
  605. EOF
  606. fi
  607. if [ -n "${APISERVER_TEST_ARGS:-}" ]; then
  608. cat >>$file <<EOF
  609. APISERVER_TEST_ARGS: $(yaml-quote ${APISERVER_TEST_ARGS})
  610. EOF
  611. fi
  612. if [ -n "${APISERVER_TEST_LOG_LEVEL:-}" ]; then
  613. cat >>$file <<EOF
  614. APISERVER_TEST_LOG_LEVEL: $(yaml-quote ${APISERVER_TEST_LOG_LEVEL})
  615. EOF
  616. fi
  617. if [ -n "${CONTROLLER_MANAGER_TEST_ARGS:-}" ]; then
  618. cat >>$file <<EOF
  619. CONTROLLER_MANAGER_TEST_ARGS: $(yaml-quote ${CONTROLLER_MANAGER_TEST_ARGS})
  620. EOF
  621. fi
  622. if [ -n "${CONTROLLER_MANAGER_TEST_LOG_LEVEL:-}" ]; then
  623. cat >>$file <<EOF
  624. CONTROLLER_MANAGER_TEST_LOG_LEVEL: $(yaml-quote ${CONTROLLER_MANAGER_TEST_LOG_LEVEL})
  625. EOF
  626. fi
  627. if [ -n "${SCHEDULER_TEST_ARGS:-}" ]; then
  628. cat >>$file <<EOF
  629. SCHEDULER_TEST_ARGS: $(yaml-quote ${SCHEDULER_TEST_ARGS})
  630. EOF
  631. fi
  632. if [ -n "${SCHEDULER_TEST_LOG_LEVEL:-}" ]; then
  633. cat >>$file <<EOF
  634. SCHEDULER_TEST_LOG_LEVEL: $(yaml-quote ${SCHEDULER_TEST_LOG_LEVEL})
  635. EOF
  636. fi
  637. if [ -n "${INITIAL_ETCD_CLUSTER:-}" ]; then
  638. cat >>$file <<EOF
  639. INITIAL_ETCD_CLUSTER: $(yaml-quote ${INITIAL_ETCD_CLUSTER})
  640. EOF
  641. fi
  642. else
  643. # Node-only env vars.
  644. cat >>$file <<EOF
  645. KUBERNETES_MASTER: $(yaml-quote "false")
  646. ZONE: $(yaml-quote ${ZONE})
  647. EXTRA_DOCKER_OPTS: $(yaml-quote ${EXTRA_DOCKER_OPTS:-})
  648. EOF
  649. if [ -n "${KUBEPROXY_TEST_ARGS:-}" ]; then
  650. cat >>$file <<EOF
  651. KUBEPROXY_TEST_ARGS: $(yaml-quote ${KUBEPROXY_TEST_ARGS})
  652. EOF
  653. fi
  654. if [ -n "${KUBEPROXY_TEST_LOG_LEVEL:-}" ]; then
  655. cat >>$file <<EOF
  656. KUBEPROXY_TEST_LOG_LEVEL: $(yaml-quote ${KUBEPROXY_TEST_LOG_LEVEL})
  657. EOF
  658. fi
  659. fi
  660. if [ -n "${NODE_LABELS:-}" ]; then
  661. cat >>$file <<EOF
  662. NODE_LABELS: $(yaml-quote ${NODE_LABELS})
  663. EOF
  664. fi
  665. if [ -n "${EVICTION_HARD:-}" ]; then
  666. cat >>$file <<EOF
  667. EVICTION_HARD: $(yaml-quote ${EVICTION_HARD})
  668. EOF
  669. fi
  670. if [[ "${master}" == "true" && "${MASTER_OS_DISTRIBUTION}" == "coreos" ]] || \
  671. [[ "${master}" == "false" && "${NODE_OS_DISTRIBUTION}" == "coreos" ]]; then
  672. # CoreOS-only env vars. TODO(yifan): Make them available on other distros.
  673. cat >>$file <<EOF
  674. KUBERNETES_CONTAINER_RUNTIME: $(yaml-quote ${CONTAINER_RUNTIME:-rkt})
  675. RKT_VERSION: $(yaml-quote ${RKT_VERSION:-})
  676. RKT_PATH: $(yaml-quote ${RKT_PATH:-})
  677. RKT_STAGE1_IMAGE: $(yaml-quote ${RKT_STAGE1_IMAGE:-})
  678. KUBERNETES_CONFIGURE_CBR0: $(yaml-quote ${KUBERNETES_CONFIGURE_CBR0:-true})
  679. EOF
  680. fi
  681. if [[ "${ENABLE_CLUSTER_AUTOSCALER}" == "true" ]]; then
  682. cat >>$file <<EOF
  683. ENABLE_CLUSTER_AUTOSCALER: $(yaml-quote ${ENABLE_CLUSTER_AUTOSCALER})
  684. AUTOSCALER_MIG_CONFIG: $(yaml-quote ${AUTOSCALER_MIG_CONFIG})
  685. EOF
  686. fi
  687. # Federation specific environment variables.
  688. if [[ -n "${FEDERATION:-}" ]]; then
  689. cat >>$file <<EOF
  690. FEDERATION: $(yaml-quote ${FEDERATION})
  691. EOF
  692. fi
  693. if [ -n "${FEDERATIONS_DOMAIN_MAP:-}" ]; then
  694. cat >>$file <<EOF
  695. FEDERATIONS_DOMAIN_MAP: $(yaml-quote ${FEDERATIONS_DOMAIN_MAP})
  696. EOF
  697. fi
  698. if [ -n "${FEDERATION_NAME:-}" ]; then
  699. cat >>$file <<EOF
  700. FEDERATION_NAME: $(yaml-quote ${FEDERATION_NAME})
  701. EOF
  702. fi
  703. if [ -n "${DNS_ZONE_NAME:-}" ]; then
  704. cat >>$file <<EOF
  705. DNS_ZONE_NAME: $(yaml-quote ${DNS_ZONE_NAME})
  706. EOF
  707. fi
  708. if [ -n "${SCHEDULING_ALGORITHM_PROVIDER:-}" ]; then
  709. cat >>$file <<EOF
  710. SCHEDULING_ALGORITHM_PROVIDER: $(yaml-quote ${SCHEDULING_ALGORITHM_PROVIDER})
  711. EOF
  712. fi
  713. }
  714. function sha1sum-file() {
  715. if which shasum >/dev/null 2>&1; then
  716. shasum -a1 "$1" | awk '{ print $1 }'
  717. else
  718. sha1sum "$1" | awk '{ print $1 }'
  719. fi
  720. }
  721. # Create certificate pairs for the cluster.
  722. # $1: The public IP for the master.
  723. #
  724. # These are used for static cert distribution (e.g. static clustering) at
  725. # cluster creation time. This will be obsoleted once we implement dynamic
  726. # clustering.
  727. #
  728. # The following certificate pairs are created:
  729. #
  730. # - ca (the cluster's certificate authority)
  731. # - server
  732. # - kubelet
  733. # - kubecfg (for kubectl)
  734. #
  735. # TODO(roberthbailey): Replace easyrsa with a simple Go program to generate
  736. # the certs that we need.
  737. #
  738. # Assumed vars
  739. # KUBE_TEMP
  740. # MASTER_NAME
  741. #
  742. # Vars set:
  743. # CERT_DIR
  744. # CA_CERT_BASE64
  745. # MASTER_CERT_BASE64
  746. # MASTER_KEY_BASE64
  747. # KUBELET_CERT_BASE64
  748. # KUBELET_KEY_BASE64
  749. # KUBECFG_CERT_BASE64
  750. # KUBECFG_KEY_BASE64
  751. function create-certs {
  752. local -r primary_cn="${1}"
  753. # Determine extra certificate names for master
  754. local octets=($(echo "${SERVICE_CLUSTER_IP_RANGE}" | sed -e 's|/.*||' -e 's/\./ /g'))
  755. ((octets[3]+=1))
  756. local -r service_ip=$(echo "${octets[*]}" | sed 's/ /./g')
  757. local sans=""
  758. for extra in $@; do
  759. if [[ -n "${extra}" ]]; then
  760. sans="${sans}IP:${extra},"
  761. fi
  762. done
  763. sans="${sans}IP:${service_ip},DNS:kubernetes,DNS:kubernetes.default,DNS:kubernetes.default.svc,DNS:kubernetes.default.svc.${DNS_DOMAIN},DNS:${MASTER_NAME}"
  764. echo "Generating certs for alternate-names: ${sans}"
  765. PRIMARY_CN="${primary_cn}" SANS="${sans}" generate-certs
  766. CERT_DIR="${KUBE_TEMP}/easy-rsa-master/easyrsa3"
  767. # By default, linux wraps base64 output every 76 cols, so we use 'tr -d' to remove whitespaces.
  768. # Note 'base64 -w0' doesn't work on Mac OS X, which has different flags.
  769. CA_CERT_BASE64=$(cat "${CERT_DIR}/pki/ca.crt" | base64 | tr -d '\r\n')
  770. MASTER_CERT_BASE64=$(cat "${CERT_DIR}/pki/issued/${MASTER_NAME}.crt" | base64 | tr -d '\r\n')
  771. MASTER_KEY_BASE64=$(cat "${CERT_DIR}/pki/private/${MASTER_NAME}.key" | base64 | tr -d '\r\n')
  772. KUBELET_CERT_BASE64=$(cat "${CERT_DIR}/pki/issued/kubelet.crt" | base64 | tr -d '\r\n')
  773. KUBELET_KEY_BASE64=$(cat "${CERT_DIR}/pki/private/kubelet.key" | base64 | tr -d '\r\n')
  774. KUBECFG_CERT_BASE64=$(cat "${CERT_DIR}/pki/issued/kubecfg.crt" | base64 | tr -d '\r\n')
  775. KUBECFG_KEY_BASE64=$(cat "${CERT_DIR}/pki/private/kubecfg.key" | base64 | tr -d '\r\n')
  776. }
  777. # Runs the easy RSA commands to generate certificate files.
  778. # The generated files are at ${KUBE_TEMP}/easy-rsa-master/easyrsa3
  779. #
  780. # Assumed vars
  781. # KUBE_TEMP
  782. # MASTER_NAME
  783. # PRIMARY_CN: Primary canonical name
  784. # SANS: Subject alternate names
  785. #
  786. #
  787. function generate-certs {
  788. local -r cert_create_debug_output=$(mktemp "${KUBE_TEMP}/cert_create_debug_output.XXX")
  789. # Note: This was heavily cribbed from make-ca-cert.sh
  790. (set -x
  791. cd "${KUBE_TEMP}"
  792. curl -L -O --connect-timeout 20 --retry 6 --retry-delay 2 https://storage.googleapis.com/kubernetes-release/easy-rsa/easy-rsa.tar.gz
  793. tar xzf easy-rsa.tar.gz
  794. cd easy-rsa-master/easyrsa3
  795. ./easyrsa init-pki
  796. ./easyrsa --batch "--req-cn=${PRIMARY_CN}@$(date +%s)" build-ca nopass
  797. ./easyrsa --subject-alt-name="${SANS}" build-server-full "${MASTER_NAME}" nopass
  798. ./easyrsa build-client-full kubelet nopass
  799. ./easyrsa build-client-full kubecfg nopass) &>${cert_create_debug_output} || {
  800. # If there was an error in the subshell, just die.
  801. # TODO(roberthbailey): add better error handling here
  802. cat "${cert_create_debug_output}" >&2
  803. echo "=== Failed to generate certificates: Aborting ===" >&2
  804. exit 2
  805. }
  806. }
  807. #
  808. # Using provided master env, extracts value from provided key.
  809. #
  810. # Args:
  811. # $1 master env (kube-env of master; result of calling get-master-env)
  812. # $2 env key to use
  813. function get-env-val() {
  814. local match=`(echo "${1}" | grep ${2}) || echo ""`
  815. if [[ -z ${match} ]]; then
  816. echo ""
  817. fi
  818. echo ${match} | cut -d : -f 2 | cut -d \' -f 2
  819. }
  820. # Load the master env by calling get-master-env, and extract important values
  821. function parse-master-env() {
  822. # Get required master env vars
  823. local master_env=$(get-master-env)
  824. KUBELET_TOKEN=$(get-env-val "${master_env}" "KUBELET_TOKEN")
  825. KUBE_PROXY_TOKEN=$(get-env-val "${master_env}" "KUBE_PROXY_TOKEN")
  826. CA_CERT_BASE64=$(get-env-val "${master_env}" "CA_CERT")
  827. EXTRA_DOCKER_OPTS=$(get-env-val "${master_env}" "EXTRA_DOCKER_OPTS")
  828. KUBELET_CERT_BASE64=$(get-env-val "${master_env}" "KUBELET_CERT")
  829. KUBELET_KEY_BASE64=$(get-env-val "${master_env}" "KUBELET_KEY")
  830. }