# cat clear_k8s_ingress.sh
set -e
kubectl get ing -A -o=custom-columns='NAMESPACE:.metadata.namespace,NAME:.metadata.name,SVCs:..service.name' --no-headers |while read line
do
namespa_name=$(awk '{print $1}' <<<"$line")
ingress_name=$(awk '{print $2}' <<<"$line")
service_name=$(echo "$line"|awk '{print $3}' |awk -F ',' '{print $1}')
if [[ $(kubectl get svc -A |grep $service_name) == "" ]]
then
echo "$service_name不存在,需要删除$namespa_name命名空间下的$ingress_name"
echo -e "删除指令: \033[32mkubectl delete ing -n $namespa_name $ingress_name\033[0m"
kubectl delete ing -n $namespa_name $ingress_name
fi
done