diff --git a/docs/man/man1/kubectl-annotate.1 b/docs/man/man1/kubectl-annotate.1
index 252e91655f1..c218eb0cea1 100644
--- a/docs/man/man1/kubectl-annotate.1
+++ b/docs/man/man1/kubectl-annotate.1
@@ -43,7 +43,7 @@ resourcequotas (quota) or secrets.
.PP
\fB\-o\fP, \fB\-\-output\fP=""
- Output format. One of: json|yaml|template|templatefile|wide.
+ Output format. One of: json|yaml|template|templatefile|wide|jsonpath.
.PP
\fB\-\-output\-version\fP=""
@@ -67,8 +67,9 @@ resourcequotas (quota) or secrets.
.PP
\fB\-t\fP, \fB\-\-template\fP=""
- Template string or path to template file to use when \-o=template or \-o=templatefile. The template format is golang templates [
-\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]]
+ Template string or path to template file to use when \-o=template, \-o=templatefile or \-o=jsonpath. The template format is golang templates [
+\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]]. The jsonpath template is composed of jsonpath expressions enclosed by {} [
+\[la]http://releases.k8s.io/HEAD/docs/user-guide/jsonpath.md\[ra]]
.SH OPTIONS INHERITED FROM PARENT COMMANDS
diff --git a/docs/man/man1/kubectl-config-view.1 b/docs/man/man1/kubectl-config-view.1
index 603cd5ea111..eee772a414c 100644
--- a/docs/man/man1/kubectl-config-view.1
+++ b/docs/man/man1/kubectl-config-view.1
@@ -42,7 +42,7 @@ You can use \-\-output=template \-\-template=TEMPLATE to extract specific values
.PP
\fB\-o\fP, \fB\-\-output\fP=""
- Output format. One of: json|yaml|template|templatefile|wide.
+ Output format. One of: json|yaml|template|templatefile|wide|jsonpath.
.PP
\fB\-\-output\-version\fP=""
@@ -62,8 +62,9 @@ You can use \-\-output=template \-\-template=TEMPLATE to extract specific values
.PP
\fB\-t\fP, \fB\-\-template\fP=""
- Template string or path to template file to use when \-o=template or \-o=templatefile. The template format is golang templates [
-\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]]
+ Template string or path to template file to use when \-o=template, \-o=templatefile or \-o=jsonpath. The template format is golang templates [
+\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]]. The jsonpath template is composed of jsonpath expressions enclosed by {} [
+\[la]http://releases.k8s.io/HEAD/docs/user-guide/jsonpath.md\[ra]]
.SH OPTIONS INHERITED FROM PARENT COMMANDS
diff --git a/docs/man/man1/kubectl-expose.1 b/docs/man/man1/kubectl-expose.1
index c29f68c13ab..b756c84d31c 100644
--- a/docs/man/man1/kubectl-expose.1
+++ b/docs/man/man1/kubectl-expose.1
@@ -60,7 +60,7 @@ re\-use the labels from the resource it exposes.
.PP
\fB\-o\fP, \fB\-\-output\fP=""
- Output format. One of: json|yaml|template|templatefile|wide.
+ Output format. One of: json|yaml|template|templatefile|wide|jsonpath.
.PP
\fB\-\-output\-version\fP=""
@@ -104,8 +104,9 @@ re\-use the labels from the resource it exposes.
.PP
\fB\-t\fP, \fB\-\-template\fP=""
- Template string or path to template file to use when \-o=template or \-o=templatefile. The template format is golang templates [
-\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]]
+ Template string or path to template file to use when \-o=template, \-o=templatefile or \-o=jsonpath. The template format is golang templates [
+\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]]. The jsonpath template is composed of jsonpath expressions enclosed by {} [
+\[la]http://releases.k8s.io/HEAD/docs/user-guide/jsonpath.md\[ra]]
.PP
\fB\-\-type\fP=""
diff --git a/docs/man/man1/kubectl-get.1 b/docs/man/man1/kubectl-get.1
index 88e5049138a..3bd96d0d7dd 100644
--- a/docs/man/man1/kubectl-get.1
+++ b/docs/man/man1/kubectl-get.1
@@ -49,7 +49,7 @@ of the \-\-template flag, you can filter the attributes of the fetched resource(
.PP
\fB\-o\fP, \fB\-\-output\fP=""
- Output format. One of: json|yaml|template|templatefile|wide.
+ Output format. One of: json|yaml|template|templatefile|wide|jsonpath.
.PP
\fB\-\-output\-version\fP=""
@@ -69,8 +69,9 @@ of the \-\-template flag, you can filter the attributes of the fetched resource(
.PP
\fB\-t\fP, \fB\-\-template\fP=""
- Template string or path to template file to use when \-o=template or \-o=templatefile. The template format is golang templates [
-\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]]
+ Template string or path to template file to use when \-o=template, \-o=templatefile or \-o=jsonpath. The template format is golang templates [
+\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]]. The jsonpath template is composed of jsonpath expressions enclosed by {} [
+\[la]http://releases.k8s.io/HEAD/docs/user-guide/jsonpath.md\[ra]]
.PP
\fB\-w\fP, \fB\-\-watch\fP=false
diff --git a/docs/man/man1/kubectl-label.1 b/docs/man/man1/kubectl-label.1
index 6193052d95d..8a80008cf9b 100644
--- a/docs/man/man1/kubectl-label.1
+++ b/docs/man/man1/kubectl-label.1
@@ -40,7 +40,7 @@ If \-\-resource\-version is specified, then updates will use this resource versi
.PP
\fB\-o\fP, \fB\-\-output\fP=""
- Output format. One of: json|yaml|template|templatefile|wide.
+ Output format. One of: json|yaml|template|templatefile|wide|jsonpath.
.PP
\fB\-\-output\-version\fP=""
@@ -68,8 +68,9 @@ If \-\-resource\-version is specified, then updates will use this resource versi
.PP
\fB\-t\fP, \fB\-\-template\fP=""
- Template string or path to template file to use when \-o=template or \-o=templatefile. The template format is golang templates [
-\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]]
+ Template string or path to template file to use when \-o=template, \-o=templatefile or \-o=jsonpath. The template format is golang templates [
+\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]]. The jsonpath template is composed of jsonpath expressions enclosed by {} [
+\[la]http://releases.k8s.io/HEAD/docs/user-guide/jsonpath.md\[ra]]
.SH OPTIONS INHERITED FROM PARENT COMMANDS
diff --git a/docs/man/man1/kubectl-rolling-update.1 b/docs/man/man1/kubectl-rolling-update.1
index 5d36dabd08d..954eb0c3dd3 100644
--- a/docs/man/man1/kubectl-rolling-update.1
+++ b/docs/man/man1/kubectl-rolling-update.1
@@ -48,7 +48,7 @@ existing replication controller and overwrite at least one (common) label in its
.PP
\fB\-o\fP, \fB\-\-output\fP=""
- Output format. One of: json|yaml|template|templatefile|wide.
+ Output format. One of: json|yaml|template|templatefile|wide|jsonpath.
.PP
\fB\-\-output\-version\fP=""
@@ -72,8 +72,9 @@ existing replication controller and overwrite at least one (common) label in its
.PP
\fB\-t\fP, \fB\-\-template\fP=""
- Template string or path to template file to use when \-o=template or \-o=templatefile. The template format is golang templates [
-\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]]
+ Template string or path to template file to use when \-o=template, \-o=templatefile or \-o=jsonpath. The template format is golang templates [
+\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]]. The jsonpath template is composed of jsonpath expressions enclosed by {} [
+\[la]http://releases.k8s.io/HEAD/docs/user-guide/jsonpath.md\[ra]]
.PP
\fB\-\-timeout\fP=5m0s
diff --git a/docs/man/man1/kubectl-run.1 b/docs/man/man1/kubectl-run.1
index af56c147cc0..b28b60d606b 100644
--- a/docs/man/man1/kubectl-run.1
+++ b/docs/man/man1/kubectl-run.1
@@ -56,7 +56,7 @@ Creates a replication controller to manage the created container(s).
.PP
\fB\-o\fP, \fB\-\-output\fP=""
- Output format. One of: json|yaml|template|templatefile|wide.
+ Output format. One of: json|yaml|template|templatefile|wide|jsonpath.
.PP
\fB\-\-output\-version\fP=""
@@ -92,8 +92,9 @@ Creates a replication controller to manage the created container(s).
.PP
\fB\-t\fP, \fB\-\-template\fP=""
- Template string or path to template file to use when \-o=template or \-o=templatefile. The template format is golang templates [
-\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]]
+ Template string or path to template file to use when \-o=template, \-o=templatefile or \-o=jsonpath. The template format is golang templates [
+\[la]http://golang.org/pkg/text/template/#pkg-overview\[ra]]. The jsonpath template is composed of jsonpath expressions enclosed by {} [
+\[la]http://releases.k8s.io/HEAD/docs/user-guide/jsonpath.md\[ra]]
.PP
\fB\-\-tty\fP=false
diff --git a/docs/user-guide/jsonpath.md b/docs/user-guide/jsonpath.md
new file mode 100644
index 00000000000..9c5ca3a511b
--- /dev/null
+++ b/docs/user-guide/jsonpath.md
@@ -0,0 +1,103 @@
+
+
+
+
+
+
+
+
+
+
+
PLEASE NOTE: This document applies to the HEAD of the source tree
+
+If you are using a released version of Kubernetes, you should
+refer to the docs that go with that version.
+
+
+The latest 1.0.x release of this document can be found
+[here](http://releases.k8s.io/release-1.0/docs/user-guide/jsonpath.md).
+
+Documentation for other releases can be found at
+[releases.k8s.io](http://releases.k8s.io).
+
+--
+
+
+
+
+
+# JSONPath template syntax
+
+JSONPath template is composed of JSONPath expressions enclosed by {}.
+And we add three functions in addition to the original JSONPath syntax:
+
+1. The `$` operator is optional since the expression always start from the root object by default.
+2. We can use `""` to quote text inside JSONPath expression.
+3. We can use `range` operator to iterate list.
+
+
+The result object is printed as its String() function.
+
+Given the input:
+
+```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 | the plain text | kind is {.kind} | kind is List
+"" | quote | {"{"} | {
+@ | the current object | {@} | the same as input
+. or [] | child operator | {.kind} or {['kind']}| List
+.. | recursive descent | {..name} | 127.0.0.1 127.0.0.2 myself e2e
+* | wildcard. Get all objects| {.items[*].metadata.name} | [127.0.0.1 127.0.0.2]
+[start:end :step] | subscript operator | {.users[0].name}| myself
+[,] | union operator | {.items[*]['metadata.name', 'status.capacity']} | 127.0.0.1 127.0.0.2 map[cpu:4] map[cpu:8]
+?() | filter | {.users[?(@.name=="e2e")].user.password} | secret
+range, end | iterate list | {range .items[*]}[{.metadata.name}, {.status.capacity}] {end} | [127.0.0.1, map[cpu:4]] [127.0.0.2, map[cpu:8]]
+
+
+
+
+
+[]()
+
diff --git a/docs/user-guide/kubectl/kubectl_annotate.md b/docs/user-guide/kubectl/kubectl_annotate.md
index ce68bf7494f..2b8b9c33a79 100644
--- a/docs/user-guide/kubectl/kubectl_annotate.md
+++ b/docs/user-guide/kubectl/kubectl_annotate.md
@@ -81,13 +81,13 @@ $ kubectl annotate pods foo description-
--all[=false]: select all resources in the namespace of the specified resource types
-h, --help[=false]: help for annotate
--no-headers[=false]: When using the default output, don't print headers.
- -o, --output="": Output format. One of: json|yaml|template|templatefile|wide.
+ -o, --output="": Output format. One of: json|yaml|template|templatefile|wide|jsonpath.
--output-version="": Output the formatted object with the given version (default api-version).
--overwrite[=false]: If true, allow annotations to be overwritten, otherwise reject annotation updates that overwrite existing annotations.
--resource-version="": If non-empty, the annotation update will only succeed if this is the current resource-version for the object. Only valid when specifying a single resource.
-a, --show-all[=false]: When printing, show all resources (default hide terminated pods.)
--sort-by="": If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. 'ObjectMeta.Name'). The field in the API resource specified by this JSONPath expression must be an integer or a string.
- -t, --template="": Template string or path to template file to use when -o=template or -o=templatefile. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]
+ -t, --template="": Template string or path to template file to use when -o=template, -o=templatefile or -o=jsonpath. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]. The jsonpath template is composed of jsonpath expressions enclosed by {} [http://releases.k8s.io/HEAD/docs/user-guide/jsonpath.md]
```
### Options inherited from parent commands
@@ -123,7 +123,7 @@ $ kubectl annotate pods foo description-
* [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager
-###### Auto generated by spf13/cobra at 2015-08-18 17:36:23.97715989 +0000 UTC
+###### Auto generated by spf13/cobra at 2015-08-13 08:45:23.477684199 +0000 UTC
[]()
diff --git a/docs/user-guide/kubectl/kubectl_config_view.md b/docs/user-guide/kubectl/kubectl_config_view.md
index cd4ae1f1ab4..06e13690d22 100644
--- a/docs/user-guide/kubectl/kubectl_config_view.md
+++ b/docs/user-guide/kubectl/kubectl_config_view.md
@@ -64,12 +64,12 @@ $ kubectl config view -o template --template='{{range .users}}{{ if eq .name "e2
--merge=true: merge together the full hierarchy of kubeconfig files
--minify[=false]: remove all information not used by current-context from the output
--no-headers[=false]: When using the default output, don't print headers.
- -o, --output="": Output format. One of: json|yaml|template|templatefile|wide.
+ -o, --output="": Output format. One of: json|yaml|template|templatefile|wide|jsonpath.
--output-version="": Output the formatted object with the given version (default api-version).
--raw[=false]: display raw byte data
-a, --show-all[=false]: When printing, show all resources (default hide terminated pods.)
--sort-by="": If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. 'ObjectMeta.Name'). The field in the API resource specified by this JSONPath expression must be an integer or a string.
- -t, --template="": Template string or path to template file to use when -o=template or -o=templatefile. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]
+ -t, --template="": Template string or path to template file to use when -o=template, -o=templatefile or -o=jsonpath. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]. The jsonpath template is composed of jsonpath expressions enclosed by {} [http://releases.k8s.io/HEAD/docs/user-guide/jsonpath.md]
```
### Options inherited from parent commands
@@ -105,7 +105,7 @@ $ kubectl config view -o template --template='{{range .users}}{{ if eq .name "e2
* [kubectl config](kubectl_config.md) - config modifies kubeconfig files
-###### Auto generated by spf13/cobra at 2015-08-18 17:36:23.977277472 +0000 UTC
+###### Auto generated by spf13/cobra at 2015-08-13 08:45:23.478129098 +0000 UTC
[]()
diff --git a/docs/user-guide/kubectl/kubectl_expose.md b/docs/user-guide/kubectl/kubectl_expose.md
index 41482e22d85..5da034512c3 100644
--- a/docs/user-guide/kubectl/kubectl_expose.md
+++ b/docs/user-guide/kubectl/kubectl_expose.md
@@ -76,7 +76,7 @@ $ kubectl expose rc streamer --port=4100 --protocol=udp --name=video-stream
-l, --labels="": Labels to apply to the service created by this call.
--name="": The name for the newly created object.
--no-headers[=false]: When using the default output, don't print headers.
- -o, --output="": Output format. One of: json|yaml|template|templatefile|wide.
+ -o, --output="": Output format. One of: json|yaml|template|templatefile|wide|jsonpath.
--output-version="": Output the formatted object with the given version (default api-version).
--overrides="": An inline JSON override for the generated object. If this is non-empty, it is used to override the generated object. Requires that the object supply a valid apiVersion field.
--port=-1: The port that the service should serve on. Copied from the resource being exposed, if unspecified
@@ -87,7 +87,7 @@ $ kubectl expose rc streamer --port=4100 --protocol=udp --name=video-stream
-a, --show-all[=false]: When printing, show all resources (default hide terminated pods.)
--sort-by="": If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. 'ObjectMeta.Name'). The field in the API resource specified by this JSONPath expression must be an integer or a string.
--target-port="": Name or number for the port on the container that the service should direct traffic to. Optional.
- -t, --template="": Template string or path to template file to use when -o=template or -o=templatefile. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]
+ -t, --template="": Template string or path to template file to use when -o=template, -o=templatefile or -o=jsonpath. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]. The jsonpath template is composed of jsonpath expressions enclosed by {} [http://releases.k8s.io/HEAD/docs/user-guide/jsonpath.md]
--type="": Type for this service: ClusterIP, NodePort, or LoadBalancer. Default is 'ClusterIP' unless --create-external-load-balancer is specified.
```
@@ -124,7 +124,7 @@ $ kubectl expose rc streamer --port=4100 --protocol=udp --name=video-stream
* [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager
-###### Auto generated by spf13/cobra at 2015-08-18 17:36:23.976918949 +0000 UTC
+###### Auto generated by spf13/cobra at 2015-08-13 08:45:23.476872373 +0000 UTC
[]()
diff --git a/docs/user-guide/kubectl/kubectl_get.md b/docs/user-guide/kubectl/kubectl_get.md
index 440ad8fa419..2bfca8ae7eb 100644
--- a/docs/user-guide/kubectl/kubectl_get.md
+++ b/docs/user-guide/kubectl/kubectl_get.md
@@ -49,7 +49,7 @@ By specifying the output as 'template' and providing a Go template as the value
of the --template flag, you can filter the attributes of the fetched resource(s).
```
-kubectl get [(-o|--output=)json|yaml|template|wide|...] (TYPE [(NAME | -l label] | TYPE/NAME ...) [flags]
+kubectl get [(-o|--output=)json|yaml|template|templatefile|wide|jsonpath|...] (TYPE [NAME | -l label] | TYPE/NAME ...) [flags]
```
### Examples
@@ -88,12 +88,12 @@ $ kubectl get rc/web service/frontend pods/web-pod-13je7
-h, --help[=false]: help for get
-L, --label-columns=[]: Accepts a comma separated list of labels that are going to be presented as columns. Names are case-sensitive. You can also use multiple flag statements like -L label1 -L label2...
--no-headers[=false]: When using the default output, don't print headers.
- -o, --output="": Output format. One of: json|yaml|template|templatefile|wide.
+ -o, --output="": Output format. One of: json|yaml|template|templatefile|wide|jsonpath.
--output-version="": Output the formatted object with the given version (default api-version).
-l, --selector="": Selector (label query) to filter on
-a, --show-all[=false]: When printing, show all resources (default hide terminated pods.)
--sort-by="": If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. 'ObjectMeta.Name'). The field in the API resource specified by this JSONPath expression must be an integer or a string.
- -t, --template="": Template string or path to template file to use when -o=template or -o=templatefile. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]
+ -t, --template="": Template string or path to template file to use when -o=template, -o=templatefile or -o=jsonpath. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]. The jsonpath template is composed of jsonpath expressions enclosed by {} [http://releases.k8s.io/HEAD/docs/user-guide/jsonpath.md]
-w, --watch[=false]: After listing/getting the requested object, watch for changes.
--watch-only[=false]: Watch for changes to the requested object(s), without listing/getting first.
```
@@ -131,7 +131,7 @@ $ kubectl get rc/web service/frontend pods/web-pod-13je7
* [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager
-###### Auto generated by spf13/cobra at 2015-08-18 17:36:23.974605429 +0000 UTC
+###### Auto generated by spf13/cobra at 2015-08-20 01:10:19.603989785 +0000 UTC
[]()
diff --git a/docs/user-guide/kubectl/kubectl_label.md b/docs/user-guide/kubectl/kubectl_label.md
index 320a6ef783b..ae776d89ec8 100644
--- a/docs/user-guide/kubectl/kubectl_label.md
+++ b/docs/user-guide/kubectl/kubectl_label.md
@@ -78,14 +78,14 @@ $ kubectl label pods foo bar-
-f, --filename=[]: Filename, directory, or URL to a file identifying the resource to update the labels
-h, --help[=false]: help for label
--no-headers[=false]: When using the default output, don't print headers.
- -o, --output="": Output format. One of: json|yaml|template|templatefile|wide.
+ -o, --output="": Output format. One of: json|yaml|template|templatefile|wide|jsonpath.
--output-version="": Output the formatted object with the given version (default api-version).
--overwrite[=false]: If true, allow labels to be overwritten, otherwise reject label updates that overwrite existing labels.
--resource-version="": If non-empty, the labels update will only succeed if this is the current resource-version for the object. Only valid when specifying a single resource.
-l, --selector="": Selector (label query) to filter on
-a, --show-all[=false]: When printing, show all resources (default hide terminated pods.)
--sort-by="": If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. 'ObjectMeta.Name'). The field in the API resource specified by this JSONPath expression must be an integer or a string.
- -t, --template="": Template string or path to template file to use when -o=template or -o=templatefile. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]
+ -t, --template="": Template string or path to template file to use when -o=template, -o=templatefile or -o=jsonpath. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]. The jsonpath template is composed of jsonpath expressions enclosed by {} [http://releases.k8s.io/HEAD/docs/user-guide/jsonpath.md]
```
### Options inherited from parent commands
@@ -121,7 +121,7 @@ $ kubectl label pods foo bar-
* [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager
-###### Auto generated by spf13/cobra at 2015-08-18 17:36:23.977042997 +0000 UTC
+###### Auto generated by spf13/cobra at 2015-08-20 01:10:19.61318045 +0000 UTC
[]()
diff --git a/docs/user-guide/kubectl/kubectl_rolling-update.md b/docs/user-guide/kubectl/kubectl_rolling-update.md
index 0580645cb8a..c65b2dd895c 100644
--- a/docs/user-guide/kubectl/kubectl_rolling-update.md
+++ b/docs/user-guide/kubectl/kubectl_rolling-update.md
@@ -75,13 +75,13 @@ $ kubectl rolling-update frontend --image=image:v2
-h, --help[=false]: help for rolling-update
--image="": Image to use for upgrading the replication controller. Can not be used with --filename/-f
--no-headers[=false]: When using the default output, don't print headers.
- -o, --output="": Output format. One of: json|yaml|template|templatefile|wide.
+ -o, --output="": Output format. One of: json|yaml|template|templatefile|wide|jsonpath.
--output-version="": Output the formatted object with the given version (default api-version).
--poll-interval=3s: Time delay between polling for replication controller status after the update. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
--rollback[=false]: If true, this is a request to abort an existing rollout that is partially rolled out. It effectively reverses current and next and runs a rollout
-a, --show-all[=false]: When printing, show all resources (default hide terminated pods.)
--sort-by="": If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. 'ObjectMeta.Name'). The field in the API resource specified by this JSONPath expression must be an integer or a string.
- -t, --template="": Template string or path to template file to use when -o=template or -o=templatefile. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]
+ -t, --template="": Template string or path to template file to use when -o=template, -o=templatefile or -o=jsonpath. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]. The jsonpath template is composed of jsonpath expressions enclosed by {} [http://releases.k8s.io/HEAD/docs/user-guide/jsonpath.md]
--timeout=5m0s: Max time to wait for a replication controller to update before giving up. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
--update-period=1m0s: Time to wait between updating pods. Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
```
@@ -119,7 +119,7 @@ $ kubectl rolling-update frontend --image=image:v2
* [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager
-###### Auto generated by spf13/cobra at 2015-08-18 17:36:23.97558907 +0000 UTC
+###### Auto generated by spf13/cobra at 2015-08-20 01:10:19.606885893 +0000 UTC
[]()
diff --git a/docs/user-guide/kubectl/kubectl_run.md b/docs/user-guide/kubectl/kubectl_run.md
index 17487cafa13..fc7b118d21d 100644
--- a/docs/user-guide/kubectl/kubectl_run.md
+++ b/docs/user-guide/kubectl/kubectl_run.md
@@ -82,7 +82,7 @@ $ kubectl run nginx --image=nginx --command -- ...
--image="": The image for the container to run.
-l, --labels="": Labels to apply to the pod(s).
--no-headers[=false]: When using the default output, don't print headers.
- -o, --output="": Output format. One of: json|yaml|template|templatefile|wide.
+ -o, --output="": Output format. One of: json|yaml|template|templatefile|wide|jsonpath.
--output-version="": Output the formatted object with the given version (default api-version).
--overrides="": An inline JSON override for the generated object. If this is non-empty, it is used to override the generated object. Requires that the object supply a valid apiVersion field.
--port=-1: The port that this container exposes.
@@ -91,7 +91,7 @@ $ kubectl run nginx --image=nginx --command -- ...
-a, --show-all[=false]: When printing, show all resources (default hide terminated pods.)
--sort-by="": If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. 'ObjectMeta.Name'). The field in the API resource specified by this JSONPath expression must be an integer or a string.
-i, --stdin[=false]: Keep stdin open on the container(s) in the pod, even if nothing is attached.
- -t, --template="": Template string or path to template file to use when -o=template or -o=templatefile. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]
+ -t, --template="": Template string or path to template file to use when -o=template, -o=templatefile or -o=jsonpath. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]. The jsonpath template is composed of jsonpath expressions enclosed by {} [http://releases.k8s.io/HEAD/docs/user-guide/jsonpath.md]
--tty[=false]: Allocated a TTY for each container in the pod. Because -t is currently shorthand for --template, -t is not supported for --tty. This shorthand is deprecated and we expect to adopt -t for --tty soon.
```
@@ -128,7 +128,7 @@ $ kubectl run nginx --image=nginx --command -- ...
* [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager
-###### Auto generated by spf13/cobra at 2015-08-18 17:36:23.976238275 +0000 UTC
+###### Auto generated by spf13/cobra at 2015-08-14 06:15:57.11465464 +0000 UTC
[]()
diff --git a/hack/lib/test.sh b/hack/lib/test.sh
index c48337763a6..907c2ca0050 100644
--- a/hack/lib/test.sh
+++ b/hack/lib/test.sh
@@ -51,6 +51,31 @@ kube::test::get_object_assert() {
fi
}
+kube::test::get_object_jsonpath_assert() {
+ local object=$1
+ local request=$2
+ local expected=$3
+
+ res=$(eval kubectl get "${kube_flags[@]}" $object -o jsonpath -t \"$request\")
+
+ if [[ "$res" =~ ^$expected$ ]]; then
+ echo -n ${green}
+ echo "Successful get $object $request: $res"
+ echo -n ${reset}
+ return 0
+ else
+ echo ${bold}${red}
+ echo "FAIL!"
+ echo "Get $object $request"
+ echo " Expected: $expected"
+ echo " Got: $res"
+ echo ${reset}${red}
+ caller
+ echo ${reset}
+ return 1
+ fi
+}
+
kube::test::describe_object_assert() {
local resource=$1
local object=$2
diff --git a/hack/test-cmd.sh b/hack/test-cmd.sh
index 815503f8eec..8884172ff25 100755
--- a/hack/test-cmd.sh
+++ b/hack/test-cmd.sh
@@ -234,6 +234,11 @@ runTests() {
kube::test::get_object_assert 'pod valid-pod' "{{$id_field}}" 'valid-pod'
kube::test::get_object_assert 'pod/valid-pod' "{{$id_field}}" 'valid-pod'
kube::test::get_object_assert 'pods/valid-pod' "{{$id_field}}" 'valid-pod'
+ # Repeat above test using jsonpath template
+ kube::test::get_object_jsonpath_assert pods "{.items[*]$id_field}" 'valid-pod'
+ kube::test::get_object_jsonpath_assert 'pod valid-pod' "{$id_field}" 'valid-pod'
+ kube::test::get_object_jsonpath_assert 'pod/valid-pod' "{$id_field}" 'valid-pod'
+ kube::test::get_object_jsonpath_assert 'pods/valid-pod' "{$id_field}" 'valid-pod'
# Describe command should print detailed information
kube::test::describe_object_assert pods 'valid-pod' "Name:" "Image(s):" "Node:" "Labels:" "Status:" "Replication Controllers"
# Describe command (resource only) should print detailed information
diff --git a/pkg/kubectl/cmd/get.go b/pkg/kubectl/cmd/get.go
index 7396fc00253..83409ac857d 100644
--- a/pkg/kubectl/cmd/get.go
+++ b/pkg/kubectl/cmd/get.go
@@ -69,7 +69,7 @@ func NewCmdGet(f *cmdutil.Factory, out io.Writer) *cobra.Command {
validArgs := p.HandledResources()
cmd := &cobra.Command{
- Use: "get [(-o|--output=)json|yaml|template|wide|...] (TYPE [(NAME | -l label] | TYPE/NAME ...) [flags]",
+ Use: "get [(-o|--output=)json|yaml|template|templatefile|wide|jsonpath|...] (TYPE [NAME | -l label] | TYPE/NAME ...) [flags]",
Short: "Display one or many resources",
Long: get_long,
Example: get_example,
diff --git a/pkg/kubectl/cmd/util/printing.go b/pkg/kubectl/cmd/util/printing.go
index 9473cd14798..c33f8b4994e 100644
--- a/pkg/kubectl/cmd/util/printing.go
+++ b/pkg/kubectl/cmd/util/printing.go
@@ -28,10 +28,10 @@ import (
// AddPrinterFlags adds printing related flags to a command (e.g. output format, no headers, template path)
func AddPrinterFlags(cmd *cobra.Command) {
- cmd.Flags().StringP("output", "o", "", "Output format. One of: json|yaml|template|templatefile|wide.")
+ cmd.Flags().StringP("output", "o", "", "Output format. One of: json|yaml|template|templatefile|wide|jsonpath.")
cmd.Flags().String("output-version", "", "Output the formatted object with the given version (default api-version).")
cmd.Flags().Bool("no-headers", false, "When using the default output, don't print headers.")
- cmd.Flags().StringP("template", "t", "", "Template string or path to template file to use when -o=template or -o=templatefile. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]")
+ cmd.Flags().StringP("template", "t", "", "Template string or path to template file to use when -o=template, -o=templatefile or -o=jsonpath. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview]. The jsonpath template is composed of jsonpath expressions enclosed by {} [http://releases.k8s.io/HEAD/docs/user-guide/jsonpath.md]")
cmd.Flags().String("sort-by", "", "If non-empty, sort list types using this field specification. The field specification is expressed as a JSONPath expression (e.g. 'ObjectMeta.Name'). The field in the API resource specified by this JSONPath expression must be an integer or a string.")
cmd.Flags().BoolP("show-all", "a", false, "When printing, show all resources (default hide terminated pods.)")
}
diff --git a/pkg/kubectl/resource_printer.go b/pkg/kubectl/resource_printer.go
index 99bb6f9747d..971eb0425a0 100644
--- a/pkg/kubectl/resource_printer.go
+++ b/pkg/kubectl/resource_printer.go
@@ -35,6 +35,7 @@ import (
"k8s.io/kubernetes/pkg/conversion"
"k8s.io/kubernetes/pkg/runtime"
"k8s.io/kubernetes/pkg/util"
+ "k8s.io/kubernetes/pkg/util/jsonpath"
"k8s.io/kubernetes/pkg/volume"
)
@@ -71,6 +72,15 @@ func GetPrinter(format, formatArgument string) (ResourcePrinter, bool, error) {
if err != nil {
return nil, false, fmt.Errorf("error parsing template %s, %v\n", string(data), err)
}
+ case "jsonpath":
+ if len(formatArgument) == 0 {
+ return nil, false, fmt.Errorf("jsonpath format specified but no jsonpath template given")
+ }
+ var err error
+ printer, err = NewJSONPathPrinter(formatArgument)
+ if err != nil {
+ return nil, false, fmt.Errorf("error parsing jsonpath %s, %v\n", formatArgument, err)
+ }
case "wide":
fallthrough
case "":
@@ -1214,3 +1224,37 @@ func indirect(v reflect.Value) (rv reflect.Value, isNil bool) {
}
return v, false
}
+
+// JSONPathPrinter is an implementation of ResourcePrinter which formats data with jsonpath expression.
+type JSONPathPrinter struct {
+ rawTemplate string
+ *jsonpath.JSONPath
+}
+
+func NewJSONPathPrinter(tmpl string) (*JSONPathPrinter, error) {
+ j := jsonpath.New("out")
+ if err := j.Parse(tmpl); err != nil {
+ return nil, err
+ }
+ return &JSONPathPrinter{tmpl, j}, nil
+}
+
+// PrintObj formats the obj with the JSONPath Template.
+func (j *JSONPathPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
+ data, err := json.Marshal(obj)
+ if err != nil {
+ return err
+ }
+ out := map[string]interface{}{}
+ if err := json.Unmarshal(data, &out); err != nil {
+ return err
+ }
+ if err = j.JSONPath.Execute(w, out); err != nil {
+ fmt.Fprintf(w, "Error executing template: %v\n", err)
+ fmt.Fprintf(w, "template was:\n\t%v\n", j.rawTemplate)
+ fmt.Fprintf(w, "raw data was:\n\t%v\n", string(data))
+ fmt.Fprintf(w, "object given to template engine was:\n\t%+v\n", out)
+ return fmt.Errorf("error executing jsonpath '%v': '%v'\n----data----\n%+v\n", j.rawTemplate, err, out)
+ }
+ return nil
+}
diff --git a/pkg/kubectl/resource_printer_test.go b/pkg/kubectl/resource_printer_test.go
index 31fd068fe24..9f2830be95c 100644
--- a/pkg/kubectl/resource_printer_test.go
+++ b/pkg/kubectl/resource_printer_test.go
@@ -77,14 +77,6 @@ func TestVersionedPrinter(t *testing.T) {
}
}
-func TestYAMLPrinter(t *testing.T) {
- testPrinter(t, &YAMLPrinter{}, yaml.Unmarshal)
-}
-
-func TestJSONPrinter(t *testing.T) {
- testPrinter(t, &JSONPrinter{}, json.Unmarshal)
-}
-
func TestPrintDefault(t *testing.T) {
printer, found, err := GetPrinter("", "")
if err != nil {
@@ -95,90 +87,72 @@ func TestPrintDefault(t *testing.T) {
}
}
-type internalType struct {
- Name string
+type TestPrintType struct {
+ Data string
}
-func (*internalType) IsAnAPIObject() {
+func (*TestPrintType) IsAnAPIObject() {}
-}
+type TestUnknownType struct{}
-func TestPrintJSONForObject(t *testing.T) {
- buf := bytes.NewBuffer([]byte{})
- printer, found, err := GetPrinter("json", "")
- if err != nil || !found {
- t.Fatalf("unexpected error: %#v", err)
- }
- if err := printer.PrintObj(&internalType{Name: "foo"}, buf); err != nil {
- t.Fatalf("unexpected error: %#v", err)
- }
- obj := map[string]interface{}{}
- if err := json.Unmarshal(buf.Bytes(), &obj); err != nil {
- t.Fatalf("unexpected error: %#v\n%s", err, buf.String())
- }
- if obj["Name"] != "foo" {
- t.Errorf("unexpected field: %#v", obj)
- }
-}
+func (*TestUnknownType) IsAnAPIObject() {}
-func TestPrintJSON(t *testing.T) {
- buf := bytes.NewBuffer([]byte{})
- printer, found, err := GetPrinter("json", "")
- if err != nil || !found {
- t.Fatalf("unexpected error: %#v", err)
- }
- printer.PrintObj(&api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, buf)
- obj := map[string]interface{}{}
- if err := json.Unmarshal(buf.Bytes(), &obj); err != nil {
- t.Errorf("unexpected error: %#v\n%s", err, buf.String())
- }
-}
-
-func TestPrintYAML(t *testing.T) {
- buf := bytes.NewBuffer([]byte{})
- printer, found, err := GetPrinter("yaml", "")
- if err != nil || !found {
- t.Fatalf("unexpected error: %#v", err)
- }
- printer.PrintObj(&api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, buf)
- obj := map[string]interface{}{}
- if err := yaml.Unmarshal(buf.Bytes(), &obj); err != nil {
- t.Errorf("unexpected error: %#v\n%s", err, buf.String())
- }
-}
-
-func TestPrintTemplate(t *testing.T) {
- buf := bytes.NewBuffer([]byte{})
- printer, found, err := GetPrinter("template", "{{if .id}}{{.id}}{{end}}{{if .metadata.name}}{{.metadata.name}}{{end}}")
- if err != nil || !found {
- t.Fatalf("unexpected error: %#v", err)
- }
- unversionedPod := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
- obj, err := api.Scheme.ConvertToVersion(unversionedPod, testapi.Version())
- err = printer.PrintObj(obj, buf)
+func TestPrinter(t *testing.T) {
+ //test inputs
+ simpleTest := &TestPrintType{"foo"}
+ podTest := &api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}
+ testapi, err := api.Scheme.ConvertToVersion(podTest, testapi.Version())
if err != nil {
- t.Fatalf("unexpected error: %#v", err)
+ t.Fatalf("unexpected error: %v", err)
}
- if buf.String() != "foo" {
- t.Errorf("unexpected output: %s", buf.String())
+
+ printerTests := []struct {
+ Name string
+ Format string
+ FormatArgument string
+ Input runtime.Object
+ Expect string
+ }{
+ {"test json", "json", "", simpleTest, "{\n \"Data\": \"foo\"\n}\n"},
+ {"test yaml", "yaml", "", simpleTest, "Data: foo\n"},
+ {"test template", "template", "{{if .id}}{{.id}}{{end}}{{if .metadata.name}}{{.metadata.name}}{{end}}",
+ podTest, "foo"},
+ {"test jsonpath", "jsonpath", "{.metadata.name}", podTest, "foo"},
+ {"emits versioned objects", "template", "{{.kind}}", testapi, "Pod"},
}
+ for _, test := range printerTests {
+ buf := bytes.NewBuffer([]byte{})
+ printer, found, err := GetPrinter(test.Format, test.FormatArgument)
+ if err != nil || !found {
+ t.Errorf("unexpected error: %#v", err)
+ }
+ if err := printer.PrintObj(test.Input, buf); err != nil {
+ t.Errorf("unexpected error: %#v", err)
+ }
+ if buf.String() != test.Expect {
+ t.Errorf("in %s, expect %q, got %q", test.Name, test.Expect, buf.String(), buf.String())
+ }
+ }
+
}
-func TestPrintEmptyTemplate(t *testing.T) {
- if _, _, err := GetPrinter("template", ""); err == nil {
- t.Errorf("unexpected non-error")
+func TestBadPrinter(t *testing.T) {
+ badPrinterTests := []struct {
+ Name string
+ Format string
+ FormatArgument string
+ Error error
+ }{
+ {"empty template", "template", "", fmt.Errorf("template format specified but no template given")},
+ {"bad template", "template", "{{ .Name", fmt.Errorf("error parsing template {{ .Name, template: output:1: unclosed action\n")},
+ {"bad templatefile", "templatefile", "", fmt.Errorf("templatefile format specified but no template file given")},
+ {"bad jsonpath", "jsonpath", "{.Name", fmt.Errorf("error parsing jsonpath {.Name, unclosed action\n")},
}
-}
-
-func TestPrintBadTemplate(t *testing.T) {
- if _, _, err := GetPrinter("template", "{{ .Name"); err == nil {
- t.Errorf("unexpected non-error")
- }
-}
-
-func TestPrintBadTemplateFile(t *testing.T) {
- if _, _, err := GetPrinter("templatefile", ""); err == nil {
- t.Errorf("unexpected non-error")
+ for _, test := range badPrinterTests {
+ _, _, err := GetPrinter(test.Format, test.FormatArgument)
+ if err == nil || err.Error() != test.Error.Error() {
+ t.Errorf("in %s, expect %s, got %s", test.Name, test.Error, err)
+ }
}
}
@@ -227,15 +201,13 @@ func testPrinter(t *testing.T, printer ResourcePrinter, unmarshalFunc func(data
}
}
-type TestPrintType struct {
- Data string
+func TestYAMLPrinter(t *testing.T) {
+ testPrinter(t, &YAMLPrinter{}, yaml.Unmarshal)
}
-func (*TestPrintType) IsAnAPIObject() {}
-
-type TestUnknownType struct{}
-
-func (*TestUnknownType) IsAnAPIObject() {}
+func TestJSONPrinter(t *testing.T) {
+ testPrinter(t, &JSONPrinter{}, json.Unmarshal)
+}
func PrintCustomType(obj *TestPrintType, w io.Writer, withNamespace bool, wide bool, showAll bool, columnLabels []string) error {
_, err := fmt.Fprintf(w, "%s", obj.Data)
@@ -284,27 +256,6 @@ func TestUnknownTypePrinting(t *testing.T) {
}
}
-func TestTemplateEmitsVersionedObjects(t *testing.T) {
- // kind is always blank in memory and set on the wire
- printer, err := NewTemplatePrinter([]byte(`{{.kind}}`))
- if err != nil {
- t.Fatalf("tmpl fail: %v", err)
- }
- obj, err := api.Scheme.ConvertToVersion(&api.Pod{}, testapi.Version())
- if err != nil {
- t.Fatalf("unexpected error: %v", err)
- }
-
- buffer := &bytes.Buffer{}
- err = printer.PrintObj(obj, buffer)
- if err != nil {
- t.Fatalf("print fail: %v", err)
- }
- if e, a := "Pod", string(buffer.Bytes()); e != a {
- t.Errorf("Expected %v, got %v", e, a)
- }
-}
-
func TestTemplatePanic(t *testing.T) {
tmpl := `{{and ((index .currentState.info "foo").state.running.startedAt) .currentState.info.net.state.running.startedAt}}`
printer, err := NewTemplatePrinter([]byte(tmpl))
@@ -450,6 +401,10 @@ func TestPrinters(t *testing.T) {
if err != nil {
t.Fatal(err)
}
+ jsonpathPrinter, err := NewJSONPathPrinter("{.metadata.name}")
+ if err != nil {
+ t.Fatal(err)
+ }
printers := map[string]ResourcePrinter{
"humanReadable": NewHumanReadablePrinter(true, false, false, false, []string{}),
"humanReadableHeaders": NewHumanReadablePrinter(false, false, false, false, []string{}),
@@ -457,6 +412,7 @@ func TestPrinters(t *testing.T) {
"yaml": &YAMLPrinter{},
"template": templatePrinter,
"template2": templatePrinter2,
+ "jsonpath": jsonpathPrinter,
}
objects := map[string]runtime.Object{
"pod": &api.Pod{ObjectMeta: om("pod")},
@@ -471,6 +427,7 @@ func TestPrinters(t *testing.T) {
// map of printer name to set of objects it should fail on.
expectedErrors := map[string]util.StringSet{
"template2": util.NewStringSet("pod", "emptyPodList", "endpoints"),
+ "jsonpath": util.NewStringSet("emptyPodList", "nonEmptyPodList", "endpoints"),
}
for pName, p := range printers {
diff --git a/pkg/util/jsonpath/jsonpath.go b/pkg/util/jsonpath/jsonpath.go
index f74ed35e9b2..b924e76f5dd 100644
--- a/pkg/util/jsonpath/jsonpath.go
+++ b/pkg/util/jsonpath/jsonpath.go
@@ -21,7 +21,6 @@ import (
"fmt"
"io"
"reflect"
- "strconv"
"k8s.io/kubernetes/third_party/golang/template"
)
@@ -214,10 +213,9 @@ func (j *JSONPath) evalIdentifier(input []reflect.Value, node *IdentifierNode) (
func (j *JSONPath) evalArray(input []reflect.Value, node *ArrayNode) ([]reflect.Value, error) {
result := []reflect.Value{}
for _, value := range input {
- if value.Kind() == reflect.Interface {
- value = reflect.ValueOf(value.Interface())
- }
- if value.Kind() != reflect.Array && value.Kind() != reflect.Slice {
+
+ value, isNil := template.Indirect(value)
+ if isNil || (value.Kind() != reflect.Array && value.Kind() != reflect.Slice) {
return input, fmt.Errorf("%v is not array or slice", value)
}
params := node.Params
@@ -265,9 +263,11 @@ func (j *JSONPath) evalField(input []reflect.Value, node *FieldNode) ([]reflect.
results := []reflect.Value{}
for _, value := range input {
var result reflect.Value
- if value.Kind() == reflect.Interface {
- value = reflect.ValueOf(value.Interface())
+ value, isNil := template.Indirect(value)
+ if isNil {
+ continue
}
+
if value.Kind() == reflect.Struct {
result = value.FieldByName(node.Value)
} else if value.Kind() == reflect.Map {
@@ -287,6 +287,11 @@ func (j *JSONPath) evalField(input []reflect.Value, node *FieldNode) ([]reflect.
func (j *JSONPath) evalWildcard(input []reflect.Value, node *WildcardNode) ([]reflect.Value, error) {
results := []reflect.Value{}
for _, value := range input {
+ value, isNil := template.Indirect(value)
+ if isNil {
+ continue
+ }
+
kind := value.Kind()
if kind == reflect.Struct {
for i := 0; i < value.NumField(); i++ {
@@ -310,6 +315,11 @@ func (j *JSONPath) evalRecursive(input []reflect.Value, node *RecursiveNode) ([]
result := []reflect.Value{}
for _, value := range input {
results := []reflect.Value{}
+ value, isNil := template.Indirect(value)
+ if isNil {
+ continue
+ }
+
kind := value.Kind()
if kind == reflect.Struct {
for i := 0; i < value.NumField(); i++ {
@@ -340,9 +350,8 @@ func (j *JSONPath) evalRecursive(input []reflect.Value, node *RecursiveNode) ([]
func (j *JSONPath) evalFilter(input []reflect.Value, node *FilterNode) ([]reflect.Value, error) {
results := []reflect.Value{}
for _, value := range input {
- if value.Kind() == reflect.Interface {
- value = reflect.ValueOf(value.Interface())
- }
+ value, _ = template.Indirect(value)
+
if value.Kind() != reflect.Array && value.Kind() != reflect.Slice {
return input, fmt.Errorf("%v is not array or slice", value)
}
@@ -407,77 +416,11 @@ func (j *JSONPath) evalFilter(input []reflect.Value, node *FilterNode) ([]reflec
// evalToText translates reflect value to corresponding text
func (j *JSONPath) evalToText(v reflect.Value) ([]byte, error) {
- if v.Kind() == reflect.Interface {
- v = reflect.ValueOf(v.Interface())
+ iface, ok := template.PrintableValue(v)
+ if !ok {
+ return nil, fmt.Errorf("can't print type %s", v.Type())
}
var buffer bytes.Buffer
- switch v.Kind() {
- case reflect.Invalid:
- //pass
- case reflect.Ptr:
- text, err := j.evalToText(reflect.Indirect(v))
- if err != nil {
- return nil, err
- }
- buffer.Write(text)
- case reflect.Bool:
- if variable := v.Bool(); variable {
- buffer.WriteString("True")
- } else {
- buffer.WriteString("False")
- }
- case reflect.Float32:
- buffer.WriteString(strconv.FormatFloat(v.Float(), 'f', -1, 32))
- case reflect.Float64:
- buffer.WriteString(strconv.FormatFloat(v.Float(), 'f', -1, 64))
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- buffer.WriteString(strconv.FormatInt(v.Int(), 10))
- case reflect.String:
- buffer.WriteString(v.String())
- case reflect.Array, reflect.Slice:
- buffer.WriteString("[")
- for i := 0; i < v.Len(); i++ {
- text, err := j.evalToText(v.Index(i))
- if err != nil {
- return nil, err
- }
- buffer.Write(text)
- if i != v.Len()-1 {
- buffer.WriteString(", ")
- }
- }
- buffer.WriteString("]")
- case reflect.Struct:
- buffer.WriteString("{")
- for i := 0; i < v.NumField(); i++ {
- text, err := j.evalToText(v.Field(i))
- if err != nil {
- return nil, err
- }
- pair := fmt.Sprintf("%s: %s", v.Type().Field(i).Name, text)
- buffer.WriteString(pair)
- if i != v.NumField()-1 {
- buffer.WriteString(", ")
- }
- }
- buffer.WriteString("}")
- case reflect.Map:
- buffer.WriteString("{")
- for i, key := range v.MapKeys() {
- text, err := j.evalToText(v.MapIndex(key))
- if err != nil {
- return nil, err
- }
- pair := fmt.Sprintf("%s: %s", key, text)
- buffer.WriteString(pair)
- if i != len(v.MapKeys())-1 {
- buffer.WriteString(", ")
- }
- }
- buffer.WriteString("}")
-
- default:
- return nil, fmt.Errorf("%v is not printable", v.Kind())
- }
+ fmt.Fprint(&buffer, iface)
return buffer.Bytes(), nil
}
diff --git a/pkg/util/jsonpath/jsonpath_test.go b/pkg/util/jsonpath/jsonpath_test.go
index 78cebc92f0c..4a98a7261d2 100644
--- a/pkg/util/jsonpath/jsonpath_test.go
+++ b/pkg/util/jsonpath/jsonpath_test.go
@@ -19,6 +19,7 @@ package jsonpath
import (
"bytes"
"encoding/json"
+ "fmt"
"testing"
)
@@ -69,25 +70,30 @@ func testFailJSONPath(tests []jsonpathTest, t *testing.T) {
}
}
+type book struct {
+ Category string
+ Author string
+ Title string
+ Price float32
+}
+
+func (b book) String() string {
+ return fmt.Sprintf("{Category: %s, Author: %s, Title: %s, Price: %v}", b.Category, b.Author, b.Title, b.Price)
+}
+
+type bicycle struct {
+ Color string
+ Price float32
+}
+
+type store struct {
+ Book []book
+ Bicycle bicycle
+ Name string
+ Labels map[string]int
+}
+
func TestStructInput(t *testing.T) {
- type book struct {
- Category string
- Author string
- Title string
- Price float32
- }
-
- type bicycle struct {
- Color string
- Price float32
- }
-
- type store struct {
- Book []book
- Bicycle bicycle
- Name string
- Labels map[string]int
- }
storeData := store{
Name: "jsonpath",
@@ -106,13 +112,13 @@ func TestStructInput(t *testing.T) {
storeTests := []jsonpathTest{
{"plain", "hello jsonpath", nil, "hello jsonpath"},
- {"recursive", "{..}", []int{1, 2, 3}, "[1, 2, 3]"},
+ {"recursive", "{..}", []int{1, 2, 3}, "[1 2 3]"},
{"filter", "{[?(@<5)]}", []int{2, 6, 3, 7}, "2 3"},
{"quote", `{"{"}`, nil, "{"},
{"union", "{[1,3,4]}", []int{0, 1, 2, 3, 4}, "1 3 4"},
{"array", "{[0:2]}", []string{"Monday", "Tudesday"}, "Monday Tudesday"},
{"variable", "hello {.Name}", storeData, "hello jsonpath"},
- {"dict/", "{.Labels.web/html}", storeData, "15"},
+ {"dict/", "{$.Labels.web/html}", storeData, "15"},
{"dict-", "{.Labels.k8s-app}", storeData, "20"},
{"nest", "{.Bicycle.Color}", storeData, "red"},
{"allarray", "{.Book[*].Author}", storeData, "Nigel Rees Evelyn Waugh Herman Melville"},
@@ -197,18 +203,20 @@ func TestKubenates(t *testing.T) {
if err != nil {
t.Error(err)
}
+
nodesTests := []jsonpathTest{
{"range item", "{range .items[*]}{.metadata.name}, {end}{.kind}", nodesData, `127.0.0.1, 127.0.0.2, List`},
{"range addresss", "{.items[*].status.addresses[*].address}", nodesData,
`127.0.0.1 127.0.0.2 127.0.0.3`},
{"double range", "{range .items[*]}{range .status.addresses[*]}{.address}, {end}{end}", nodesData,
`127.0.0.1, 127.0.0.2, 127.0.0.3, `},
+ {"recursive name", "{..name}", nodesData, `127.0.0.1 127.0.0.2 myself e2e`},
{"item name", "{.items[*].metadata.name}", nodesData, `127.0.0.1 127.0.0.2`},
{"union nodes capacity", "{.items[*]['metadata.name', 'status.capacity']}", nodesData,
- `127.0.0.1 127.0.0.2 {cpu: 4} {cpu: 8}`},
+ `127.0.0.1 127.0.0.2 map[cpu:4] map[cpu:8]`},
{"range nodes capacity", "{range .items[*]}[{.metadata.name}, {.status.capacity}] {end}", nodesData,
- `[127.0.0.1, {cpu: 4}] [127.0.0.2, {cpu: 8}] `},
- {"user password", `{.users[?(@.name=="e2e")].user.password}`, nodesData, "secret"},
+ `[127.0.0.1, map[cpu:4]] [127.0.0.2, map[cpu:8]] `},
+ {"user password", `{.users[?(@.name=="e2e")].user.password}`, &nodesData, "secret"},
}
testJSONPath(nodesTests, t)
}
diff --git a/pkg/util/jsonpath/parser.go b/pkg/util/jsonpath/parser.go
index fafecdf087a..85aa2c1316e 100644
--- a/pkg/util/jsonpath/parser.go
+++ b/pkg/util/jsonpath/parser.go
@@ -151,7 +151,7 @@ func (p *Parser) parseInsideAction(cur *ListNode) error {
return fmt.Errorf("unclosed action")
case r == ' ':
p.consumeText()
- case r == '@': //the current object, just pass it
+ case r == '@' || r == '$': //the current object, just pass it
p.consumeText()
case r == '[':
return p.parseArray(cur)
diff --git a/third_party/golang/template/exec.go b/third_party/golang/template/exec.go
index 11fef63f486..82519edc933 100644
--- a/third_party/golang/template/exec.go
+++ b/third_party/golang/template/exec.go
@@ -1,9 +1,14 @@
+//This package is copied from Go library text/template.
+//The original private functions indirect and printableValue
+//are exported as public functions.
package template
import (
"reflect"
"fmt"
)
+var Indirect = indirect
+var PrintableValue = printableValue
var (
errorType = reflect.TypeOf((*error)(nil)).Elem()