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 ํ…œํ”Œ๋ฆฟ ๊ตฌ๋ฌธ ์™ธ์—๋„ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ธฐ๋Šฅ๊ณผ ๊ตฌ๋ฌธ์ด ์œ ํšจํ•˜๋‹ค.

  1. ํฐ๋”ฐ์˜ดํ‘œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ JSONPath ํ‘œํ˜„์‹ ๋‚ด๋ถ€์˜ ํ…์ŠคํŠธ๋ฅผ ์ธ์šฉํ•œ๋‹ค.
  2. ๋ชฉ๋ก์„ ๋ฐ˜๋ณตํ•˜๋ ค๋ฉด range, end ์˜คํผ๋ ˆ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  3. ๋ชฉ๋ก์—์„œ ๋’ค๋กœ ์ด๋™ํ•˜๋ ค๋ฉด negative slice ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. negative ์ธ๋ฑ์Šค๋Š” ๋ชฉ๋ก์„ "์ˆœํ™˜(wrap around)" ํ•˜์ง€ ์•Š์œผ๋ฉฐ, -index + listLength >= 0 ์ธ ํ•œ ์œ ํšจํ•˜๋‹ค.

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"}
    }
  ]
}
FunctionDescriptionExampleResult
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}'
์ตœ์ข… ์ˆ˜์ • February 23, 2023 at 9:03 AM PST: [ko] Update outdated files in dev-1.26-ko.1 (M112-M125) (41aeff23ce)