Kubernetes 如何使用jsonpath正确隔离对象
背景/背景 最后,我尝试使用此命令作为我的基础来提取当前Kubernetes节点的IP地址(即从中运行命令):Kubernetes 如何使用jsonpath正确隔离对象,kubernetes,jsonpath,Kubernetes,Jsonpath,背景/背景 最后,我尝试使用此命令作为我的基础来提取当前Kubernetes节点的IP地址(即从中运行命令): # $HOSTNAME is the hostname of the machine in linux kubectl get nodes -ojsonpath="{.items[?(@.status.addresses[1].address == '$HOSTNAME')].status.addresses[?(@.type == 'InternalIP')].addres
# $HOSTNAME is the hostname of the machine in linux
kubectl get nodes -ojsonpath="{.items[?(@.status.addresses[1].address == '$HOSTNAME')].status.addresses[?(@.type == 'InternalIP')].address}"
但与此同时,我试图更好地理解为什么我不能正确地隔离这个jsonpath表达式
Jsonpath问题
我的问题是该命令中的jsonpath不一定正确,因为我假设Hostname类型的address对象是addresses列表中的第二个对象。然而,除非我做出这样的假设,否则我无法找出如何隔离该对象
据我所知,我应该将硬编码的1
替换为?(@.type==“Hostname”)
,如下所示:
{.items[?(@.status.addresses[?(@.type == 'Hostname')].address == '$HOSTNAME')].status.addresses[?(@.type == 'InternalIP')].address}
但这在kubernetes和每一个在线jsonpath测试仪中都失败了
期望值
预期输出是类型为==InternalIP
的对象的address
值,以便类型为==hostname
的对象的address
值与$hostname变量匹配(下面的示例)
示例json
这里是完整的kubernetes输出的简化版本,采用json格式。层次匹配,只是删除了多余的部分
{
"items": [
{
"status": {
"addresses":[
{
"address": "1.2.3.4",
"type":"InternalIp"
},
{
"address":"host1",
"type":"Hostname"
}
]
}
},
{
"status": {
"addresses":[
{
"address": "1.2.3.5",
"type":"InternalIp"
},
{
"address":"host2",
"type":"Hostname"
}
]
}
}
]
}
所以我希望这个命令:
{.items[?(@.status.addresses[?(@.type == 'Hostname')].address == 'host1')].status.addresses[?(@.type == 'InternalIP')].address}
应输出:
1.2.3.4
但这会在Kubernetes中得到错误未终止过滤器
我不明白我遗漏了什么,因为原始命令带有硬编码的1
:
{.items[?(@.status.addresses[1].address == 'host1')].status.addresses[?(@.type == 'InternalIp')].address}
产出:
1.2.3.4
正如所料。JSONPath的表达能力不足以满足您的需要;您需要切换到-o go template=
,或者使用真正的编程语言来过滤-o json
输出