diff --git a/pkg/kubectl/cmd/cmd_test.go b/pkg/kubectl/cmd/cmd_test.go index e81a5170ff3..af5cdad1eee 100644 --- a/pkg/kubectl/cmd/cmd_test.go +++ b/pkg/kubectl/cmd/cmd_test.go @@ -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, diff --git a/pkg/kubectl/cmd/delete.go b/pkg/kubectl/cmd/delete.go index 0f64324987f..1aa1aaeeada 100644 --- a/pkg/kubectl/cmd/delete.go +++ b/pkg/kubectl/cmd/delete.go @@ -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() diff --git a/pkg/kubectl/cmd/edit.go b/pkg/kubectl/cmd/edit.go index fd871715d13..8c7f5a449a4 100644 --- a/pkg/kubectl/cmd/edit.go +++ b/pkg/kubectl/cmd/edit.go @@ -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() diff --git a/pkg/kubectl/cmd/get.go b/pkg/kubectl/cmd/get.go index d46c2e4587a..f438cc162e2 100644 --- a/pkg/kubectl/cmd/get.go +++ b/pkg/kubectl/cmd/get.go @@ -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() diff --git a/pkg/kubectl/cmd/label.go b/pkg/kubectl/cmd/label.go index e2144c5628b..763b2cc4111 100644 --- a/pkg/kubectl/cmd/label.go +++ b/pkg/kubectl/cmd/label.go @@ -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() diff --git a/pkg/kubectl/cmd/patch.go b/pkg/kubectl/cmd/patch.go index 038d19a3a5f..ec3aba2aad4 100644 --- a/pkg/kubectl/cmd/patch.go +++ b/pkg/kubectl/cmd/patch.go @@ -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() diff --git a/pkg/kubectl/cmd/taint.go b/pkg/kubectl/cmd/taint.go index 2cd125ae783..796bfb65c35 100644 --- a/pkg/kubectl/cmd/taint.go +++ b/pkg/kubectl/cmd/taint.go @@ -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() diff --git a/pkg/kubectl/cmd/util/factory.go b/pkg/kubectl/cmd/util/factory.go index 2c22149519b..b9a11758b36 100644 --- a/pkg/kubectl/cmd/util/factory.go +++ b/pkg/kubectl/cmd/util/factory.go @@ -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 } diff --git a/pkg/kubectl/resource_printer.go b/pkg/kubectl/resource_printer.go index 2e046f9e2ba..b1f17830dca 100644 --- a/pkg/kubectl/resource_printer.go +++ b/pkg/kubectl/resource_printer.go @@ -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 diff --git a/pkg/kubectl/resource_printer_test.go b/pkg/kubectl/resource_printer_test.go index bacaa1086bf..74a39d10545 100644 --- a/pkg/kubectl/resource_printer_test.go +++ b/pkg/kubectl/resource_printer_test.go @@ -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 {