|
| 1 | +#!/usr/bin/env bash |
| 2 | +# https://github.com/olivergondza/bash-strict-mode |
| 3 | +set -eEuo pipefail |
| 4 | +trap 's=$?; echo >&2 "$0: Error on line "$LINENO": $BASH_COMMAND"; exit $s' ERR |
| 5 | + |
| 6 | +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null && pwd)" |
| 7 | + |
| 8 | +function main() { |
| 9 | + if [[ $# -ne 2 ]]; then |
| 10 | + echo >&2 "Usage: $0 [IMAGE_A] [IMAGE_B]" |
| 11 | + exit 1 |
| 12 | + fi |
| 13 | + local img_a=$1 |
| 14 | + local img_b=$2 |
| 15 | + |
| 16 | + inv_a="$(mktemp -d gitops-must-gather-A-XXXX)" |
| 17 | + inv_b="$(mktemp -d gitops-must-gather-B-XXXX)" |
| 18 | + trap "rm -rf '${inv_a}' '${inv_b}'" EXIT |
| 19 | + |
| 20 | + gather "$img_a" "$inv_a" |
| 21 | + gather "$img_b" "$inv_b" |
| 22 | + |
| 23 | + diff --color=auto --recursive \ |
| 24 | + --ignore-matching-lines="resourceVersion: " \ |
| 25 | + "${inv_a}" "${inv_b}" |
| 26 | +} |
| 27 | + |
| 28 | +function gather() { |
| 29 | + image=$1 |
| 30 | + dir=$2 |
| 31 | + |
| 32 | + if ! oc adm must-gather --image="$image" --dest-dir="${dir}" 2>&1 | tee "${dir}/oc-adm-output.log"; then |
| 33 | + echo >&2 "Failed gathering for $image" |
| 34 | + return 1 |
| 35 | + fi |
| 36 | + |
| 37 | + sanitize "$image" "$dir" |
| 38 | +} |
| 39 | + |
| 40 | +function sanitize() { |
| 41 | + image=$1 |
| 42 | + dir=$2 |
| 43 | + |
| 44 | + # Unify names of the directories its name is based on image name |
| 45 | + mv "$dir"/*-sha256-* "$dir/__RESOURCES__" |
| 46 | + |
| 47 | + # In log files, drop image name, generated resource names, timestamp, line numbers, and transfer metrics |
| 48 | + sed -i -r \ |
| 49 | + -e "s~${image}~__IMAGE_TAG__~g" \ |
| 50 | + -e "s~must-gather-[a-z0-9]{5}~must-gather-XXXXX~g" \ |
| 51 | + -e 's~[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]+Z~__TIMESTAMP__~g' \ |
| 52 | + -e 's~gather_gitops:[0-9]+~gather_gitops:LL~g' \ |
| 53 | + -e '/total size is .* speedup is .*/d' \ |
| 54 | + -e '/sent .* received .* bytes\/sec/d' \ |
| 55 | + "$dir/oc-adm-output.log" "$dir/must-gather.logs" "$dir/__RESOURCES__/gather.logs" "$dir/__RESOURCES__/gather_gitops.log" |
| 56 | + |
| 57 | + # Timestamps are not going to match, just test there is the same number of them |
| 58 | + wc -l < "$dir/timestamp" > "$dir/timestamp" |
| 59 | + wc -l < "$dir/__RESOURCES__/timestamp" > "$dir/__RESOURCES__/timestamp" |
| 60 | +} |
| 61 | + |
| 62 | +main "$@" |
0 commit comments