From 332beb70547e39a0390ecd4b2d16dfccf519b06b Mon Sep 17 00:00:00 2001 From: lethe2211 Date: Mon, 23 Nov 2020 16:15:08 +0900 Subject: [PATCH 01/11] Translate reference/kubectl/jsonpath/ into Japanese --- content/ja/docs/reference/_index.md | 2 +- content/ja/docs/reference/kubectl/jsonpath.md | 112 ++++++++++++++++++ content/ja/docs/reference/kubectl/overview.md | 6 +- 3 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 content/ja/docs/reference/kubectl/jsonpath.md diff --git a/content/ja/docs/reference/_index.md b/content/ja/docs/reference/_index.md index 4276875967..0496a730c4 100644 --- a/content/ja/docs/reference/_index.md +++ b/content/ja/docs/reference/_index.md @@ -31,7 +31,7 @@ content_type: concept ## CLIリファレンス * [kubectl](/docs/reference/kubectl/overview/) - コマンドの実行やKubernetesクラスターの管理に使う主要なCLIツールです。 - * [JSONPath](/docs/reference/kubectl/jsonpath/) - kubectlで[JSONPath記法](https://goessner.net/articles/JsonPath/)を使うための構文ガイドです。 + * [JSONPath](/ja/docs/reference/kubectl/jsonpath/) - kubectlで[JSONPath記法](https://goessner.net/articles/JsonPath/)を使うための構文ガイドです。 * [kubeadm](/docs/reference/setup-tools/kubeadm/kubeadm/) - セキュアなKubernetesクラスターを簡単にプロビジョニングするためのCLIツールです。 ## コンポーネントリファレンス diff --git a/content/ja/docs/reference/kubectl/jsonpath.md b/content/ja/docs/reference/kubectl/jsonpath.md new file mode 100644 index 0000000000..a8084a49fa --- /dev/null +++ b/content/ja/docs/reference/kubectl/jsonpath.md @@ -0,0 +1,112 @@ +--- +title: JSONPathのサポート +content_type: concept +weight: 25 +--- + + +KubectlはJSONPathのテンプレートをサポートしています。 + + + +JSONPathのテンプレートは、中括弧({})によって囲まれたJSONPathの式によって構成されています。 +Kubectlでは、JSONPathの式を使うことで、JSONオブジェクトの特定のフィールドをフィルターしたり、出力のフォーマットを変更することができます。 +本来のJSONPathのテンプレートの構文に加え、以下の機能と構文が使えます: + +1. JSONPathの式の内部でテキストをクォートするために、ダブルクォーテーションが使えます。 +2. リストを反復するために、`range`、`end`オペレーターが使えます。 +3. リストを末尾側から参照するために、負の数のインデックスが使えます。負の数のインデックスはリストを「周回」せず、`-index + listLength >= 0`が満たされる限りにおいて有効になります。 + +{{< note >}} + +- 式は常にルートのオブジェクトから始まるので、`$`オペレーターの入力は任意になります。 + +- 結果のオブジェクトはString()関数を適用した形で表示されます。 + +{{< /note >}} + +以下のようなJSONの入力が与えられたとします。 + +```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` +`..` | 子孫要素を再帰的に探す | `{..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の式を使った例: + +```shell +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}' +``` + +{{< note >}} +Windowsでは、空白が含まれるJSONPathのテンプレートをクォートする際、(上の例のようにシングルクォーテーションを使うのではなく、)ダブルクォーテーションを使わなければなりません。 +また、テンプレート内のリテラルをクォートする際には、シングルクォーテーションか、エスケープされたダブルクォーテーションを使わなければなりません。例えば: + +```cmd +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}" +``` +{{< /note >}} + +{{< note >}} + +JSONPathの正規表現はサポートされていません。正規表現を利用した検索を行いたい場合は、`jq`のようなツールを使ってください。 + +```shell +# 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' +``` +{{< /note >}} diff --git a/content/ja/docs/reference/kubectl/overview.md b/content/ja/docs/reference/kubectl/overview.md index 71ce1844ec..2ddd60b62e 100644 --- a/content/ja/docs/reference/kubectl/overview.md +++ b/content/ja/docs/reference/kubectl/overview.md @@ -191,8 +191,8 @@ kubectl [command] [TYPE] [NAME] -o `-o custom-columns=` | [カスタムカラム](#custom-columns)のコンマ区切りのリストを使用して、テーブルを表示します。 `-o custom-columns-file=` | ``ファイル内の[カスタムカラム](#custom-columns)のテンプレートを使用して、テーブルを表示します。 `-o json` | JSON形式のAPIオブジェクトを出力します。 -`-o jsonpath=