kubectl.sh 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/bin/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. set -o errexit
  16. set -o nounset
  17. set -o pipefail
  18. # Stop the bleeding, turn off the warning until we fix token gen.
  19. # echo "-=-=-=-=-=-=-=-=-=-="
  20. # echo "NOTE:"
  21. # echo "kubectl.sh is deprecated and will be removed soon."
  22. # echo "please replace all usage with calls to the kubectl"
  23. # echo "binary and ensure that it is in your PATH."
  24. # echo ""
  25. # echo "Please see 'kubectl help config' for more details"
  26. # echo "about configuring kubectl for your cluster."
  27. # echo "-=-=-=-=-=-=-=-=-=-="
  28. KUBE_ROOT=${KUBE_ROOT:-$(dirname "${BASH_SOURCE}")/..}
  29. source "${KUBE_ROOT}/cluster/kube-util.sh"
  30. # Get the absolute path of the directory component of a file, i.e. the
  31. # absolute path of the dirname of $1.
  32. get_absolute_dirname() {
  33. echo "$(cd "$(dirname "$1")" && pwd)"
  34. }
  35. # Detect the OS name/arch so that we can find our binary
  36. case "$(uname -s)" in
  37. Darwin)
  38. host_os=darwin
  39. ;;
  40. Linux)
  41. host_os=linux
  42. ;;
  43. *)
  44. echo "Unsupported host OS. Must be Linux or Mac OS X." >&2
  45. exit 1
  46. ;;
  47. esac
  48. case "$(uname -m)" in
  49. x86_64*)
  50. host_arch=amd64
  51. ;;
  52. i?86_64*)
  53. host_arch=amd64
  54. ;;
  55. amd64*)
  56. host_arch=amd64
  57. ;;
  58. arm*)
  59. host_arch=arm
  60. ;;
  61. i?86*)
  62. host_arch=386
  63. ;;
  64. s390x*)
  65. host_arch=s390x
  66. ;;
  67. ppc64le*)
  68. host_arch=ppc64le
  69. ;;
  70. *)
  71. echo "Unsupported host arch. Must be x86_64, 386, arm, s390x or ppc64le." >&2
  72. exit 1
  73. ;;
  74. esac
  75. # If KUBECTL_PATH isn't set, gather up the list of likely places and use ls
  76. # to find the latest one.
  77. if [[ -z "${KUBECTL_PATH:-}" ]]; then
  78. locations=(
  79. "${KUBE_ROOT}/_output/bin/kubectl"
  80. "${KUBE_ROOT}/_output/dockerized/bin/${host_os}/${host_arch}/kubectl"
  81. "${KUBE_ROOT}/_output/local/bin/${host_os}/${host_arch}/kubectl"
  82. "${KUBE_ROOT}/platforms/${host_os}/${host_arch}/kubectl"
  83. )
  84. kubectl=$( (ls -t "${locations[@]}" 2>/dev/null || true) | head -1 )
  85. if [[ ! -x "$kubectl" ]]; then
  86. {
  87. echo "It looks as if you don't have a compiled kubectl binary"
  88. echo
  89. echo "If you are running from a clone of the git repo, please run"
  90. echo "'./build/run.sh make cross'. Note that this requires having"
  91. echo "Docker installed."
  92. echo
  93. echo "If you are running from a binary release tarball, something is wrong. "
  94. echo "Look at http://kubernetes.io/ for information on how to contact the "
  95. echo "development team for help."
  96. } >&2
  97. exit 1
  98. fi
  99. elif [[ ! -x "${KUBECTL_PATH}" ]]; then
  100. {
  101. echo "KUBECTL_PATH environment variable set to '${KUBECTL_PATH}', but "
  102. echo "this doesn't seem to be a valid executable."
  103. } >&2
  104. exit 1
  105. fi
  106. kubectl="${KUBECTL_PATH:-${kubectl}}"
  107. if [[ "$KUBERNETES_PROVIDER" == "gke" ]]; then
  108. detect-project &> /dev/null
  109. elif [[ "$KUBERNETES_PROVIDER" == "ubuntu" ]]; then
  110. detect-master > /dev/null
  111. config=(
  112. "--server=http://${KUBE_MASTER_IP}:8080"
  113. )
  114. fi
  115. if false; then
  116. # disable these debugging messages by default
  117. echo "current-context: \"$(${kubectl} "${config[@]:+${config[@]}}" config view -o template --template='{{index . "current-context"}}')\"" >&2
  118. echo "Running:" "${kubectl}" "${config[@]:+${config[@]}}" "${@+$@}" >&2
  119. fi
  120. "${kubectl}" "${config[@]:+${config[@]}}" "${@+$@}"