#!/bin/bash
path_current=`pwd`
path_script=$(cd "$(dirname "$0")"; pwd)
cd ${path_script}
echo "$(date)" >> ./check.txt
podnamspace="-n test"
shpods=$(kubectl describe pods "sh-" ${podnamspace}|grep "^Name:"|awk '{print $2}')
echo ${shpods}
sh_tm_max=0
for sh in ${shpods[@]}
do
mytime=$(kubectl describe pod ${sh} ${podnamspace}|grep "^Start Time:")&& mytime=$(echo ${mytime:11}) && mytime=$(date -d "$mytime" +%s)
elapse=$(expr ${sh_tm_max} - ${mytime})
if [ $? == 0 ] && [ ${elapse} -lt 0 ]; then
sh_tm_max=${mytime}
fi
done
echo "sh pods start time: $sh_tm_max"
apipods=$(kubectl describe pods "api-" ${podnamspace}|grep "^Name:"|awk '{print $2}')
echo ${apipods}
for api in ${apipods[@]}
do
mytime=$(kubectl describe pod ${api} ${podnamspace}|grep "^Start Time:")&& mytime=$(echo ${mytime:11}) && mytime=$(date -d "$mytime" +%s)
elapse=$(expr ${sh_tm_max} - ${mytime})
howMinute=$(expr $elapse / 60)
output="${api} pod start time: $mytime, ${sh_tm_max} - ${mytime}=[${elapse}s, ${howMinute}m]"
echo "$output" && echo "$output" >> ./check.txt
if [ $? == 0 ] && [ $howMinute -ge 0 ]; then
echo "delete this pod $api"
kubectl delete pod $api ${podnamspace} && echo "success to delete $api" >> ./check.txt
fi
done