我找到了用于测试多个值但不提取多个值的 jsonpath 示例。
我想从
image
获取 name
和 kubectl get pods
。
这让我很感动
name
kubectl get pods -o=jsonpath='{.items[*].spec.containers[*].name}' | xargs -n 1
这让我很感动
image
kubectl get pods -o=jsonpath='{.items[*].spec.containers[*].image}' | xargs -n 1
但是
kubectl get pods -o=jsonpath='{.items[*].spec.containers[*].[name,image}' | xargs -n 2
抱怨
invalid array index image
- 是否有获取节点相邻值列表的语法?
使用以下命令获取名称和图像:
kubectl get pods -Ao jsonpath='{range .items[*]}{@.metadata.name}{" "}{@.spec.template.spec.containers[].image}{"\n"}{end}'
它将给出如下输出:
name image
有用的命令,我必须对其进行一些修改才能使其工作(因 -a 标志而失败)。另外,我在应用程序标签中添加了一个过滤器,并添加了一个字段来获取:命名空间、pod 名称、图像
kubectl get pods --all-namespaces -o jsonpath='{range .items[*]}{@.metadata.namespace}{"\t"}{@.metadata.name}{"\t"}{@.spec.containers[*].image}{"\n"}{end}' -l app=nginx
谢谢!我必须做一些改变,但这对我有用:
#!/bin/bash
releases=$(kubectl get deployment -A --output=jsonpath='{range .items[*]}{@.metadata.namespace}{"|"}{@.metadata.name}{"\n"}{end}')
for release in $releases; do
namespace=$( echo $release | cut -d "|" -f 1)
deployment=$( echo $release | cut -d "|" -f 2)
kubectl rollout restart deployments -n "${namespace}" "${deployment}"
done
任何在 Windows 上尝试上述命令的人都会收到如下错误消息:
error: error parsing jsonpath {range .items[*]}{@.metadata.name}{" "}{@.spec.template.spec.containers[].image}{"\n"}{end}, unrecognized character in action: U+005C '\'
要使其正常工作,请在 JSONPath 模板的开头和结尾使用双引号,并在文字中使用单引号,如下所示:
kubectl get pods -Ao jsonpath="{range .items[*]}{@.metadata.name}{' '}{@.spec.template.spec.containers[].image}{'\n'}{end}"
...如 kubectl 参考文献中所述:
注意: 在 Windows 上,您必须双引号包含空格的任何 JSONPath 模板(而不是如上面 bash 所示的单引号)。这又意味着您必须在模板中的任何文字周围使用单引号或转义双引号