Debian Codegraph @

tools/internal_ci/linux/grpc_xds_k8s_install_test_driver.sh

raw
1 #!/usr/bin/env bash
2 # Copyright 2020 gRPC authors.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 # TODO(sergiitk): move to grpc/grpc when implementing support of other languages
16 set -eo pipefail
17
18 # Constants
19 readonly PYTHON_VERSION="${PYTHON_VERSION:-3.7}"
20 # Test driver
21 readonly TEST_DRIVER_REPO_NAME="grpc"
22 readonly TEST_DRIVER_REPO_URL="https://github.com/${TEST_DRIVER_REPO_OWNER:-grpc}/grpc.git"
23 readonly TEST_DRIVER_BRANCH="${TEST_DRIVER_BRANCH:-master}"
24 readonly TEST_DRIVER_PATH="tools/run_tests/xds_k8s_test_driver"
25 readonly TEST_DRIVER_PROTOS_PATH="src/proto/grpc/testing"
26
27 # GKE cluster identifiers.
28 readonly GKE_CLUSTER_PSM_LB="psm-lb"
29 readonly GKE_CLUSTER_PSM_SECURITY="psm-security"
30 readonly GKE_CLUSTER_PSM_BASIC="psm-basic"
31
32 #######################################
33 # Determines the cluster name and zone based on the given cluster identifier.
34 # Globals:
35 # GKE_CLUSTER_NAME: Set to reflect the cluster name to use
36 # GKE_CLUSTER_ZONE: Set to reflect the cluster zone to use.
37 # Arguments:
38 # The cluster identifier
39 # Outputs:
40 # Writes the output to stdout, stderr
41 #######################################
42 activate_gke_cluster() {
43 case $1 in
44 GKE_CLUSTER_PSM_LB)
45 GKE_CLUSTER_NAME="interop-test-psm-lb-v1-us-central1-a"
46 GKE_CLUSTER_ZONE="us-central1-a"
47 ;;
48 GKE_CLUSTER_PSM_SECURITY)
49 GKE_CLUSTER_NAME="interop-test-psm-sec-v2-us-central1-a"
50 GKE_CLUSTER_ZONE="us-central1-a"
51 ;;
52 GKE_CLUSTER_PSM_BASIC)
53 GKE_CLUSTER_NAME="interop-test-psm-basic"
54 GKE_CLUSTER_ZONE="us-central1-c"
55 ;;
56 *)
57 echo "Unknown GKE cluster: ${1}"
58 exit 1
59 ;;
60 esac
61 echo "Activated GKE cluster: GKE_CLUSTER_NAME=${GKE_CLUSTER_NAME} GKE_CLUSTER_ZONE=${GKE_CLUSTER_ZONE}"
62 }
63
64 #######################################
65 # Determines the secondary cluster name and zone based on the given cluster
66 # identifier.
67 # Globals:
68 # GKE_CLUSTER_NAME: Set to reflect the cluster name to use
69 # GKE_CLUSTER_ZONE: Set to reflect the cluster zone to use.
70 # Arguments:
71 # The cluster identifier
72 # Outputs:
73 # Writes the output to stdout, stderr
74 #######################################
75 activate_secondary_gke_cluster() {
76 case $1 in
77 GKE_CLUSTER_PSM_LB)
78 SECONDARY_GKE_CLUSTER_NAME="interop-test-psm-lb-v1-us-west1-b"
79 SECONDARY_GKE_CLUSTER_ZONE="us-west1-b"
80 ;;
81 GKE_CLUSTER_PSM_SECURITY)
82 SECONDARY_GKE_CLUSTER_NAME="interop-test-psm-sec-v2-us-west1-b"
83 SECONDARY_GKE_CLUSTER_ZONE="us-west1-b"
84 ;;
85 *)
86 echo "Unknown secondary GKE cluster: ${1}"
87 exit 1
88 ;;
89 esac
90 echo "Activated secondary GKE cluster: GKE_CLUSTER_NAME=${GKE_CLUSTER_NAME} GKE_CLUSTER_ZONE=${GKE_CLUSTER_ZONE}"
91 }
92
93 #######################################
94 # Run command end report its exit code. Doesn't exit on non-zero exit code.
95 # Globals:
96 # None
97 # Arguments:
98 # Command to execute
99 # Outputs:
100 # Writes the output of given command to stdout, stderr
101 #######################################
102 run_ignore_exit_code() {
103 local exit_code=-1
104 "$@" || exit_code=$?
105 echo "Exit code: ${exit_code}"
106 }
107
108 #######################################
109 # Parses information about git repository at given path to global variables.
110 # Globals:
111 # GIT_ORIGIN_URL: Populated with the origin URL of git repo used for the build
112 # GIT_COMMIT: Populated with the SHA-1 of git commit being built
113 # GIT_COMMIT_SHORT: Populated with the short SHA-1 of git commit being built
114 # Arguments:
115 # Git source dir
116 #######################################
117 parse_src_repo_git_info() {
118 local src_dir="${SRC_DIR:?SRC_DIR must be set}"
119 readonly GIT_ORIGIN_URL=$(git -C "${src_dir}" remote get-url origin)
120 readonly GIT_COMMIT=$(git -C "${src_dir}" rev-parse HEAD)
121 readonly GIT_COMMIT_SHORT=$(git -C "${src_dir}" rev-parse --short HEAD)
122 }
123
124
125 #######################################
126 # Checks if the given string is a version branch.
127 # Version branches: "master", "v1.47.x"
128 # NOT version branches: "v1.47.0", "1.47.x", "", "dev", "main"
129 # Arguments:
130 # Version to test
131 #######################################
132 is_version_branch() {
133 if [ $# -eq 0 ]; then
134 echo "Usage is_version_branch VERSION"
135 false
136 return
137 fi
138 if [[ $1 == "master" ]]; then
139 true
140 return
141 fi
142 # Do not inline version_regex: keep it a string to avoid issues with escaping chars in ~= expr.
143 local version_regex='^v[0-9]+\.[0-9]+\.x$'
144 [[ "${1}" =~ $version_regex ]]
145 }
146
147 #######################################
148 # List GCR image tags matching given tag name.
149 # Arguments:
150 # Image name
151 # Tag name
152 # Outputs:
153 # Writes the table with the list of found tags to stdout.
154 # If no tags found, the output is an empty string.
155 #######################################
156 gcloud_gcr_list_image_tags() {
157 gcloud container images list-tags --format="table[box](tags,digest,timestamp.date())" --filter="tags:$2" "$1"
158 }
159
160 #######################################
161 # A helper to execute `gcloud -q components update`.
162 # Arguments:
163 # None
164 # Outputs:
165 # Writes the output of `gcloud` command to stdout, stderr
166 #######################################
167 gcloud_update() {
168 echo "Update gcloud components:"
169 gcloud -q components update
170 }
171
172 #######################################
173 # Create kube context authenticated with GKE cluster, saves context name.
174 # to KUBE_CONTEXT
175 # Globals:
176 # GKE_CLUSTER_NAME
177 # GKE_CLUSTER_ZONE
178 # KUBE_CONTEXT: Populated with name of kubectl context with GKE cluster access
179 # SECONDARY_KUBE_CONTEXT: Populated with name of kubectl context with secondary GKE cluster access, if any
180 # Arguments:
181 # None
182 # Outputs:
183 # Writes the output of `gcloud` command to stdout, stderr
184 # Writes authorization info $HOME/.kube/config
185 #######################################
186 gcloud_get_cluster_credentials() {
187 if [[ -n "${SECONDARY_GKE_CLUSTER_NAME}" && -n "${SECONDARY_GKE_CLUSTER_ZONE}" ]]; then
188 gcloud container clusters get-credentials "${SECONDARY_GKE_CLUSTER_NAME}" --zone "${SECONDARY_GKE_CLUSTER_ZONE}"
189 readonly SECONDARY_KUBE_CONTEXT="$(kubectl config current-context)"
190 else
191 readonly SECONDARY_KUBE_CONTEXT=""
192 fi
193 gcloud container clusters get-credentials "${GKE_CLUSTER_NAME}" --zone "${GKE_CLUSTER_ZONE}"
194 readonly KUBE_CONTEXT="$(kubectl config current-context)"
195 }
196
197 #######################################
198 # Clone the source code of the test driver to $TEST_DRIVER_REPO_DIR, unless
199 # given folder exists.
200 # Globals:
201 # TEST_DRIVER_REPO_URL
202 # TEST_DRIVER_BRANCH
203 # TEST_DRIVER_REPO_DIR: path to the repo containing the test driver
204 # TEST_DRIVER_REPO_DIR_USE_EXISTING: set non-empty value to use exiting
205 # clone of the driver repo located at $TEST_DRIVER_REPO_DIR.
206 # Useful for debugging the build script locally.
207 # Arguments:
208 # None
209 # Outputs:
210 # Writes the output of `git` command to stdout, stderr
211 # Writes driver source code to $TEST_DRIVER_REPO_DIR
212 #######################################
213 test_driver_get_source() {
214 if [[ -n "${TEST_DRIVER_REPO_DIR_USE_EXISTING}" && -d "${TEST_DRIVER_REPO_DIR}" ]]; then
215 echo "Using exiting driver directory: ${TEST_DRIVER_REPO_DIR}."
216 else
217 echo "Cloning driver to ${TEST_DRIVER_REPO_URL} branch ${TEST_DRIVER_BRANCH} to ${TEST_DRIVER_REPO_DIR}"
218 git clone -b "${TEST_DRIVER_BRANCH}" --depth=1 "${TEST_DRIVER_REPO_URL}" "${TEST_DRIVER_REPO_DIR}"
219 fi
220 }
221
222 #######################################
223 # Install Python modules from required in $TEST_DRIVER_FULL_DIR/requirements.txt
224 # to Python virtual environment. Creates and activates Python venv if necessary.
225 # Globals:
226 # TEST_DRIVER_FULL_DIR
227 # PYTHON_VERSION
228 # Arguments:
229 # None
230 # Outputs:
231 # Writes the output of `python`, `pip` commands to stdout, stderr
232 # Writes the list of installed modules to stdout
233 #######################################
234 test_driver_pip_install() {
235 echo "Install python dependencies"
236 cd "${TEST_DRIVER_FULL_DIR}"
237
238 # Create and activate virtual environment unless already using one
239 if [[ -z "${VIRTUAL_ENV}" ]]; then
240 local venv_dir="${TEST_DRIVER_FULL_DIR}/venv"
241 if [[ -d "${venv_dir}" ]]; then
242 echo "Found python virtual environment directory: ${venv_dir}"
243 else
244 echo "Creating python virtual environment: ${venv_dir}"
245 "python${PYTHON_VERSION}" -m venv "${venv_dir}"
246 fi
247 # Intentional: No need to check python venv activate script.
248 # shellcheck source=/dev/null
249 source "${venv_dir}/bin/activate"
250 fi
251
252 python3 -m pip install -r requirements.txt
253 echo "Installed Python packages:"
254 python3 -m pip list
255 }
256
257 #######################################
258 # Compile proto-files needed for the test driver
259 # Globals:
260 # TEST_DRIVER_REPO_DIR
261 # TEST_DRIVER_FULL_DIR
262 # TEST_DRIVER_PROTOS_PATH
263 # Arguments:
264 # None
265 # Outputs:
266 # Writes the output of `python -m grpc_tools.protoc` to stdout, stderr
267 # Writes the list if compiled python code to stdout
268 # Writes compiled python code with proto messages and grpc services to
269 # $TEST_DRIVER_FULL_DIR/src/proto
270 #######################################
271 test_driver_compile_protos() {
272 declare -a protos
273 protos=(
274 "${TEST_DRIVER_PROTOS_PATH}/test.proto"
275 "${TEST_DRIVER_PROTOS_PATH}/messages.proto"
276 "${TEST_DRIVER_PROTOS_PATH}/empty.proto"
277 )
278 echo "Generate python code from grpc.testing protos: ${protos[*]}"
279 cd "${TEST_DRIVER_REPO_DIR}"
280 python3 -m grpc_tools.protoc \
281 --proto_path=. \
282 --python_out="${TEST_DRIVER_FULL_DIR}" \
283 --grpc_python_out="${TEST_DRIVER_FULL_DIR}" \
284 "${protos[@]}"
285 local protos_out_dir="${TEST_DRIVER_FULL_DIR}/${TEST_DRIVER_PROTOS_PATH}"
286 echo "Generated files ${protos_out_dir}:"
287 ls -Fl "${protos_out_dir}"
288 }
289
290 #######################################
291 # Installs the test driver and it's requirements.
292 # https://github.com/grpc/grpc/tree/master/tools/run_tests/xds_k8s_test_driver#installation
293 # Globals:
294 # TEST_DRIVER_REPO_DIR: Populated with the path to the repo containing
295 # the test driver
296 # TEST_DRIVER_FULL_DIR: Populated with the path to the test driver source code
297 # Arguments:
298 # The directory for test driver's source code
299 # Outputs:
300 # Writes the output to stdout, stderr
301 #######################################
302 test_driver_install() {
303 readonly TEST_DRIVER_REPO_DIR="${1:?Usage test_driver_install TEST_DRIVER_REPO_DIR}"
304 readonly TEST_DRIVER_FULL_DIR="${TEST_DRIVER_REPO_DIR}/${TEST_DRIVER_PATH}"
305 test_driver_get_source
306 test_driver_pip_install
307 test_driver_compile_protos
308 }
309
310 #######################################
311 # Outputs Kokoro image version and Ubuntu's lsb_release
312 # Arguments:
313 # None
314 # Outputs:
315 # Writes the output to stdout
316 #######################################
317 kokoro_print_version() {
318 echo "Kokoro VM version:"
319 if [[ -f /VERSION ]]; then
320 cat /VERSION
321 fi
322 run_ignore_exit_code lsb_release -a
323 }
324
325 #######################################
326 # Report extra information about the job via sponge properties.
327 # Globals:
328 # KOKORO_ARTIFACTS_DIR
329 # GIT_ORIGIN_URL
330 # GIT_COMMIT_SHORT
331 # TESTGRID_EXCLUDE
332 # Arguments:
333 # None
334 # Outputs:
335 # Writes the output to stdout
336 # Writes job properties to $KOKORO_ARTIFACTS_DIR/custom_sponge_config.csv
337 #######################################
338 kokoro_write_sponge_properties() {
339 # CSV format: "property_name","property_value"
340 # Bump TESTS_FORMAT_VERSION when reported test name changed enough to when it
341 # makes more sense to discard previous test results from a testgrid board.
342 # Use GIT_ORIGIN_URL to exclude test runs executed against repo forks from
343 # testgrid reports.
344 cat >"${KOKORO_ARTIFACTS_DIR}/custom_sponge_config.csv" <<EOF
345 TESTS_FORMAT_VERSION,2
346 TESTGRID_EXCLUDE,${TESTGRID_EXCLUDE:-0}
347 GIT_ORIGIN_URL,${GIT_ORIGIN_URL:?GIT_ORIGIN_URL must be set}
348 GIT_COMMIT_SHORT,${GIT_COMMIT_SHORT:?GIT_COMMIT_SHORT must be set}
349 EOF
350 echo "Sponge properties:"
351 cat "${KOKORO_ARTIFACTS_DIR}/custom_sponge_config.csv"
352 }
353
354 #######################################
355 # Configure Python virtual environment on Kokoro VM.
356 # Arguments:
357 # None
358 # Outputs:
359 # Writes the output of `pyenv` commands to stdout
360 #######################################
361 kokoro_setup_python_virtual_environment() {
362 # Kokoro provides pyenv, so use it instead of `python -m venv`
363 echo "Setup pyenv environment"
364 eval "$(pyenv init -)"
365 eval "$(pyenv virtualenv-init -)"
366 py_latest_patch="$(pyenv versions --bare --skip-aliases | grep -E "^${PYTHON_VERSION}\.[0-9]{1,2}$" | sort --version-sort | tail -n 1)"
367 echo "Activating python ${py_latest_patch} virtual environment"
368 pyenv virtualenv --no-pip "${py_latest_patch}" k8s_xds_test_runner
369 pyenv local k8s_xds_test_runner
370 pyenv activate k8s_xds_test_runner
371 python3 -m ensurepip
372 # pip is fixed to 21.0.1 due to issue https://github.com/pypa/pip/pull/9835
373 # internal details: b/186411224
374 # TODO(sergiitk): revert https://github.com/grpc/grpc/pull/26087 when 21.1.1 released
375 python3 -m pip install -U pip==21.0.1
376 python3 -m pip --version
377 }
378
379 #######################################
380 # Installs and configures the test driver on Kokoro VM.
381 # Globals:
382 # KOKORO_ARTIFACTS_DIR
383 # KOKORO_JOB_NAME
384 # TEST_DRIVER_REPO_NAME
385 # TESTING_VERSION: Populated with the version branch under test, f.e. v1.42.x, master
386 # SRC_DIR: Populated with absolute path to the source repo on Kokoro VM
387 # TEST_DRIVER_REPO_DIR: Populated with the path to the repo containing
388 # the test driver
389 # TEST_DRIVER_FULL_DIR: Populated with the path to the test driver source code
390 # TEST_DRIVER_FLAGFILE: Populated with relative path to test driver flagfile
391 # TEST_XML_OUTPUT_DIR: Populated with the path to test xUnit XML report
392 # KUBE_CONTEXT: Populated with name of kubectl context with GKE cluster access
393 # SECONDARY_KUBE_CONTEXT: Populated with name of kubectl context with secondary GKE cluster access, if any
394 # GIT_ORIGIN_URL: Populated with the origin URL of git repo used for the build
395 # GIT_COMMIT: Populated with the SHA-1 of git commit being built
396 # GIT_COMMIT_SHORT: Populated with the short SHA-1 of git commit being built
397 # Arguments:
398 # The name of github repository being built
399 # Outputs:
400 # Writes the output to stdout, stderr, files
401 #######################################
402 kokoro_setup_test_driver() {
403 local src_repository_name="${1:?Usage kokoro_setup_test_driver GITHUB_REPOSITORY_NAME}"
404 # Capture Kokoro VM version info in the log.
405 kokoro_print_version
406
407 # All grpc kokoro jobs names structured to have the version identifier in the third position:
408 # - grpc/core/master/linux/...
409 # - grpc/core/v1.42.x/branch/linux/...
410 # - grpc/java/v1.47.x/branch/...
411 # - grpc/go/v1.47.x/branch/...
412 # - grpc/node/v1.6.x/...
413 readonly TESTING_VERSION=$(echo "${KOKORO_JOB_NAME}" | cut -d '/' -f3)
414
415 # Kokoro clones repo to ${KOKORO_ARTIFACTS_DIR}/github/${GITHUB_REPOSITORY}
416 local github_root="${KOKORO_ARTIFACTS_DIR}/github"
417 readonly SRC_DIR="${github_root}/${src_repository_name}"
418 local test_driver_repo_dir
419 test_driver_repo_dir="${TEST_DRIVER_REPO_DIR:-$(mktemp -d)/${TEST_DRIVER_REPO_NAME}}"
420 parse_src_repo_git_info SRC_DIR
421 kokoro_write_sponge_properties
422 kokoro_setup_python_virtual_environment
423
424 # gcloud requires python, so this should be executed after pyenv setup
425 gcloud_update
426 gcloud_get_cluster_credentials
427 test_driver_install "${test_driver_repo_dir}"
428 # shellcheck disable=SC2034 # Used in the main script
429 readonly TEST_DRIVER_FLAGFILE="config/grpc-testing.cfg"
430 # Test artifacts dir: xml reports, logs, etc.
431 local artifacts_dir="${KOKORO_ARTIFACTS_DIR}/artifacts"
432 # Folders after $artifacts_dir reported as target name
433 readonly TEST_XML_OUTPUT_DIR="${artifacts_dir}/${KOKORO_JOB_NAME}"
434 mkdir -p "${artifacts_dir}" "${TEST_XML_OUTPUT_DIR}"
435 }
436
437 #######################################
438 # Installs and configures the test driver for testing build script locally.
439 # Globals:
440 # TEST_DRIVER_REPO_NAME
441 # TEST_DRIVER_REPO_DIR: Unless provided, populated with a temporary dir with
442 # the path to the test driver repo
443 # SRC_DIR: Populated with absolute path to the source repo
444 # KUBE_CONTEXT: Populated with name of kubectl context with GKE cluster access
445 # TEST_DRIVER_FLAGFILE: Populated with relative path to test driver flagfile
446 # TEST_XML_OUTPUT_DIR: Populated with the path to test xUnit XML report
447 # GIT_ORIGIN_URL: Populated with the origin URL of git repo used for the build
448 # GIT_COMMIT: Populated with the SHA-1 of git commit being built
449 # GIT_COMMIT_SHORT: Populated with the short SHA-1 of git commit being built
450 # SECONDARY_KUBE_CONTEXT: Populated with name of kubectl context with secondary GKE cluster access, if any
451 # Arguments:
452 # The path to the folder containing the build script
453 # Outputs:
454 # Writes the output to stdout, stderr, files
455 #######################################
456 local_setup_test_driver() {
457 local script_dir="${1:?Usage: local_setup_test_driver SCRIPT_DIR}"
458 readonly SRC_DIR="$(git -C "${script_dir}" rev-parse --show-toplevel)"
459 parse_src_repo_git_info "${SRC_DIR}"
460 readonly KUBE_CONTEXT="${KUBE_CONTEXT:-$(kubectl config current-context)}"
461 readonly SECONDARY_KUBE_CONTEXT="${SECONDARY_KUBE_CONTEXT}"
462
463 local test_driver_repo_dir
464 test_driver_repo_dir="${TEST_DRIVER_REPO_DIR:-$(mktemp -d)/${TEST_DRIVER_REPO_NAME}}"
465 test_driver_install "${test_driver_repo_dir}"
466
467 # shellcheck disable=SC2034 # Used in the main script
468 readonly TEST_DRIVER_FLAGFILE="config/local-dev.cfg"
469 # Test out
470 readonly TEST_XML_OUTPUT_DIR="${TEST_DRIVER_FULL_DIR}/out"
471 mkdir -p "${TEST_XML_OUTPUT_DIR}"
472 }
473
474 #######################################
475 # Tag and push the given Docker image
476 # Arguments:
477 # The Docker image name
478 # The Docker image original tag name
479 # The Docker image new tag name
480 # Outputs:
481 # Writes the output to stdout, stderr, files
482 #######################################
483 tag_and_push_docker_image() {
484 local image_name="$1"
485 local from_tag="$2"
486 local to_tag="$3"
487
488 docker tag "${image_name}:${from_tag}" "${image_name}:${to_tag}"
489 docker push "${image_name}:${to_tag}"
490 }