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"}
    }
  ]
}
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}'
최종 수정 February 23, 2023 at 9:03 AM PST: [ko] Update outdated files in dev-1.26-ko.1 (M112-M125) (41aeff23ce)