grpc_xds_k8s_install_test_driver.sh 17 KB

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