k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/clean-up-old-snapshots.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2020 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  set -o errexit
    18  set -o nounset
    19  set -o pipefail
    20  
    21  print-usage-and-exit() {
    22    >&2 echo "Usage:"
    23    >&2 echo "    $0 delete|list <comma-sep-projects> <comma-sep-prefixes> <days-old>"
    24    >&2 echo "Example:"
    25    >&2 echo "    $0 list k8s-e2e-gce-1-1,k8s-e2e-gce-1-2  ci-e2e-scalability,ci-e2e-kubemark 30"
    26    exit "$1"
    27  }
    28  
    29  list-old-snapshots() {
    30    project=$1
    31    regexp=$2
    32    days_old=$3
    33    (
    34      set -o xtrace
    35      gcloud compute snapshots list \
    36        --project "$project" \
    37        --filter="creationTimestamp < -P${days_old}D AND name~\"${regexp}\"" \
    38        --format="value(name)"
    39    )
    40  }
    41  
    42  process-old-snapshots() {
    43    command=$1
    44    project=$2
    45    regexp=$3
    46    days_old=$4
    47    for snapshot in $(list-old-snapshots "$project" "$regexp" "$days_old"); do
    48      if [[ "$command" == "delete" ]]; then
    49        echo "Removing $snapshot ..."
    50        gcloud compute snapshots delete -q --project "$project" "$snapshot"
    51      else
    52        echo "Found: $snapshot"
    53      fi
    54    done
    55  }
    56  
    57  main() {
    58    if [ "$#" -ne 4 ]; then
    59      >&2 echo "Wrong number of parameters, expected 4, got $#"
    60      print-usage-and-exit 1
    61    fi
    62  
    63    command=$1
    64    projects=$2
    65    prefixes=$3
    66    days_old=$4
    67  
    68    if ! [[ "$command" =~ ^(list|delete)$ ]]; then
    69      >&2 echo "Invalid command: $command"
    70      print-usage-and-exit 2
    71    fi
    72    if ! [[ "$projects" =~ ^[a-z0-9,-]+$ ]]; then
    73      >&2 echo "Illegal characters in projects parameter: $projects"
    74      print-usage-and-exit 3
    75    fi
    76    if ! [[ "$prefixes" =~ ^[a-z0-9,.-]+$ ]]; then
    77      >&2 echo "Illegal characters in prefixes parameter: $prefixes"
    78      print-usage-and-exit 4
    79    fi
    80    if ! [[ "$days_old" =~ ^[0-9]+$ ]]; then
    81      >&2 echo "Days-old parameter must be an integer: $days_old"
    82      print-usage-and-exit 5
    83    fi
    84  
    85    prefixes_alt="${prefixes//,/|}"
    86    regexp="^(${prefixes_alt}).*-[0-9]{19}$"
    87    echo "Looking for snapshots matched by $regexp"
    88  
    89    IFS=',' read -ra projects_arr <<< "$projects"
    90  
    91    for project in "${projects_arr[@]}"; do
    92      echo "Processing project $project"
    93      process-old-snapshots "$command" "$project" "$regexp" "$days_old"
    94    done
    95  }
    96  
    97  main "$@"