Merge pull request #63312 from deads2k/cli-44-encoder

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

remove unnecessary encoder

Followups to https://github.com/kubernetes/kubernetes/pull/63105
This commit is contained in:
Kubernetes Submit Queue 2018-05-03 16:11:17 -07:00 committed by GitHub
commit b52ebfa28e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 25 deletions

View File

@ -66,7 +66,6 @@ func (f *HumanPrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrint
return nil, printers.NoCompatiblePrinterError{Options: f} return nil, printers.NoCompatiblePrinterError{Options: f}
} }
encoder := scheme.Codecs.LegacyCodec(scheme.Registry.RegisteredGroupVersions()...)
decoder := scheme.Codecs.UniversalDecoder() decoder := scheme.Codecs.UniversalDecoder()
showKind := false showKind := false
@ -84,7 +83,7 @@ func (f *HumanPrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrint
columnLabels = *f.ColumnLabels columnLabels = *f.ColumnLabels
} }
p := printers.NewHumanReadablePrinter(encoder, decoder, printers.PrintOptions{ p := printers.NewHumanReadablePrinter(decoder, printers.PrintOptions{
Kind: f.Kind, Kind: f.Kind,
WithKind: showKind, WithKind: showKind,
NoHeaders: f.NoHeaders, NoHeaders: f.NoHeaders,

View File

@ -296,7 +296,8 @@ func (o *RollingUpdateOptions) Run() error {
uncastVersionedObj, err := legacyscheme.Scheme.ConvertToVersion(infos[0].Object, v1.SchemeGroupVersion) uncastVersionedObj, err := legacyscheme.Scheme.ConvertToVersion(infos[0].Object, v1.SchemeGroupVersion)
if err != nil { if err != nil {
return err glog.V(4).Infof("Object %T is not a ReplicationController", infos[0].Object)
return fmt.Errorf("%s contains a %v not a ReplicationController", filename, infos[0].Object.GetObjectKind().GroupVersionKind())
} }
switch t := uncastVersionedObj.(type) { switch t := uncastVersionedObj.(type) {
case *v1.ReplicationController: case *v1.ReplicationController:

View File

@ -245,7 +245,7 @@ func SuggestApiResources(parent string) string {
func ValidArgList(f ClientAccessFactory) []string { func ValidArgList(f ClientAccessFactory) []string {
validArgs := []string{} validArgs := []string{}
humanReadablePrinter := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{}) humanReadablePrinter := printers.NewHumanReadablePrinter(nil, printers.PrintOptions{})
printersinternal.AddHandlers(humanReadablePrinter) printersinternal.AddHandlers(humanReadablePrinter)
validArgs = humanReadablePrinter.HandledResources() validArgs = humanReadablePrinter.HandledResources()

View File

@ -71,11 +71,10 @@ var _ PrintHandler = &HumanReadablePrinter{}
// NewHumanReadablePrinter creates a HumanReadablePrinter. // NewHumanReadablePrinter creates a HumanReadablePrinter.
// If encoder and decoder are provided, an attempt to convert unstructured types to internal types is made. // If encoder and decoder are provided, an attempt to convert unstructured types to internal types is made.
func NewHumanReadablePrinter(encoder runtime.Encoder, decoder runtime.Decoder, options PrintOptions) *HumanReadablePrinter { func NewHumanReadablePrinter(decoder runtime.Decoder, options PrintOptions) *HumanReadablePrinter {
printer := &HumanReadablePrinter{ printer := &HumanReadablePrinter{
handlerMap: make(map[reflect.Type]*handlerEntry), handlerMap: make(map[reflect.Type]*handlerEntry),
options: options, options: options,
encoder: encoder,
decoder: decoder, decoder: decoder,
} }
return printer return printer
@ -297,7 +296,7 @@ func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) er
// check if the object is unstructured. If so, let's attempt to convert it to a type we can understand before // check if the object is unstructured. If so, let's attempt to convert it to a type we can understand before
// trying to print, since the printers are keyed by type. This is extremely expensive. // trying to print, since the printers are keyed by type. This is extremely expensive.
if h.encoder != nil && h.decoder != nil { if h.decoder != nil {
obj, _ = decodeUnknownObject(obj, h.decoder) obj, _ = decodeUnknownObject(obj, h.decoder)
} }

View File

@ -192,7 +192,7 @@ func TestPrintUnstructuredObject(t *testing.T) {
for _, test := range tests { for _, test := range tests {
out.Reset() out.Reset()
printer := printers.NewHumanReadablePrinter(nil, nil, test.options).With(AddDefaultHandlers) printer := printers.NewHumanReadablePrinter(nil, test.options).With(AddDefaultHandlers)
printer.PrintObj(test.object, out) printer.PrintObj(test.object, out)
matches, err := regexp.MatchString(test.expected, out.String()) matches, err := regexp.MatchString(test.expected, out.String())
@ -388,7 +388,7 @@ func ErrorPrintHandler(obj *TestPrintType, w io.Writer, options printers.PrintOp
func TestCustomTypePrinting(t *testing.T) { func TestCustomTypePrinting(t *testing.T) {
columns := []string{"Data"} columns := []string{"Data"}
printer := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{}) printer := printers.NewHumanReadablePrinter(nil, printers.PrintOptions{})
printer.Handler(columns, nil, PrintCustomType) printer.Handler(columns, nil, PrintCustomType)
obj := TestPrintType{"test object"} obj := TestPrintType{"test object"}
@ -405,7 +405,7 @@ func TestCustomTypePrinting(t *testing.T) {
func TestPrintHandlerError(t *testing.T) { func TestPrintHandlerError(t *testing.T) {
columns := []string{"Data"} columns := []string{"Data"}
printer := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{}) printer := printers.NewHumanReadablePrinter(nil, printers.PrintOptions{})
printer.Handler(columns, nil, ErrorPrintHandler) printer.Handler(columns, nil, ErrorPrintHandler)
obj := TestPrintType{"test object"} obj := TestPrintType{"test object"}
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}
@ -416,7 +416,7 @@ func TestPrintHandlerError(t *testing.T) {
} }
func TestUnknownTypePrinting(t *testing.T) { func TestUnknownTypePrinting(t *testing.T) {
printer := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{}) printer := printers.NewHumanReadablePrinter(nil, printers.PrintOptions{})
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}
err := printer.PrintObj(&TestUnknownType{}, buffer) err := printer.PrintObj(&TestUnknownType{}, buffer)
if err == nil { if err == nil {
@ -635,10 +635,10 @@ func TestPrinters(t *testing.T) {
jsonpathPrinter = printers.NewVersionedPrinter(jsonpathPrinter, legacyscheme.Scheme, legacyscheme.Scheme, v1.SchemeGroupVersion) jsonpathPrinter = printers.NewVersionedPrinter(jsonpathPrinter, legacyscheme.Scheme, legacyscheme.Scheme, v1.SchemeGroupVersion)
allPrinters := map[string]printers.ResourcePrinter{ allPrinters := map[string]printers.ResourcePrinter{
"humanReadable": printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{ "humanReadable": printers.NewHumanReadablePrinter(nil, printers.PrintOptions{
NoHeaders: true, NoHeaders: true,
}), }),
"humanReadableHeaders": printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{}), "humanReadableHeaders": printers.NewHumanReadablePrinter(nil, printers.PrintOptions{}),
"json": &printers.JSONPrinter{}, "json": &printers.JSONPrinter{},
"yaml": &printers.YAMLPrinter{}, "yaml": &printers.YAMLPrinter{},
"template": templatePrinter, "template": templatePrinter,
@ -683,7 +683,7 @@ func TestPrinters(t *testing.T) {
func TestPrintEventsResultSorted(t *testing.T) { func TestPrintEventsResultSorted(t *testing.T) {
// Arrange // Arrange
printer := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{}) printer := printers.NewHumanReadablePrinter(nil, printers.PrintOptions{})
AddHandlers(printer) AddHandlers(printer)
obj := api.EventList{ obj := api.EventList{
@ -728,7 +728,7 @@ func TestPrintEventsResultSorted(t *testing.T) {
} }
func TestPrintNodeStatus(t *testing.T) { func TestPrintNodeStatus(t *testing.T) {
printer := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{}) printer := printers.NewHumanReadablePrinter(nil, printers.PrintOptions{})
AddHandlers(printer) AddHandlers(printer)
table := []struct { table := []struct {
node api.Node node api.Node
@ -818,7 +818,7 @@ func TestPrintNodeStatus(t *testing.T) {
} }
func TestPrintNodeRole(t *testing.T) { func TestPrintNodeRole(t *testing.T) {
printer := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{}) printer := printers.NewHumanReadablePrinter(nil, printers.PrintOptions{})
AddHandlers(printer) AddHandlers(printer)
table := []struct { table := []struct {
node api.Node node api.Node
@ -863,7 +863,7 @@ func TestPrintNodeRole(t *testing.T) {
} }
func TestPrintNodeOSImage(t *testing.T) { func TestPrintNodeOSImage(t *testing.T) {
printer := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{ printer := printers.NewHumanReadablePrinter(nil, printers.PrintOptions{
ColumnLabels: []string{}, ColumnLabels: []string{},
Wide: true, Wide: true,
}) })
@ -908,7 +908,7 @@ func TestPrintNodeOSImage(t *testing.T) {
} }
func TestPrintNodeKernelVersion(t *testing.T) { func TestPrintNodeKernelVersion(t *testing.T) {
printer := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{ printer := printers.NewHumanReadablePrinter(nil, printers.PrintOptions{
ColumnLabels: []string{}, ColumnLabels: []string{},
Wide: true, Wide: true,
}) })
@ -953,7 +953,7 @@ func TestPrintNodeKernelVersion(t *testing.T) {
} }
func TestPrintNodeContainerRuntimeVersion(t *testing.T) { func TestPrintNodeContainerRuntimeVersion(t *testing.T) {
printer := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{ printer := printers.NewHumanReadablePrinter(nil, printers.PrintOptions{
ColumnLabels: []string{}, ColumnLabels: []string{},
Wide: true, Wide: true,
}) })
@ -998,7 +998,7 @@ func TestPrintNodeContainerRuntimeVersion(t *testing.T) {
} }
func TestPrintNodeName(t *testing.T) { func TestPrintNodeName(t *testing.T) {
printer := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{ printer := printers.NewHumanReadablePrinter(nil, printers.PrintOptions{
Wide: true, Wide: true,
}) })
AddHandlers(printer) AddHandlers(printer)
@ -1035,7 +1035,7 @@ func TestPrintNodeName(t *testing.T) {
} }
func TestPrintNodeExternalIP(t *testing.T) { func TestPrintNodeExternalIP(t *testing.T) {
printer := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{ printer := printers.NewHumanReadablePrinter(nil, printers.PrintOptions{
Wide: true, Wide: true,
}) })
AddHandlers(printer) AddHandlers(printer)
@ -1083,7 +1083,7 @@ func TestPrintNodeExternalIP(t *testing.T) {
} }
func TestPrintNodeInternalIP(t *testing.T) { func TestPrintNodeInternalIP(t *testing.T) {
printer := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{ printer := printers.NewHumanReadablePrinter(nil, printers.PrintOptions{
Wide: true, Wide: true,
}) })
AddHandlers(printer) AddHandlers(printer)
@ -1485,7 +1485,7 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) {
for i, test := range table { for i, test := range table {
if test.isNamespaced { if test.isNamespaced {
// Expect output to include namespace when requested. // Expect output to include namespace when requested.
printer := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{ printer := printers.NewHumanReadablePrinter(nil, printers.PrintOptions{
WithNamespace: true, WithNamespace: true,
}) })
AddHandlers(printer) AddHandlers(printer)
@ -1500,7 +1500,7 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) {
} }
} else { } else {
// Expect error when trying to get all namespaces for un-namespaced object. // Expect error when trying to get all namespaces for un-namespaced object.
printer := printers.NewHumanReadablePrinter(nil, nil, printers.PrintOptions{ printer := printers.NewHumanReadablePrinter(nil, printers.PrintOptions{
WithNamespace: true, WithNamespace: true,
}) })
buffer := &bytes.Buffer{} buffer := &bytes.Buffer{}
@ -1578,7 +1578,7 @@ func TestPrintPodTable(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
p := printers.NewHumanReadablePrinter(nil, nil, test.opts).With(AddHandlers).AddTabWriter(false) p := printers.NewHumanReadablePrinter(nil, test.opts).With(AddHandlers).AddTabWriter(false)
if err := p.PrintObj(table, buf); err != nil { if err := p.PrintObj(table, buf); err != nil {
t.Fatal(err) t.Fatal(err)
} }