Information in this document may be out of date
This document has an older update date than the original, so the information it contains may be out of date. If you're able to read English, see the English version for the most up-to-date information: JSONPath Support
JSONPath ์ง์
Kubectl์ JSONPath ํ ํ๋ฆฟ์ ์ง์ํ๋ค.
JSONPath ํ ํ๋ฆฟ์ ์ค๊ดํธ {}๋ก ๋๋ฌ์ธ์ธ JSONPath ํํ์์ผ๋ก ๊ตฌ์ฑ๋๋ค. Kubectl์ JSONPath ํํ์์ ์ฌ์ฉํ์ฌ JSON ์ค๋ธ์ ํธ์ ํน์ ํ๋๋ฅผ ํํฐ๋งํ๊ณ ์ถ๋ ฅ ํ์์ ์ง์ ํ๋ค. ์๋ณธ JSONPath ํ ํ๋ฆฟ ๊ตฌ๋ฌธ ์ธ์๋ ๋ค์๊ณผ ๊ฐ์ ๊ธฐ๋ฅ๊ณผ ๊ตฌ๋ฌธ์ด ์ ํจํ๋ค.
- ํฐ๋ฐ์ดํ๋ฅผ ์ฌ์ฉํ์ฌ JSONPath ํํ์ ๋ด๋ถ์ ํ ์คํธ๋ฅผ ์ธ์ฉํ๋ค.
- ๋ชฉ๋ก์ ๋ฐ๋ณตํ๋ ค๋ฉด
range
,end
์คํผ๋ ์ดํฐ๋ฅผ ์ฌ์ฉํ๋ค. - ๋ชฉ๋ก์์ ๋ค๋ก ์ด๋ํ๋ ค๋ฉด negative slice ์ธ๋ฑ์ค๋ฅผ ์ฌ์ฉํ๋ค. negative ์ธ๋ฑ์ค๋ ๋ชฉ๋ก์ "์ํ(wrap around)" ํ์ง ์์ผ๋ฉฐ,
-index + listLength >= 0
์ธ ํ ์ ํจํ๋ค.
์ฐธ๊ณ :
ํํ์์ ํญ์ ๋ฃจํธ ์ค๋ธ์ ํธ์์ ์์ํ๋ฏ๋ก
$
์คํผ๋ ์ดํฐ๋ ์ ํ ์ฌํญ์ด๋ค.๊ฒฐ๊ณผ ์ค๋ธ์ ํธ๋ String() ํจ์๋ก ์ถ๋ ฅ๋๋ค.
JSON ์ ๋ ฅ ์ ๋ค์๊ณผ ๊ฐ๋ค.
{
"kind": "List",
"items":[
{
"kind":"None",
"metadata":{"name":"127.0.0.1"},
"status":{
"capacity":{"cpu":"4"},
"addresses":[{"type": "LegacyHostIP", "address":"127.0.0.1"}]
}
},
{
"kind":"None",
"metadata":{"name":"127.0.0.2"},
"status":{
"capacity":{"cpu":"8"},
"addresses":[
{"type": "LegacyHostIP", "address":"127.0.0.2"},
{"type": "another", "address":"127.0.0.3"}
]
}
}
],
"users":[
{
"name": "myself",
"user": {}
},
{
"name": "e2e",
"user": {"username": "admin", "password": "secret"}
}
]
}
Function | Description | Example | Result |
---|---|---|---|
text | ์ผ๋ฐ ํ ์คํธ | kind is {.kind} | kind is List |
@ | ํ์ฌ ์ค๋ธ์ ํธ | {@} | ์ ๋ ฅ๊ณผ ๋์ผ |
. or [] | ์์ ์คํผ๋ ์ดํฐ | {.kind} , {['kind']} or {['name\.type']} | List |
.. | ์ฌ๊ท ํํฅ(recursive descent) | {..name} | 127.0.0.1 127.0.0.2 myself e2e |
* | ์์ผ๋ ์นด๋. ๋ชจ๋ ์ค๋ธ์ ํธ ๊ฐ์ ธ์ค๊ธฐ | {.items[*].metadata.name} | [127.0.0.1 127.0.0.2] |
[start:end:step] | ์๋ ์ฒจ์ ์คํผ๋ ์ดํฐ | {.users[0].name} | myself |
[,] | ์กฐํฉ ์คํผ๋ ์ดํฐ | {.items[*]['metadata.name', 'status.capacity']} | 127.0.0.1 127.0.0.2 map[cpu:4] map[cpu:8] |
?() | ํํฐ | {.users[?(@.name=="e2e")].user.password} | secret |
range , end | ๋ฐ๋ณต ๋ชฉ๋ก | {range .items[*]}[{.metadata.name}, {.status.capacity}] {end} | [127.0.0.1, map[cpu:4]] [127.0.0.2, map[cpu:8]] |
'' | ํด์๋ ๋ฌธ์์ด ์ธ์ฉ | {range .items[*]}{.metadata.name}{'\t'}{end} | 127.0.0.1 127.0.0.2 |
kubectl
๋ฐ JSONPath ํํ์์ ์ฌ์ฉํ๋ ์๋ ๋ค์๊ณผ ๊ฐ๋ค.
kubectl get pods -o json
kubectl get pods -o=jsonpath='{@}'
kubectl get pods -o=jsonpath='{.items[0]}'
kubectl get pods -o=jsonpath='{.items[0].metadata.name}'
kubectl get pods -o=jsonpath="{.items[*]['metadata.name', 'status.capacity']}"
kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'
์ฐธ๊ณ :
์๋์ฐ์์ ๊ณต๋ฐฑ์ด ํฌํจ๋ JSONPath ํ ํ๋ฆฟ์ ํฐ๋ฐ์ดํ(์์ bash์ ํ์๋ ์์๋ฐ์ดํ๊ฐ ์๋)๋ก ๋ฌถ์ด์ผ ํ๋ค. ์ฆ, ํ ํ๋ฆฟ์ ๋ชจ๋ ๋ฌธ์ ์ฃผ๋ณ์ ์์๋ฐ์ดํ ๋๋ ์ด์ค์ผ์ดํ๋ ํฐ๋ฐ์ดํ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค. ์๋ฅผ ๋ค๋ฉด, ๋ค์๊ณผ ๊ฐ๋ค.
kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{'\t'}{.status.startTime}{'\n'}{end}"
kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{\"\t\"}{.status.startTime}{\"\n\"}{end}"
์ฐธ๊ณ :
JSONPath ์ ๊ท์์ ์ง์๋์ง ์๋๋ค. ์ ๊ท ํํ์์ ์ด์ฉํด ๋งค์นํ๋ ค๋ฉด jq
์ ๊ฐ์ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
# kubectl์ JSONPath ์ถ๋ ฅ์ ๋ํ ์ ๊ท ํํ์์ ์ง์ํ์ง ์๋๋ค.
# ๋ค์ ์ปค๋งจ๋๋ ์๋ํ์ง ์๋๋ค.
kubectl get pods -o jsonpath='{.items[?(@.metadata.name=~/^test$/)].metadata.name}'
# ๋ค์ ์ปค๋งจ๋๋ ์ํ๋ ๊ฒฐ๊ณผ๋ฅผ ์ป๋๋ค.
kubectl get pods -o json | jq -r '.items[] | select(.metadata.name | test("test-")).spec.containers[].image'