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

Add a new preserve_namespace flag #24

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
13 changes: 11 additions & 2 deletions prune-releases.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function usage()
echo ' --namespace-filter="^feature-.+" <Namespace regex filter>'
echo ' --helm-release-negate-filter="-permanent$" <Negated Helm release regex filter>'
echo ' --namespace-negate-filter="-permanent$" <Negated Namespace regex filter>'
echo ' --preserve-namespace'
echo ' --dry-run'
echo ""
echo "Example: $0 --older-than=\"2 weeks ago\" --helm-release-filter=\"^feature-.+-web$\" --namespace-filter=\"^feature-.+\""
Expand All @@ -25,6 +26,7 @@ older_than_filter=""
release_filter=""
namespace_filter=""
dry_run=""
preserve_namespace=""

while [ "$1" != "" ]; do
PARAM=`echo $1 | awk -F= '{print $1}'`
Expand Down Expand Up @@ -55,6 +57,9 @@ while [ "$1" != "" ]; do
--dry-run)
dry_run="TRUE"
;;
--preserve-namespace)
delete_namespace="TRUE"
;;
*)
echo "ERROR: unknown parameter \"$PARAM\""
usage
Expand All @@ -76,6 +81,10 @@ if [ -n "$dry_run" ]; then
echo "Dry run mode. Nothing will be deleted."
fi

if [ -n "$preserve_namespace" ]; then
echo "Namespace will not be deleted."
fi

counter=0
counter_keep=0
counter_delete=0
Expand Down Expand Up @@ -135,8 +144,8 @@ while read release_line ; do
counter_delete=$((counter_delete+1))
[ -z "$dry_run" ] && helm delete --namespace $release_namespace $release_name

# Delete the namespace if there are no other helm releases in it
if [ "$(helm list -a --namespace $release_namespace --output json | jq ". | length")" -eq 0 ]; then
# Delete the namespace if there are no other helm releases in it and the preserve_namespace flag is False
if [[ -z "$preserve_namespace" && "$(helm list -a --namespace $release_namespace --output json | jq ". | length")" -eq 0 ]]; then
[ -z "$dry_run" ] && kubectl delete ns $release_namespace
fi
fi
Expand Down