From 6597e76ed87cec411de5ffac0713c261fe99f764 Mon Sep 17 00:00:00 2001 From: Joshua Carp Date: Sun, 2 Oct 2016 23:54:13 -0400 Subject: [PATCH] Escape special characters in jsonpath field names. Example: `{.items[*].metadata.labels.kubernetes\.io/hostname}` [Resolves #31984] --- pkg/util/jsonpath/jsonpath_test.go | 16 ++++++++++++++-- pkg/util/jsonpath/parser.go | 12 +++++++----- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/pkg/util/jsonpath/jsonpath_test.go b/pkg/util/jsonpath/jsonpath_test.go index a85517e61832..72d547104bc3 100644 --- a/pkg/util/jsonpath/jsonpath_test.go +++ b/pkg/util/jsonpath/jsonpath_test.go @@ -212,7 +212,12 @@ func TestKubernetes(t *testing.T) { "items":[ { "kind":"None", - "metadata":{"name":"127.0.0.1"}, + "metadata":{ + "name":"127.0.0.1", + "labels":{ + "kubernetes.io/hostname":"127.0.0.1" + } + }, "status":{ "capacity":{"cpu":"4"}, "addresses":[{"type": "LegacyHostIP", "address":"127.0.0.1"}] @@ -220,7 +225,12 @@ func TestKubernetes(t *testing.T) { }, { "kind":"None", - "metadata":{"name":"127.0.0.2"}, + "metadata":{ + "name":"127.0.0.2", + "labels":{ + "kubernetes.io/hostname":"127.0.0.2" + } + }, "status":{ "capacity":{"cpu":"8"}, "addresses":[ @@ -260,6 +270,8 @@ func TestKubernetes(t *testing.T) { {"range nodes capacity", `{range .items[*]}[{.metadata.name}, {.status.capacity}] {end}`, nodesData, "[127.0.0.1, map[cpu:4]] [127.0.0.2, map[cpu:8]] "}, {"user password", `{.users[?(@.name=="e2e")].user.password}`, &nodesData, "secret"}, + {"hostname", `{.items[0].metadata.labels.kubernetes\.io/hostname}`, &nodesData, "127.0.0.1"}, + {"hostname filter", `{.items[?(@.metadata.labels.kubernetes\.io/hostname=="127.0.0.1")].kind}`, &nodesData, "None"}, } testJSONPath(nodesTests, false, t) diff --git a/pkg/util/jsonpath/parser.go b/pkg/util/jsonpath/parser.go index 1ff82a3b9cb7..2883f7897fbc 100644 --- a/pkg/util/jsonpath/parser.go +++ b/pkg/util/jsonpath/parser.go @@ -382,19 +382,21 @@ Loop: // parseField scans a field until a terminator func (p *Parser) parseField(cur *ListNode) error { p.consumeText() - var r rune +Loop: for { - r = p.next() - if isTerminator(r) { + switch r := p.next(); { + case r == '\\': + p.next() + case isTerminator(r): p.backup() - break + break Loop } } value := p.consumeText() if value == "*" { cur.append(newWildcard()) } else { - cur.append(newField(value)) + cur.append(newField(strings.Replace(value, "\\", "", -1))) } return p.parseInsideAction(cur) }