sigs.k8s.io/kubebuilder/v3@v3.14.0/test/common.sh (about) 1 #!/usr/bin/env bash 2 3 # Copyright 2018 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 # Not every exact cluster version has an equal tools version, and visa versa. 18 # This function returns the exact tools version for a k8s version based on its minor. 19 function convert_to_tools_ver { 20 local k8s_ver=${1:?"k8s version must be set to arg 1"} 21 local maj_min=$(echo $k8s_ver | grep -oE '^[0-9]+\.[0-9]+') 22 case $maj_min in 23 # 1.14-1.19 work with the 1.19 server bins and kubectl. 24 "1.14"|"1.15"|"1.16"|"1.17"|"1.18"|"1.19") echo "1.19.2";; 25 # Tests in 1.20 and 1.21 with their counterpart version's apiserver. 26 "1.20"|"1.21") echo "1.21.5";; 27 "1.22") echo "1.22.1";; 28 "1.23") echo "1.23.3";; 29 "1.24") echo "1.24.1";; 30 "1.25") echo "1.25.0";; 31 "1.26") echo "1.26.0";; 32 "1.27") echo "1.27.1";; 33 "1.28") echo "1.28.3";; 34 *) 35 echo "k8s version $k8s_ver not supported" 36 exit 1 37 esac 38 } 39 40 set -o errexit 41 set -o nounset 42 set -o pipefail 43 44 # Enable tracing in this script off by setting the TRACE variable in your 45 # environment to any value: 46 # 47 # $ TRACE=1 test.sh 48 TRACE=${TRACE:-""} 49 if [ -n "$TRACE" ]; then 50 set -x 51 fi 52 53 export KIND_K8S_VERSION="${KIND_K8S_VERSION:-"v1.28.0"}" 54 tools_k8s_version=$(convert_to_tools_ver "${KIND_K8S_VERSION#v*}") 55 kind_version=0.20.0 56 goarch=amd64 57 58 if [[ "$OSTYPE" == "linux-gnu" ]]; then 59 goos="linux" 60 elif [[ "$OSTYPE" == "darwin"* ]]; then 61 goos="darwin" 62 #elif [[ "$OS" == "Windows_NT" ]]; then 63 # goos="windows" 64 else 65 echo "OS '$OSTYPE' not supported. Aborting." >&2 66 exit 1 67 fi 68 69 # Turn colors in this script off by setting the NO_COLOR variable in your 70 # environment to any value: 71 # 72 # $ NO_COLOR=1 test.sh 73 NO_COLOR=${NO_COLOR:-""} 74 if [ -z "$NO_COLOR" ]; then 75 header=$'\e[1;33m' 76 reset=$'\e[0m' 77 else 78 header='' 79 reset='' 80 fi 81 82 function header_text { 83 echo "$header$*$reset" 84 } 85 86 # Certain tools are installed to GOBIN. 87 export PATH="$(go env GOPATH)/bin:${PATH}" 88 89 # Kubebuilder's bin path should be the last added to PATH such that it is preferred. 90 tmp_root=/tmp 91 kb_root_dir=$tmp_root/kubebuilder 92 mkdir -p "$kb_root_dir" 93 export PATH="${kb_root_dir}/bin:${PATH}" 94 95 # Skip fetching and untaring the tools by setting the SKIP_FETCH_TOOLS variable 96 # in your environment to any value: 97 # 98 # $ SKIP_FETCH_TOOLS=1 ./test.sh 99 # 100 # If you skip fetching tools, this script will use the tools already on your 101 # machine, but rebuild the kubebuilder and kubebuilder-bin binaries. 102 SKIP_FETCH_TOOLS=${SKIP_FETCH_TOOLS:-""} 103 104 # Build kubebuilder 105 function build_kb { 106 header_text "Building kubebuilder" 107 108 go build -o "${kb_root_dir}/bin/kubebuilder" ./cmd 109 kb="${kb_root_dir}/bin/kubebuilder" 110 } 111 112 # Fetch k8s API tools and manage them globally with setup-envtest. 113 function fetch_tools { 114 if ! is_installed setup-envtest; then 115 header_text "Installing setup-envtest to $(go env GOPATH)/bin" 116 117 go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest 118 fi 119 120 if [ -z "$SKIP_FETCH_TOOLS" ]; then 121 header_text "Installing e2e tools with setup-envtest" 122 123 setup-envtest use $tools_k8s_version 124 fi 125 126 # Export KUBEBUILDER_ASSETS. 127 eval $(setup-envtest use -i -p env $tools_k8s_version) 128 # Downloaded tools should be used instead of counterparts present in the environment. 129 export PATH="${KUBEBUILDER_ASSETS}:${PATH}" 130 } 131 132 # Installing kind in a temporal dir if no previously installed to GOBIN. 133 function install_kind { 134 if ! is_installed kind ; then 135 header_text "Installing kind to $(go env GOPATH)/bin" 136 137 go install sigs.k8s.io/kind@v$kind_version 138 fi 139 } 140 141 # Check if a program is previously installed 142 function is_installed { 143 if command -v $1 &>/dev/null; then 144 return 0 145 fi 146 return 1 147 } 148 149 function listPkgDirs() { 150 go list -f '{{.Dir}}' ./... | grep -v generated 151 } 152 153 #Lists all go files 154 function listFiles() { 155 # pipeline is much faster than for loop 156 listPkgDirs | xargs -I {} find {} \( -name '*.go' -o -name '*.sh' \) | grep -v generated 157 }