[utility] ensure_ondemand_pod_exists improvements

This PS covers the case when ondemand pod does not
exists but the ondemand job is present. In this
case ensure_ondemand_pod_exists() function calls
do_cleanup() before re-creating ondemand job.

Change-Id: Iffd28317f38eb0d3ec47886e790902b12b5e22e3
This commit is contained in:
Sergiy Markin 2024-07-09 15:32:29 +00:00
parent 4f1c04c9ef
commit f60481c86a
6 changed files with 77 additions and 56 deletions

View File

@ -13,4 +13,4 @@
apiVersion: v1
description: etcdctl Client
name: etcdctl-utility
version: 0.1.5
version: 0.1.6

View File

@ -85,7 +85,7 @@ function ensure_ondemand_pod_exists() {
ONDEMAND_POD=$(echo "$POD_LISTING" | awk '{print $1}')
STATUS=$(echo "$POD_LISTING" | awk '{print $3}')
if [[ "$STATUS" == "Terminating" ]]; then
kubectl wait -n "$NAMESPACE" --for=delete pod/"$ONDEMAND_POD" --timeout=300s
kubectl wait -n "$NAMESPACE" --for=delete pod/"$ONDEMAND_POD" --timeout=300s 2>/dev/null
unset ONDEMAND_POD
unset STATUS
fi
@ -115,6 +115,8 @@ function ensure_ondemand_pod_exists() {
# If we reached this point with no ONDEMAND_POD, then we need to create
# a new on-demand job.
if [[ -z "$ONDEMAND_POD" ]]; then
echo "Cleaning up any remainings of previous runs..."
do_cleanup "$ONDEMAND_JOB" "$NAMESPACE"
echo "Creating new on-demand job in the $NAMESPACE namespace..."
/tmp/etcd-ondemand-job.sh "$NAMESPACE"
if [[ $? -ne 0 ]]; then
@ -143,12 +145,12 @@ function ensure_ondemand_pod_exists() {
ONDEMAND_POD=$(echo "$POD_LISTING" | awk '{print $1}')
STATUS=$(echo "$POD_LISTING" | awk '{print $3}')
if [[ "$STATUS" == "Terminating" ]]; then
kubectl wait -n "$NAMESPACE" --for=delete pod/"$ONDEMAND_POD" --timeout=300s
kubectl wait -n "$NAMESPACE" --for=delete pod/"$ONDEMAND_POD" --timeout=300s 2>/dev/null
unset ONDEMAND_POD
elif [[ "$STATUS" != "Running" ]]; then
kubectl wait -n "$NAMESPACE" --for condition=ready pod/"$ONDEMAND_POD" --timeout=300s
kubectl wait -n "$NAMESPACE" --for condition=ready pod/"$ONDEMAND_POD" --timeout=300s 2>/dev/null
elif [[ "$STATUS" != "Pending" ]]; then
kubectl wait -n "$NAMESPACE" --for condition=ready pod/"$ONDEMAND_POD" --timeout=300s
kubectl wait -n "$NAMESPACE" --for condition=ready pod/"$ONDEMAND_POD" --timeout=300s 2>/dev/null
fi
fi
@ -160,7 +162,7 @@ function ensure_ondemand_pod_exists() {
exit 1
fi
kubectl wait --for condition=ready --timeout=300s -n "$NAMESPACE" "pod/${ONDEMAND_POD}"
kubectl wait --for condition=ready --timeout=300s -n "$NAMESPACE" "pod/${ONDEMAND_POD}" 2>/dev/null
if [[ $? -ne 0 ]]; then
echo "ERROR: Failed to create a new on-demand pod. Exiting..."
exit 1
@ -176,20 +178,25 @@ function remove_job() {
JOB=$1
# Cleanup the last attempted job if there is one, wait for the pod to be deleted.
kubectl get job -n "$NAMESPACE" "$JOB"
kubectl get job -n "$NAMESPACE" "$JOB" 2>/dev/null
if [[ $? -eq 0 ]]; then
echo "Removing on-demand job $NAMESPACE $JOB"
ONDEMAND_POD=$(kubectl get pod -n "$NAMESPACE" | grep "$JOB" | awk '{print $1}')
kubectl delete job --ignore-not-found -n "$NAMESPACE" "$JOB"
kubectl wait --for=delete --timeout=300s -n "$NAMESPACE" job/"${JOB}"
if [[ $? -ne 0 ]]; then
echo "ERROR: could not destroy the $NAMESPACE $JOB job. Exiting..."
exit 1
ONDEMAND_JOB=$(kubectl get job -n "$NAMESPACE" | grep "$JOB" | awk '{print $1}')
if [[ ! -z ${ONDEMAND_JOB} ]]; then
kubectl delete job --ignore-not-found -n "$NAMESPACE" "$JOB"
kubectl wait --for=delete --timeout=300s -n "$NAMESPACE" job/"${JOB}" 2>/dev/null
if [[ $? -ne 0 ]]; then
echo "ERROR: could not destroy the $NAMESPACE $JOB job. Exiting..."
exit 1
fi
fi
kubectl wait --for=delete --timeout=300s -n "$NAMESPACE" pod/"${ONDEMAND_POD}"
if [[ $? -ne 0 ]]; then
echo "ERROR: could not destroy the $NAMESPACE $ONDEMAND_POD pod. Exiting..."
exit 1
ONDEMAND_POD=$(kubectl get pod -n "$NAMESPACE" | grep "$JOB" | awk '{print $1}')
if [[ ! -z ${ONDEMAND_POD} ]]; then
kubectl wait --for=delete --timeout=300s -n "$NAMESPACE" pod/"${ONDEMAND_POD}" 2>/dev/null
if [[ $? -ne 0 ]]; then
echo "ERROR: could not destroy the $NAMESPACE $ONDEMAND_POD pod. Exiting..."
exit 1
fi
fi
fi
}

View File

@ -13,4 +13,4 @@
apiVersion: v1
description: Porthole MySql Client
name: mysqlclient-utility
version: 0.1.8
version: 0.1.9

View File

@ -99,7 +99,7 @@ function ensure_ondemand_pod_exists() {
ONDEMAND_POD=$(echo "$POD_LISTING" | awk '{print $1}')
STATUS=$(echo "$POD_LISTING" | awk '{print $3}')
if [[ "$STATUS" == "Terminating" ]]; then
kubectl wait -n "$NAMESPACE" --for=delete pod/"$ONDEMAND_POD" --timeout=300s
kubectl wait -n "$NAMESPACE" --for=delete pod/"$ONDEMAND_POD" --timeout=300s 2>/dev/null
unset ONDEMAND_POD
unset STATUS
fi
@ -129,6 +129,8 @@ function ensure_ondemand_pod_exists() {
# If we reached this point with no ONDEMAND_POD, then we need to create
# a new on-demand job.
if [[ -z "$ONDEMAND_POD" ]]; then
echo "Cleaning up any remainings of previous runs..."
do_cleanup "$ONDEMAND_JOB" "$NAMESPACE"
echo "Creating new on-demand job in the $NAMESPACE namespace..."
/tmp/mariadb-ondemand-job.sh "$NAMESPACE"
if [[ $? -ne 0 ]]; then
@ -149,32 +151,31 @@ function ensure_ondemand_pod_exists() {
fi
done
# Determine the status of the on demand pod if it exists
POD_LISTING=$(kubectl get pod --selector application="$ONDEMAND_JOB",component=ondemand --no-headers --namespace "$NAMESPACE")
# Determine the status of the on demand pod if it exists POD_LISTING=$(kubectl get pod --selector application="$ONDEMAND_JOB",component=ondemand --no-headers --namespace "$NAMESPACE")
unset STATUS
unset ONDEMAND_POD
if [[ ! -z "$POD_LISTING" ]]; then
ONDEMAND_POD=$(echo "$POD_LISTING" | awk '{print $1}')
STATUS=$(echo "$POD_LISTING" | awk '{print $3}')
if [[ "$STATUS" == "Terminating" ]]; then
kubectl wait -n "$NAMESPACE" --for=delete pod/"$ONDEMAND_POD" --timeout=300s
kubectl wait -n "$NAMESPACE" --for=delete pod/"$ONDEMAND_POD" --timeout=300s 2>/dev/null
unset ONDEMAND_POD
elif [[ "$STATUS" != "Running" ]]; then
kubectl wait -n "$NAMESPACE" --for condition=ready pod/"$ONDEMAND_POD" --timeout=300s
kubectl wait -n "$NAMESPACE" --for condition=ready pod/"$ONDEMAND_POD" --timeout=300s 2>/dev/null
elif [[ "$STATUS" != "Pending" ]]; then
kubectl wait -n "$NAMESPACE" --for condition=ready pod/"$ONDEMAND_POD" --timeout=300s
kubectl wait -n "$NAMESPACE" --for condition=ready pod/"$ONDEMAND_POD" --timeout=300s 2>/dev/null
fi
fi
ONDEMAND_POD=$(kubectl get pods -n "$NAMESPACE" --selector=job-name="$ONDEMAND_JOB" -o json | jq -r .items[].metadata.name)
ONDEMAND_POD=$(kubectl get pods -n "$NAMESPACE" --selector=job-name="$ONDEMAND_JOB" -o json 2>/dev/null| jq -r .items[].metadata.name)
if [[ -z "$ONDEMAND_POD" ]]; then
echo "ERROR: Failed to obtain the ONDEMAND_POD name."
exit 1
fi
kubectl wait --for condition=ready --timeout=300s -n "$NAMESPACE" "pod/${ONDEMAND_POD}"
kubectl wait --for condition=ready --timeout=300s -n "$NAMESPACE" "pod/${ONDEMAND_POD}" 2>/dev/null
if [[ $? -ne 0 ]]; then
echo "ERROR: Failed to create a new on-demand pod. Exiting..."
exit 1
@ -235,20 +236,25 @@ function remove_job() {
JOB=$2
# Cleanup the last attempted job if there is one, wait for the pod to be deleted.
kubectl get job -n "$NAMESPACE" "$JOB"
kubectl get job -n "$NAMESPACE" "$JOB" 2>/dev/null
if [[ $? -eq 0 ]]; then
echo "Removing on-demand job $NAMESPACE $JOB"
ONDEMAND_POD=$(kubectl get pod -n "$NAMESPACE" | grep "$JOB" | awk '{print $1}')
kubectl delete job --ignore-not-found -n "$NAMESPACE" "$JOB"
kubectl wait --for=delete --timeout=300s -n "$NAMESPACE" job/"${JOB}"
if [[ $? -ne 0 ]]; then
echo "ERROR: could not destroy the $NAMESPACE $JOB job. Exiting..."
exit 1
fi
kubectl wait --for=delete --timeout=300s -n "$NAMESPACE" pod/"${ONDEMAND_POD}"
if [[ $? -ne 0 ]]; then
echo "ERROR: could not destroy the $NAMESPACE $ONDEMAND_POD pod. Exiting..."
exit 1
ONDEMAND_JOB=$(kubectl get job -n "$NAMESPACE" | grep "$JOB" | awk '{print $1}')
if [[ ! -z ${ONDEMAND_JOB} ]]; then
kubectl delete job --ignore-not-found -n "$NAMESPACE" "$JOB"
kubectl wait --for=delete --timeout=300s -n "$NAMESPACE" job/"${JOB}" 2>/dev/null
if [[ $? -ne 0 ]]; then
echo "ERROR: could not destroy the $NAMESPACE $JOB job. Exiting..."
exit 1
fi
ONDEMAND_POD=$(kubectl get pod -n "$NAMESPACE" | grep "$JOB" | awk '{print $1}')
if [[ ! -z ${ONDEMAND_POD} ]]; then
kubectl wait --for=delete --timeout=300s -n "$NAMESPACE" pod/"${ONDEMAND_POD}" 2>/dev/null
if [[ $? -ne 0 ]]; then
echo "ERROR: could not destroy the $NAMESPACE $ONDEMAND_POD pod. Exiting..."
exit 1
fi
fi
fi
fi
}
@ -897,6 +903,7 @@ function do_sql_prompt() {
# Params: [namespace]
function do_cleanup() {
# If a namespace is given go ahead and try to clean it up.
if [[ ! -z "$2" ]]; then
remove_job "$2" "$ONDEMAND_JOB"

View File

@ -14,4 +14,4 @@
apiVersion: v1
description: PostgreSQL Client
name: postgresql-utility
version: 0.1.4
version: 0.1.5

View File

@ -99,7 +99,7 @@ function ensure_ondemand_pod_exists() {
ONDEMAND_POD=$(echo "$POD_LISTING" | awk '{print $1}')
STATUS=$(echo "$POD_LISTING" | awk '{print $3}')
if [[ "$STATUS" == "Terminating" ]]; then
kubectl wait -n "$NAMESPACE" --for=delete pod/"$ONDEMAND_POD" --timeout=300s
kubectl wait -n "$NAMESPACE" --for=delete pod/"$ONDEMAND_POD" --timeout=300s 2>/dev/null
unset ONDEMAND_POD
unset STATUS
fi
@ -129,6 +129,8 @@ function ensure_ondemand_pod_exists() {
# If we reached this point with no ONDEMAND_POD, then we need to create
# a new on-demand job.
if [[ -z "$ONDEMAND_POD" ]]; then
echo "Cleaning up any remainings of previous runs..."
do_cleanup "$ONDEMAND_JOB" "$NAMESPACE"
echo "Creating new on-demand job in the $NAMESPACE namespace..."
/tmp/pg-ondemand-job.sh "$NAMESPACE"
if [[ $? -ne 0 ]]; then
@ -157,24 +159,24 @@ function ensure_ondemand_pod_exists() {
ONDEMAND_POD=$(echo "$POD_LISTING" | awk '{print $1}')
STATUS=$(echo "$POD_LISTING" | awk '{print $3}')
if [[ "$STATUS" == "Terminating" ]]; then
kubectl wait -n "$NAMESPACE" --for=delete pod/"$ONDEMAND_POD" --timeout=300s
kubectl wait -n "$NAMESPACE" --for=delete pod/"$ONDEMAND_POD" --timeout=300s 2>/dev/null
unset ONDEMAND_POD
elif [[ "$STATUS" != "Running" ]]; then
kubectl wait -n "$NAMESPACE" --for condition=ready pod/"$ONDEMAND_POD" --timeout=300s
kubectl wait -n "$NAMESPACE" --for condition=ready pod/"$ONDEMAND_POD" --timeout=300s 2>/dev/null
elif [[ "$STATUS" != "Pending" ]]; then
kubectl wait -n "$NAMESPACE" --for condition=ready pod/"$ONDEMAND_POD" --timeout=300s
kubectl wait -n "$NAMESPACE" --for condition=ready pod/"$ONDEMAND_POD" --timeout=300s 2>/dev/null
fi
fi
ONDEMAND_POD=$(kubectl get pods -n "$NAMESPACE" --selector=job-name="$ONDEMAND_JOB" -o json | jq -r .items[].metadata.name)
ONDEMAND_POD=$(kubectl get pods -n "$NAMESPACE" --selector=job-name="$ONDEMAND_JOB" -o json 2>/dev/null| jq -r .items[].metadata.name)
if [[ -z "$ONDEMAND_POD" ]]; then
echo "ERROR: Failed to obtain the ONDEMAND_POD name."
exit 1
fi
kubectl wait --for condition=ready --timeout=300s -n "$NAMESPACE" "pod/${ONDEMAND_POD}"
kubectl wait --for condition=ready --timeout=300s -n "$NAMESPACE" "pod/${ONDEMAND_POD}" 2>/dev/null
if [[ $? -ne 0 ]]; then
echo "ERROR: Failed to create a new on-demand pod. Exiting..."
exit 1
@ -234,20 +236,25 @@ function remove_job() {
JOB=$2
# Cleanup the last attempted job if there is one, wait for the pod to be deleted.
kubectl get job -n "$NAMESPACE" "$JOB"
kubectl get job -n "$NAMESPACE" "$JOB" 2>/dev/null
if [[ $? -eq 0 ]]; then
echo "Removing on-demand job $NAMESPACE $JOB"
ONDEMAND_POD=$(kubectl get pod -n "$NAMESPACE" | grep "$JOB" | awk '{print $1}')
kubectl delete job --ignore-not-found -n "$NAMESPACE" "$JOB"
kubectl wait --for=delete --timeout=300s -n "$NAMESPACE" job/"${JOB}"
if [[ $? -ne 0 ]]; then
echo "ERROR: could not destroy the $NAMESPACE $JOB job. Exiting..."
exit 1
ONDEMAND_JOB=$(kubectl get job -n "$NAMESPACE" | grep "$JOB" | awk '{print $1}')
if [[ ! -z ${ONDEMAND_JOB} ]]; then
kubectl delete job --ignore-not-found -n "$NAMESPACE" "$JOB"
kubectl wait --for=delete --timeout=300s -n "$NAMESPACE" job/"${JOB}" 2>/dev/null
if [[ $? -ne 0 ]]; then
echo "ERROR: could not destroy the $NAMESPACE $JOB job. Exiting..."
exit 1
fi
fi
kubectl wait --for=delete --timeout=300s -n "$NAMESPACE" pod/"${ONDEMAND_POD}"
if [[ $? -ne 0 ]]; then
echo "ERROR: could not destroy the $NAMESPACE $ONDEMAND_POD pod. Exiting..."
exit 1
ONDEMAND_POD=$(kubectl get pod -n "$NAMESPACE" | grep "$JOB" | awk '{print $1}')
if [[ ! -z ${ONDEMAND_POD} ]]; then
kubectl wait --for=delete --timeout=300s -n "$NAMESPACE" pod/"${ONDEMAND_POD}" 2>/dev/null
if [[ $? -ne 0 ]]; then
echo "ERROR: could not destroy the $NAMESPACE $ONDEMAND_POD pod. Exiting..."
exit 1
fi
fi
fi
}