sigs.k8s.io/cluster-api@v1.7.1/hack/ensure-kubectl.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2019 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  if [[ "${TRACE-0}" == "1" ]]; then
    22      set -o xtrace
    23  fi
    24  
    25  
    26  # shellcheck source=./hack/utils.sh
    27  source "$(dirname "${BASH_SOURCE[0]}")/utils.sh"
    28  
    29  GOPATH_BIN="$(go env GOPATH)/bin/"
    30  MINIMUM_KUBECTL_VERSION=v1.28.0
    31  goarch="$(go env GOARCH)"
    32  goos="$(go env GOOS)"
    33  
    34  # Ensure the kubectl tool exists and is a viable version, or installs it
    35  verify_kubectl_version() {
    36  
    37    # If kubectl is not available on the path, get it
    38    if ! [ -x "$(command -v kubectl)" ]; then
    39      if [ "$goos" == "linux" ] || [ "$goos" == "darwin" ]; then
    40        if ! [ -d "${GOPATH_BIN}" ]; then
    41          mkdir -p "${GOPATH_BIN}"
    42        fi
    43        echo 'kubectl not found, installing'
    44        curl -sLo "${GOPATH_BIN}/kubectl" "https://dl.k8s.io/release/${MINIMUM_KUBECTL_VERSION}/bin/${goos}/${goarch}/kubectl"
    45        chmod +x "${GOPATH_BIN}/kubectl"
    46        verify_gopath_bin
    47      else
    48        echo "Missing required binary in path: kubectl"
    49        return 2
    50      fi
    51    fi
    52  
    53    local kubectl_version
    54    IFS=" " read -ra kubectl_version <<< "$(kubectl version --client)"
    55    if [[ "${MINIMUM_KUBECTL_VERSION}" != $(echo -e "${MINIMUM_KUBECTL_VERSION}\n${kubectl_version[2]}" | sort -s -t. -k 1,1 -k 2,2n -k 3,3n | head -n1) ]]; then
    56      cat <<EOF
    57  Detected kubectl version: ${kubectl_version[2]}.
    58  Requires ${MINIMUM_KUBECTL_VERSION} or greater.
    59  Please install ${MINIMUM_KUBECTL_VERSION} or later.
    60  EOF
    61      return 2
    62    fi
    63  }
    64  
    65  verify_kubectl_version