NamePrinter should not hardcode scheme

pull/6/head
Clayton Coleman 2017-02-19 17:37:48 -05:00
parent 19ae89dcd8
commit 7cdb0eb89f
No known key found for this signature in database
GPG Key ID: 3D16906B4F1C5CB3
2 changed files with 19 additions and 17 deletions

View File

@ -131,7 +131,7 @@ func TestPrinter(t *testing.T) {
{"test jsonpath", "jsonpath", "{.metadata.name}", podTest, []schema.GroupVersion{v1.SchemeGroupVersion}, "foo"},
{"test jsonpath list", "jsonpath", "{.items[*].metadata.name}", podListTest, []schema.GroupVersion{v1.SchemeGroupVersion}, "foo bar"},
{"test jsonpath empty list", "jsonpath", "{.items[*].metadata.name}", emptyListTest, []schema.GroupVersion{v1.SchemeGroupVersion}, ""},
{"test name", "name", "", podTest, []schema.GroupVersion{v1.SchemeGroupVersion}, "pod/foo\n"},
{"test name", "name", "", podTest, []schema.GroupVersion{v1.SchemeGroupVersion}, "pods/foo\n"},
{"emits versioned objects", "template", "{{.kind}}", testapi, []schema.GroupVersion{v1.SchemeGroupVersion}, "Pod"},
}
for _, test := range printerTests {
@ -342,7 +342,7 @@ func TestNamePrinter(t *testing.T) {
Name: "foo",
},
},
"pod/foo\n"},
"pods/foo\n"},
"List": {
&v1.List{
TypeMeta: metav1.TypeMeta{
@ -357,7 +357,7 @@ func TestNamePrinter(t *testing.T) {
},
},
},
"pod/foo\npod/bar\n"},
"pods/foo\npods/bar\n"},
}
printer, _, _ := printers.GetStandardPrinter("name", "", false, false, api.Registry.RESTMapper(api.Registry.EnabledVersions()...), api.Scheme, []runtime.Decoder{api.Codecs.UniversalDecoder(), unstructured.UnstructuredJSONScheme})
for name, item := range tests {

View File

@ -21,15 +21,15 @@ import (
"io"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
)
// NamePrinter is an implementation of ResourcePrinter which outputs "resource/name" pair of an object.
type NamePrinter struct {
Decoder runtime.Decoder
Typer runtime.ObjectTyper
Decoders []runtime.Decoder
Typer runtime.ObjectTyper
Mapper meta.RESTMapper
}
func (p *NamePrinter) AfterPrint(w io.Writer, res string) error {
@ -44,7 +44,7 @@ func (p *NamePrinter) PrintObj(obj runtime.Object, w io.Writer) error {
if err != nil {
return err
}
if errs := runtime.DecodeList(items, p.Decoder, unstructured.UnstructuredJSONScheme); len(errs) > 0 {
if errs := runtime.DecodeList(items, p.Decoders...); len(errs) > 0 {
return utilerrors.NewAggregate(errs)
}
for _, obj := range items {
@ -62,22 +62,24 @@ func (p *NamePrinter) PrintObj(obj runtime.Object, w io.Writer) error {
}
}
if kind := obj.GetObjectKind().GroupVersionKind(); len(kind.Kind) == 0 {
// this is the old code. It's unnecessary on decoded external objects, but on internal objects
// you may have to do it. Tests are definitely calling it with internals and I'm not sure who else
// is
kind := obj.GetObjectKind().GroupVersionKind()
if len(kind.Kind) == 0 {
if gvks, _, err := p.Typer.ObjectKinds(obj); err == nil {
// TODO: this is wrong, it assumes that meta knows about all Kinds - should take a RESTMapper
_, resource := meta.KindToResource(gvks[0])
fmt.Fprintf(w, "%s/%s\n", resource.Resource, name)
for _, gvk := range gvks {
if mappings, err := p.Mapper.RESTMappings(gvk.GroupKind(), gvk.Version); err == nil && len(mappings) > 0 {
fmt.Fprintf(w, "%s/%s\n", mappings[0].Resource, name)
}
}
} else {
fmt.Fprintf(w, "<unknown>/%s\n", name)
}
} else {
// TODO: this is wrong, it assumes that meta knows about all Kinds - should take a RESTMapper
_, resource := meta.KindToResource(kind)
fmt.Fprintf(w, "%s/%s\n", resource.Resource, name)
if mappings, err := p.Mapper.RESTMappings(kind.GroupKind(), kind.Version); err == nil && len(mappings) > 0 {
fmt.Fprintf(w, "%s/%s\n", mappings[0].Resource, name)
} else {
fmt.Fprintf(w, "<unknown>/%s\n", name)
}
}
return nil