Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hack/bin/integration-cleanup.sh: Use jq magic instead of bash #1432

Merged
merged 5 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions hack/bin/filter-old-releases.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.[]
| .parsedUpdatedTime = ( .updated
| sub("(?<time>.*)\\.[\\d]+(?<tz>.*)"; "\(.time)\(.tz)")
| strptime("%Y-%m-%d %H:%M:%S %z %Z")
| mktime
)
| select ( .parsedTime < (now - 3 * 60 * 60))
| [.name, .namespace]
| @tsv
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: why tabs?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think jq can either output a json array, a csv or a tsv. Out of these I thought tsv was the most compatible with bash as I can awk '{print $1}'. Do you have a better idea?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just stumbled upon it and was curious, as FS usually defaults to space.

34 changes: 9 additions & 25 deletions hack/bin/integration-cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,15 @@
#
# Motivation: cleanup of old test clusters that were not deleted (e.g. by the CI system, because it broke)

date_seconds() {
d=$1
if [[ $OSTYPE =~ darwin* ]]; then
date -j -f "%b %d %H:%M:%S %Y" "$date_str" +"%s"
else
date -d "$d" +"%s"
fi
}
set -euo pipefail

date_now=$(date +"%s")
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
TOP_LEVEL="$DIR/../.."

# Since helm only shows information as hard-to-parse text pending https://github.com/kubernetes/helm/pull/2950
# we are stuck with brittle grep/awk magic.

IFS=$'\n'
for release in $(helm list -A | grep test-); do
name=$(echo "$release" | awk '{print $1}')
namespace=$(echo "$release" | awk '{print $2}')
date_str=$(echo "$release" | awk '{print $4" "$5" "$6}')
date_s=$(date_seconds "$date_str")
diff=$(( $date_now - $date_s ))
if [ $diff -ge 7200 ]; then
echo "test release '$name' older than 2 hours; deleting..."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wanna keep that echo statement in some modified form?

releases=$(helm list -A -f 'test-' -o json \
| jq -r -f "$DIR/filter-old-releases.jq")
while read line; do
name=$(awk '{print $1}' <<<"$line")
namespace=$(awk '{print $2}' <<<"$line")
helm delete -n "$namespace" "$name"
fi

done
unset IFS
done <<<"$releases"