从 kubectl 输出中过滤值
Filtering values from kubectl output
我有一个 k8s 集群,如果我 运行:
kubectl get pods --all-namespaces -o jsonpath="{..image}" |\
tr -s '[[:space:]]' '\n' |\
sort |\
uniq -c
-这有效。
现在,我必须列出所有不以特定字符串开头的图像,比如 "random.domain.com"
如何使用jsonpath过滤掉属性值?
下面我已经试过了。
kubectl get pods --all-namespaces -o jsonpath="{..image}[?(@.image!="random.domain.com")]" |\
tr -s '[[:space:]]' '\n' |\
sort |\
uniq -c
作为解决方法,我使用 -
kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec.containers[*].image}" |\
tr -s '[[:space:]]' '\n' |sort |uniq -c| grep -v "random.domain.com"
但想知道我们如何使用 jsonpath 完成此操作。
谢谢。
我不知道如何使用 jsonpath (atm)。但是在这里你如何用 jq
做到这一点
kubectl get pods -o json | \
jq '[.items[].spec.containers[].image | select(. | startswith("random.domain.com") | not )] | unique'
AFAIS,根据 documentation,您不能使用 JSON 路径本身
我有一个 k8s 集群,如果我 运行:
kubectl get pods --all-namespaces -o jsonpath="{..image}" |\
tr -s '[[:space:]]' '\n' |\
sort |\
uniq -c
-这有效。
现在,我必须列出所有不以特定字符串开头的图像,比如 "random.domain.com"
如何使用jsonpath过滤掉属性值?
下面我已经试过了。
kubectl get pods --all-namespaces -o jsonpath="{..image}[?(@.image!="random.domain.com")]" |\
tr -s '[[:space:]]' '\n' |\
sort |\
uniq -c
作为解决方法,我使用 -
kubectl get pods --all-namespaces -o jsonpath="{.items[*].spec.containers[*].image}" |\
tr -s '[[:space:]]' '\n' |sort |uniq -c| grep -v "random.domain.com"
但想知道我们如何使用 jsonpath 完成此操作。
谢谢。
我不知道如何使用 jsonpath (atm)。但是在这里你如何用 jq
kubectl get pods -o json | \
jq '[.items[].spec.containers[].image | select(. | startswith("random.domain.com") | not )] | unique'
AFAIS,根据 documentation,您不能使用 JSON 路径本身