sigs.k8s.io/cluster-api-provider-aws@v1.5.5/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  GOPATH_BIN="$(go env GOPATH)/bin/"
    22  MINIMUM_KUBECTL_VERSION=v1.19.0
    23  GOARCH="$(go env GOARCH)"
    24  GOOS="$(go env GOOS)"
    25  
    26  # Ensure the kubectl tool exists and is a viable version, or installs it
    27  verify_kubectl_version() {
    28  
    29    # If kubectl is not available on the path, get it
    30    if ! [ -x "$(command -v kubectl)" ]; then
    31      if [ "$GOOS" == "linux" ] || [ "$GOOS" == "darwin" ]; then
    32        if ! [ -d "${GOPATH_BIN}" ]; then
    33          mkdir -p "${GOPATH_BIN}"
    34        fi
    35        echo 'kubectl not found, installing'
    36        curl -sLo "${GOPATH_BIN}/kubectl" "https://storage.googleapis.com/kubernetes-release/release/${MINIMUM_KUBECTL_VERSION}/bin/${GOOS}/${GOARCH}/kubectl"
    37        chmod +x "${GOPATH_BIN}/kubectl"
    38      else
    39        echo "Missing required binary in path: kubectl"
    40        return 2
    41      fi
    42    fi
    43  
    44    local kubectl_version
    45    IFS=" " read -ra kubectl_version <<< "$(kubectl version --client --short)"
    46    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
    47      cat <<EOF
    48  Detected kubectl version: ${kubectl_version[2]}.
    49  Requires ${MINIMUM_KUBECTL_VERSION} or greater.
    50  Please install ${MINIMUM_KUBECTL_VERSION} or later.
    51  EOF
    52      return 2
    53    fi
    54  }
    55  
    56  verify_kubectl_version