我一直在使用 jq(1) 并從 kubernetes api (api/v1/nodes) 得到類似的回應。
{
"kind": "NodeList",
"apiVersion": "v1",
"metadata": {
"selfLink": "/api/v1/nodes",
"resourceVersion": "8768"
},
"items": [
{
"metadata": {
"name": "ip-101-191-101-101.ec2.internal",
"selfLink": "/api/v1/nodes/ip-101-191-101-101.ec2.internal",
"uid": "2l3kje2ili-23e232-2e3ee-edwed-232398h9e3h98h",
"resourceVersion": "8768",
"creationTimestamp": "2020-11-19T12:27:05Z",
},
"spec": {
"podCIDR": "101.191.101.101/24",
"providerID": "aws:///us-west-1e/i-lidss9jsjldsjli",
"taints": [
{
"key": "worker-group",
"value": "prometheus",
"effect": "NoSchedule"
}
]
}
}
]
}
正如您所看到的,Array 中可以有很多項。有一些帶有 .spec.taints,而另一些則沒有。有些可能是空的,有些可能是滿的。
我的目標是忽略所有有污點的專案 "effect": "NoSchedule"
我發現這很麻煩,因為無論我嘗試什么并且我已經在網上搜索了好幾天,我都無法讓它正常作業。
我已經走了這么遠,但現在卡住了
curl api | jq -c '.items[].spec.taints |= map(select(.effect | . != "NoSchedule"))'
任何幫助,將不勝感激。
uj5u.com熱心網友回復:
您可以使用del洗掉您想要的那些:
curl api | jq 'del(.items[] | select(.spec.taints[].effect == "NoSchedule")?)'
uj5u.com熱心網友回復:
這將從.items陣列中洗掉每個擁有陣列的成員,而該陣列.spec.taints又至少有一個.effect設定為 的物件項"NoSchedule"。在所有其他情況下,即只有.effect設定為其他內容的物件,或者.taints是空的,或者根本沒有.taints,或者根本沒有.spec,最多只有一個空物件{}作為.items陣列的成員,該陣列成員將被保留。
.items -= (.items | map(select(.spec.taints[].effect == "NoSchedule")?))
演示
uj5u.com熱心網友回復:
我希望這會很有用,在這個例子中,有 3 個專案,一個有效果:NoSchedule,一個有效果:anyother,一個有 taints 空。如果正確,您只需要第二個:
{
"kind": "NodeList",
"apiVersion": "v1",
"metadata": {
"selfLink": "/api/v1/nodes",
"resourceVersion": "8768"
},
"items": [
{
"metadata": {
"name": "ip-101-191-101-101.ec2.internal",
"selfLink": "/api/v1/nodes/ip-101-191-101-101.ec2.internal",
"uid": "2l3kje2ili-23e232-2e3ee-edwed-232398h9e3h98h",
"resourceVersion": "8768",
"creationTimestamp": "2020-11-19T12:27:05Z"
},
"spec": {
"podCIDR": "101.191.101.101/24",
"providerID": "aws:///us-west-1e/i-lidss9jsjldsjli",
"taints": [
{
"key": "worker-group",
"value": "prometheus",
"effect": "NoSchedule"
}
]
}
},
{
"metadata": {
"name": "ip-101-191-101-101.ec2.internal",
"selfLink": "/api/v1/nodes/ip-101-191-101-101.ec2.internal",
"uid": "2l3kje2ili-23e232-2e3ee-edwed-232398h9e3h98h",
"resourceVersion": "8768",
"creationTimestamp": "2020-11-19T12:27:05Z"
},
"spec": {
"podCIDR": "101.191.101.101/24",
"providerID": "aws:///us-west-1e/i-lidss9jsjldsjli",
"taints": [
{
"key": "worker-group",
"value": "prometheus",
"effect": "anyother"
}
]
}
},
{
"metadata": {
"name": "ip-101-191-101-101.ec2.internal",
"selfLink": "/api/v1/nodes/ip-101-191-101-101.ec2.internal",
"uid": "2l3kje2ili-23e232-2e3ee-edwed-232398h9e3h98h",
"resourceVersion": "8768",
"creationTimestamp": "2020-11-19T12:27:05Z"
},
"spec": {
"podCIDR": "101.191.101.101/24",
"providerID": "aws:///us-west-1e/i-lidss9jsjldsjli",
"taints": [
]
}
}
]
命令將是:
jq '.items[]|select(.spec.taints[].effect!="NoSchedule") ' data1.jtxt
結果:
{
"metadata": {
"name": "ip-101-191-101-101.ec2.internal",
"selfLink": "/api/v1/nodes/ip-101-191-101-101.ec2.internal",
"uid": "2l3kje2ili-23e232-2e3ee-edwed-232398h9e3h98h",
"resourceVersion": "8768",
"creationTimestamp": "2020-11-19T12:27:05Z"
},
"spec": {
"podCIDR": "101.191.101.101/24",
"providerID": "aws:///us-west-1e/i-lidss9jsjldsjli",
"taints": [
{
"key": "worker-group",
"value": "prometheus",
"effect": "anyother"
}
]
}
}
注意:我將您的資料放在一個檔案中以模擬輸入。
uj5u.com熱心網友回復:
基于這個問題,我的理解是taints可以有多個條目。它可以是NoSchedule和 其他值的混合。jq如果至少存在一個帶effect值的污點,這里將忽略該專案NoSchedule。
. as $in | $in * { items: $in.items | map(select(.spec.taints | all(.effect != "NoSchedule"))) }```
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/397742.html
上一篇:當WebAPI決議方法引數中的物件時更改JSON目標
下一篇:如何使用松露框架為硬幣設定圖示
