github.com/zppinho/prow@v0.0.0-20240510014325-1738badeb017/test/integration/clear.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 Kubernetes resources in the test cluster. This script guarantees that
    17  # these operations are only executed against the test cluster, and so is safer
    18  # than running kubectl directly (because your default context might be set to a
    19  # production instance).
    20  
    21  set -o errexit
    22  set -o nounset
    23  set -o pipefail
    24  
    25  SCRIPT_ROOT="$(cd "$(dirname "$0")" && pwd)"
    26  source "${SCRIPT_ROOT}"/lib.sh
    27  
    28  function usage() {
    29    >&2 cat <<EOF
    30  Delete Kubernetes resources in the test cluster.
    31  
    32  Usage: $0 [options]
    33  
    34  Examples:
    35    # Delete ProwJob CRs and test pods.
    36    $0 -all
    37  
    38    # Delete all ProwJob CRs.
    39    $0 -prowjobs
    40  
    41    # Delete all test pods.
    42    $0 -test-pods
    43  
    44  Options:
    45      -all:
    46          Alias for -test-pods -prowjobs -components.
    47  
    48      -components:
    49          Delete all Prow components (deployments). This brings down the Prow
    50          components (pods) such that they are not restarted by Kubernetes (which
    51          would happen if you deleted just their pods).
    52  
    53      -test-data:
    54          Alias for -test-pods -prowjobs.
    55  
    56      -prowjobs:
    57          Delete all ProwJob CRs.
    58  
    59      -test-pods:
    60          Delete all test pods.
    61  
    62      -help:
    63          Display this help message.
    64  EOF
    65  }
    66  
    67  function main() {
    68    declare -a delete_args
    69  
    70    if ! (($#)); then
    71      echo >&2 "teardown: missing flag: must provide one of -all, -prowjobs, or -test-pods"
    72      return 1
    73    fi
    74  
    75    for arg in "$@"; do
    76      case "${arg}" in
    77        -all)
    78          delete_args+=(-prowjobs)
    79          delete_args+=(-test-pods)
    80          delete_args+=(-components)
    81          ;;
    82        -prowjobs)
    83          delete_args+=("${arg}")
    84          ;;
    85        -test-pods)
    86          delete_args+=("${arg}")
    87          ;;
    88        -components)
    89          delete_args+=("${arg}")
    90          ;;
    91        -test-data)
    92          delete_args+=(-prowjobs)
    93          delete_args+=(-test-pods)
    94          ;;
    95        -help)
    96          usage
    97          return
    98          ;;
    99        --*)
   100          echo >&2 "cannot use flags with two leading dashes ('--...'), use single dashes instead ('-...')"
   101          return 1
   102          ;;
   103      esac
   104    done
   105  
   106    if [[ -n "${delete_args[*]}" ]]; then
   107      if [[ " ${delete_args[*]} " =~ " -prowjobs " ]]; then
   108        delete_prowjobs
   109      fi
   110      if [[ " ${delete_args[*]} " =~ " -test-pods " ]]; then
   111        delete_test_pods
   112      fi
   113      if [[ " ${delete_args[*]} " =~ " -components " ]]; then
   114        delete_components
   115      fi
   116    fi
   117  }
   118  
   119  function delete_components() {
   120    log "KIND cluster: deleting all Prow components (deployments)"
   121    do_kubectl delete deployments.apps --all
   122  }
   123  
   124  function delete_test_pods() {
   125    log "KIND cluster: deleting all pods in the test-pods namespace"
   126    do_kubectl --namespace=test-pods delete pods --all
   127  }
   128  
   129  function delete_prowjobs() {
   130    log "KIND cluster: deleting all ProwJobs in the default namespace"
   131    do_kubectl delete prowjobs.prow.k8s.io --all
   132  }
   133  
   134  main "$@"