mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 04:33:26 +00:00
Fix kubectl -template to be versioned
This commit is contained in:
parent
66d62229f6
commit
9bb0d33a3f
@ -28,6 +28,7 @@ import (
|
|||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/latest"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
@ -45,35 +46,26 @@ func GetPrinter(format, templateFile string, defaultPrinter ResourcePrinter) (Re
|
|||||||
case "yaml":
|
case "yaml":
|
||||||
printer = &YAMLPrinter{}
|
printer = &YAMLPrinter{}
|
||||||
case "template":
|
case "template":
|
||||||
var data []byte
|
|
||||||
if len(templateFile) == 0 {
|
if len(templateFile) == 0 {
|
||||||
return nil, false, fmt.Errorf("template format specified but no template given")
|
return nil, false, fmt.Errorf("template format specified but no template given")
|
||||||
}
|
}
|
||||||
tmpl, err := template.New("output").Parse(templateFile)
|
var err error
|
||||||
|
printer, err = NewTemplatePrinter([]byte(templateFile))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, fmt.Errorf("error parsing template %s, %v\n", string(data), err)
|
return nil, false, fmt.Errorf("error parsing template %s, %v\n", templateFile, err)
|
||||||
}
|
|
||||||
printer = &TemplatePrinter{
|
|
||||||
Template: tmpl,
|
|
||||||
}
|
}
|
||||||
case "templatefile":
|
case "templatefile":
|
||||||
var data []byte
|
if len(templateFile) == 0 {
|
||||||
if len(templateFile) > 0 {
|
|
||||||
var err error
|
|
||||||
data, err = ioutil.ReadFile(templateFile)
|
|
||||||
if err != nil {
|
|
||||||
return nil, false, fmt.Errorf("error reading template %s, %v\n", templateFile, err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return nil, false, fmt.Errorf("templatefile format specified but no template file given")
|
return nil, false, fmt.Errorf("templatefile format specified but no template file given")
|
||||||
}
|
}
|
||||||
tmpl, err := template.New("output").Parse(string(data))
|
data, err := ioutil.ReadFile(templateFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, false, fmt.Errorf("error reading template %s, %v\n", templateFile, err)
|
||||||
|
}
|
||||||
|
printer, err = NewTemplatePrinter(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, fmt.Errorf("error parsing template %s, %v\n", string(data), err)
|
return nil, false, fmt.Errorf("error parsing template %s, %v\n", string(data), err)
|
||||||
}
|
}
|
||||||
printer = &TemplatePrinter{
|
|
||||||
Template: tmpl,
|
|
||||||
}
|
|
||||||
case "":
|
case "":
|
||||||
printer = defaultPrinter
|
printer = defaultPrinter
|
||||||
versioned = false
|
versioned = false
|
||||||
@ -323,12 +315,29 @@ 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
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrintObj formats the obj with the Go Template.
|
// PrintObj formats the obj with the Go Template.
|
||||||
func (t *TemplatePrinter) PrintObj(obj runtime.Object, w io.Writer) error {
|
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
|
||||||
|
}
|
||||||
|
outObj := map[string]interface{}{}
|
||||||
|
err = json.Unmarshal(data, &outObj)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return t.template.Execute(w, outObj)
|
||||||
}
|
}
|
||||||
|
|
||||||
func tabbedString(f func(*tabwriter.Writer) error) (string, error) {
|
func tabbedString(f func(*tabwriter.Writer) error) (string, error) {
|
||||||
|
@ -86,14 +86,17 @@ func TestPrintYAML(t *testing.T) {
|
|||||||
|
|
||||||
func TestPrintTemplate(t *testing.T) {
|
func TestPrintTemplate(t *testing.T) {
|
||||||
buf := bytes.NewBuffer([]byte{})
|
buf := bytes.NewBuffer([]byte{})
|
||||||
printer, versioned, err := GetPrinter("template", "{{ .Name }}", nil)
|
printer, versioned, err := GetPrinter("template", "{{.id}}", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unexpected error: %#v", err)
|
t.Fatalf("unexpected error: %#v", err)
|
||||||
}
|
}
|
||||||
if !versioned {
|
if !versioned {
|
||||||
t.Errorf("printer should be versioned")
|
t.Errorf("printer should be versioned")
|
||||||
}
|
}
|
||||||
printer.PrintObj(&api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, buf)
|
err = printer.PrintObj(&api.Pod{ObjectMeta: api.ObjectMeta{Name: "foo"}}, buf)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error: %#v", err)
|
||||||
|
}
|
||||||
if buf.String() != "foo" {
|
if buf.String() != "foo" {
|
||||||
t.Errorf("unexpected output: %s", buf.String())
|
t.Errorf("unexpected output: %s", buf.String())
|
||||||
}
|
}
|
||||||
@ -204,3 +207,19 @@ func TestUnknownTypePrinting(t *testing.T) {
|
|||||||
t.Errorf("An error was expected from printing unknown type")
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user