Merge pull request #27943 from ivan4th/refactor-printer-arguments

Automatic merge from submit-queue

Refactor Printer arguments

<!--
Checklist for submitting a Pull Request

Please remove this comment block before submitting.

1. Please read our [contributor guidelines](https://github.com/kubernetes/kubernetes/blob/master/CONTRIBUTING.md).
2. See our [developer guide](https://github.com/kubernetes/kubernetes/blob/master/docs/devel/development.md).
3. If you want this PR to automatically close an issue when it is merged,
   add `fixes #<issue number>` or `fixes #<issue number>, fixes #<issue number>`
   to close multiple issues (see: https://github.com/blog/1506-closing-issues-via-pull-requests).
4. Follow the instructions for [labeling and writing a release note for this PR](https://github.com/kubernetes/kubernetes/blob/master/docs/devel/pull-requests.md#release-notes) in the block below.
-->
[![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/.github/PULL_REQUEST_TEMPLATE.md?pixel)]()
Fixes #21260
This commit is contained in:
k8s-merge-robot 2016-07-14 08:03:40 -07:00 committed by GitHub
commit 98d769cf3d
10 changed files with 106 additions and 48 deletions

View File

@ -225,7 +225,7 @@ func NewTestFactory() (*cmdutil.Factory, *testFactory, runtime.Codec, runtime.Ne
Describer: func(*meta.RESTMapping) (kubectl.Describer, error) {
return t.Describer, t.Err
},
Printer: func(mapping *meta.RESTMapping, noHeaders, withNamespace bool, wide bool, showAll bool, showLabels bool, absoluteTimestamps bool, columnLabels []string) (kubectl.ResourcePrinter, error) {
Printer: func(mapping *meta.RESTMapping, options kubectl.PrintOptions) (kubectl.ResourcePrinter, error) {
return t.Printer, t.Err
},
Validator: func(validate bool, cacheDir string) (validation.Schema, error) {
@ -295,7 +295,7 @@ func NewAPIFactory() (*cmdutil.Factory, *testFactory, runtime.Codec, runtime.Neg
Describer: func(*meta.RESTMapping) (kubectl.Describer, error) {
return t.Describer, t.Err
},
Printer: func(mapping *meta.RESTMapping, noHeaders, withNamespace bool, wide bool, showAll bool, showLabels bool, absoluteTimestamps bool, columnLabels []string) (kubectl.ResourcePrinter, error) {
Printer: func(mapping *meta.RESTMapping, options kubectl.PrintOptions) (kubectl.ResourcePrinter, error) {
return t.Printer, t.Err
},
Validator: func(validate bool, cacheDir string) (validation.Schema, error) {
@ -371,7 +371,10 @@ func stringBody(body string) io.ReadCloser {
func Example_printReplicationControllerWithNamespace() {
f, tf, _, ns := NewAPIFactory()
tf.Printer = kubectl.NewHumanReadablePrinter(false, true, false, false, false, false, []string{})
tf.Printer = kubectl.NewHumanReadablePrinter(kubectl.PrintOptions{
WithNamespace: true,
ColumnLabels: []string{},
})
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: nil,
@ -417,7 +420,10 @@ func Example_printReplicationControllerWithNamespace() {
func Example_printMultiContainersReplicationControllerWithWide() {
f, tf, _, ns := NewAPIFactory()
tf.Printer = kubectl.NewHumanReadablePrinter(false, false, true, false, false, false, []string{})
tf.Printer = kubectl.NewHumanReadablePrinter(kubectl.PrintOptions{
Wide: true,
ColumnLabels: []string{},
})
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: nil,
@ -466,7 +472,9 @@ func Example_printMultiContainersReplicationControllerWithWide() {
func Example_printReplicationController() {
f, tf, _, ns := NewAPIFactory()
tf.Printer = kubectl.NewHumanReadablePrinter(false, false, false, false, false, false, []string{})
tf.Printer = kubectl.NewHumanReadablePrinter(kubectl.PrintOptions{
ColumnLabels: []string{},
})
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: nil,
@ -515,7 +523,10 @@ func Example_printReplicationController() {
func Example_printPodWithWideFormat() {
f, tf, _, ns := NewAPIFactory()
tf.Printer = kubectl.NewHumanReadablePrinter(false, false, true, false, false, false, []string{})
tf.Printer = kubectl.NewHumanReadablePrinter(kubectl.PrintOptions{
Wide: true,
ColumnLabels: []string{},
})
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: nil,
@ -552,7 +563,10 @@ func Example_printPodWithWideFormat() {
func Example_printPodWithShowLabels() {
f, tf, _, ns := NewAPIFactory()
tf.Printer = kubectl.NewHumanReadablePrinter(false, false, false, false, true, false, []string{})
tf.Printer = kubectl.NewHumanReadablePrinter(kubectl.PrintOptions{
ShowLabels: true,
ColumnLabels: []string{},
})
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: nil,
@ -684,7 +698,9 @@ func newAllPhasePodList() *api.PodList {
func Example_printPodHideTerminated() {
f, tf, _, ns := NewAPIFactory()
tf.Printer = kubectl.NewHumanReadablePrinter(false, false, false, false, false, false, []string{})
tf.Printer = kubectl.NewHumanReadablePrinter(kubectl.PrintOptions{
ColumnLabels: []string{},
})
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: nil,
@ -705,7 +721,10 @@ func Example_printPodHideTerminated() {
func Example_printPodShowAll() {
f, tf, _, ns := NewAPIFactory()
tf.Printer = kubectl.NewHumanReadablePrinter(false, false, false, true, false, false, []string{})
tf.Printer = kubectl.NewHumanReadablePrinter(kubectl.PrintOptions{
ShowAll: true,
ColumnLabels: []string{},
})
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: nil,
@ -728,7 +747,10 @@ func Example_printPodShowAll() {
func Example_printServiceWithNamespacesAndLabels() {
f, tf, _, ns := NewAPIFactory()
tf.Printer = kubectl.NewHumanReadablePrinter(false, true, false, false, false, false, []string{"l1"})
tf.Printer = kubectl.NewHumanReadablePrinter(kubectl.PrintOptions{
WithNamespace: true,
ColumnLabels: []string{"l1"},
})
tf.Client = &fake.RESTClient{
NegotiatedSerializer: ns,
Client: nil,

View File

@ -78,7 +78,9 @@ func NewCmdDelete(f *cmdutil.Factory, out io.Writer) *cobra.Command {
// retrieve a list of handled resources from printer as valid args
validArgs, argAliases := []string{}, []string{}
p, err := f.Printer(nil, false, false, false, false, false, false, []string{})
p, err := f.Printer(nil, kubectl.PrintOptions{
ColumnLabels: []string{},
})
cmdutil.CheckErr(err)
if p != nil {
validArgs = p.HandledResources()

View File

@ -93,7 +93,9 @@ func NewCmdEdit(f *cmdutil.Factory, out, errOut io.Writer) *cobra.Command {
// retrieve a list of handled resources from printer as valid args
validArgs, argAliases := []string{}, []string{}
p, err := f.Printer(nil, false, false, false, false, false, false, []string{})
p, err := f.Printer(nil, kubectl.PrintOptions{
ColumnLabels: []string{},
})
cmdutil.CheckErr(err)
if p != nil {
validArgs = p.HandledResources()

View File

@ -79,7 +79,9 @@ func NewCmdGet(f *cmdutil.Factory, out io.Writer) *cobra.Command {
// retrieve a list of handled resources from printer as valid args
validArgs, argAliases := []string{}, []string{}
p, err := f.Printer(nil, false, false, false, false, false, false, []string{})
p, err := f.Printer(nil, kubectl.PrintOptions{
ColumnLabels: []string{},
})
cmdutil.CheckErr(err)
if p != nil {
validArgs = p.HandledResources()

View File

@ -77,7 +77,9 @@ func NewCmdLabel(f *cmdutil.Factory, out io.Writer) *cobra.Command {
// retrieve a list of handled resources from printer as valid args
validArgs, argAliases := []string{}, []string{}
p, err := f.Printer(nil, false, false, false, false, false, false, []string{})
p, err := f.Printer(nil, kubectl.PrintOptions{
ColumnLabels: []string{},
})
cmdutil.CheckErr(err)
if p != nil {
validArgs = p.HandledResources()

View File

@ -74,7 +74,9 @@ func NewCmdPatch(f *cmdutil.Factory, out io.Writer) *cobra.Command {
// retrieve a list of handled resources from printer as valid args
validArgs, argAliases := []string{}, []string{}
p, err := f.Printer(nil, false, false, false, false, false, false, []string{})
p, err := f.Printer(nil, kubectl.PrintOptions{
ColumnLabels: []string{},
})
cmdutil.CheckErr(err)
if p != nil {
validArgs = p.HandledResources()

View File

@ -27,6 +27,7 @@ import (
"github.com/spf13/cobra"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/meta"
"k8s.io/kubernetes/pkg/kubectl"
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/runtime"
@ -72,7 +73,9 @@ func NewCmdTaint(f *cmdutil.Factory, out io.Writer) *cobra.Command {
// retrieve a list of handled resources from printer as valid args
validArgs := []string{}
p, err := f.Printer(nil, false, false, false, false, false, false, []string{})
p, err := f.Printer(nil, kubectl.PrintOptions{
ColumnLabels: []string{},
})
cmdutil.CheckErr(err)
if p != nil {
validArgs = p.HandledResources()

View File

@ -99,7 +99,7 @@ type Factory struct {
// Returns a Describer for displaying the specified RESTMapping type or an error.
Describer func(mapping *meta.RESTMapping) (kubectl.Describer, error)
// Returns a Printer for formatting objects of the given type or an error.
Printer func(mapping *meta.RESTMapping, noHeaders, withNamespace bool, wide bool, showAll bool, showLabels bool, absoluteTimestamps bool, columnLabels []string) (kubectl.ResourcePrinter, error)
Printer func(mapping *meta.RESTMapping, options kubectl.PrintOptions) (kubectl.ResourcePrinter, error)
// Returns a Scaler for changing the size of the specified RESTMapping type or an error
Scaler func(mapping *meta.RESTMapping) (kubectl.Scaler, error)
// Returns a Reaper for gracefully shutting down resources.
@ -417,8 +417,8 @@ func NewFactory(optionalClientConfig clientcmd.ClientConfig) *Factory {
JSONEncoder: func() runtime.Encoder {
return api.Codecs.LegacyCodec(registered.EnabledVersions()...)
},
Printer: func(mapping *meta.RESTMapping, noHeaders, withNamespace bool, wide bool, showAll bool, showLabels bool, absoluteTimestamps bool, columnLabels []string) (kubectl.ResourcePrinter, error) {
return kubectl.NewHumanReadablePrinter(noHeaders, withNamespace, wide, showAll, showLabels, absoluteTimestamps, columnLabels), nil
Printer: func(mapping *meta.RESTMapping, options kubectl.PrintOptions) (kubectl.ResourcePrinter, error) {
return kubectl.NewHumanReadablePrinter(options), nil
},
MapBasedSelectorForObject: func(object runtime.Object) (string, error) {
// TODO: replace with a swagger schema based approach (identify pod selector via schema introspection)
@ -1220,7 +1220,15 @@ func (f *Factory) PrinterForMapping(cmd *cobra.Command, mapping *meta.RESTMappin
if err != nil {
columnLabel = []string{}
}
printer, err = f.Printer(mapping, GetFlagBool(cmd, "no-headers"), withNamespace, GetWideFlag(cmd), GetFlagBool(cmd, "show-all"), GetFlagBool(cmd, "show-labels"), isWatch(cmd), columnLabel)
printer, err = f.Printer(mapping, kubectl.PrintOptions{
NoHeaders: GetFlagBool(cmd, "no-headers"),
WithNamespace: withNamespace,
Wide: GetWideFlag(cmd),
ShowAll: GetFlagBool(cmd, "show-all"),
ShowLabels: GetFlagBool(cmd, "show-labels"),
AbsoluteTimestamps: isWatch(cmd),
ColumnLabels: columnLabel,
})
if err != nil {
return nil, err
}

View File

@ -342,20 +342,10 @@ type HumanReadablePrinter struct {
}
// NewHumanReadablePrinter creates a HumanReadablePrinter.
func NewHumanReadablePrinter(noHeaders, withNamespace bool, wide bool, showAll bool, showLabels bool, absoluteTimestamps bool, columnLabels []string) *HumanReadablePrinter {
func NewHumanReadablePrinter(options PrintOptions) *HumanReadablePrinter {
printer := &HumanReadablePrinter{
handlerMap: make(map[reflect.Type]*handlerEntry),
Options: PrintOptions{
NoHeaders: noHeaders,
WithNamespace: withNamespace,
WithKind: false,
KindName: "",
Wide: wide,
ShowAll: showAll,
ShowLabels: showLabels,
AbsoluteTimestamps: absoluteTimestamps,
ColumnLabels: columnLabels,
},
Options: options,
}
printer.addDefaultHandlers()
return printer

View File

@ -231,7 +231,9 @@ func ErrorPrintHandler(obj *TestPrintType, w io.Writer, options PrintOptions) er
func TestCustomTypePrinting(t *testing.T) {
columns := []string{"Data"}
printer := NewHumanReadablePrinter(false, false, false, false, false, false, []string{})
printer := NewHumanReadablePrinter(PrintOptions{
ColumnLabels: []string{},
})
printer.Handler(columns, PrintCustomType)
obj := TestPrintType{"test object"}
@ -248,7 +250,9 @@ func TestCustomTypePrinting(t *testing.T) {
func TestCustomTypePrintingWithKind(t *testing.T) {
columns := []string{"Data"}
printer := NewHumanReadablePrinter(false, false, false, false, false, false, []string{})
printer := NewHumanReadablePrinter(PrintOptions{
ColumnLabels: []string{},
})
printer.Handler(columns, PrintCustomType)
printer.Options.WithKind = true
printer.Options.KindName = "test"
@ -267,7 +271,9 @@ func TestCustomTypePrintingWithKind(t *testing.T) {
func TestPrintHandlerError(t *testing.T) {
columns := []string{"Data"}
printer := NewHumanReadablePrinter(false, false, false, false, false, false, []string{})
printer := NewHumanReadablePrinter(PrintOptions{
ColumnLabels: []string{},
})
printer.Handler(columns, ErrorPrintHandler)
obj := TestPrintType{"test object"}
buffer := &bytes.Buffer{}
@ -278,7 +284,9 @@ func TestPrintHandlerError(t *testing.T) {
}
func TestUnknownTypePrinting(t *testing.T) {
printer := NewHumanReadablePrinter(false, false, false, false, false, false, []string{})
printer := NewHumanReadablePrinter(PrintOptions{
ColumnLabels: []string{},
})
buffer := &bytes.Buffer{}
err := printer.PrintObj(&TestUnknownType{}, buffer)
if err == nil {
@ -482,13 +490,18 @@ func TestPrinters(t *testing.T) {
t.Fatal(err)
}
printers := map[string]ResourcePrinter{
"humanReadable": NewHumanReadablePrinter(true, false, false, false, false, false, []string{}),
"humanReadableHeaders": NewHumanReadablePrinter(false, false, false, false, false, false, []string{}),
"json": &JSONPrinter{},
"yaml": &YAMLPrinter{},
"template": templatePrinter,
"template2": templatePrinter2,
"jsonpath": jsonpathPrinter,
"humanReadable": NewHumanReadablePrinter(PrintOptions{
NoHeaders: true,
ColumnLabels: []string{},
}),
"humanReadableHeaders": NewHumanReadablePrinter(PrintOptions{
ColumnLabels: []string{},
}),
"json": &JSONPrinter{},
"yaml": &YAMLPrinter{},
"template": templatePrinter,
"template2": templatePrinter2,
"jsonpath": jsonpathPrinter,
"name": &NamePrinter{
Typer: api.Scheme,
Decoder: api.Codecs.UniversalDecoder(),
@ -526,7 +539,9 @@ func TestPrinters(t *testing.T) {
func TestPrintEventsResultSorted(t *testing.T) {
// Arrange
printer := NewHumanReadablePrinter(false /* noHeaders */, false, false, false, false, false, []string{})
printer := NewHumanReadablePrinter(PrintOptions{
ColumnLabels: []string{},
})
obj := api.EventList{
Items: []api.Event{
@ -570,7 +585,9 @@ func TestPrintEventsResultSorted(t *testing.T) {
}
func TestPrintNodeStatus(t *testing.T) {
printer := NewHumanReadablePrinter(false, false, false, false, false, false, []string{})
printer := NewHumanReadablePrinter(PrintOptions{
ColumnLabels: []string{},
})
table := []struct {
node api.Node
status string
@ -694,7 +711,9 @@ func TestPrintHunmanReadableIngressWithColumnLabels(t *testing.T) {
},
}
buff := bytes.Buffer{}
printIngress(&ingress, &buff, PrintOptions{false, false, false, false, false, false, false, "", []string{"app_name"}})
printIngress(&ingress, &buff, PrintOptions{
ColumnLabels: []string{"app_name"},
})
output := string(buff.Bytes())
appName := ingress.ObjectMeta.Labels["app_name"]
if !strings.Contains(output, appName) {
@ -1001,7 +1020,10 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) {
for _, test := range table {
if test.isNamespaced {
// Expect output to include namespace when requested.
printer := NewHumanReadablePrinter(false, true, false, false, false, false, []string{})
printer := NewHumanReadablePrinter(PrintOptions{
WithNamespace: true,
ColumnLabels: []string{},
})
buffer := &bytes.Buffer{}
err := printer.PrintObj(test.obj, buffer)
if err != nil {
@ -1013,7 +1035,10 @@ func TestPrintHumanReadableWithNamespace(t *testing.T) {
}
} else {
// Expect error when trying to get all namespaces for un-namespaced object.
printer := NewHumanReadablePrinter(false, true, false, false, false, false, []string{})
printer := NewHumanReadablePrinter(PrintOptions{
WithNamespace: true,
ColumnLabels: []string{},
})
buffer := &bytes.Buffer{}
err := printer.PrintObj(test.obj, buffer)
if err == nil {