带 Kubernetes 的 jsonpath 中的空方括号是什么意思?
What does empty square bracket mean in jsonpath with Kubernetes?
以下kubectl表达式returns镜像版本:
kubectl get po nginx -o jsonpath='{.spec.containers[].image}{"\n"}'
但是“[]”究竟表示什么?我知道它说图像是容器的子容器,容器是规范的子容器,但为什么 empty 方括号?
{.spec.containers}
将 return 一个数组。使用空方括号意味着 所需的值是一个数组。
您可以通过以下 运行 验证这一点,查询的输出是一个数组:
kubectl get po mongo -o jsonpath='{.spec.containers}{"\n"}'
[{"image":"mongo","imagePullPolicy":"Always","name":"mongo","ports":[{"containerPort":27107,"protocol":"TCP"}],"resources":{},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File","volumeMounts":[{"mountPath":"/var/run/secrets/kubernetes.io/serviceaccount","name":"default-token-c949d","readOnly":true}]},{"image":"nginx","imagePullPolicy":"Always","name":"nginx","ports":[{"containerPort":80,"protocol":"TCP"}],"resources":{},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File","volumeMounts":[{"mountPath":"/var/run/secrets/kubernetes.io/serviceaccount","name":"default-token-c949d","readOnly":true}]}]
当与 []
一起使用时,我们希望 .spec.containers
的输出为数组并已对其进行处理:
kubectl get po mongo -o jsonpath='{.spec.containers[]}{"\n"}'
{"image":"mongo","imagePullPolicy":"Always","name":"mongo","ports":[{"containerPort":27107,"protocol":"TCP"}],"resources":{},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File","volumeMounts":[{"mountPath":"/var/run/secrets/kubernetes.io/serviceaccount","name":"default-token-c949d","readOnly":true}]}
以下kubectl表达式returns镜像版本:
kubectl get po nginx -o jsonpath='{.spec.containers[].image}{"\n"}'
但是“[]”究竟表示什么?我知道它说图像是容器的子容器,容器是规范的子容器,但为什么 empty 方括号?
{.spec.containers}
将 return 一个数组。使用空方括号意味着 所需的值是一个数组。
您可以通过以下 运行 验证这一点,查询的输出是一个数组:
kubectl get po mongo -o jsonpath='{.spec.containers}{"\n"}'
[{"image":"mongo","imagePullPolicy":"Always","name":"mongo","ports":[{"containerPort":27107,"protocol":"TCP"}],"resources":{},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File","volumeMounts":[{"mountPath":"/var/run/secrets/kubernetes.io/serviceaccount","name":"default-token-c949d","readOnly":true}]},{"image":"nginx","imagePullPolicy":"Always","name":"nginx","ports":[{"containerPort":80,"protocol":"TCP"}],"resources":{},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File","volumeMounts":[{"mountPath":"/var/run/secrets/kubernetes.io/serviceaccount","name":"default-token-c949d","readOnly":true}]}]
当与 []
一起使用时,我们希望 .spec.containers
的输出为数组并已对其进行处理:
kubectl get po mongo -o jsonpath='{.spec.containers[]}{"\n"}'
{"image":"mongo","imagePullPolicy":"Always","name":"mongo","ports":[{"containerPort":27107,"protocol":"TCP"}],"resources":{},"terminationMessagePath":"/dev/termination-log","terminationMessagePolicy":"File","volumeMounts":[{"mountPath":"/var/run/secrets/kubernetes.io/serviceaccount","name":"default-token-c949d","readOnly":true}]}