我正在try 将一个数组与多个数组结合到我的JQ命令中.
这是我的json文档的一个小示例:
{
"items": [
{
"metadata": {
"name": "pod1"
},
"spec": {
"containers": [
{
"name": "container1",
"volumeMounts": [
{
"mountPath": "/path1",
"name": "mount1"
},
{
"mountPath": "/path2",
"name": "mount2"
}
]
},
{
"name": "container2",
"volumeMounts": [
{
"mountPath": "/path3",
"name": "mount1"
}
]
}
],
"volumes": [
{
"name": "mount1",
"persistentVolumeClaim": {
"claimName": "claim1"
}
},
{
"name": "mount2",
"emptyDir": {}
}
]
}
}
]
}
我想输出所有Pod的名称、它的容器和容器挂载点的名称,最后只 Select 其卷具有密钥persistentVolumeClaim
的挂载,例如:
pod1;container1;mount1;/path1;claim1
pod1;container2;mount1;/path3;claim1
到目前为止,我能够提取Pod-name、tainer-name以及挂载名称和路径:
jq -r '.items[]
| {pod:.metadata.name, c:.spec.containers[]}
| {pod:.pod, container:.c.name, mount:.c.volumeMounts[]}
| [.pod, .container, .mount.name, .mount.mountPath]
| join(";")'
这将提供以下输出:
pod1;container1;mount1;/path1
pod1;container1;mount2;/path2
pod1;container2;mount1;/path3
现在,我需要将volumes
数组与containers
中的每个volumeMounts
数组联接,然后只为那些具有persistentVolumeClaim
键的数组 Select .我让它可以联接两个数组,但我无法在上面的命令中集成它来联接volumes
和上面命令中containers
个数组中的每个volumesMounts
:
jq -r '.items[]
| .spec.containers[].volumeMounts+.spec.volumes
| group_by(.name)
| map(add)
| .[]
| select(.persistentVolumeClaim)'