mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-14 22:33:34 +00:00
NamePrinter should not hardcode scheme
This commit is contained in:
parent
19ae89dcd8
commit
7cdb0eb89f
@ -131,7 +131,7 @@ func TestPrinter(t *testing.T) {
|
|||||||
{"test jsonpath", "jsonpath", "{.metadata.name}", podTest, []schema.GroupVersion{v1.SchemeGroupVersion}, "foo"},
|
{"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 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 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"},
|
{"emits versioned objects", "template", "{{.kind}}", testapi, []schema.GroupVersion{v1.SchemeGroupVersion}, "Pod"},
|
||||||
}
|
}
|
||||||
for _, test := range printerTests {
|
for _, test := range printerTests {
|
||||||
@ -342,7 +342,7 @@ func TestNamePrinter(t *testing.T) {
|
|||||||
Name: "foo",
|
Name: "foo",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"pod/foo\n"},
|
"pods/foo\n"},
|
||||||
"List": {
|
"List": {
|
||||||
&v1.List{
|
&v1.List{
|
||||||
TypeMeta: metav1.TypeMeta{
|
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})
|
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 {
|
for name, item := range tests {
|
||||||
|
@ -21,15 +21,15 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
|
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NamePrinter is an implementation of ResourcePrinter which outputs "resource/name" pair of an object.
|
// NamePrinter is an implementation of ResourcePrinter which outputs "resource/name" pair of an object.
|
||||||
type NamePrinter struct {
|
type NamePrinter struct {
|
||||||
Decoder runtime.Decoder
|
Decoders []runtime.Decoder
|
||||||
Typer runtime.ObjectTyper
|
Typer runtime.ObjectTyper
|
||||||
|
Mapper meta.RESTMapper
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *NamePrinter) AfterPrint(w io.Writer, res string) error {
|
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 {
|
if err != nil {
|
||||||
return err
|
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)
|
return utilerrors.NewAggregate(errs)
|
||||||
}
|
}
|
||||||
for _, obj := range items {
|
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 {
|
kind := obj.GetObjectKind().GroupVersionKind()
|
||||||
// this is the old code. It's unnecessary on decoded external objects, but on internal objects
|
if len(kind.Kind) == 0 {
|
||||||
// you may have to do it. Tests are definitely calling it with internals and I'm not sure who else
|
|
||||||
// is
|
|
||||||
if gvks, _, err := p.Typer.ObjectKinds(obj); err == nil {
|
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
|
for _, gvk := range gvks {
|
||||||
_, resource := meta.KindToResource(gvks[0])
|
if mappings, err := p.Mapper.RESTMappings(gvk.GroupKind(), gvk.Version); err == nil && len(mappings) > 0 {
|
||||||
fmt.Fprintf(w, "%s/%s\n", resource.Resource, name)
|
fmt.Fprintf(w, "%s/%s\n", mappings[0].Resource, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(w, "<unknown>/%s\n", name)
|
fmt.Fprintf(w, "<unknown>/%s\n", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// TODO: this is wrong, it assumes that meta knows about all Kinds - should take a RESTMapper
|
if mappings, err := p.Mapper.RESTMappings(kind.GroupKind(), kind.Version); err == nil && len(mappings) > 0 {
|
||||||
_, resource := meta.KindToResource(kind)
|
fmt.Fprintf(w, "%s/%s\n", mappings[0].Resource, name)
|
||||||
fmt.Fprintf(w, "%s/%s\n", resource.Resource, name)
|
} else {
|
||||||
|
fmt.Fprintf(w, "<unknown>/%s\n", name)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
Reference in New Issue
Block a user