循环输出并在 bash 中删除

loop on output and delete in bash

团队, 我需要在有错误的 k8s 集群上删除 pods 的 10s。 我得到它们如下:

kubectl get pods --all-namespaces | grep -i -e Evict -e Error | awk -F ' ' '{print , , }'
test-asdfasdf asdfasdf2215251 Error
test-asdfasdf asdfasdf2215252 Error
test-asdfasdf asdfasdf2215253 Error
test-asdfasdf asdfasdf2215254 Error
test-asdfasdf asdfasdf2215255 Error
test-asdfasdf asdfasdf2215256 Error

像这样手动删除它们:

kubectl delete pod asdfasdf2215251 -n test-asdfasdf

但是我可以编写一个脚本来查找任何 pod 上的错误并将其全部删除吗?我自己正在编写脚本,但对此很陌生,因此来晚了..

起点:

kubectl get pods --all-namespaces |
grep -i -e Evict -e Error |
awk -F ' ' '{print , }' |

将产生一个流:

test-asdfasdf asdfasdf2215251
test-asdfasdf asdfasdf2215252
test-asdfasdf asdfasdf2215253
test-asdfasdf asdfasdf2215254
test-asdfasdf asdfasdf2215255
test-asdfasdf asdfasdf2215256

我们可以去这里:

while IFS=' ' read -r arg1 arg2; do
    kubectl delete pod "$arg2" -n "$arg1"
done

我们可以去这里:

xargs -l1 -- sh -c 'kubectl delete pod "" -n ""' --

或使用 parallel 或任何其他工具来做到这一点。