Skip to content

Commit ccd5e16

Browse files
authored
Merge pull request #4720 from prometherion/issues/4719
🐛 Updating verify kubectl version script
2 parents aa0da32 + 7df4168 commit ccd5e16

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

hack/ensure-kubectl.sh

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,46 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
# This has been copied from https://github.com/kubernetes-sigs/cluster-api/blob/v1.6.0/hack/ensure-kubectl.sh
18+
1719
set -o errexit
1820
set -o nounset
1921
set -o pipefail
2022

23+
if [[ "${TRACE-0}" == "1" ]]; then
24+
set -o xtrace
25+
fi
26+
27+
28+
# shellcheck source=./hack/utils.sh
29+
source "$(dirname "${BASH_SOURCE[0]}")/utils.sh"
30+
2131
GOPATH_BIN="$(go env GOPATH)/bin/"
22-
MINIMUM_KUBECTL_VERSION=v1.19.0
23-
GOARCH="$(go env GOARCH)"
24-
GOOS="$(go env GOOS)"
32+
MINIMUM_KUBECTL_VERSION=v1.28.0
33+
goarch="$(go env GOARCH)"
34+
goos="$(go env GOOS)"
2535

2636
# Ensure the kubectl tool exists and is a viable version, or installs it
2737
verify_kubectl_version() {
2838

2939
# If kubectl is not available on the path, get it
3040
if ! [ -x "$(command -v kubectl)" ]; then
31-
if [ "$GOOS" == "linux" ] || [ "$GOOS" == "darwin" ]; then
41+
if [ "$goos" == "linux" ] || [ "$goos" == "darwin" ]; then
3242
if ! [ -d "${GOPATH_BIN}" ]; then
3343
mkdir -p "${GOPATH_BIN}"
3444
fi
3545
echo 'kubectl not found, installing'
36-
curl -sLo "${GOPATH_BIN}/kubectl" "https://dl.k8s.io/release/${MINIMUM_KUBECTL_VERSION}/bin/${GOOS}/${GOARCH}/kubectl"
46+
curl -sLo "${GOPATH_BIN}/kubectl" "https://dl.k8s.io/release/${MINIMUM_KUBECTL_VERSION}/bin/${goos}/${goarch}/kubectl"
3747
chmod +x "${GOPATH_BIN}/kubectl"
48+
verify_gopath_bin
3849
else
3950
echo "Missing required binary in path: kubectl"
4051
return 2
4152
fi
4253
fi
4354

4455
local kubectl_version
45-
IFS=" " read -ra kubectl_version <<< "$(kubectl version --client --short 2>/dev/null `# "--short" was removed` || kubectl version --client)"
56+
IFS=" " read -ra kubectl_version <<< "$(kubectl version --client)"
4657
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
4758
cat <<EOF
4859
Detected kubectl version: ${kubectl_version[2]}.

hack/utils.sh

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env bash
2-
# Copyright 2022 The Kubernetes Authors.
2+
# Copyright 2019 The Kubernetes Authors.
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
55
# you may not use this file except in compliance with the License.
@@ -13,15 +13,24 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515

16-
# This has been copied from https://github.com/kubernetes-sigs/cluster-api/blob/release-1.1/hack/utils.sh
16+
# This has been copied from https://github.com/kubernetes-sigs/cluster-api/blob/v1.6.0/hack/utils.sh
1717

1818
# get_root_path returns the root path of the project source tree
1919
get_root_path() {
2020
git rev-parse --show-toplevel
2121
}
2222

23-
# cd_root_path cds to the root path of the project source tree
24-
cd_root_path() {
25-
cd "$(get_root_path)" || exit
26-
}
23+
# ensure GOPATH/bin is in PATH as we may install binaries to that directory in
24+
# other ensure-* scripts, and expect them to be found in PATH later on
25+
verify_gopath_bin() {
26+
local gopath_bin
2727

28+
gopath_bin="$(go env GOPATH)/bin"
29+
if ! printenv PATH | grep -q "${gopath_bin}"; then
30+
cat <<EOF
31+
error: \$GOPATH/bin=${gopath_bin} is not in your PATH.
32+
See https://go.dev/doc/gopath_code for more instructions.
33+
EOF
34+
return 2
35+
fi
36+
}

0 commit comments

Comments
 (0)