Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
16c624b2
Commit
16c624b2
authored
Dec 23, 2014
by
Daniel Smith
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve error reporting for bad templates
parent
69162355
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
58 additions
and
6 deletions
+58
-6
resource_printer.go
pkg/kubecfg/resource_printer.go
+16
-5
resource_printer_test.go
pkg/kubecfg/resource_printer_test.go
+17
-0
resource_printer.go
pkg/kubectl/resource_printer.go
+8
-1
resource_printer_test.go
pkg/kubectl/resource_printer_test.go
+17
-0
No files found.
pkg/kubecfg/resource_printer.go
View file @
16c624b2
...
@@ -327,7 +327,8 @@ func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) er
...
@@ -327,7 +327,8 @@ func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) er
// TemplatePrinter is an implementation of ResourcePrinter which formats data with a Go Template.
// TemplatePrinter is an implementation of ResourcePrinter which formats data with a Go Template.
type
TemplatePrinter
struct
{
type
TemplatePrinter
struct
{
template
*
template
.
Template
rawTemplate
string
template
*
template
.
Template
}
}
func
NewTemplatePrinter
(
tmpl
[]
byte
)
(
*
TemplatePrinter
,
error
)
{
func
NewTemplatePrinter
(
tmpl
[]
byte
)
(
*
TemplatePrinter
,
error
)
{
...
@@ -335,17 +336,27 @@ func NewTemplatePrinter(tmpl []byte) (*TemplatePrinter, error) {
...
@@ -335,17 +336,27 @@ func NewTemplatePrinter(tmpl []byte) (*TemplatePrinter, error) {
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
return
&
TemplatePrinter
{
t
},
nil
return
&
TemplatePrinter
{
string
(
tmpl
),
t
},
nil
}
}
// Print parses the data as JSON, and re-formats it with the Go Template.
// Print parses the data as JSON, and re-formats it with the Go Template.
func
(
t
*
TemplatePrinter
)
Print
(
data
[]
byte
,
w
io
.
Writer
)
error
{
func
(
t
*
TemplatePrinter
)
Print
(
data
[]
byte
,
w
io
.
Writer
)
error
{
o
bj
:=
map
[
string
]
interface
{}{}
o
ut
:=
map
[
string
]
interface
{}{}
err
:=
json
.
Unmarshal
(
data
,
&
o
bj
)
err
:=
json
.
Unmarshal
(
data
,
&
o
ut
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
return
t
.
template
.
Execute
(
w
,
obj
)
if
err
:=
t
.
template
.
Execute
(
w
,
out
);
err
!=
nil
{
// It is way easier to debug this stuff when it shows up in
// stdout instead of just stdin. So in addition to returning
// a nice error, also print useful stuff with the writer.
fmt
.
Fprintf
(
w
,
"Error executing template: %v
\n
"
,
err
)
fmt
.
Fprintf
(
w
,
"template was:
\n
%v
\n
"
,
t
.
rawTemplate
)
fmt
.
Fprintf
(
w
,
"raw data was:
\n
%v
\n
"
,
string
(
data
))
fmt
.
Fprintf
(
w
,
"object given to template engine was:
\n
%+v
\n
"
,
out
)
return
fmt
.
Errorf
(
"error executing template '%v': '%v'
\n
----data----
\n
%#v
\n
"
,
t
.
rawTemplate
,
err
,
out
)
}
return
nil
}
}
// PrintObj formats the obj with the Go Template.
// PrintObj formats the obj with the Go Template.
...
...
pkg/kubecfg/resource_printer_test.go
View file @
16c624b2
...
@@ -177,3 +177,20 @@ func TestTemplateEmitsVersionedObjects(t *testing.T) {
...
@@ -177,3 +177,20 @@ func TestTemplateEmitsVersionedObjects(t *testing.T) {
t
.
Errorf
(
"Expected %v, got %v"
,
e
,
a
)
t
.
Errorf
(
"Expected %v, got %v"
,
e
,
a
)
}
}
}
}
func
TestTemplatePanic
(
t
*
testing
.
T
)
{
tmpl
:=
`{{and ((index .currentState.info "update-demo").state.running.startedAt) .currentState.info.net.state.running.startedAt}}`
// kind is always blank in memory and set on the wire
printer
,
err
:=
NewTemplatePrinter
([]
byte
(
tmpl
))
if
err
!=
nil
{
t
.
Fatalf
(
"tmpl fail: %v"
,
err
)
}
buffer
:=
&
bytes
.
Buffer
{}
err
=
printer
.
PrintObj
(
&
api
.
Pod
{},
buffer
)
if
err
==
nil
{
t
.
Fatalf
(
"expected that template to crash"
)
}
if
buffer
.
String
()
==
""
{
t
.
Errorf
(
"no debugging info was printed"
)
}
}
pkg/kubectl/resource_printer.go
View file @
16c624b2
...
@@ -417,7 +417,14 @@ func (p *TemplatePrinter) PrintObj(obj runtime.Object, w io.Writer) error {
...
@@ -417,7 +417,14 @@ func (p *TemplatePrinter) PrintObj(obj runtime.Object, w io.Writer) error {
return
err
return
err
}
}
if
err
=
p
.
template
.
Execute
(
w
,
out
);
err
!=
nil
{
if
err
=
p
.
template
.
Execute
(
w
,
out
);
err
!=
nil
{
return
fmt
.
Errorf
(
"error executing template '%v': '%v'
\n
----data----
\n
%#v
\n
"
,
p
.
rawTemplate
,
err
,
out
)
// It is way easier to debug this stuff when it shows up in
// stdout instead of just stdin. So in addition to returning
// a nice error, also print useful stuff with the writer.
fmt
.
Fprintf
(
w
,
"Error executing template: %v
\n
"
,
err
)
fmt
.
Fprintf
(
w
,
"template was:
\n\t
%v
\n
"
,
p
.
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 template '%v': '%v'
\n
----data----
\n
%+v
\n
"
,
p
.
rawTemplate
,
err
,
out
)
}
}
return
nil
return
nil
}
}
...
...
pkg/kubectl/resource_printer_test.go
View file @
16c624b2
...
@@ -290,6 +290,23 @@ func TestTemplateEmitsVersionedObjects(t *testing.T) {
...
@@ -290,6 +290,23 @@ func TestTemplateEmitsVersionedObjects(t *testing.T) {
}
}
}
}
func
TestTemplatePanic
(
t
*
testing
.
T
)
{
tmpl
:=
`{{and ((index .currentState.info "update-demo").state.running.startedAt) .currentState.info.net.state.running.startedAt}}`
// kind is always blank in memory and set on the wire
printer
,
err
:=
NewTemplatePrinter
([]
byte
(
tmpl
),
testapi
.
Version
(),
api
.
Scheme
)
if
err
!=
nil
{
t
.
Fatalf
(
"tmpl fail: %v"
,
err
)
}
buffer
:=
&
bytes
.
Buffer
{}
err
=
printer
.
PrintObj
(
&
api
.
Pod
{},
buffer
)
if
err
==
nil
{
t
.
Fatalf
(
"expected that template to crash"
)
}
if
buffer
.
String
()
==
""
{
t
.
Errorf
(
"no debugging info was printed"
)
}
}
func
TestPrinters
(
t
*
testing
.
T
)
{
func
TestPrinters
(
t
*
testing
.
T
)
{
om
:=
func
(
name
string
)
api
.
ObjectMeta
{
return
api
.
ObjectMeta
{
Name
:
name
}
}
om
:=
func
(
name
string
)
api
.
ObjectMeta
{
return
api
.
ObjectMeta
{
Name
:
name
}
}
templatePrinter
,
err
:=
NewTemplatePrinter
([]
byte
(
"{{.name}}"
),
testapi
.
Version
(),
api
.
Scheme
)
templatePrinter
,
err
:=
NewTemplatePrinter
([]
byte
(
"{{.name}}"
),
testapi
.
Version
(),
api
.
Scheme
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment