1#!/usr/bin/env bash 2# Copyright 2021 The 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 16set -eo pipefail 17 18# Constants 19readonly GITHUB_REPOSITORY_NAME="grpc" 20readonly TEST_DRIVER_INSTALL_SCRIPT_URL="https://raw.githubusercontent.com/${TEST_DRIVER_REPO_OWNER:-grpc}/psm-interop/${TEST_DRIVER_BRANCH:-main}/.kokoro/psm_interop_kokoro_lib.sh" 21# Keep orphaned resources last 2 days. 22readonly KEEP_HOURS="${KEEP_HOURS:-48}" 23 24cleanup::activate_cluster() { 25 activate_gke_cluster "$1" 26 gcloud container clusters get-credentials "${GKE_CLUSTER_NAME}" \ 27 --zone "${GKE_CLUSTER_ZONE}" 28 CLEANUP_KUBE_CONTEXT="$(kubectl config current-context)" 29} 30 31cleanup::activate_secondary_cluster_as_primary() { 32 activate_secondary_gke_cluster "$1" 33 GKE_CLUSTER_NAME="${SECONDARY_GKE_CLUSTER_NAME}" 34 GKE_CLUSTER_ZONE="${SECONDARY_GKE_CLUSTER_ZONE}" 35 gcloud container clusters get-credentials "${GKE_CLUSTER_NAME}" \ 36 --zone "${GKE_CLUSTER_ZONE}" 37 CLEANUP_KUBE_CONTEXT="$(kubectl config current-context)" 38} 39 40cleanup::job::cleanup_td() { 41 cleanup::run_clean "$1" --mode=td 42} 43 44####################################### 45# The PSM_LB cluster is used by k8s_lb tests. 46# The keep hours is reduced to 6. 47####################################### 48cleanup::job::cleanup_cluster_lb_primary() { 49 cleanup::activate_cluster GKE_CLUSTER_PSM_LB 50 cleanup::run_clean "$1" --mode=k8s 51} 52 53####################################### 54# Secondary PSM_LB cluster is used by k8s_lb tests. 55# The keep hours is reduced to 6. 56####################################### 57cleanup::job::cleanup_cluster_lb_secondary() { 58 cleanup::activate_secondary_cluster_as_primary GKE_CLUSTER_PSM_LB 59 cleanup::run_clean "$1" --mode=k8s --secondary 60} 61 62####################################### 63# The BASIC cluster is used by url-map tests. Only cleaning the xds client 64# namespaces; the xds server namespaces are shared. 65# The keep hours is reduced to 6. 66####################################### 67cleanup::job::cleanup_cluster_url_map() { 68 cleanup::activate_cluster GKE_CLUSTER_PSM_BASIC 69 cleanup::run_clean "$1" --mode=k8s 70} 71 72####################################### 73# The SECURITY cluster is used by the security and authz test suites. 74####################################### 75cleanup::job::cleanup_cluster_security() { 76 cleanup::activate_cluster GKE_CLUSTER_PSM_SECURITY 77 cleanup::run_clean "$1" --mode=k8s 78} 79 80####################################### 81# Set common variables for the cleanup script. 82# Globals: 83# TEST_DRIVER_FLAGFILE: Relative path to test driver flagfile 84# TEST_XML_OUTPUT_DIR: Output directory for the test xUnit XML report 85# CLEANUP_KUBE_CONTEXT: The name of kubectl context with GKE cluster access. 86# Arguments: 87# Test job name. Currently only used to generate asset path, and uses 88# values from the cleanup_jobs array of main(). 89# TODO(sergiitk): turn job_name into action test methods of the cleanup. 90# Outputs: 91# Writes the output of test execution to stdout, stderr, 92# ${TEST_XML_OUTPUT_DIR}/${job_name}/sponge_log.log 93####################################### 94cleanup::run_clean() { 95 local job_name="${1:?Usage: cleanup::run_clean job_name}" 96 local out_dir="${TEST_XML_OUTPUT_DIR}/${job_name}" 97 mkdir -pv "${out_dir}" 98 # TODO(sergiitk): make it a test, where job_name is a separate method. 99 python3 -m bin.cleanup.cleanup \ 100 --flagfile="${TEST_DRIVER_FLAGFILE}" \ 101 --kube_context="${CLEANUP_KUBE_CONTEXT:-unset}" \ 102 --keep_hours="${KEEP_HOURS}" \ 103 "${@:2}" \ 104 |& tee "${out_dir}/sponge_log.log" 105} 106 107####################################### 108# Main function: provision software necessary to execute the cleanup tasks; 109# run them, and report the status. 110####################################### 111main() { 112 local script_dir 113 script_dir="$(dirname "$0")" 114 115 # Source the test captured from the master branch. 116 echo "Sourcing test driver install captured from: ${TEST_DRIVER_INSTALL_SCRIPT_URL}" 117 source /dev/stdin <<< "$(curl -s "${TEST_DRIVER_INSTALL_SCRIPT_URL}")" 118 set +x 119 120 # Valid cluster variables needed for the automatic driver setup. 121 activate_gke_cluster GKE_CLUSTER_PSM_BASIC 122 kokoro_setup_test_driver "${GITHUB_REPOSITORY_NAME}" 123 124 # Run tests 125 cd "${TEST_DRIVER_FULL_DIR}" 126 local failed_jobs=0 127 declare -a cleanup_jobs 128 cleanup_jobs=( 129 "cleanup_td" 130 "cleanup_cluster_lb_primary" 131 "cleanup_cluster_lb_secondary" 132 "cleanup_cluster_security" 133 "cleanup_cluster_url_map" 134 ) 135 for job_name in "${cleanup_jobs[@]}"; do 136 echo "-------------------- Starting job ${job_name} --------------------" 137 set -x 138 "cleanup::job::${job_name}" "${job_name}" || (( ++failed_jobs )) 139 set +x 140 echo "-------------------- Finished job ${job_name} --------------------" 141 done 142 echo "Failed job suites: ${failed_jobs}" 143 if (( failed_jobs > 0 )); then 144 exit 1 145 fi 146} 147 148main "$@" 149