kubectl.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. 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[0]}")/..}
  29. source "${KUBE_ROOT}/cluster/kube-util.sh"
  30. source "${KUBE_ROOT}/cluster/clientbin.sh"
  31. # If KUBECTL_PATH isn't set, gather up the list of likely places and use ls
  32. # to find the latest one.
  33. if [[ -z "${KUBECTL_PATH:-}" ]]; then
  34. kubectl=$( get_bin "kubectl" "cmd/kubectl" )
  35. if [[ ! -x "$kubectl" ]]; then
  36. print_error "kubectl"
  37. exit 1
  38. fi
  39. elif [[ ! -x "${KUBECTL_PATH}" ]]; then
  40. {
  41. echo "KUBECTL_PATH environment variable set to '${KUBECTL_PATH}', but "
  42. echo "this doesn't seem to be a valid executable."
  43. } >&2
  44. exit 1
  45. fi
  46. kubectl="${KUBECTL_PATH:-${kubectl}}"
  47. if [[ "$KUBERNETES_PROVIDER" == "gke" ]]; then
  48. detect-project &> /dev/null
  49. elif [[ "$KUBERNETES_PROVIDER" == "ubuntu" ]]; then
  50. detect-master > /dev/null
  51. config=(
  52. "--server=http://${KUBE_MASTER_IP}:8080"
  53. )
  54. fi
  55. if false; then
  56. # disable these debugging messages by default
  57. echo "current-context: \"$(${kubectl} "${config[@]:+${config[@]}}" config view -o template --template='{{index . "current-context"}}')\"" >&2
  58. echo "Running:" "${kubectl}" "${config[@]:+${config[@]}}" "${@+$@}" >&2
  59. fi
  60. if [[ "${1:-}" =~ ^(path)$ ]]; then
  61. echo "${kubectl}"
  62. exit 0
  63. fi
  64. "${kubectl}" "${config[@]:+${config[@]}}" "${@+$@}"