I've created the following code , so you can use it as a wrapper. You can name the script whatever you want. For example exclude_ns_removal
#!/usr/bin/env bash
die ()
{
echo "$@" 1>&2
exit 1
}
usage ()
{
echo "usage: $0 [-h] [-v namespace_to_ignore] " 1>&2
exit 0
}
inarray ()
{
local n=$1 h
shift
for h in "$@"
do
[[ $n = "$h" ]] && return
done
return 1
}
while getopts ":v:h" opt; do
case $opt in
h)
usage
;;
v)
case $OPTARG in
'' | *[0-9]*)
die "Digits not allowed $OPTARG"
;;
*)
val=$OPTARG
;;
esac
;;
:)
die "argument needed to -$OPTARG"
;;
*)
die "invalid switch -$OPTARG"
;;
esac
done
shift $((OPTIND - 1))
while IFS='/' read -r _ ns; do
a+=("$ns")
done < <(kubectl get namespaces --no-headers -o name)
if inarray "$val" "${a[@]}"; then
unset 'a'
{
while IFS='/' read -r _ ns; do
a+=("$ns")
for i in "${!a[@]}"
do
if [[ ${a[i]} == $val ]]; then
unset 'a[i]'
fi
done
done
} < <(kubectl get namespaces --no-headers -o name)
printf '%s\n\n' "Excluding ... $val"
for namespace in "${a[@]}"
do
printf 'Deleting ... %s\n' "$namespace"
done
else
die "No namespace found"
fi
Make the script executable:
chmod u+x exclude_ns_removal
Run it as follows:
./exclude_ns_removal -v kube-system
The result will be something like:
Excluding ... kube-system
Deleting ... app-system
Deleting ... db-system
If the output looks good, you should modify this line
printf 'Deleting ... %s\n' "$namespace"
to
kubectl delete namespace "$namespace"