如何使用jsonpath正确隔离对象

问题描述

上下文/背景

最终,我正在尝试使用此命令来提取我所在的当前Kubernetes节点的IP地址(即,从中运行命令):

/etc/ssl/openssl.cnf

但是与此同时,我试图更好地理解为什么我不能正确隔离此jsonpath表达式。

Jsonpath问题

我的问题是该命令的jsonpath不一定正确,因为我假设类型为Hostname的地址对象是地址列表中的第二个对象。但是,除非做出这样的假设,否则我无法弄清楚如何隔离该对象。

据我了解,我应该将硬编码的# $HOSTNAME is the hostname of the machine in linux kubectl get nodes -ojsonpath="{.items[?(@.status.addresses[1].address == '$HOSTNAME')].status.addresses[?(@.type == 'InternalIP')].address}" 替换为1,如下所示:

?(@.type == 'Hostname')

但这在kubernetes和每个在线jsonpath测试器中都失​​败了。

期望

预期输出是类型== {.items[?(@.status.addresses[?(@.type == 'Hostname')].address == '$HOSTNAME')].status.addresses[?(@.type == 'InternalIP')].address} 的对象的address的值,从而类型== InternalIP的对象的address的值与$ HOSTNAME变量(下面的示例)。

示例json

这是json中完整kubernetes输出的简化版本。层次结构匹配,只是删除了多余的部分。

hostname

所以我希望这个命令:

{
    "items": [
        {
            "status": {
                "addresses":[
                    {
                        "address": "1.2.3.4","type":"InternalIp"
                    },{
                        "address":"host1","type":"Hostname"
                    }
                ]
            }
        },{
            "status": {
                "addresses":[
                    {
                        "address": "1.2.3.5",{
                        "address":"host2","type":"Hostname"
                    }
                ]
            }
        }
    ]
}

输出

{.items[?(@.status.addresses[?(@.type == 'Hostname')].address == 'host1')].status.addresses[?(@.type == 'InternalIP')].address}

但这会在Kubernetes中得到错误1.2.3.4

我不明白自从带有硬编码unterminated filter的原始命令以来缺少的内容

1

输出

{.items[?(@.status.addresses[1].address == 'host1')].status.addresses[?(@.type == 'InternalIp')].address}

符合预期。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)