forked from kubernetes-sigs/kwok
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequirements.sh
executable file
·246 lines (201 loc) · 5.62 KB
/
requirements.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env bash
# Copyright 2022 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
DIR="$(dirname "${BASH_SOURCE[0]}")"
DIR="$(realpath "${DIR}")"
ROOT_DIR="$(realpath "${DIR}/..")"
LOCAL_BIN_DIR="${ROOT_DIR}/bin"
export PATH="${LOCAL_BIN_DIR}:${PATH}"
KIND_VERSION=0.23.0
KUBE_VERSION=1.31.2
# TODO: Stay at 0.9 in figuring out the Attestations of buildx.
# https://github.com/docker/buildx/pull/1412
BUILDX_VERSION=0.9.1
KUSTOMIZE_VERSION=5.3.0
GO_VERSION=1.23.0
function command_exist() {
local command="${1}"
type "${command}" >/dev/null 2>&1
}
function runtime_home() {
if [[ "${HOME}" != "" ]]; then
echo "${HOME}"
elif [[ "${USERPROFILE}" != "" ]]; then
echo "${USERPROFILE}"
else
echo "Failed get home dir" >&2
exit 1
fi
}
function runtime_arch() {
go env GOARCH
}
function runtime_os() {
go env GOOS
}
function runtime_arch_alias() {
local arch
arch="$(runtime_arch)"
case "${arch}" in
amd64)
echo "x86_64"
;;
arm64)
echo "aarch64"
;;
*)
echo "${arch}"
;;
esac
}
function _install_gcloud() {
mkdir -p /usr/local/lib/
curl https://dl.google.com/dl/cloudsdk/channels/rapid/google-cloud-sdk.tar.gz -o /tmp/google-cloud-sdk.tar.gz
tar xzf /tmp/google-cloud-sdk.tar.gz -C /usr/local/lib/
rm /tmp/google-cloud-sdk.tar.gz
/usr/local/lib/google-cloud-sdk/install.sh \
--bash-completion=false \
--usage-reporting=false \
--quiet
ln -s /usr/local/lib/google-cloud-sdk/bin/gcloud "${LOCAL_BIN_DIR}/gcloud"
ln -s /usr/local/lib/google-cloud-sdk/bin/gsutil "${LOCAL_BIN_DIR}/gsutil"
gcloud info
gcloud config list
gcloud auth list
}
function install_gsutil() {
if command_exist gsutil; then
return 0
fi
mkdir -p "${LOCAL_BIN_DIR}"
_install_gcloud
if ! command_exist gsutil; then
echo gsutil is installed but not effective >&2
return 1
fi
gsutil version
}
function install_go() {
if command_exist go; then
if [[ $(go version | awk -F . '{ print $2 }') -ge $(echo "${GO_VERSION}" | awk -F . '{ print $2 }') ]]; then
return 0
fi
fi
mkdir -p "${LOCAL_BIN_DIR}/go${GO_VERSION}"
curl -SL -o /tmp/go.tar.gz "https://dl.google.com/go/go${GO_VERSION}.$(runtime_os)-$(runtime_arch).tar.gz" &&
tar -C "${LOCAL_BIN_DIR}/go${GO_VERSION}" -xzf /tmp/go.tar.gz &&
rm /tmp/go.tar.gz
ln -s "${LOCAL_BIN_DIR}/go${GO_VERSION}/go/bin/go" "${LOCAL_BIN_DIR}/go${GO_VERSION}/go/bin/gofmt" "${LOCAL_BIN_DIR}/"
if ! command_exist go; then
echo go is installed but not effective >&2
return 1
fi
if [[ $(go version | awk -F . '{ print $2 }') -lt $(echo "${GO_VERSION}" | awk -F . '{ print $2 }') ]]; then
echo go version is lower than ${GO_VERSION} >&2
return 1
fi
}
function install_kind() {
if command_exist kind; then
return 0
fi
mkdir -p "${LOCAL_BIN_DIR}"
curl -SL -o "${LOCAL_BIN_DIR}/kind" "https://kind.sigs.k8s.io/dl/v${KIND_VERSION}/kind-$(runtime_os)-$(runtime_arch)" &&
chmod +x "${LOCAL_BIN_DIR}/kind"
if ! command_exist kind; then
echo kind is installed but not effective >&2
return 1
fi
kind version
}
function install_kubectl() {
if command_exist kubectl; then
return 0
fi
mkdir -p "${LOCAL_BIN_DIR}"
curl -SL -o "${LOCAL_BIN_DIR}/kubectl" "https://dl.k8s.io/release/v${KUBE_VERSION}/bin/$(runtime_os)/$(runtime_arch)/kubectl" &&
chmod +x "${LOCAL_BIN_DIR}/kubectl"
if ! command_exist kubectl; then
echo kubectl is installed but not effective >&2
return 1
fi
kubectl version --client=true
}
function install_buildx() {
local binary
if docker buildx version; then
return 0
fi
binary="$(runtime_home)/.docker/cli-plugins/docker-buildx"
mkdir -p "$(dirname "${binary}")" &&
wget -O "${binary}" "https://github.com/docker/buildx/releases/download/v${BUILDX_VERSION}/buildx-v${BUILDX_VERSION}.$(runtime_os)-$(runtime_arch)" &&
chmod +x "${binary}"
if ! docker buildx version; then
echo docker-buildx is installed but not effective >&2
return 1
fi
if ! docker buildx inspect --builder kwok >/dev/null 2>&1; then
docker buildx create --use --name kwok >/dev/null 2>&1
fi
}
function install_kustomize() {
if command_exist kustomize; then
return 0
fi
mkdir -p "${LOCAL_BIN_DIR}"
GOBIN="${LOCAL_BIN_DIR}" go install sigs.k8s.io/kustomize/kustomize/v5@v${KUSTOMIZE_VERSION}
if ! command_exist kustomize; then
echo kustomize is installed but not effective >&2
return 1
fi
kustomize version
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
function usage() {
local requirements=(
gsutil
kubectl
kind
buildx
)
echo "Usage: ${0} [flags] [requirements]"
echo " Empty argument will install all requirements."
echo " REQUIREMENT:"
for c in "${requirements[@]}"; do
echo " - ${c}"
done
}
function main() {
local arg
while [[ $# -gt 0 ]]; do
arg="$1"
case "${arg}" in
--help)
usage
exit 0
;;
-*)
echo "Unknown argument: ${arg}"
usage
exit 1
;;
*)
"install_${arg}" || exit 1
shift
;;
esac
done
}
main "${@}"
fi