Skip to content

Commit

Permalink
Merge pull request #33901 from jmcarp/issue-31984
Browse files Browse the repository at this point in the history
Automatic merge from submit-queue

Escape special characters in jsonpath field names.

There may be a better way to do this, but this seemed like the simplest possible version.

Example: `{.items[*].metadata.labels.kubernetes\.io/hostname}`

[Resolves #31984]
  • Loading branch information
Kubernetes Submit Queue committed Oct 18, 2016
2 parents 2a21384 + b48deeb commit f98c06b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
16 changes: 14 additions & 2 deletions pkg/util/jsonpath/jsonpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,25 @@ 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"}]
}
},
{
"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":[
Expand Down Expand Up @@ -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)

Expand Down
22 changes: 14 additions & 8 deletions pkg/util/jsonpath/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,23 +382,29 @@ Loop:
// parseField scans a field until a terminator
func (p *Parser) parseField(cur *ListNode) error {
p.consumeText()
var r rune
for {
r = p.next()
if isTerminator(r) {
p.backup()
break
}
for p.advance() {
}
value := p.consumeText()
if value == "*" {
cur.append(newWildcard())
} else {
cur.append(newField(value))
cur.append(newField(strings.Replace(value, "\\", "", -1)))
}
return p.parseInsideAction(cur)
}

// advance scans until next non-escaped terminator
func (p *Parser) advance() bool {
r := p.next()
if r == '\\' {
p.next()
} else if isTerminator(r) {
p.backup()
return false
}
return true
}

// isTerminator reports whether the input is at valid termination character to appear after an identifier.
func isTerminator(r rune) bool {
if isSpace(r) || isEndOfLine(r) {
Expand Down

0 comments on commit f98c06b

Please sign in to comment.