util.sh 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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. function kube::util::sourced_variable {
  16. # Call this function to tell shellcheck that a variable is supposed to
  17. # be used from other calling context. This helps quiet an "unused
  18. # variable" warning from shellcheck and also document your code.
  19. true
  20. }
  21. kube::util::sortable_date() {
  22. date "+%Y%m%d-%H%M%S"
  23. }
  24. # arguments: target, item1, item2, item3, ...
  25. # returns 0 if target is in the given items, 1 otherwise.
  26. kube::util::array_contains() {
  27. local search="$1"
  28. local element
  29. shift
  30. for element; do
  31. if [[ "${element}" == "${search}" ]]; then
  32. return 0
  33. fi
  34. done
  35. return 1
  36. }
  37. kube::util::wait_for_url() {
  38. local url=$1
  39. local prefix=${2:-}
  40. local wait=${3:-1}
  41. local times=${4:-30}
  42. local maxtime=${5:-1}
  43. command -v curl >/dev/null || {
  44. kube::log::usage "curl must be installed"
  45. exit 1
  46. }
  47. local i
  48. for i in $(seq 1 "${times}"); do
  49. local out
  50. if out=$(curl --max-time "${maxtime}" -gkfs "${url}" 2>/dev/null); then
  51. kube::log::status "On try ${i}, ${prefix}: ${out}"
  52. return 0
  53. fi
  54. sleep "${wait}"
  55. done
  56. kube::log::error "Timed out waiting for ${prefix} to answer at ${url}; tried ${times} waiting ${wait} between each"
  57. return 1
  58. }
  59. # Example: kube::util::wait_for_success 120 5 "kubectl get nodes|grep localhost"
  60. # arguments: wait time, sleep time, shell command
  61. # returns 0 if the shell command get output, 1 otherwise.
  62. kube::util::wait_for_success(){
  63. local wait_time="$1"
  64. local sleep_time="$2"
  65. local cmd="$3"
  66. while [ "$wait_time" -gt 0 ]; do
  67. if eval "$cmd"; then
  68. return 0
  69. else
  70. sleep "$sleep_time"
  71. wait_time=$((wait_time-sleep_time))
  72. fi
  73. done
  74. return 1
  75. }
  76. # Example: kube::util::trap_add 'echo "in trap DEBUG"' DEBUG
  77. # See: http://stackoverflow.com/questions/3338030/multiple-bash-traps-for-the-same-signal
  78. kube::util::trap_add() {
  79. local trap_add_cmd
  80. trap_add_cmd=$1
  81. shift
  82. for trap_add_name in "$@"; do
  83. local existing_cmd
  84. local new_cmd
  85. # Grab the currently defined trap commands for this trap
  86. existing_cmd=$(trap -p "${trap_add_name}" | awk -F"'" '{print $2}')
  87. if [[ -z "${existing_cmd}" ]]; then
  88. new_cmd="${trap_add_cmd}"
  89. else
  90. new_cmd="${trap_add_cmd};${existing_cmd}"
  91. fi
  92. # Assign the test. Disable the shellcheck warning telling that trap
  93. # commands should be single quoted to avoid evaluating them at this
  94. # point instead evaluating them at run time. The logic of adding new
  95. # commands to a single trap requires them to be evaluated right away.
  96. # shellcheck disable=SC2064
  97. trap "${new_cmd}" "${trap_add_name}"
  98. done
  99. }
  100. # Opposite of kube::util::ensure-temp-dir()
  101. kube::util::cleanup-temp-dir() {
  102. rm -rf "${KUBE_TEMP}"
  103. }
  104. # Create a temp dir that'll be deleted at the end of this bash session.
  105. #
  106. # Vars set:
  107. # KUBE_TEMP
  108. kube::util::ensure-temp-dir() {
  109. if [[ -z ${KUBE_TEMP-} ]]; then
  110. KUBE_TEMP=$(mktemp -d 2>/dev/null || mktemp -d -t kubernetes.XXXXXX)
  111. kube::util::trap_add kube::util::cleanup-temp-dir EXIT
  112. fi
  113. }
  114. kube::util::host_os() {
  115. local host_os
  116. case "$(uname -s)" in
  117. Darwin)
  118. host_os=darwin
  119. ;;
  120. Linux)
  121. host_os=linux
  122. ;;
  123. *)
  124. kube::log::error "Unsupported host OS. Must be Linux or Mac OS X."
  125. exit 1
  126. ;;
  127. esac
  128. echo "${host_os}"
  129. }
  130. kube::util::host_arch() {
  131. local host_arch
  132. case "$(uname -m)" in
  133. x86_64*)
  134. host_arch=amd64
  135. ;;
  136. i?86_64*)
  137. host_arch=amd64
  138. ;;
  139. amd64*)
  140. host_arch=amd64
  141. ;;
  142. aarch64*)
  143. host_arch=arm64
  144. ;;
  145. arm64*)
  146. host_arch=arm64
  147. ;;
  148. arm*)
  149. host_arch=arm
  150. ;;
  151. i?86*)
  152. host_arch=x86
  153. ;;
  154. s390x*)
  155. host_arch=s390x
  156. ;;
  157. ppc64le*)
  158. host_arch=ppc64le
  159. ;;
  160. *)
  161. kube::log::error "Unsupported host arch. Must be x86_64, 386, arm, arm64, s390x or ppc64le."
  162. exit 1
  163. ;;
  164. esac
  165. echo "${host_arch}"
  166. }
  167. # This figures out the host platform without relying on golang. We need this as
  168. # we don't want a golang install to be a prerequisite to building yet we need
  169. # this info to figure out where the final binaries are placed.
  170. kube::util::host_platform() {
  171. echo "$(kube::util::host_os)/$(kube::util::host_arch)"
  172. }
  173. # looks for $1 in well-known output locations for the platform ($2)
  174. # $KUBE_ROOT must be set
  175. kube::util::find-binary-for-platform() {
  176. local -r lookfor="$1"
  177. local -r platform="$2"
  178. local locations=(
  179. "${KUBE_ROOT}/_output/bin/${lookfor}"
  180. "${KUBE_ROOT}/_output/dockerized/bin/${platform}/${lookfor}"
  181. "${KUBE_ROOT}/_output/local/bin/${platform}/${lookfor}"
  182. "${KUBE_ROOT}/platforms/${platform}/${lookfor}"
  183. )
  184. # Also search for binary in bazel build tree.
  185. # The bazel go rules place some binaries in subtrees like
  186. # "bazel-bin/source/path/linux_amd64_pure_stripped/binaryname", so make sure
  187. # the platform name is matched in the path.
  188. while IFS=$'\n' read -r location; do
  189. locations+=("$location");
  190. done < <(find "${KUBE_ROOT}/bazel-bin/" -type f -executable \
  191. \( -path "*/${platform/\//_}*/${lookfor}" -o -path "*/${lookfor}" \) 2>/dev/null || true)
  192. # List most recently-updated location.
  193. local -r bin=$( (ls -t "${locations[@]}" 2>/dev/null || true) | head -1 )
  194. echo -n "${bin}"
  195. }
  196. # looks for $1 in well-known output locations for the host platform
  197. # $KUBE_ROOT must be set
  198. kube::util::find-binary() {
  199. kube::util::find-binary-for-platform "$1" "$(kube::util::host_platform)"
  200. }
  201. # Run all known doc generators (today gendocs and genman for kubectl)
  202. # $1 is the directory to put those generated documents
  203. kube::util::gen-docs() {
  204. local dest="$1"
  205. # Find binary
  206. gendocs=$(kube::util::find-binary "gendocs")
  207. genkubedocs=$(kube::util::find-binary "genkubedocs")
  208. genman=$(kube::util::find-binary "genman")
  209. genyaml=$(kube::util::find-binary "genyaml")
  210. genfeddocs=$(kube::util::find-binary "genfeddocs")
  211. # TODO: If ${genfeddocs} is not used from anywhere (it isn't used at
  212. # least from k/k tree), remove it completely.
  213. kube::util::sourced_variable "${genfeddocs}"
  214. mkdir -p "${dest}/docs/user-guide/kubectl/"
  215. "${gendocs}" "${dest}/docs/user-guide/kubectl/"
  216. mkdir -p "${dest}/docs/admin/"
  217. "${genkubedocs}" "${dest}/docs/admin/" "kube-apiserver"
  218. "${genkubedocs}" "${dest}/docs/admin/" "kube-controller-manager"
  219. "${genkubedocs}" "${dest}/docs/admin/" "kube-proxy"
  220. "${genkubedocs}" "${dest}/docs/admin/" "kube-scheduler"
  221. "${genkubedocs}" "${dest}/docs/admin/" "kubelet"
  222. "${genkubedocs}" "${dest}/docs/admin/" "kubeadm"
  223. mkdir -p "${dest}/docs/man/man1/"
  224. "${genman}" "${dest}/docs/man/man1/" "kube-apiserver"
  225. "${genman}" "${dest}/docs/man/man1/" "kube-controller-manager"
  226. "${genman}" "${dest}/docs/man/man1/" "kube-proxy"
  227. "${genman}" "${dest}/docs/man/man1/" "kube-scheduler"
  228. "${genman}" "${dest}/docs/man/man1/" "kubelet"
  229. "${genman}" "${dest}/docs/man/man1/" "kubectl"
  230. "${genman}" "${dest}/docs/man/man1/" "kubeadm"
  231. mkdir -p "${dest}/docs/yaml/kubectl/"
  232. "${genyaml}" "${dest}/docs/yaml/kubectl/"
  233. # create the list of generated files
  234. pushd "${dest}" > /dev/null || return 1
  235. touch docs/.generated_docs
  236. find . -type f | cut -sd / -f 2- | LC_ALL=C sort > docs/.generated_docs
  237. popd > /dev/null || return 1
  238. }
  239. # Removes previously generated docs-- we don't want to check them in. $KUBE_ROOT
  240. # must be set.
  241. kube::util::remove-gen-docs() {
  242. if [ -e "${KUBE_ROOT}/docs/.generated_docs" ]; then
  243. # remove all of the old docs; we don't want to check them in.
  244. while read -r file; do
  245. rm "${KUBE_ROOT}/${file}" 2>/dev/null || true
  246. done <"${KUBE_ROOT}/docs/.generated_docs"
  247. # The docs/.generated_docs file lists itself, so we don't need to explicitly
  248. # delete it.
  249. fi
  250. }
  251. # Takes a group/version and returns the path to its location on disk, sans
  252. # "pkg". E.g.:
  253. # * default behavior: extensions/v1beta1 -> apis/extensions/v1beta1
  254. # * default behavior for only a group: experimental -> apis/experimental
  255. # * Special handling for empty group: v1 -> api/v1, unversioned -> api/unversioned
  256. # * Special handling for groups suffixed with ".k8s.io": foo.k8s.io/v1 -> apis/foo/v1
  257. # * Very special handling for when both group and version are "": / -> api
  258. #
  259. # $KUBE_ROOT must be set.
  260. kube::util::group-version-to-pkg-path() {
  261. local group_version="$1"
  262. while IFS=$'\n' read -r api; do
  263. if [[ "${api}" = "${group_version/.*k8s.io/}" ]]; then
  264. echo "vendor/k8s.io/api/${group_version/.*k8s.io/}"
  265. return
  266. fi
  267. done < <(cd "${KUBE_ROOT}/staging/src/k8s.io/api" && find . -name types.go -exec dirname {} \; | sed "s|\./||g" | sort)
  268. # "v1" is the API GroupVersion
  269. if [[ "${group_version}" == "v1" ]]; then
  270. echo "vendor/k8s.io/api/core/v1"
  271. return
  272. fi
  273. # Special cases first.
  274. # TODO(lavalamp): Simplify this by moving pkg/api/v1 and splitting pkg/api,
  275. # moving the results to pkg/apis/api.
  276. case "${group_version}" in
  277. # both group and version are "", this occurs when we generate deep copies for internal objects of the legacy v1 API.
  278. __internal)
  279. echo "pkg/apis/core"
  280. ;;
  281. meta/v1)
  282. echo "vendor/k8s.io/apimachinery/pkg/apis/meta/v1"
  283. ;;
  284. meta/v1beta1)
  285. echo "vendor/k8s.io/apimachinery/pkg/apis/meta/v1beta1"
  286. ;;
  287. *.k8s.io)
  288. echo "pkg/apis/${group_version%.*k8s.io}"
  289. ;;
  290. *.k8s.io/*)
  291. echo "pkg/apis/${group_version/.*k8s.io/}"
  292. ;;
  293. *)
  294. echo "pkg/apis/${group_version%__internal}"
  295. ;;
  296. esac
  297. }
  298. # Takes a group/version and returns the swagger-spec file name.
  299. # default behavior: extensions/v1beta1 -> extensions_v1beta1
  300. # special case for v1: v1 -> v1
  301. kube::util::gv-to-swagger-name() {
  302. local group_version="$1"
  303. case "${group_version}" in
  304. v1)
  305. echo "v1"
  306. ;;
  307. *)
  308. echo "${group_version%/*}_${group_version#*/}"
  309. ;;
  310. esac
  311. }
  312. # Returns the name of the upstream remote repository name for the local git
  313. # repo, e.g. "upstream" or "origin".
  314. kube::util::git_upstream_remote_name() {
  315. git remote -v | grep fetch |\
  316. grep -E 'github.com[/:]kubernetes/kubernetes|k8s.io/kubernetes' |\
  317. head -n 1 | awk '{print $1}'
  318. }
  319. # Exits script if working directory is dirty. If it's run interactively in the terminal
  320. # the user can commit changes in a second terminal. This script will wait.
  321. kube::util::ensure_clean_working_dir() {
  322. while ! git diff HEAD --exit-code &>/dev/null; do
  323. echo -e "\nUnexpected dirty working directory:\n"
  324. if tty -s; then
  325. git status -s
  326. else
  327. git diff -a # be more verbose in log files without tty
  328. exit 1
  329. fi | sed 's/^/ /'
  330. echo -e "\nCommit your changes in another terminal and then continue here by pressing enter."
  331. read -r
  332. done 1>&2
  333. }
  334. # Find the base commit using:
  335. # $PULL_BASE_SHA if set (from Prow)
  336. # current ref from the remote upstream branch
  337. kube::util::base_ref() {
  338. local -r git_branch=$1
  339. if [[ -n ${PULL_BASE_SHA:-} ]]; then
  340. echo "${PULL_BASE_SHA}"
  341. return
  342. fi
  343. full_branch="$(kube::util::git_upstream_remote_name)/${git_branch}"
  344. # make sure the branch is valid, otherwise the check will pass erroneously.
  345. if ! git describe "${full_branch}" >/dev/null; then
  346. # abort!
  347. exit 1
  348. fi
  349. echo "${full_branch}"
  350. }
  351. # Checks whether there are any files matching pattern $2 changed between the
  352. # current branch and upstream branch named by $1.
  353. # Returns 1 (false) if there are no changes
  354. # 0 (true) if there are changes detected.
  355. kube::util::has_changes() {
  356. local -r git_branch=$1
  357. local -r pattern=$2
  358. local -r not_pattern=${3:-totallyimpossiblepattern}
  359. local base_ref
  360. base_ref=$(kube::util::base_ref "${git_branch}")
  361. echo "Checking for '${pattern}' changes against '${base_ref}'"
  362. # notice this uses ... to find the first shared ancestor
  363. if git diff --name-only "${base_ref}...HEAD" | grep -v -E "${not_pattern}" | grep "${pattern}" > /dev/null; then
  364. return 0
  365. fi
  366. # also check for pending changes
  367. if git status --porcelain | grep -v -E "${not_pattern}" | grep "${pattern}" > /dev/null; then
  368. echo "Detected '${pattern}' uncommitted changes."
  369. return 0
  370. fi
  371. echo "No '${pattern}' changes detected."
  372. return 1
  373. }
  374. kube::util::download_file() {
  375. local -r url=$1
  376. local -r destination_file=$2
  377. rm "${destination_file}" 2&> /dev/null || true
  378. for i in $(seq 5)
  379. do
  380. if ! curl -fsSL --retry 3 --keepalive-time 2 "${url}" -o "${destination_file}"; then
  381. echo "Downloading ${url} failed. $((5-i)) retries left."
  382. sleep 1
  383. else
  384. echo "Downloading ${url} succeed"
  385. return 0
  386. fi
  387. done
  388. return 1
  389. }
  390. # Test whether openssl is installed.
  391. # Sets:
  392. # OPENSSL_BIN: The path to the openssl binary to use
  393. function kube::util::test_openssl_installed {
  394. if ! openssl version >& /dev/null; then
  395. echo "Failed to run openssl. Please ensure openssl is installed"
  396. exit 1
  397. fi
  398. OPENSSL_BIN=$(command -v openssl)
  399. }
  400. # creates a client CA, args are sudo, dest-dir, ca-id, purpose
  401. # purpose is dropped in after "key encipherment", you usually want
  402. # '"client auth"'
  403. # '"server auth"'
  404. # '"client auth","server auth"'
  405. function kube::util::create_signing_certkey {
  406. local sudo=$1
  407. local dest_dir=$2
  408. local id=$3
  409. local purpose=$4
  410. # Create client ca
  411. ${sudo} /usr/bin/env bash -e <<EOF
  412. rm -f "${dest_dir}/${id}-ca.crt" "${dest_dir}/${id}-ca.key"
  413. ${OPENSSL_BIN} req -x509 -sha256 -new -nodes -days 365 -newkey rsa:2048 -keyout "${dest_dir}/${id}-ca.key" -out "${dest_dir}/${id}-ca.crt" -subj "/C=xx/ST=x/L=x/O=x/OU=x/CN=ca/emailAddress=x/"
  414. echo '{"signing":{"default":{"expiry":"43800h","usages":["signing","key encipherment",${purpose}]}}}' > "${dest_dir}/${id}-ca-config.json"
  415. EOF
  416. }
  417. # signs a client certificate: args are sudo, dest-dir, CA, filename (roughly), username, groups...
  418. function kube::util::create_client_certkey {
  419. local sudo=$1
  420. local dest_dir=$2
  421. local ca=$3
  422. local id=$4
  423. local cn=${5:-$4}
  424. local groups=""
  425. local SEP=""
  426. shift 5
  427. while [ -n "${1:-}" ]; do
  428. groups+="${SEP}{\"O\":\"$1\"}"
  429. SEP=","
  430. shift 1
  431. done
  432. ${sudo} /usr/bin/env bash -e <<EOF
  433. cd ${dest_dir}
  434. echo '{"CN":"${cn}","names":[${groups}],"hosts":[""],"key":{"algo":"rsa","size":2048}}' | ${CFSSL_BIN} gencert -ca=${ca}.crt -ca-key=${ca}.key -config=${ca}-config.json - | ${CFSSLJSON_BIN} -bare client-${id}
  435. mv "client-${id}-key.pem" "client-${id}.key"
  436. mv "client-${id}.pem" "client-${id}.crt"
  437. rm -f "client-${id}.csr"
  438. EOF
  439. }
  440. # signs a serving certificate: args are sudo, dest-dir, ca, filename (roughly), subject, hosts...
  441. function kube::util::create_serving_certkey {
  442. local sudo=$1
  443. local dest_dir=$2
  444. local ca=$3
  445. local id=$4
  446. local cn=${5:-$4}
  447. local hosts=""
  448. local SEP=""
  449. shift 5
  450. while [ -n "${1:-}" ]; do
  451. hosts+="${SEP}\"$1\""
  452. SEP=","
  453. shift 1
  454. done
  455. ${sudo} /usr/bin/env bash -e <<EOF
  456. cd ${dest_dir}
  457. echo '{"CN":"${cn}","hosts":[${hosts}],"key":{"algo":"rsa","size":2048}}' | ${CFSSL_BIN} gencert -ca=${ca}.crt -ca-key=${ca}.key -config=${ca}-config.json - | ${CFSSLJSON_BIN} -bare serving-${id}
  458. mv "serving-${id}-key.pem" "serving-${id}.key"
  459. mv "serving-${id}.pem" "serving-${id}.crt"
  460. rm -f "serving-${id}.csr"
  461. EOF
  462. }
  463. # creates a self-contained kubeconfig: args are sudo, dest-dir, ca file, host, port, client id, token(optional)
  464. function kube::util::write_client_kubeconfig {
  465. local sudo=$1
  466. local dest_dir=$2
  467. local ca_file=$3
  468. local api_host=$4
  469. local api_port=$5
  470. local client_id=$6
  471. local token=${7:-}
  472. cat <<EOF | ${sudo} tee "${dest_dir}"/"${client_id}".kubeconfig > /dev/null
  473. apiVersion: v1
  474. kind: Config
  475. clusters:
  476. - cluster:
  477. certificate-authority: ${ca_file}
  478. server: https://${api_host}:${api_port}/
  479. name: local-up-cluster
  480. users:
  481. - user:
  482. token: ${token}
  483. client-certificate: ${dest_dir}/client-${client_id}.crt
  484. client-key: ${dest_dir}/client-${client_id}.key
  485. name: local-up-cluster
  486. contexts:
  487. - context:
  488. cluster: local-up-cluster
  489. user: local-up-cluster
  490. name: local-up-cluster
  491. current-context: local-up-cluster
  492. EOF
  493. # flatten the kubeconfig files to make them self contained
  494. username=$(whoami)
  495. ${sudo} /usr/bin/env bash -e <<EOF
  496. $(kube::util::find-binary kubectl) --kubeconfig="${dest_dir}/${client_id}.kubeconfig" config view --minify --flatten > "/tmp/${client_id}.kubeconfig"
  497. mv -f "/tmp/${client_id}.kubeconfig" "${dest_dir}/${client_id}.kubeconfig"
  498. chown ${username} "${dest_dir}/${client_id}.kubeconfig"
  499. EOF
  500. }
  501. # list_staging_repos outputs a sorted list of repos in staging/src/k8s.io
  502. # each entry will just be the $repo portion of staging/src/k8s.io/$repo/...
  503. # $KUBE_ROOT must be set.
  504. function kube::util::list_staging_repos() {
  505. (
  506. cd "${KUBE_ROOT}/staging/src/k8s.io" && \
  507. find . -mindepth 1 -maxdepth 1 -type d | cut -c 3- | sort
  508. )
  509. }
  510. # Determines if docker can be run, failures may simply require that the user be added to the docker group.
  511. function kube::util::ensure_docker_daemon_connectivity {
  512. IFS=" " read -ra DOCKER <<< "${DOCKER_OPTS}"
  513. # Expand ${DOCKER[@]} only if it's not unset. This is to work around
  514. # Bash 3 issue with unbound variable.
  515. DOCKER=(docker ${DOCKER[@]:+"${DOCKER[@]}"})
  516. if ! "${DOCKER[@]}" info > /dev/null 2>&1 ; then
  517. cat <<'EOF' >&2
  518. Can't connect to 'docker' daemon. please fix and retry.
  519. Possible causes:
  520. - Docker Daemon not started
  521. - Linux: confirm via your init system
  522. - macOS w/ docker-machine: run `docker-machine ls` and `docker-machine start <name>`
  523. - macOS w/ Docker for Mac: Check the menu bar and start the Docker application
  524. - DOCKER_HOST hasn't been set or is set incorrectly
  525. - Linux: domain socket is used, DOCKER_* should be unset. In Bash run `unset ${!DOCKER_*}`
  526. - macOS w/ docker-machine: run `eval "$(docker-machine env <name>)"`
  527. - macOS w/ Docker for Mac: domain socket is used, DOCKER_* should be unset. In Bash run `unset ${!DOCKER_*}`
  528. - Other things to check:
  529. - Linux: User isn't in 'docker' group. Add and relogin.
  530. - Something like 'sudo usermod -a -G docker ${USER}'
  531. - RHEL7 bug and workaround: https://bugzilla.redhat.com/show_bug.cgi?id=1119282#c8
  532. EOF
  533. return 1
  534. fi
  535. }
  536. # Wait for background jobs to finish. Return with
  537. # an error status if any of the jobs failed.
  538. kube::util::wait-for-jobs() {
  539. local fail=0
  540. local job
  541. for job in $(jobs -p); do
  542. wait "${job}" || fail=$((fail + 1))
  543. done
  544. return ${fail}
  545. }
  546. # kube::util::join <delim> <list...>
  547. # Concatenates the list elements with the delimiter passed as first parameter
  548. #
  549. # Ex: kube::util::join , a b c
  550. # -> a,b,c
  551. function kube::util::join {
  552. local IFS="$1"
  553. shift
  554. echo "$*"
  555. }
  556. # Downloads cfssl/cfssljson into $1 directory if they do not already exist in PATH
  557. #
  558. # Assumed vars:
  559. # $1 (cfssl directory) (optional)
  560. #
  561. # Sets:
  562. # CFSSL_BIN: The path of the installed cfssl binary
  563. # CFSSLJSON_BIN: The path of the installed cfssljson binary
  564. #
  565. function kube::util::ensure-cfssl {
  566. if command -v cfssl &>/dev/null && command -v cfssljson &>/dev/null; then
  567. CFSSL_BIN=$(command -v cfssl)
  568. CFSSLJSON_BIN=$(command -v cfssljson)
  569. return 0
  570. fi
  571. host_arch=$(kube::util::host_arch)
  572. if [[ "${host_arch}" != "amd64" ]]; then
  573. echo "Cannot download cfssl on non-amd64 hosts and cfssl does not appear to be installed."
  574. echo "Please install cfssl and cfssljson and verify they are in \$PATH."
  575. echo "Hint: export PATH=\$PATH:\$GOPATH/bin; go get -u github.com/cloudflare/cfssl/cmd/..."
  576. exit 1
  577. fi
  578. # Create a temp dir for cfssl if no directory was given
  579. local cfssldir=${1:-}
  580. if [[ -z "${cfssldir}" ]]; then
  581. kube::util::ensure-temp-dir
  582. cfssldir="${KUBE_TEMP}/cfssl"
  583. fi
  584. mkdir -p "${cfssldir}"
  585. pushd "${cfssldir}" > /dev/null || return 1
  586. echo "Unable to successfully run 'cfssl' from ${PATH}; downloading instead..."
  587. kernel=$(uname -s)
  588. case "${kernel}" in
  589. Linux)
  590. curl --retry 10 -L -o cfssl https://pkg.cfssl.org/R1.2/cfssl_linux-amd64
  591. curl --retry 10 -L -o cfssljson https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
  592. ;;
  593. Darwin)
  594. curl --retry 10 -L -o cfssl https://pkg.cfssl.org/R1.2/cfssl_darwin-amd64
  595. curl --retry 10 -L -o cfssljson https://pkg.cfssl.org/R1.2/cfssljson_darwin-amd64
  596. ;;
  597. *)
  598. echo "Unknown, unsupported platform: ${kernel}." >&2
  599. echo "Supported platforms: Linux, Darwin." >&2
  600. exit 2
  601. esac
  602. chmod +x cfssl || true
  603. chmod +x cfssljson || true
  604. CFSSL_BIN="${cfssldir}/cfssl"
  605. CFSSLJSON_BIN="${cfssldir}/cfssljson"
  606. if [[ ! -x ${CFSSL_BIN} || ! -x ${CFSSLJSON_BIN} ]]; then
  607. echo "Failed to download 'cfssl'. Please install cfssl and cfssljson and verify they are in \$PATH."
  608. echo "Hint: export PATH=\$PATH:\$GOPATH/bin; go get -u github.com/cloudflare/cfssl/cmd/..."
  609. exit 1
  610. fi
  611. popd > /dev/null || return 1
  612. }
  613. # kube::util::ensure_dockerized
  614. # Confirms that the script is being run inside a kube-build image
  615. #
  616. function kube::util::ensure_dockerized {
  617. if [[ -f /kube-build-image ]]; then
  618. return 0
  619. else
  620. echo "ERROR: This script is designed to be run inside a kube-build container"
  621. exit 1
  622. fi
  623. }
  624. # kube::util::ensure-gnu-sed
  625. # Determines which sed binary is gnu-sed on linux/darwin
  626. #
  627. # Sets:
  628. # SED: The name of the gnu-sed binary
  629. #
  630. function kube::util::ensure-gnu-sed {
  631. # NOTE: the echo below is a workaround to ensure sed is executed before the grep.
  632. # see: https://github.com/kubernetes/kubernetes/issues/87251
  633. if LANG=C sed --help 2>&1 | grep -q "GNU\|BusyBox"; then
  634. SED="sed"
  635. elif command -v gsed &>/dev/null; then
  636. SED="gsed"
  637. else
  638. kube::log::error "Failed to find GNU sed as sed or gsed. If you are on Mac: brew install gnu-sed." >&2
  639. return 1
  640. fi
  641. kube::util::sourced_variable "${SED}"
  642. }
  643. # kube::util::check-file-in-alphabetical-order <file>
  644. # Check that the file is in alphabetical order
  645. #
  646. function kube::util::check-file-in-alphabetical-order {
  647. local failure_file="$1"
  648. if ! diff -u "${failure_file}" <(LC_ALL=C sort "${failure_file}"); then
  649. {
  650. echo
  651. echo "${failure_file} is not in alphabetical order. Please sort it:"
  652. echo
  653. echo " LC_ALL=C sort -o ${failure_file} ${failure_file}"
  654. echo
  655. } >&2
  656. false
  657. fi
  658. }
  659. # kube::util::require-jq
  660. # Checks whether jq is installed.
  661. function kube::util::require-jq {
  662. if ! command -v jq &>/dev/null; then
  663. echo "jq not found. Please install." 1>&2
  664. return 1
  665. fi
  666. }
  667. # outputs md5 hash of $1, works on macOS and Linux
  668. function kube::util::md5() {
  669. if which md5 >/dev/null 2>&1; then
  670. md5 -q "$1"
  671. else
  672. md5sum "$1" | awk '{ print $1 }'
  673. fi
  674. }
  675. # kube::util::read-array
  676. # Reads in stdin and adds it line by line to the array provided. This can be
  677. # used instead of "mapfile -t", and is bash 3 compatible.
  678. #
  679. # Assumed vars:
  680. # $1 (name of array to create/modify)
  681. #
  682. # Example usage:
  683. # kube::util::read-array files < <(ls -1)
  684. #
  685. function kube::util::read-array {
  686. local i=0
  687. unset -v "$1"
  688. while IFS= read -r "$1[i++]"; do :; done
  689. eval "[[ \${$1[--i]} ]]" || unset "$1[i]" # ensures last element isn't empty
  690. }
  691. # Some useful colors.
  692. if [[ -z "${color_start-}" ]]; then
  693. declare -r color_start="\033["
  694. declare -r color_red="${color_start}0;31m"
  695. declare -r color_yellow="${color_start}0;33m"
  696. declare -r color_green="${color_start}0;32m"
  697. declare -r color_blue="${color_start}1;34m"
  698. declare -r color_cyan="${color_start}1;36m"
  699. declare -r color_norm="${color_start}0m"
  700. kube::util::sourced_variable "${color_start}"
  701. kube::util::sourced_variable "${color_red}"
  702. kube::util::sourced_variable "${color_yellow}"
  703. kube::util::sourced_variable "${color_green}"
  704. kube::util::sourced_variable "${color_blue}"
  705. kube::util::sourced_variable "${color_cyan}"
  706. kube::util::sourced_variable "${color_norm}"
  707. fi
  708. # ex: ts=2 sw=2 et filetype=sh