Sometimes we need to ensure that resources in Kubernetes are fully deleted before we setup other resources. In Kubernetes the timing and the synchronization can be very import and relevant.
In that blog post we see a function of a bash script that exactly does that job for namespaces. We are using a “for loop” combined with a nested “while loop” and other functionalities in bash to address that topic.
The blog post is organized in following sections:
- Example scenario
- Used bash scripting functionality
- Filter result values from a kubectl command
- Bash function code
- Summary
1. Example scenario
In that example we verify three different namespaces in a Kubernetes cluster, if they are deleted for two times. If they are deleted we move on with the remaining bash script and if they are not deleted we will stop the script execution.
2. Used bash scripting functionality
In our bash script “function” we are using a “for loop” combined with “an array” and a nested “while loop“.
These nested loops containing “if .. else” statements in combination of “string compare” with “variables” and an “exit” statement to end the script execution and we use a “break” statement to continue in the “function“, if the namespaces were successfully deleted.
Also we are using “bash pipes” in combination “assign output to a variable” to get the needed value from a terminal output.
List of used bash functionality:
3. Filter result values from a kubectl command
To filter the terminal output we use “assign output to a variable” in combination with a “grep” command for regular expressions and an awk command which is also for regular expressions to get the right value from the terminal output created by our kubectl command.
Example output of the used kubectl
command
kubectl get namespace default
NAME STATUS AGE
default Active 74d
4. Bash function code
Here is the full function:
function verifyDeletion () {
export max_retrys=2
j=0
array=("cert-manager" "olm" "operators")
export STATUS_SUCCESS=""
for i in "${array[@]}"
do
echo ""
echo "------------------------------------------------------------------------"
echo "Check $i"
j=0
export FIND=$i
while :
do
((j++))
STATUS_CHECK=$(kubectl get namespace $FIND | grep $FIND | awk '{print $2;}')
echo "Status: $STATUS_CHECK"
if [ "$STATUS_CHECK" = "$STATUS_SUCCESS" ]; then
echo "$(date +'%F %H:%M:%S') Status: $FIND is deleted"
echo "------------------------------------------------------------------------"
break
elif [[ $j -eq $max_retrys ]]; then
echo "$(date +'%F %H:%M:%S') Please run 'delete-everything-kubernetes.sh' first!"
echo "$(date +'%F %H:%M:%S') Prereqs aren't ready!"
echo "------------------------------------------------------------------------"
exit 1
else
echo "$(date +'%F %H:%M:%S') Status: $FIND($STATUS_CHECK)"
echo "------------------------------------------------------------------------"
fi
sleep 3
done
done
}
5. Summary
It is function is very useful and a good starting point for other nested loops related to bash automation for Kubernetes kubectl commands.
I hope this was useful to you and let’s see what’s next?
Greetings,
Thomas
#bash, #bashscripting, #kubernetes
Hi Thomas, Thanks for the brief explanation. In your verifyDeletion function you mentioned about delete-everything-kubernetes.sh BUT where is that bash script in this blog post or GitHub project?
LikeLike
As you see this is only a comment for a situation in this repository. It is not relevant to understand how the looping works.
Here is the repo with the script:
This is the repository https://github.com/IBM/operator-sample-go/tree/main/scripts
LikeLike
Thanks for your response, it helps me to understand how this works. Appreciate your posts on various topics, great Thomas!
LikeLiked by 1 person