mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-31 08:36:16 +00:00
Fix kubecfg -template to be versioned
This commit is contained in:
@@ -26,7 +26,6 @@ import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||
@@ -324,14 +323,12 @@ func getPrinter() kubecfg.ResourcePrinter {
|
||||
} else {
|
||||
data = []byte(*templateStr)
|
||||
}
|
||||
tmpl, err := template.New("output").Parse(string(data))
|
||||
var err error
|
||||
printer, err = kubecfg.NewTemplatePrinter(data)
|
||||
if err != nil {
|
||||
glog.Fatalf("Error parsing template %s, %v\n", string(data), err)
|
||||
glog.Fatalf("Error '%v' parsing template:\n'%s'", err, string(data))
|
||||
return nil
|
||||
}
|
||||
printer = &kubecfg.TemplatePrinter{
|
||||
Template: tmpl,
|
||||
}
|
||||
default:
|
||||
printer = humanReadablePrinter()
|
||||
}
|
||||
|
@@ -319,19 +319,32 @@ func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) er
|
||||
|
||||
// TemplatePrinter is an implementation of ResourcePrinter which formats data with a Go Template.
|
||||
type TemplatePrinter struct {
|
||||
Template *template.Template
|
||||
template *template.Template
|
||||
}
|
||||
|
||||
func NewTemplatePrinter(tmpl []byte) (*TemplatePrinter, error) {
|
||||
t, err := template.New("output").Parse(string(tmpl))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &TemplatePrinter{t}, nil
|
||||
}
|
||||
|
||||
// Print parses the data as JSON, and re-formats it with the Go Template.
|
||||
func (t *TemplatePrinter) Print(data []byte, w io.Writer) error {
|
||||
obj, err := latest.Codec.Decode(data)
|
||||
obj := map[string]interface{}{}
|
||||
err := json.Unmarshal(data, &obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return t.PrintObj(obj, w)
|
||||
return t.template.Execute(w, obj)
|
||||
}
|
||||
|
||||
// PrintObj formats the obj with the Go Template.
|
||||
func (t *TemplatePrinter) PrintObj(obj runtime.Object, w io.Writer) error {
|
||||
return t.Template.Execute(w, obj)
|
||||
data, err := latest.Codec.Encode(obj)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return t.Print(data, w)
|
||||
}
|
||||
|
@@ -161,3 +161,19 @@ func TestUnknownTypePrinting(t *testing.T) {
|
||||
t.Errorf("An error was expected from printing unknown type")
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
buffer := &bytes.Buffer{}
|
||||
err = printer.PrintObj(&api.Pod{}, 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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user