github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/test/integration/teardown.sh (about) 1 #!/usr/bin/env bash 2 # Copyright 2021 The Kubernetes 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 16 # Delete the KIND cluster used for integration tests. 17 18 set -o errexit 19 set -o nounset 20 set -o pipefail 21 22 SCRIPT_ROOT="$(cd "$(dirname "$0")" && pwd)" 23 source "${SCRIPT_ROOT}"/lib.sh 24 25 function usage() { 26 >&2 cat <<EOF 27 Tear down resources created by the integration tests. 28 29 Usage: $0 [options] 30 31 Examples: 32 # Tear down all resources (KIND cluster and local Docker registry). 33 $0 -all 34 35 # Tear down only the KIND cluster. 36 $0 -kind-cluster 37 38 # Tear down only the local Docker registry. 39 $0 -local-registry 40 41 # Save KIND logs (these can be verbose) to directory "./logs". 42 $0 -save-logs=logs 43 44 Options: 45 -all: 46 Delete both the KIND cluster and local Docker registry. These two 47 resources are ultimately just Docker containers. You can check if these 48 have been deleted by watching "docker ps -a" output. 49 50 -kind-cluster: 51 Only delete the KIND cluster (container named "${_KIND_CLUSTER_NAME}-control-plane"). 52 53 -local-registry: 54 Only delete the local Docker registry (container named 55 "${LOCAL_DOCKER_REGISTRY_NAME}"). 56 57 -save-logs='': 58 Save KIND cluster logs to the given directory before tearing anything 59 down. This saves all log output of all containers in all namespaces. 60 61 This flag gets implied if the ARTIFACTS environment variable is set. 62 63 -help: 64 Display this help message. 65 EOF 66 } 67 68 function main() { 69 local artifacts_dir 70 declare -a teardown_args 71 72 if ! (($#)); then 73 echo >&2 "teardown: missing flag: must provide one of -all, -kind-cluster, or -local-registry" 74 return 1 75 fi 76 77 for arg in "$@"; do 78 case "${arg}" in 79 -all) 80 teardown_args+=(-kind-cluster) 81 teardown_args+=(-local-registry) 82 ;; 83 -kind-cluster) 84 teardown_args+=("${arg}") 85 ;; 86 -local-registry) 87 teardown_args+=("${arg}") 88 ;; 89 -save-logs=*) 90 artifacts_dir="${arg#-save-logs=}" 91 ;; 92 -help) 93 usage 94 return 95 ;; 96 --*) 97 echo >&2 "cannot use flags with two leading dashes ('--...'), use single dashes instead ('-...')" 98 return 1 99 ;; 100 esac 101 done 102 103 if [[ -n "${artifacts_dir:-}" ]]; then 104 save_logs "${artifacts_dir}" 105 fi 106 107 if [[ -n "${teardown_args[*]}" ]]; then 108 if [[ " ${teardown_args[*]} " =~ " -kind-cluster " ]]; then 109 teardown_kind_cluster 110 fi 111 if [[ " ${teardown_args[*]} " =~ " -local-registry " ]]; then 112 teardown_local_registry 113 fi 114 fi 115 } 116 117 function save_logs() { 118 local log_dir 119 log_dir="${1:-}" 120 121 # Grab KIND logs. KIND does a thorough job of exporting all logs from all pods 122 # across all namespaces, including previously deleted (failed) containers, so 123 # this is much better than what we can manually collect with kubectl. 124 # 125 # TODO(listx): Make horologium_test.go delete the 1-minute periodic job as 126 # part of cleanup. 127 # TODO(listx): Make horologium_test.go name its jobs after its test name. 128 log "Saving logs to ${log_dir}" 129 kind export logs --name "${_KIND_CLUSTER_NAME}" "${log_dir}" || true 130 } 131 132 function teardown_kind_cluster() { 133 log "Tearing down the KIND cluster" 134 kind delete cluster --name "${_KIND_CLUSTER_NAME}" || true 135 } 136 function teardown_local_registry() { 137 log "Deleting local registry (if any)" 138 docker stop "${LOCAL_DOCKER_REGISTRY_NAME}" 2>/dev/null || true 139 docker rm -f "${LOCAL_DOCKER_REGISTRY_NAME}" 2>/dev/null || true 140 } 141 142 main "$@"