k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/cluster/kubectl.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2014 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  set -o errexit
    18  set -o nounset
    19  set -o pipefail
    20  
    21  # Stop the bleeding, turn off the warning until we fix token gen.
    22  # echo "-=-=-=-=-=-=-=-=-=-="
    23  # echo "NOTE:"
    24  # echo "kubectl.sh is deprecated and will be removed soon."
    25  # echo "please replace all usage with calls to the kubectl"
    26  # echo "binary and ensure that it is in your PATH."
    27  # echo ""
    28  # echo "Please see 'kubectl help config' for more details"
    29  # echo "about configuring kubectl for your cluster."
    30  # echo "-=-=-=-=-=-=-=-=-=-="
    31  
    32  
    33  KUBE_ROOT=${KUBE_ROOT:-$(dirname "${BASH_SOURCE[0]}")/..}
    34  source "${KUBE_ROOT}/cluster/kube-util.sh"
    35  source "${KUBE_ROOT}/hack/lib/util.sh"
    36  source "${KUBE_ROOT}/hack/lib/logging.sh"
    37  
    38  # If KUBECTL_PATH isn't set, gather up the list of likely places and use ls
    39  # to find the latest one.
    40  if [[ -z "${KUBECTL_PATH:-}" ]]; then
    41    kubectl=$( kube::util::find-binary "kubectl" )
    42  
    43    if [[ ! -x "$kubectl" ]]; then
    44      {
    45        echo "It looks as if you don't have a compiled kubectl binary"
    46        echo
    47        echo "If you are running from a clone of the git repo, please run"
    48        echo "'./build/run.sh make cross'. Note that this requires having"
    49        echo "Docker installed."
    50        echo
    51        echo "If you are running from a binary release tarball, something is wrong. "
    52        echo "Look at http://kubernetes.io/ for information on how to contact the "
    53        echo "development team for help."
    54      } >&2
    55      exit 1
    56    fi
    57  elif [[ ! -x "${KUBECTL_PATH}" ]]; then
    58    {
    59      echo "KUBECTL_PATH environment variable set to '${KUBECTL_PATH}', but "
    60      echo "this doesn't seem to be a valid executable."
    61    } >&2
    62    exit 1
    63  fi
    64  kubectl="${KUBECTL_PATH:-${kubectl}}"
    65  
    66  if [[ "$KUBERNETES_PROVIDER" == "gke" ]]; then
    67    detect-project &> /dev/null
    68  elif [[ "$KUBERNETES_PROVIDER" == "ubuntu" ]]; then
    69    detect-master > /dev/null
    70    config=(
    71      "--server=http://${KUBE_MASTER_IP}:8080"
    72    )
    73  fi
    74  
    75  
    76  if false; then
    77    # disable these debugging messages by default
    78    echo "current-context: \"$(${kubectl} "${config[@]:+${config[@]}}" config view -o template --template='{{index . "current-context"}}')\"" >&2
    79    echo "Running:" "${kubectl}" "${config[@]:+${config[@]}}" "${@+$@}" >&2
    80  fi
    81  
    82  if [[ "${1:-}" =~ ^(path)$ ]]; then
    83    echo "${kubectl}"
    84    exit 0
    85  fi
    86  
    87  "${kubectl}" "${config[@]:+${config[@]}}" "${@+$@}"
    88