按照对 Pod 的认识,我们可以通过 field-selector
来找到我们需要的状态的 Pod,但是当我们通过同样的方法去找需要的 CRDs 的时候,比如 sparkapp
,会发现报错。
# kubectl get pod -n allblue --field-selector status.phase=Running
NAME READY STATUS RESTARTS AGE
allblue-service-2556-4626-test-6874f68b6f-btxvv 1/1 Running 0 19d
allblue-service-2556-4626-test-6874f68b6f-lt6lj 1/1 Running 0 19d
allblue-service-2556-4627-test-6fb6f755d4-sjfts 1/1 Running 0 19d
allblue-service-2556-4627-test-6fb6f755d4-wps74 1/1 Running 0 19d
allblue-service-2556-4672-test-cf6994f54-cjr92 1/1 Running 0 7d8h
# kubectl get sparkapp -n allblue --field-selector .status.applicationState.state=Running
Error from server (BadRequest): Unable to find "sparkoperator.k8s.io/v1beta2, Resource=sparkapplications" that match label selector "", field selector ".status.applicationState.state=Running": field label not supported: .status.applicationState.state
简单 Google 了一下,找到 Unable to use a fieldSelector with custom resources #51046,按照这个 issue 的说法,CRDs 还不支持除了 name 和 namespace 以外的 filed selector。不过依然可以通过 kubectl 和 shell 的结合来搞一下。
代码语言:javascript复制kubectl get pg -n log -o=jsonpath='{range .items[*]}{.metadata.name}{"t"}{.status.phase}{"n"}{end}' | grep -i Finished|awk '{print $1}'|xargs kubectl delete pg -n log