sigs.k8s.io/cluster-api@v1.7.1/hack/ensure-go.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  # shellcheck source=./hack/utils.sh
    26  source "$(dirname "${BASH_SOURCE[0]}")/utils.sh"
    27  
    28  # Ensure the go tool exists and is a viable version.
    29  verify_go_version() {
    30    if [[ -z "$(command -v go)" ]]; then
    31      cat <<EOF
    32  Can't find 'go' in PATH, please fix and retry.
    33  See http://golang.org/doc/install for installation instructions.
    34  EOF
    35      return 2
    36    fi
    37  
    38    local go_version
    39    IFS=" " read -ra go_version <<< "$(go version)"
    40    local minimum_go_version
    41    minimum_go_version=go1.21
    42    if [[ "${minimum_go_version}" != $(echo -e "${minimum_go_version}\n${go_version[2]}" | sort -s -t. -k 1,1 -k 2,2n -k 3,3n | head -n1) && "${go_version[2]}" != "devel" ]]; then
    43      cat <<EOF
    44  Detected go version: ${go_version[*]}.
    45  Kubernetes requires ${minimum_go_version} or greater.
    46  Please install ${minimum_go_version} or later.
    47  EOF
    48      return 2
    49    fi
    50  }
    51  
    52  
    53  verify_go_version
    54  verify_gopath_bin
    55  
    56  # Explicitly opt into go modules, even though we're inside a GOPATH directory
    57  export GO111MODULE=on