move type setting into an optional layer above normal printing
This commit is contained in:
		| @@ -31,12 +31,12 @@ import ( | ||||
| 	"k8s.io/apimachinery/pkg/types" | ||||
| 	"k8s.io/apimachinery/pkg/util/json" | ||||
|  | ||||
| 	"k8s.io/kubernetes/pkg/api/legacyscheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/cmd/templates" | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/scheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/util/i18n" | ||||
| 	"k8s.io/kubernetes/pkg/printers" | ||||
| ) | ||||
| @@ -111,7 +111,7 @@ var ( | ||||
|  | ||||
| func NewAnnotateOptions(ioStreams genericclioptions.IOStreams) *AnnotateOptions { | ||||
| 	return &AnnotateOptions{ | ||||
| 		PrintFlags: printers.NewPrintFlags("annotated", legacyscheme.Scheme), | ||||
| 		PrintFlags: printers.NewPrintFlags("annotated").WithTypeSetter(scheme.Scheme), | ||||
|  | ||||
| 		RecordFlags: genericclioptions.NewRecordFlags(), | ||||
| 		Recorder:    genericclioptions.NoopRecorder{}, | ||||
|   | ||||
| @@ -42,7 +42,6 @@ import ( | ||||
| 	"k8s.io/client-go/dynamic" | ||||
| 	scaleclient "k8s.io/client-go/scale" | ||||
| 	oapi "k8s.io/kube-openapi/pkg/util/proto" | ||||
| 	"k8s.io/kubernetes/pkg/api/legacyscheme" | ||||
| 	api "k8s.io/kubernetes/pkg/apis/core" | ||||
| 	"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl" | ||||
| @@ -132,7 +131,7 @@ func NewApplyOptions(ioStreams genericclioptions.IOStreams) *ApplyOptions { | ||||
| 	return &ApplyOptions{ | ||||
| 		RecordFlags: genericclioptions.NewRecordFlags(), | ||||
| 		DeleteFlags: NewDeleteFlags("that contains the configuration to apply"), | ||||
| 		PrintFlags:  printers.NewPrintFlags("created", legacyscheme.Scheme), | ||||
| 		PrintFlags:  printers.NewPrintFlags("created").WithTypeSetter(scheme.Scheme), | ||||
|  | ||||
| 		Overwrite:    true, | ||||
| 		OpenApiPatch: true, | ||||
|   | ||||
| @@ -26,13 +26,13 @@ import ( | ||||
| 	"k8s.io/apimachinery/pkg/api/meta" | ||||
| 	"k8s.io/apimachinery/pkg/runtime" | ||||
| 	"k8s.io/apimachinery/pkg/types" | ||||
| 	"k8s.io/kubernetes/pkg/api/legacyscheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/cmd/templates" | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/cmd/util/editor" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/scheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/util/i18n" | ||||
| 	"k8s.io/kubernetes/pkg/printers" | ||||
| ) | ||||
| @@ -83,7 +83,7 @@ var ( | ||||
|  | ||||
| func NewSetLastAppliedOptions(ioStreams genericclioptions.IOStreams) *SetLastAppliedOptions { | ||||
| 	return &SetLastAppliedOptions{ | ||||
| 		PrintFlags: printers.NewPrintFlags("configured", legacyscheme.Scheme), | ||||
| 		PrintFlags: printers.NewPrintFlags("configured").WithTypeSetter(scheme.Scheme), | ||||
| 		IOStreams:  ioStreams, | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -26,6 +26,7 @@ go_library( | ||||
|         "//pkg/kubectl/cmd/util:go_default_library", | ||||
|         "//pkg/kubectl/genericclioptions:go_default_library", | ||||
|         "//pkg/kubectl/genericclioptions/resource:go_default_library", | ||||
|         "//pkg/kubectl/scheme:go_default_library", | ||||
|         "//pkg/printers:go_default_library", | ||||
|         "//pkg/registry/rbac/reconciliation:go_default_library", | ||||
|         "//vendor/github.com/golang/glog:go_default_library", | ||||
|   | ||||
| @@ -30,6 +30,7 @@ import ( | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/scheme" | ||||
| 	"k8s.io/kubernetes/pkg/printers" | ||||
| 	"k8s.io/kubernetes/pkg/registry/rbac/reconciliation" | ||||
| ) | ||||
| @@ -63,7 +64,7 @@ var ( | ||||
| func NewReconcileOptions(ioStreams genericclioptions.IOStreams) *ReconcileOptions { | ||||
| 	return &ReconcileOptions{ | ||||
| 		FilenameOptions: &resource.FilenameOptions{}, | ||||
| 		PrintFlags:      printers.NewPrintFlags("reconciled", legacyscheme.Scheme), | ||||
| 		PrintFlags:      printers.NewPrintFlags("reconciled").WithTypeSetter(scheme.Scheme), | ||||
| 		IOStreams:       ioStreams, | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -32,6 +32,7 @@ import ( | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/scheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/util/i18n" | ||||
| 	"k8s.io/kubernetes/pkg/printers" | ||||
| ) | ||||
| @@ -82,7 +83,7 @@ type AutoscaleOptions struct { | ||||
|  | ||||
| func NewAutoscaleOptions(ioStreams genericclioptions.IOStreams) *AutoscaleOptions { | ||||
| 	return &AutoscaleOptions{ | ||||
| 		PrintFlags:      printers.NewPrintFlags("autoscaled", legacyscheme.Scheme), | ||||
| 		PrintFlags:      printers.NewPrintFlags("autoscaled").WithTypeSetter(scheme.Scheme), | ||||
| 		FilenameOptions: &resource.FilenameOptions{}, | ||||
| 		RecordFlags:     genericclioptions.NewRecordFlags(), | ||||
| 		Recorder:        genericclioptions.NoopRecorder{}, | ||||
|   | ||||
| @@ -30,6 +30,7 @@ import ( | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/scheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/util/i18n" | ||||
| 	"k8s.io/kubernetes/pkg/printers" | ||||
|  | ||||
| @@ -99,7 +100,7 @@ func (o *CertificateOptions) Validate() error { | ||||
|  | ||||
| func NewCmdCertificateApprove(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command { | ||||
| 	options := CertificateOptions{ | ||||
| 		PrintFlags: printers.NewPrintFlags("approved", legacyscheme.Scheme), | ||||
| 		PrintFlags: printers.NewPrintFlags("approved").WithTypeSetter(scheme.Scheme), | ||||
| 		IOStreams:  ioStreams, | ||||
| 	} | ||||
| 	cmd := &cobra.Command{ | ||||
| @@ -156,7 +157,7 @@ func (o *CertificateOptions) RunCertificateApprove(force bool) error { | ||||
|  | ||||
| func NewCmdCertificateDeny(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command { | ||||
| 	options := CertificateOptions{ | ||||
| 		PrintFlags: printers.NewPrintFlags("denied", legacyscheme.Scheme), | ||||
| 		PrintFlags: printers.NewPrintFlags("denied").WithTypeSetter(scheme.Scheme), | ||||
| 		IOStreams:  ioStreams, | ||||
| 	} | ||||
| 	cmd := &cobra.Command{ | ||||
|   | ||||
| @@ -26,13 +26,13 @@ import ( | ||||
| 	"github.com/spf13/cobra" | ||||
|  | ||||
| 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||
| 	"k8s.io/kubernetes/pkg/api/legacyscheme" | ||||
| 	api "k8s.io/kubernetes/pkg/apis/core" | ||||
| 	"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/cmd/templates" | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/polymorphichelpers" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/scheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/util/i18n" | ||||
| 	"k8s.io/kubernetes/pkg/printers" | ||||
| ) | ||||
| @@ -57,7 +57,7 @@ type ClusterInfoDumpOptions struct { | ||||
| // NewCmdCreateSecret groups subcommands to create various types of secrets | ||||
| func NewCmdClusterInfoDump(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command { | ||||
| 	o := &ClusterInfoDumpOptions{ | ||||
| 		PrintFlags: printers.NewPrintFlags("", legacyscheme.Scheme), | ||||
| 		PrintFlags: printers.NewPrintFlags("").WithTypeSetter(scheme.Scheme), | ||||
|  | ||||
| 		IOStreams: ioStreams, | ||||
| 	} | ||||
|   | ||||
| @@ -23,7 +23,6 @@ import ( | ||||
| 	"k8s.io/api/core/v1" | ||||
| 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||
| 	"k8s.io/apimachinery/pkg/runtime" | ||||
| 	"k8s.io/kubernetes/pkg/api/legacyscheme" | ||||
| 	api "k8s.io/kubernetes/pkg/apis/core" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/scheme" | ||||
| 	"k8s.io/kubernetes/pkg/printers" | ||||
| @@ -82,7 +81,7 @@ func TestIllegalPackageSourceCheckerThroughPrintFlags(t *testing.T) { | ||||
| 	} | ||||
|  | ||||
| 	for _, tc := range testCases { | ||||
| 		printFlags := printers.NewPrintFlags("succeeded", legacyscheme.Scheme) | ||||
| 		printFlags := printers.NewPrintFlags("succeeded").WithTypeSetter(scheme.Scheme) | ||||
| 		printFlags.OutputFormat = &tc.output | ||||
|  | ||||
| 		printer, err := printFlags.ToPrinter() | ||||
|   | ||||
| @@ -31,6 +31,8 @@ type kubectlConfigPrintFlags struct { | ||||
| 	NamePrintFlags     *printers.NamePrintFlags | ||||
| 	TemplateFlags      *printers.KubeTemplatePrintFlags | ||||
|  | ||||
| 	TypeSetter *printers.TypeSetterPrinter | ||||
|  | ||||
| 	OutputFormat *string | ||||
| } | ||||
|  | ||||
| @@ -45,15 +47,15 @@ func (f *kubectlConfigPrintFlags) ToPrinter() (printers.ResourcePrinter, error) | ||||
| 	} | ||||
|  | ||||
| 	if p, err := f.JSONYamlPrintFlags.ToPrinter(outputFormat); !printers.IsNoCompatiblePrinterError(err) { | ||||
| 		return p, err | ||||
| 		return f.TypeSetter.WrapToPrinter(p, err) | ||||
| 	} | ||||
|  | ||||
| 	if p, err := f.NamePrintFlags.ToPrinter(outputFormat); !printers.IsNoCompatiblePrinterError(err) { | ||||
| 		return p, err | ||||
| 		return f.TypeSetter.WrapToPrinter(p, err) | ||||
| 	} | ||||
|  | ||||
| 	if p, err := f.TemplateFlags.ToPrinter(outputFormat); !printers.IsNoCompatiblePrinterError(err) { | ||||
| 		return p, err | ||||
| 		return f.TypeSetter.WrapToPrinter(p, err) | ||||
| 	} | ||||
|  | ||||
| 	return nil, printers.NoCompatiblePrinterError{Options: f} | ||||
| @@ -75,14 +77,16 @@ func (f *kubectlConfigPrintFlags) WithDefaultOutput(output string) *kubectlConfi | ||||
| 	return f | ||||
| } | ||||
|  | ||||
| func newKubeConfigPrintFlags(scheme runtime.ObjectConvertor) *kubectlConfigPrintFlags { | ||||
| func newKubeConfigPrintFlags(scheme runtime.ObjectTyper) *kubectlConfigPrintFlags { | ||||
| 	outputFormat := "" | ||||
|  | ||||
| 	return &kubectlConfigPrintFlags{ | ||||
| 		OutputFormat: &outputFormat, | ||||
|  | ||||
| 		JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme), | ||||
| 		NamePrintFlags:     printers.NewNamePrintFlags("", scheme), | ||||
| 		JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(), | ||||
| 		NamePrintFlags:     printers.NewNamePrintFlags(""), | ||||
| 		TemplateFlags:      printers.NewKubeTemplatePrintFlags(), | ||||
|  | ||||
| 		TypeSetter: printers.NewTypeSetter(scheme), | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -103,7 +103,7 @@ type ConvertOptions struct { | ||||
|  | ||||
| func NewConvertOptions(ioStreams genericclioptions.IOStreams) *ConvertOptions { | ||||
| 	return &ConvertOptions{ | ||||
| 		PrintFlags: printers.NewPrintFlags("converted", scheme.Scheme).WithDefaultOutput("yaml"), | ||||
| 		PrintFlags: printers.NewPrintFlags("converted").WithTypeSetter(scheme.Scheme).WithDefaultOutput("yaml"), | ||||
| 		local:      true, | ||||
| 		IOStreams:  ioStreams, | ||||
| 	} | ||||
|   | ||||
| @@ -31,6 +31,8 @@ type PrintFlags struct { | ||||
| 	NamePrintFlags     *printers.NamePrintFlags | ||||
| 	TemplateFlags      *printers.KubeTemplatePrintFlags | ||||
|  | ||||
| 	TypeSetter *printers.TypeSetterPrinter | ||||
|  | ||||
| 	OutputFormat *string | ||||
| } | ||||
|  | ||||
| @@ -45,15 +47,15 @@ func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) { | ||||
| 	} | ||||
|  | ||||
| 	if p, err := f.JSONYamlPrintFlags.ToPrinter(outputFormat); !printers.IsNoCompatiblePrinterError(err) { | ||||
| 		return p, err | ||||
| 		return f.TypeSetter.WrapToPrinter(p, err) | ||||
| 	} | ||||
|  | ||||
| 	if p, err := f.NamePrintFlags.ToPrinter(outputFormat); !printers.IsNoCompatiblePrinterError(err) { | ||||
| 		return p, err | ||||
| 		return f.TypeSetter.WrapToPrinter(p, err) | ||||
| 	} | ||||
|  | ||||
| 	if p, err := f.TemplateFlags.ToPrinter(outputFormat); !printers.IsNoCompatiblePrinterError(err) { | ||||
| 		return p, err | ||||
| 		return f.TypeSetter.WrapToPrinter(p, err) | ||||
| 	} | ||||
|  | ||||
| 	return nil, printers.NoCompatiblePrinterError{Options: f} | ||||
| @@ -69,14 +71,16 @@ func (f *PrintFlags) AddFlags(cmd *cobra.Command) { | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func NewPrintFlags(operation string, scheme runtime.ObjectConvertor) *PrintFlags { | ||||
| func NewPrintFlags(operation string, scheme runtime.ObjectTyper) *PrintFlags { | ||||
| 	outputFormat := "" | ||||
|  | ||||
| 	return &PrintFlags{ | ||||
| 		OutputFormat: &outputFormat, | ||||
|  | ||||
| 		JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme), | ||||
| 		NamePrintFlags:     printers.NewNamePrintFlags(operation, scheme), | ||||
| 		JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(), | ||||
| 		NamePrintFlags:     printers.NewNamePrintFlags(operation), | ||||
| 		TemplateFlags:      printers.NewKubeTemplatePrintFlags(), | ||||
|  | ||||
| 		TypeSetter: printers.NewTypeSetter(scheme), | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -41,6 +41,7 @@ import ( | ||||
| 	"k8s.io/client-go/kubernetes" | ||||
| 	restclient "k8s.io/client-go/rest" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/scheme" | ||||
|  | ||||
| 	"k8s.io/apimachinery/pkg/runtime/schema" | ||||
| 	"k8s.io/kubernetes/pkg/api/legacyscheme" | ||||
| @@ -107,7 +108,7 @@ var ( | ||||
|  | ||||
| func NewCmdCordon(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command { | ||||
| 	options := &DrainOptions{ | ||||
| 		PrintFlags: printers.NewPrintFlags("cordoned", legacyscheme.Scheme), | ||||
| 		PrintFlags: printers.NewPrintFlags("cordoned").WithTypeSetter(scheme.Scheme), | ||||
|  | ||||
| 		IOStreams: ioStreams, | ||||
| 	} | ||||
| @@ -139,7 +140,7 @@ var ( | ||||
|  | ||||
| func NewCmdUncordon(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command { | ||||
| 	options := &DrainOptions{ | ||||
| 		PrintFlags: printers.NewPrintFlags("uncordoned", legacyscheme.Scheme), | ||||
| 		PrintFlags: printers.NewPrintFlags("uncordoned").WithTypeSetter(scheme.Scheme), | ||||
| 		IOStreams:  ioStreams, | ||||
| 	} | ||||
|  | ||||
| @@ -195,7 +196,7 @@ var ( | ||||
|  | ||||
| func NewDrainOptions(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *DrainOptions { | ||||
| 	return &DrainOptions{ | ||||
| 		PrintFlags: printers.NewPrintFlags("drained", legacyscheme.Scheme), | ||||
| 		PrintFlags: printers.NewPrintFlags("drained").WithTypeSetter(scheme.Scheme), | ||||
|  | ||||
| 		IOStreams:          ioStreams, | ||||
| 		backOff:            clockwork.NewRealClock(), | ||||
|   | ||||
| @@ -834,7 +834,7 @@ func TestDeletePods(t *testing.T) { | ||||
| 			defer tf.Cleanup() | ||||
|  | ||||
| 			o := DrainOptions{ | ||||
| 				PrintFlags: printers.NewPrintFlags("drained", legacyscheme.Scheme), | ||||
| 				PrintFlags: printers.NewPrintFlags("drained").WithTypeSetter(scheme.Scheme), | ||||
| 			} | ||||
| 			o.Out = os.Stdout | ||||
|  | ||||
|   | ||||
| @@ -36,6 +36,7 @@ import ( | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/scheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/util/i18n" | ||||
| 	"k8s.io/kubernetes/pkg/printers" | ||||
| ) | ||||
| @@ -110,7 +111,7 @@ type ExposeServiceOptions struct { | ||||
| func NewExposeServiceOptions(ioStreams genericclioptions.IOStreams) *ExposeServiceOptions { | ||||
| 	return &ExposeServiceOptions{ | ||||
| 		RecordFlags: genericclioptions.NewRecordFlags(), | ||||
| 		PrintFlags:  printers.NewPrintFlags("exposed", legacyscheme.Scheme), | ||||
| 		PrintFlags:  printers.NewPrintFlags("exposed").WithTypeSetter(scheme.Scheme), | ||||
|  | ||||
| 		Recorder:  genericclioptions.NoopRecorder{}, | ||||
| 		IOStreams: ioStreams, | ||||
|   | ||||
| @@ -161,8 +161,8 @@ func NewGetPrintFlags(scheme runtime.ObjectConvertor) *PrintFlags { | ||||
| 		OutputFormat: &outputFormat, | ||||
| 		NoHeaders:    &noHeaders, | ||||
|  | ||||
| 		JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme), | ||||
| 		NamePrintFlags:     printers.NewNamePrintFlags("", scheme), | ||||
| 		JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(), | ||||
| 		NamePrintFlags:     printers.NewNamePrintFlags(""), | ||||
| 		TemplateFlags:      printers.NewKubeTemplatePrintFlags(), | ||||
| 		HumanReadableFlags: NewHumanPrintFlags(), | ||||
| 		CustomColumnsFlags: printers.NewCustomColumnsPrintFlags(), | ||||
|   | ||||
| @@ -32,13 +32,13 @@ import ( | ||||
| 	utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||||
| 	"k8s.io/apimachinery/pkg/util/json" | ||||
| 	"k8s.io/apimachinery/pkg/util/validation" | ||||
| 	"k8s.io/kubernetes/pkg/api/legacyscheme" | ||||
|  | ||||
| 	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructuredscheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/cmd/templates" | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/scheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/util/i18n" | ||||
| 	"k8s.io/kubernetes/pkg/printers" | ||||
| ) | ||||
| @@ -115,7 +115,7 @@ func NewLabelOptions(ioStreams genericclioptions.IOStreams) *LabelOptions { | ||||
| 		RecordFlags: genericclioptions.NewRecordFlags(), | ||||
| 		Recorder:    genericclioptions.NoopRecorder{}, | ||||
|  | ||||
| 		PrintFlags: printers.NewPrintFlags("labeled", legacyscheme.Scheme), | ||||
| 		PrintFlags: printers.NewPrintFlags("labeled").WithTypeSetter(scheme.Scheme), | ||||
|  | ||||
| 		IOStreams: ioStreams, | ||||
| 	} | ||||
|   | ||||
| @@ -33,7 +33,6 @@ import ( | ||||
| 	"k8s.io/apimachinery/pkg/util/sets" | ||||
| 	"k8s.io/apimachinery/pkg/util/strategicpatch" | ||||
| 	"k8s.io/apimachinery/pkg/util/yaml" | ||||
| 	"k8s.io/kubernetes/pkg/api/legacyscheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/cmd/templates" | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions" | ||||
| @@ -99,7 +98,7 @@ func NewPatchOptions(ioStreams genericclioptions.IOStreams) *PatchOptions { | ||||
| 	return &PatchOptions{ | ||||
| 		RecordFlags: genericclioptions.NewRecordFlags(), | ||||
| 		Recorder:    genericclioptions.NoopRecorder{}, | ||||
| 		PrintFlags:  printers.NewPrintFlags("patched", legacyscheme.Scheme), | ||||
| 		PrintFlags:  printers.NewPrintFlags("patched").WithTypeSetter(scheme.Scheme), | ||||
| 		IOStreams:   ioStreams, | ||||
| 	} | ||||
| } | ||||
|   | ||||
| @@ -29,7 +29,6 @@ import ( | ||||
| 	"k8s.io/apimachinery/pkg/api/errors" | ||||
| 	"k8s.io/apimachinery/pkg/runtime" | ||||
| 	"k8s.io/apimachinery/pkg/util/wait" | ||||
| 	"k8s.io/kubernetes/pkg/api/legacyscheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/cmd/templates" | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| @@ -97,7 +96,7 @@ func NewReplaceOptions(streams genericclioptions.IOStreams) *ReplaceOptions { | ||||
| 		// we only support "-o name" for this command, so only register the name printer | ||||
| 		PrintFlags: &printers.PrintFlags{ | ||||
| 			OutputFormat:   &outputFormat, | ||||
| 			NamePrintFlags: printers.NewNamePrintFlags("replaced", legacyscheme.Scheme), | ||||
| 			NamePrintFlags: printers.NewNamePrintFlags("replaced"), | ||||
| 		}, | ||||
| 		DeleteFlags: NewDeleteFlags("to use to replace the resource."), | ||||
|  | ||||
|   | ||||
| @@ -38,6 +38,7 @@ import ( | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/scheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/util" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/util/i18n" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/validation" | ||||
| @@ -114,7 +115,7 @@ type RollingUpdateOptions struct { | ||||
|  | ||||
| func NewRollingUpdateOptions(streams genericclioptions.IOStreams) *RollingUpdateOptions { | ||||
| 	return &RollingUpdateOptions{ | ||||
| 		PrintFlags:      printers.NewPrintFlags("rolling updated", legacyscheme.Scheme), | ||||
| 		PrintFlags:      printers.NewPrintFlags("rolling updated").WithTypeSetter(scheme.Scheme), | ||||
| 		FilenameOptions: &resource.FilenameOptions{}, | ||||
| 		DeploymentKey:   "deployment", | ||||
| 		Timeout:         timeout, | ||||
|   | ||||
| @@ -26,6 +26,7 @@ go_library( | ||||
|         "//pkg/kubectl/cmd/util:go_default_library", | ||||
|         "//pkg/kubectl/genericclioptions:go_default_library", | ||||
|         "//pkg/kubectl/genericclioptions/resource:go_default_library", | ||||
|         "//pkg/kubectl/scheme:go_default_library", | ||||
|         "//pkg/kubectl/util/i18n:go_default_library", | ||||
|         "//pkg/printers:go_default_library", | ||||
|         "//pkg/util/interrupt:go_default_library", | ||||
|   | ||||
| @@ -29,6 +29,7 @@ import ( | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/scheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/util/i18n" | ||||
| 	"k8s.io/kubernetes/pkg/printers" | ||||
| ) | ||||
| @@ -63,7 +64,7 @@ var ( | ||||
|  | ||||
| func NewCmdRolloutPause(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command { | ||||
| 	o := &PauseConfig{ | ||||
| 		PrintFlags: printers.NewPrintFlags("paused", legacyscheme.Scheme), | ||||
| 		PrintFlags: printers.NewPrintFlags("paused").WithTypeSetter(scheme.Scheme), | ||||
| 		IOStreams:  streams, | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -29,6 +29,7 @@ import ( | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/scheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/util/i18n" | ||||
| 	"k8s.io/kubernetes/pkg/printers" | ||||
| ) | ||||
| @@ -61,7 +62,7 @@ var ( | ||||
|  | ||||
| func NewCmdRolloutResume(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command { | ||||
| 	o := &ResumeConfig{ | ||||
| 		PrintFlags: printers.NewPrintFlags("resumed", legacyscheme.Scheme), | ||||
| 		PrintFlags: printers.NewPrintFlags("resumed").WithTypeSetter(scheme.Scheme), | ||||
| 		IOStreams:  streams, | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -27,6 +27,7 @@ import ( | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/cmd/templates" | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/scheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/util/i18n" | ||||
| 	"k8s.io/kubernetes/pkg/printers" | ||||
| ) | ||||
| @@ -64,7 +65,7 @@ var ( | ||||
|  | ||||
| func NewCmdRolloutUndo(f cmdutil.Factory, out io.Writer) *cobra.Command { | ||||
| 	o := &UndoOptions{ | ||||
| 		PrintFlags: printers.NewPrintFlags("", legacyscheme.Scheme), | ||||
| 		PrintFlags: printers.NewPrintFlags("").WithTypeSetter(scheme.Scheme), | ||||
| 		ToRevision: int64(0), | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -127,7 +127,7 @@ type RunOptions struct { | ||||
|  | ||||
| func NewRunOptions(streams genericclioptions.IOStreams) *RunOptions { | ||||
| 	return &RunOptions{ | ||||
| 		PrintFlags:  printers.NewPrintFlags("created", legacyscheme.Scheme), | ||||
| 		PrintFlags:  printers.NewPrintFlags("created").WithTypeSetter(scheme.Scheme), | ||||
| 		DeleteFlags: NewDeleteFlags("to use to replace the resource."), | ||||
| 		RecordFlags: genericclioptions.NewRecordFlags(), | ||||
|  | ||||
|   | ||||
| @@ -198,7 +198,7 @@ func TestRunArgsFollowDashRules(t *testing.T) { | ||||
| 			cmd.Flags().Set("image", "nginx") | ||||
| 			cmd.Flags().Set("generator", "run/v1") | ||||
|  | ||||
| 			printFlags := printers.NewPrintFlags("created", legacyscheme.Scheme) | ||||
| 			printFlags := printers.NewPrintFlags("created").WithTypeSetter(scheme.Scheme) | ||||
| 			printer, err := printFlags.ToPrinter() | ||||
| 			if err != nil { | ||||
| 				t.Errorf("unexpected error: %v", err) | ||||
| @@ -366,7 +366,7 @@ func TestGenerateService(t *testing.T) { | ||||
| 				}), | ||||
| 			} | ||||
|  | ||||
| 			printFlags := printers.NewPrintFlags("created", legacyscheme.Scheme) | ||||
| 			printFlags := printers.NewPrintFlags("created").WithTypeSetter(scheme.Scheme) | ||||
| 			printer, err := printFlags.ToPrinter() | ||||
| 			if err != nil { | ||||
| 				t.Errorf("unexpected error: %v", err) | ||||
|   | ||||
| @@ -26,7 +26,6 @@ import ( | ||||
| 	"k8s.io/apimachinery/pkg/api/meta" | ||||
| 	"k8s.io/apimachinery/pkg/runtime/schema" | ||||
| 	"k8s.io/apimachinery/pkg/types" | ||||
| 	"k8s.io/kubernetes/pkg/api/legacyscheme" | ||||
| 	"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset" | ||||
| 	batchclient "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/typed/batch/internalversion" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl" | ||||
| @@ -101,7 +100,7 @@ func NewScaleOptions(ioStreams genericclioptions.IOStreams) *ScaleOptions { | ||||
| 		// we only support "-o name" for this command, so only register the name printer | ||||
| 		PrintFlags: &printers.PrintFlags{ | ||||
| 			OutputFormat:   &outputFormat, | ||||
| 			NamePrintFlags: printers.NewNamePrintFlags("scaled", legacyscheme.Scheme), | ||||
| 			NamePrintFlags: printers.NewNamePrintFlags("scaled"), | ||||
| 		}, | ||||
| 		RecordFlags:     genericclioptions.NewRecordFlags(), | ||||
| 		CurrentReplicas: -1, | ||||
|   | ||||
| @@ -19,7 +19,6 @@ go_library( | ||||
|     importpath = "k8s.io/kubernetes/pkg/kubectl/cmd/set", | ||||
|     visibility = ["//build/visible_to:pkg_kubectl_cmd_set_CONSUMERS"], | ||||
|     deps = [ | ||||
|         "//pkg/api/legacyscheme:go_default_library", | ||||
|         "//pkg/apis/rbac:go_default_library", | ||||
|         "//pkg/kubectl:go_default_library", | ||||
|         "//pkg/kubectl/cmd/templates:go_default_library", | ||||
|   | ||||
| @@ -30,7 +30,6 @@ import ( | ||||
| 	"k8s.io/apimachinery/pkg/types" | ||||
| 	utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||||
| 	"k8s.io/client-go/kubernetes" | ||||
| 	"k8s.io/kubernetes/pkg/api/legacyscheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/cmd/templates" | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| 	envutil "k8s.io/kubernetes/pkg/kubectl/cmd/util/env" | ||||
| @@ -127,7 +126,7 @@ type EnvOptions struct { | ||||
| // pod templates are selected by default and allowing environment to be overwritten | ||||
| func NewEnvOptions(streams genericclioptions.IOStreams) *EnvOptions { | ||||
| 	return &EnvOptions{ | ||||
| 		PrintFlags: printers.NewPrintFlags("env updated", legacyscheme.Scheme), | ||||
| 		PrintFlags: printers.NewPrintFlags("env updated").WithTypeSetter(scheme.Scheme), | ||||
|  | ||||
| 		ContainerSelector: "*", | ||||
| 		Overwrite:         true, | ||||
|   | ||||
| @@ -63,11 +63,7 @@ func TestSetEnvLocal(t *testing.T) { | ||||
|  | ||||
| 	streams, _, buf, bufErr := genericclioptions.NewTestIOStreams() | ||||
| 	opts := NewEnvOptions(streams) | ||||
| 	opts.PrintFlags = &printers.PrintFlags{ | ||||
| 		JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme), | ||||
| 		NamePrintFlags:     printers.NewNamePrintFlags("", scheme.Scheme), | ||||
| 		OutputFormat:       &outputFormat, | ||||
| 	} | ||||
| 	opts.PrintFlags = printers.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme) | ||||
| 	opts.FilenameOptions = resource.FilenameOptions{ | ||||
| 		Filenames: []string{"../../../../test/e2e/testing-manifests/statefulset/cassandra/controller.yaml"}, | ||||
| 	} | ||||
| @@ -105,11 +101,7 @@ func TestSetMultiResourcesEnvLocal(t *testing.T) { | ||||
| 	outputFormat := "name" | ||||
| 	streams, _, buf, bufErr := genericclioptions.NewTestIOStreams() | ||||
| 	opts := NewEnvOptions(streams) | ||||
| 	opts.PrintFlags = &printers.PrintFlags{ | ||||
| 		JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme), | ||||
| 		NamePrintFlags:     printers.NewNamePrintFlags("", scheme.Scheme), | ||||
| 		OutputFormat:       &outputFormat, | ||||
| 	} | ||||
| 	opts.PrintFlags = printers.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme) | ||||
| 	opts.FilenameOptions = resource.FilenameOptions{ | ||||
| 		Filenames: []string{"../../../../test/fixtures/pkg/kubectl/cmd/set/multi-resource-yaml.yaml"}, | ||||
| 	} | ||||
| @@ -491,11 +483,7 @@ func TestSetEnvRemote(t *testing.T) { | ||||
| 			outputFormat := "yaml" | ||||
| 			streams := genericclioptions.NewTestIOStreamsDiscard() | ||||
| 			opts := NewEnvOptions(streams) | ||||
| 			opts.PrintFlags = &printers.PrintFlags{ | ||||
| 				JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme), | ||||
| 				NamePrintFlags:     printers.NewNamePrintFlags("", scheme.Scheme), | ||||
| 				OutputFormat:       &outputFormat, | ||||
| 			} | ||||
| 			opts.PrintFlags = printers.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme) | ||||
| 			opts.Local = false | ||||
| 			opts.IOStreams = streams | ||||
| 			err := opts.Complete(tf, NewCmdEnv(tf, streams), input.args) | ||||
|   | ||||
| @@ -26,7 +26,6 @@ import ( | ||||
| 	"k8s.io/apimachinery/pkg/runtime" | ||||
| 	"k8s.io/apimachinery/pkg/types" | ||||
| 	utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||||
| 	"k8s.io/kubernetes/pkg/api/legacyscheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/cmd/templates" | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions" | ||||
| @@ -88,7 +87,7 @@ var ( | ||||
|  | ||||
| func NewImageOptions(streams genericclioptions.IOStreams) *SetImageOptions { | ||||
| 	return &SetImageOptions{ | ||||
| 		PrintFlags:  printers.NewPrintFlags("image updated", legacyscheme.Scheme), | ||||
| 		PrintFlags:  printers.NewPrintFlags("image updated").WithTypeSetter(scheme.Scheme), | ||||
| 		RecordFlags: genericclioptions.NewRecordFlags(), | ||||
|  | ||||
| 		Recorder: genericclioptions.NoopRecorder{}, | ||||
|   | ||||
| @@ -70,12 +70,7 @@ func TestImageLocal(t *testing.T) { | ||||
| 	cmd.Flags().Set("local", "true") | ||||
|  | ||||
| 	opts := SetImageOptions{ | ||||
| 		PrintFlags: &printers.PrintFlags{ | ||||
| 			JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme), | ||||
| 			NamePrintFlags:     printers.NewNamePrintFlags("", scheme.Scheme), | ||||
|  | ||||
| 			OutputFormat: &outputFormat, | ||||
| 		}, | ||||
| 		PrintFlags: printers.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme), | ||||
| 		FilenameOptions: resource.FilenameOptions{ | ||||
| 			Filenames: []string{"../../../../test/e2e/testing-manifests/statefulset/cassandra/controller.yaml"}}, | ||||
| 		Local:     true, | ||||
| @@ -97,10 +92,7 @@ func TestImageLocal(t *testing.T) { | ||||
| } | ||||
|  | ||||
| func TestSetImageValidation(t *testing.T) { | ||||
| 	printFlags := &printers.PrintFlags{ | ||||
| 		JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme), | ||||
| 		NamePrintFlags:     printers.NewNamePrintFlags("", scheme.Scheme), | ||||
| 	} | ||||
| 	printFlags := printers.NewPrintFlags("").WithTypeSetter(scheme.Scheme) | ||||
|  | ||||
| 	testCases := []struct { | ||||
| 		name         string | ||||
| @@ -191,12 +183,7 @@ func TestSetMultiResourcesImageLocal(t *testing.T) { | ||||
| 	cmd.Flags().Set("local", "true") | ||||
|  | ||||
| 	opts := SetImageOptions{ | ||||
| 		PrintFlags: &printers.PrintFlags{ | ||||
| 			JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme), | ||||
| 			NamePrintFlags:     printers.NewNamePrintFlags("", scheme.Scheme), | ||||
|  | ||||
| 			OutputFormat: &outputFormat, | ||||
| 		}, | ||||
| 		PrintFlags: printers.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme), | ||||
| 		FilenameOptions: resource.FilenameOptions{ | ||||
| 			Filenames: []string{"../../../../test/fixtures/pkg/kubectl/cmd/set/multi-resource-yaml.yaml"}}, | ||||
| 		Local:     true, | ||||
| @@ -583,12 +570,7 @@ func TestSetImageRemote(t *testing.T) { | ||||
| 			cmd := NewCmdImage(tf, streams) | ||||
| 			cmd.Flags().Set("output", outputFormat) | ||||
| 			opts := SetImageOptions{ | ||||
| 				PrintFlags: &printers.PrintFlags{ | ||||
| 					JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme), | ||||
| 					NamePrintFlags:     printers.NewNamePrintFlags("", scheme.Scheme), | ||||
|  | ||||
| 					OutputFormat: &outputFormat, | ||||
| 				}, | ||||
| 				PrintFlags: printers.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme), | ||||
|  | ||||
| 				Local:     false, | ||||
| 				IOStreams: streams, | ||||
|   | ||||
| @@ -29,7 +29,6 @@ import ( | ||||
| 	"k8s.io/apimachinery/pkg/runtime" | ||||
| 	"k8s.io/apimachinery/pkg/types" | ||||
| 	utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||||
| 	"k8s.io/kubernetes/pkg/api/legacyscheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/cmd/templates" | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| @@ -95,7 +94,7 @@ type SetResourcesOptions struct { | ||||
| // pod templates are selected by default. | ||||
| func NewResourcesOptions(streams genericclioptions.IOStreams) *SetResourcesOptions { | ||||
| 	return &SetResourcesOptions{ | ||||
| 		PrintFlags:  printers.NewPrintFlags("resource requirements updated", legacyscheme.Scheme), | ||||
| 		PrintFlags:  printers.NewPrintFlags("resource requirements updated").WithTypeSetter(scheme.Scheme), | ||||
| 		RecordFlags: genericclioptions.NewRecordFlags(), | ||||
|  | ||||
| 		Recorder: genericclioptions.NoopRecorder{}, | ||||
|   | ||||
| @@ -70,12 +70,7 @@ func TestResourcesLocal(t *testing.T) { | ||||
| 	cmd.Flags().Set("local", "true") | ||||
|  | ||||
| 	opts := SetResourcesOptions{ | ||||
| 		PrintFlags: &printers.PrintFlags{ | ||||
| 			JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme), | ||||
| 			NamePrintFlags:     printers.NewNamePrintFlags("", scheme.Scheme), | ||||
|  | ||||
| 			OutputFormat: &outputFormat, | ||||
| 		}, | ||||
| 		PrintFlags: printers.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme), | ||||
| 		FilenameOptions: resource.FilenameOptions{ | ||||
| 			Filenames: []string{"../../../../test/e2e/testing-manifests/statefulset/cassandra/controller.yaml"}}, | ||||
| 		Local:             true, | ||||
| @@ -124,12 +119,7 @@ func TestSetMultiResourcesLimitsLocal(t *testing.T) { | ||||
| 	cmd.Flags().Set("local", "true") | ||||
|  | ||||
| 	opts := SetResourcesOptions{ | ||||
| 		PrintFlags: &printers.PrintFlags{ | ||||
| 			JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme), | ||||
| 			NamePrintFlags:     printers.NewNamePrintFlags("", scheme.Scheme), | ||||
|  | ||||
| 			OutputFormat: &outputFormat, | ||||
| 		}, | ||||
| 		PrintFlags: printers.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme), | ||||
| 		FilenameOptions: resource.FilenameOptions{ | ||||
| 			Filenames: []string{"../../../../test/fixtures/pkg/kubectl/cmd/set/multi-resource-yaml.yaml"}}, | ||||
| 		Local:             true, | ||||
| @@ -504,12 +494,7 @@ func TestSetResourcesRemote(t *testing.T) { | ||||
| 			cmd := NewCmdResources(tf, streams) | ||||
| 			cmd.Flags().Set("output", outputFormat) | ||||
| 			opts := SetResourcesOptions{ | ||||
| 				PrintFlags: &printers.PrintFlags{ | ||||
| 					JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme), | ||||
| 					NamePrintFlags:     printers.NewNamePrintFlags("", scheme.Scheme), | ||||
|  | ||||
| 					OutputFormat: &outputFormat, | ||||
| 				}, | ||||
| 				PrintFlags: printers.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme), | ||||
|  | ||||
| 				Limits:            "cpu=200m,memory=512Mi", | ||||
| 				ContainerSelector: "*", | ||||
|   | ||||
| @@ -29,7 +29,6 @@ import ( | ||||
| 	"k8s.io/apimachinery/pkg/runtime" | ||||
| 	"k8s.io/apimachinery/pkg/types" | ||||
| 	"k8s.io/apimachinery/pkg/util/validation" | ||||
| 	"k8s.io/kubernetes/pkg/api/legacyscheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/cmd/templates" | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions" | ||||
| @@ -80,7 +79,7 @@ var ( | ||||
|  | ||||
| func NewSelectorOptions(streams genericclioptions.IOStreams) *SetSelectorOptions { | ||||
| 	return &SetSelectorOptions{ | ||||
| 		PrintFlags:  printers.NewPrintFlags("selector updated", legacyscheme.Scheme), | ||||
| 		PrintFlags:  printers.NewPrintFlags("selector updated").WithTypeSetter(scheme.Scheme), | ||||
| 		RecordFlags: genericclioptions.NewRecordFlags(), | ||||
|  | ||||
| 		Recorder: genericclioptions.NoopRecorder{}, | ||||
|   | ||||
| @@ -29,7 +29,6 @@ import ( | ||||
| 	"k8s.io/apimachinery/pkg/runtime" | ||||
| 	"k8s.io/apimachinery/pkg/types" | ||||
| 	utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||||
| 	"k8s.io/kubernetes/pkg/api/legacyscheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/cmd/templates" | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions" | ||||
| @@ -80,7 +79,7 @@ type SetServiceAccountOptions struct { | ||||
|  | ||||
| func NewSetServiceAccountOptions(streams genericclioptions.IOStreams) *SetServiceAccountOptions { | ||||
| 	return &SetServiceAccountOptions{ | ||||
| 		PrintFlags:  printers.NewPrintFlags("serviceaccount updated", legacyscheme.Scheme), | ||||
| 		PrintFlags:  printers.NewPrintFlags("serviceaccount updated").WithTypeSetter(scheme.Scheme), | ||||
| 		RecordFlags: genericclioptions.NewRecordFlags(), | ||||
|  | ||||
| 		Recorder: genericclioptions.NoopRecorder{}, | ||||
|   | ||||
| @@ -90,12 +90,7 @@ func TestSetServiceAccountLocal(t *testing.T) { | ||||
| 			cmd.Flags().Set("local", "true") | ||||
| 			testapi.Default = testapi.Groups[input.apiGroup] | ||||
| 			saConfig := SetServiceAccountOptions{ | ||||
| 				PrintFlags: &printers.PrintFlags{ | ||||
| 					JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme), | ||||
| 					NamePrintFlags:     printers.NewNamePrintFlags("", scheme.Scheme), | ||||
|  | ||||
| 					OutputFormat: &outputFormat, | ||||
| 				}, | ||||
| 				PrintFlags: printers.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme), | ||||
| 				fileNameOptions: resource.FilenameOptions{ | ||||
| 					Filenames: []string{input.yaml}}, | ||||
| 				local:     true, | ||||
| @@ -133,12 +128,7 @@ func TestSetServiceAccountMultiLocal(t *testing.T) { | ||||
| 	cmd.Flags().Set("output", outputFormat) | ||||
| 	cmd.Flags().Set("local", "true") | ||||
| 	opts := SetServiceAccountOptions{ | ||||
| 		PrintFlags: &printers.PrintFlags{ | ||||
| 			JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme), | ||||
| 			NamePrintFlags:     printers.NewNamePrintFlags("", scheme.Scheme), | ||||
|  | ||||
| 			OutputFormat: &outputFormat, | ||||
| 		}, | ||||
| 		PrintFlags: printers.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme), | ||||
| 		fileNameOptions: resource.FilenameOptions{ | ||||
| 			Filenames: []string{"../../../../test/fixtures/pkg/kubectl/cmd/set/multi-resource-yaml.yaml"}}, | ||||
| 		local:     true, | ||||
| @@ -375,12 +365,7 @@ func TestSetServiceAccountRemote(t *testing.T) { | ||||
| 			cmd := NewCmdServiceAccount(tf, streams) | ||||
| 			cmd.Flags().Set("output", outputFormat) | ||||
| 			saConfig := SetServiceAccountOptions{ | ||||
| 				PrintFlags: &printers.PrintFlags{ | ||||
| 					JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme), | ||||
| 					NamePrintFlags:     printers.NewNamePrintFlags("", scheme.Scheme), | ||||
|  | ||||
| 					OutputFormat: &outputFormat, | ||||
| 				}, | ||||
| 				PrintFlags: printers.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme), | ||||
|  | ||||
| 				local:     false, | ||||
| 				IOStreams: streams, | ||||
| @@ -422,13 +407,8 @@ func TestServiceAccountValidation(t *testing.T) { | ||||
| 			cmd := NewCmdServiceAccount(tf, streams) | ||||
|  | ||||
| 			saConfig := &SetServiceAccountOptions{ | ||||
| 				PrintFlags: &printers.PrintFlags{ | ||||
| 					JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme), | ||||
| 					NamePrintFlags:     printers.NewNamePrintFlags("", scheme.Scheme), | ||||
|  | ||||
| 					OutputFormat: &outputFormat, | ||||
| 				}, | ||||
| 				IOStreams: streams, | ||||
| 				PrintFlags: printers.NewPrintFlags("").WithDefaultOutput(outputFormat).WithTypeSetter(scheme.Scheme), | ||||
| 				IOStreams:  streams, | ||||
| 			} | ||||
| 			err := saConfig.Complete(tf, cmd, input.args) | ||||
| 			assert.EqualError(t, err, input.errorString) | ||||
|   | ||||
| @@ -27,7 +27,6 @@ import ( | ||||
| 	"k8s.io/apimachinery/pkg/types" | ||||
| 	utilerrors "k8s.io/apimachinery/pkg/util/errors" | ||||
| 	"k8s.io/apimachinery/pkg/util/sets" | ||||
| 	"k8s.io/kubernetes/pkg/api/legacyscheme" | ||||
| 	"k8s.io/kubernetes/pkg/apis/rbac" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/cmd/templates" | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| @@ -83,7 +82,7 @@ type SubjectOptions struct { | ||||
|  | ||||
| func NewSubjectOptions(streams genericclioptions.IOStreams) *SubjectOptions { | ||||
| 	return &SubjectOptions{ | ||||
| 		PrintFlags: printers.NewPrintFlags("subjects updated", legacyscheme.Scheme), | ||||
| 		PrintFlags: printers.NewPrintFlags("subjects updated").WithTypeSetter(scheme.Scheme), | ||||
|  | ||||
| 		IOStreams: streams, | ||||
| 	} | ||||
|   | ||||
| @@ -35,6 +35,7 @@ import ( | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/scheme" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/util/i18n" | ||||
| 	"k8s.io/kubernetes/pkg/printers" | ||||
| 	taintutils "k8s.io/kubernetes/pkg/util/taints" | ||||
| @@ -86,7 +87,7 @@ var ( | ||||
|  | ||||
| func NewCmdTaint(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra.Command { | ||||
| 	options := &TaintOptions{ | ||||
| 		PrintFlags: printers.NewPrintFlags("tainted", legacyscheme.Scheme), | ||||
| 		PrintFlags: printers.NewPrintFlags("tainted").WithTypeSetter(scheme.Scheme), | ||||
| 		IOStreams:  streams, | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -15,7 +15,6 @@ go_library( | ||||
|         "//build/visible_to:pkg_kubectl_cmd_util_editor_CONSUMERS", | ||||
|     ], | ||||
|     deps = [ | ||||
|         "//pkg/api/legacyscheme:go_default_library", | ||||
|         "//pkg/apis/core:go_default_library", | ||||
|         "//pkg/kubectl:go_default_library", | ||||
|         "//pkg/kubectl/cmd/util:go_default_library", | ||||
|   | ||||
| @@ -42,7 +42,6 @@ import ( | ||||
| 	"k8s.io/apimachinery/pkg/util/strategicpatch" | ||||
| 	"k8s.io/apimachinery/pkg/util/validation/field" | ||||
| 	"k8s.io/apimachinery/pkg/util/yaml" | ||||
| 	"k8s.io/kubernetes/pkg/api/legacyscheme" | ||||
| 	api "k8s.io/kubernetes/pkg/apis/core" | ||||
| 	"k8s.io/kubernetes/pkg/kubectl" | ||||
| 	cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" | ||||
| @@ -89,7 +88,7 @@ func NewEditOptions(editMode EditMode, ioStreams genericclioptions.IOStreams) *E | ||||
|  | ||||
| 		EditMode: editMode, | ||||
|  | ||||
| 		PrintFlags: printers.NewPrintFlags("edited", legacyscheme.Scheme), | ||||
| 		PrintFlags: printers.NewPrintFlags("edited").WithTypeSetter(scheme.Scheme), | ||||
|  | ||||
| 		WindowsLineEndings: goruntime.GOOS == "windows", | ||||
|  | ||||
|   | ||||
| @@ -24,6 +24,7 @@ go_library( | ||||
|         "tabwriter.go", | ||||
|         "template.go", | ||||
|         "template_flags.go", | ||||
|         "typesetter.go", | ||||
|     ], | ||||
|     importpath = "k8s.io/kubernetes/pkg/printers", | ||||
|     deps = [ | ||||
|   | ||||
| @@ -21,7 +21,6 @@ import ( | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/spf13/cobra" | ||||
|  | ||||
| 	"k8s.io/apimachinery/pkg/runtime" | ||||
| ) | ||||
|  | ||||
| @@ -75,6 +74,8 @@ type PrintFlags struct { | ||||
| 	JSONYamlPrintFlags *JSONYamlPrintFlags | ||||
| 	NamePrintFlags     *NamePrintFlags | ||||
|  | ||||
| 	TypeSetterPrinter *TypeSetterPrinter | ||||
|  | ||||
| 	OutputFormat *string | ||||
| } | ||||
|  | ||||
| @@ -90,13 +91,13 @@ func (f *PrintFlags) ToPrinter() (ResourcePrinter, error) { | ||||
|  | ||||
| 	if f.JSONYamlPrintFlags != nil { | ||||
| 		if p, err := f.JSONYamlPrintFlags.ToPrinter(outputFormat); !IsNoCompatiblePrinterError(err) { | ||||
| 			return p, err | ||||
| 			return f.TypeSetterPrinter.WrapToPrinter(p, err) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if f.NamePrintFlags != nil { | ||||
| 		if p, err := f.NamePrintFlags.ToPrinter(outputFormat); !IsNoCompatiblePrinterError(err) { | ||||
| 			return p, err | ||||
| 			return f.TypeSetterPrinter.WrapToPrinter(p, err) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| @@ -118,14 +119,20 @@ func (f *PrintFlags) WithDefaultOutput(output string) *PrintFlags { | ||||
| 	return f | ||||
| } | ||||
|  | ||||
| func NewPrintFlags(operation string, scheme runtime.ObjectConvertor) *PrintFlags { | ||||
| // WithTypeSetter sets a wrapper than will surround the returned printer with a printer to type resources | ||||
| func (f *PrintFlags) WithTypeSetter(scheme *runtime.Scheme) *PrintFlags { | ||||
| 	f.TypeSetterPrinter = NewTypeSetter(scheme) | ||||
| 	return f | ||||
| } | ||||
|  | ||||
| func NewPrintFlags(operation string) *PrintFlags { | ||||
| 	outputFormat := "" | ||||
|  | ||||
| 	return &PrintFlags{ | ||||
| 		OutputFormat: &outputFormat, | ||||
|  | ||||
| 		JSONYamlPrintFlags: NewJSONYamlPrintFlags(scheme), | ||||
| 		NamePrintFlags:     NewNamePrintFlags(operation, scheme), | ||||
| 		JSONYamlPrintFlags: NewJSONYamlPrintFlags(), | ||||
| 		NamePrintFlags:     NewNamePrintFlags(operation), | ||||
| 	} | ||||
| } | ||||
|  | ||||
|   | ||||
| @@ -59,6 +59,7 @@ func init() { | ||||
| } | ||||
|  | ||||
| var testData = TestStruct{ | ||||
| 	TypeMeta:   metav1.TypeMeta{APIVersion: "foo/bar", Kind: "TestStruct"}, | ||||
| 	Key:        "testValue", | ||||
| 	Map:        map[string]int{"TestSubkey": 1}, | ||||
| 	StringList: []string{"a", "b", "c"}, | ||||
| @@ -232,6 +233,7 @@ func testPrinter(t *testing.T, printer printers.ResourcePrinter, unmarshalFunc f | ||||
| 	} | ||||
|  | ||||
| 	obj := &v1.Pod{ | ||||
| 		TypeMeta:   metav1.TypeMeta{APIVersion: "v1", Kind: "Pod"}, | ||||
| 		ObjectMeta: metav1.ObjectMeta{Name: "foo"}, | ||||
| 	} | ||||
| 	buf.Reset() | ||||
| @@ -253,11 +255,11 @@ func testPrinter(t *testing.T, printer printers.ResourcePrinter, unmarshalFunc f | ||||
| } | ||||
|  | ||||
| func TestYAMLPrinter(t *testing.T) { | ||||
| 	testPrinter(t, &printers.YAMLPrinter{}, yaml.Unmarshal) | ||||
| 	testPrinter(t, printers.NewTypeSetter(legacyscheme.Scheme).ToPrinter(&printers.YAMLPrinter{}), yaml.Unmarshal) | ||||
| } | ||||
|  | ||||
| func TestJSONPrinter(t *testing.T) { | ||||
| 	testPrinter(t, &printers.JSONPrinter{}, json.Unmarshal) | ||||
| 	testPrinter(t, printers.NewTypeSetter(legacyscheme.Scheme).ToPrinter(&printers.JSONPrinter{}), json.Unmarshal) | ||||
| } | ||||
|  | ||||
| func TestFormatResourceName(t *testing.T) { | ||||
| @@ -363,38 +365,27 @@ func TestNamePrinter(t *testing.T) { | ||||
| 			}, | ||||
| 			"pod/foo\n"}, | ||||
| 		"List": { | ||||
| 			&v1.List{ | ||||
| 				TypeMeta: metav1.TypeMeta{ | ||||
| 					Kind: "List", | ||||
| 			&unstructured.UnstructuredList{ | ||||
| 				Object: map[string]interface{}{ | ||||
| 					"kind":       "List", | ||||
| 					"apiVersion": "v1", | ||||
| 				}, | ||||
| 				Items: []runtime.RawExtension{ | ||||
| 				Items: []unstructured.Unstructured{ | ||||
| 					{ | ||||
| 						Object: &v1.Pod{ | ||||
| 							TypeMeta: metav1.TypeMeta{ | ||||
| 								Kind: "Pod", | ||||
| 							}, | ||||
| 							ObjectMeta: metav1.ObjectMeta{ | ||||
| 								Name: "foo", | ||||
| 							}, | ||||
| 						}, | ||||
| 					}, | ||||
| 					{ | ||||
| 						Object: &unstructured.Unstructured{ | ||||
| 							Object: map[string]interface{}{ | ||||
| 								"kind":       "Pod", | ||||
| 								"apiVersion": "v1", | ||||
| 								"metadata": map[string]interface{}{ | ||||
| 									"name": "bar", | ||||
| 								}, | ||||
| 						Object: map[string]interface{}{ | ||||
| 							"kind":       "Pod", | ||||
| 							"apiVersion": "v1", | ||||
| 							"metadata": map[string]interface{}{ | ||||
| 								"name": "bar", | ||||
| 							}, | ||||
| 						}, | ||||
| 					}, | ||||
| 				}, | ||||
| 			}, | ||||
| 			"pod/foo\npod/bar\n"}, | ||||
| 			"pod/bar\n"}, | ||||
| 	} | ||||
|  | ||||
| 	printFlags := printers.NewPrintFlags("", legacyscheme.Scheme).WithDefaultOutput("name") | ||||
| 	printFlags := printers.NewPrintFlags("").WithTypeSetter(legacyscheme.Scheme).WithDefaultOutput("name") | ||||
| 	printer, err := printFlags.ToPrinter() | ||||
| 	if err != nil { | ||||
| 		t.Fatalf("unexpected err: %v", err) | ||||
| @@ -557,14 +548,11 @@ func TestPrinters(t *testing.T) { | ||||
| 	} | ||||
|  | ||||
| 	genericPrinters := map[string]printers.ResourcePrinter{ | ||||
| 		"json":      &printers.JSONPrinter{}, | ||||
| 		"yaml":      &printers.YAMLPrinter{}, | ||||
| 		"json":      printers.NewTypeSetter(legacyscheme.Scheme).ToPrinter(&printers.JSONPrinter{}), | ||||
| 		"yaml":      printers.NewTypeSetter(legacyscheme.Scheme).ToPrinter(&printers.YAMLPrinter{}), | ||||
| 		"template":  templatePrinter, | ||||
| 		"template2": templatePrinter2, | ||||
| 		"jsonpath":  jsonpathPrinter, | ||||
| 		"name": &printers.NamePrinter{ | ||||
| 			Typer: legacyscheme.Scheme, | ||||
| 		}, | ||||
| 	} | ||||
| 	objects := map[string]runtime.Object{ | ||||
| 		"pod":             &v1.Pod{ObjectMeta: om("pod")}, | ||||
|   | ||||
| @@ -20,15 +20,12 @@ import ( | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/spf13/cobra" | ||||
|  | ||||
| 	"k8s.io/apimachinery/pkg/runtime" | ||||
| ) | ||||
|  | ||||
| // JSONYamlPrintFlags provides default flags necessary for json/yaml printing. | ||||
| // Given the following flag values, a printer can be requested that knows | ||||
| // how to handle printing based on these values. | ||||
| type JSONYamlPrintFlags struct { | ||||
| 	Scheme runtime.ObjectConvertor | ||||
| } | ||||
|  | ||||
| // ToPrinter receives an outputFormat and returns a printer capable of | ||||
| @@ -57,6 +54,6 @@ func (f *JSONYamlPrintFlags) AddFlags(c *cobra.Command) {} | ||||
|  | ||||
| // NewJSONYamlPrintFlags returns flags associated with | ||||
| // yaml or json printing, with default values set. | ||||
| func NewJSONYamlPrintFlags(scheme runtime.ObjectConvertor) *JSONYamlPrintFlags { | ||||
| 	return &JSONYamlPrintFlags{Scheme: scheme} | ||||
| func NewJSONYamlPrintFlags() *JSONYamlPrintFlags { | ||||
| 	return &JSONYamlPrintFlags{} | ||||
| } | ||||
|   | ||||
| @@ -23,12 +23,14 @@ import ( | ||||
|  | ||||
| 	"k8s.io/api/core/v1" | ||||
| 	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||||
| 	"k8s.io/kubernetes/pkg/api/legacyscheme" | ||||
| 	"k8s.io/kubernetes/pkg/printers" | ||||
| ) | ||||
|  | ||||
| func TestPrinterSupportsExpectedJSONYamlFormats(t *testing.T) { | ||||
| 	testObject := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}} | ||||
| 	testObject := &v1.Pod{ | ||||
| 		TypeMeta:   metav1.TypeMeta{APIVersion: "v1", Kind: "Pod"}, | ||||
| 		ObjectMeta: metav1.ObjectMeta{Name: "foo"}, | ||||
| 	} | ||||
|  | ||||
| 	testCases := []struct { | ||||
| 		name           string | ||||
| @@ -60,7 +62,7 @@ func TestPrinterSupportsExpectedJSONYamlFormats(t *testing.T) { | ||||
|  | ||||
| 	for _, tc := range testCases { | ||||
| 		t.Run(tc.name, func(t *testing.T) { | ||||
| 			printFlags := printers.JSONYamlPrintFlags{Scheme: legacyscheme.Scheme} | ||||
| 			printFlags := printers.JSONYamlPrintFlags{} | ||||
|  | ||||
| 			p, err := printFlags.ToPrinter(tc.outputFormat) | ||||
| 			if tc.expectNoMatch { | ||||
|   | ||||
| @@ -37,8 +37,6 @@ type NamePrinter struct { | ||||
| 	// took place on an object, to be included in the | ||||
| 	// finalized "successful" message. | ||||
| 	Operation string | ||||
|  | ||||
| 	Typer runtime.ObjectTyper | ||||
| } | ||||
|  | ||||
| // PrintObj is an implementation of ResourcePrinter.PrintObj which decodes the object | ||||
| @@ -52,6 +50,13 @@ func (p *NamePrinter) PrintObj(obj runtime.Object, w io.Writer) error { | ||||
| 	} | ||||
|  | ||||
| 	if meta.IsListType(obj) { | ||||
| 		// we allow unstructured lists for now because they always contain the GVK information.  We should chase down | ||||
| 		// callers and stop them from passing unflattened lists | ||||
| 		// TODO chase the caller that is setting this and remove it. | ||||
| 		if _, ok := obj.(*unstructured.UnstructuredList); !ok { | ||||
| 			return fmt.Errorf("list types are not supported by name printing: %T", obj) | ||||
| 		} | ||||
|  | ||||
| 		items, err := meta.ExtractList(obj) | ||||
| 		if err != nil { | ||||
| 			return err | ||||
| @@ -64,6 +69,10 @@ func (p *NamePrinter) PrintObj(obj runtime.Object, w io.Writer) error { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 	if obj.GetObjectKind().GroupVersionKind().Empty() { | ||||
| 		return fmt.Errorf("missing apiVersion or kind; try GetObjectKind().SetGroupVersionKind() if you know the type") | ||||
| 	} | ||||
|  | ||||
| 	name := "<unknown>" | ||||
| 	if acc, err := meta.Accessor(obj); err == nil { | ||||
| 		if n := acc.GetName(); len(n) > 0 { | ||||
| @@ -71,10 +80,10 @@ func (p *NamePrinter) PrintObj(obj runtime.Object, w io.Writer) error { | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return printObj(w, name, p.Operation, p.ShortOutput, GetObjectGroupKind(obj, p.Typer)) | ||||
| 	return printObj(w, name, p.Operation, p.ShortOutput, GetObjectGroupKind(obj)) | ||||
| } | ||||
|  | ||||
| func GetObjectGroupKind(obj runtime.Object, typer runtime.ObjectTyper) schema.GroupKind { | ||||
| func GetObjectGroupKind(obj runtime.Object) schema.GroupKind { | ||||
| 	if obj == nil { | ||||
| 		return schema.GroupKind{Kind: "<unknown>"} | ||||
| 	} | ||||
| @@ -83,15 +92,6 @@ func GetObjectGroupKind(obj runtime.Object, typer runtime.ObjectTyper) schema.Gr | ||||
| 		return groupVersionKind.GroupKind() | ||||
| 	} | ||||
|  | ||||
| 	if gvks, _, err := typer.ObjectKinds(obj); err == nil { | ||||
| 		for _, gvk := range gvks { | ||||
| 			if len(gvk.Kind) == 0 { | ||||
| 				continue | ||||
| 			} | ||||
| 			return gvk.GroupKind() | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	if uns, ok := obj.(*unstructured.Unstructured); ok { | ||||
| 		if len(uns.GroupVersionKind().Kind) > 0 { | ||||
| 			return uns.GroupVersionKind().GroupKind() | ||||
|   | ||||
| @@ -21,18 +21,12 @@ import ( | ||||
| 	"strings" | ||||
|  | ||||
| 	"github.com/spf13/cobra" | ||||
|  | ||||
| 	"k8s.io/apimachinery/pkg/runtime" | ||||
|  | ||||
| 	"k8s.io/kubernetes/pkg/kubectl/scheme" | ||||
| ) | ||||
|  | ||||
| // NamePrintFlags provides default flags necessary for printing | ||||
| // a resource's fully-qualified Kind.group/name, or a successful | ||||
| // message about that resource if an Operation is provided. | ||||
| type NamePrintFlags struct { | ||||
| 	Scheme runtime.ObjectConvertor | ||||
|  | ||||
| 	// Operation describes the name of the action that | ||||
| 	// took place on an object, to be included in the | ||||
| 	// finalized "successful" message. | ||||
| @@ -51,7 +45,6 @@ func (f *NamePrintFlags) Complete(successTemplate string) error { | ||||
| func (f *NamePrintFlags) ToPrinter(outputFormat string) (ResourcePrinter, error) { | ||||
| 	namePrinter := &NamePrinter{ | ||||
| 		Operation: f.Operation, | ||||
| 		Typer:     scheme.Scheme, | ||||
| 	} | ||||
|  | ||||
| 	outputFormat = strings.ToLower(outputFormat) | ||||
| @@ -72,7 +65,7 @@ func (f *NamePrintFlags) AddFlags(c *cobra.Command) {} | ||||
|  | ||||
| // NewNamePrintFlags returns flags associated with | ||||
| // --name printing, with default values set. | ||||
| func NewNamePrintFlags(operation string, scheme runtime.ObjectConvertor) *NamePrintFlags { | ||||
| func NewNamePrintFlags(operation string) *NamePrintFlags { | ||||
| 	return &NamePrintFlags{ | ||||
| 		Operation: operation, | ||||
| 	} | ||||
|   | ||||
| @@ -27,7 +27,10 @@ import ( | ||||
| ) | ||||
|  | ||||
| func TestNamePrinterSupportsExpectedFormats(t *testing.T) { | ||||
| 	testObject := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}} | ||||
| 	testObject := &v1.Pod{ | ||||
| 		TypeMeta:   metav1.TypeMeta{APIVersion: "v1", Kind: "Pod"}, | ||||
| 		ObjectMeta: metav1.ObjectMeta{Name: "foo"}, | ||||
| 	} | ||||
|  | ||||
| 	testCases := []struct { | ||||
| 		name           string | ||||
|   | ||||
							
								
								
									
										95
									
								
								pkg/printers/typesetter.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								pkg/printers/typesetter.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,95 @@ | ||||
| /* | ||||
| Copyright 2018 The Kubernetes Authors. | ||||
|  | ||||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||||
| you may not use this file except in compliance with the License. | ||||
| You may obtain a copy of the License at | ||||
|  | ||||
|     http://www.apache.org/licenses/LICENSE-2.0 | ||||
|  | ||||
| Unless required by applicable law or agreed to in writing, software | ||||
| distributed under the License is distributed on an "AS IS" BASIS, | ||||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||
| See the License for the specific language governing permissions and | ||||
| limitations under the License. | ||||
| */ | ||||
|  | ||||
| package printers | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"io" | ||||
|  | ||||
| 	"k8s.io/apimachinery/pkg/runtime" | ||||
| 	"k8s.io/apimachinery/pkg/runtime/schema" | ||||
| ) | ||||
|  | ||||
| // TypeSetterPrinter is an implementation of ResourcePrinter wraps another printer with types set on the objects | ||||
| type TypeSetterPrinter struct { | ||||
| 	Delegate ResourcePrinter | ||||
|  | ||||
| 	Typer runtime.ObjectTyper | ||||
| } | ||||
|  | ||||
| // NewTypeSetter constructs a wrapping printer with required params | ||||
| func NewTypeSetter(typer runtime.ObjectTyper) *TypeSetterPrinter { | ||||
| 	return &TypeSetterPrinter{Typer: typer} | ||||
| } | ||||
|  | ||||
| // PrintObj is an implementation of ResourcePrinter.PrintObj which sets type information on the obj for the duration | ||||
| // of printing.  It is NOT threadsafe. | ||||
| func (p *TypeSetterPrinter) PrintObj(obj runtime.Object, w io.Writer) error { | ||||
| 	if obj == nil { | ||||
| 		return p.Delegate.PrintObj(obj, w) | ||||
| 	} | ||||
| 	if !obj.GetObjectKind().GroupVersionKind().Empty() { | ||||
| 		return p.Delegate.PrintObj(obj, w) | ||||
| 	} | ||||
|  | ||||
| 	// we were empty coming in, make sure we're empty going out.  This makes the call thread-unsafe | ||||
| 	defer func() { | ||||
| 		obj.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{}) | ||||
| 	}() | ||||
|  | ||||
| 	gvks, _, err := p.Typer.ObjectKinds(obj) | ||||
| 	if err != nil { | ||||
| 		// printers wrapped by us expect to find the type information present | ||||
| 		return fmt.Errorf("missing apiVersion or kind and cannot assign it; %v", err) | ||||
| 	} | ||||
|  | ||||
| 	for _, gvk := range gvks { | ||||
| 		if len(gvk.Kind) == 0 { | ||||
| 			continue | ||||
| 		} | ||||
| 		if len(gvk.Version) == 0 || gvk.Version == runtime.APIVersionInternal { | ||||
| 			continue | ||||
| 		} | ||||
| 		obj.GetObjectKind().SetGroupVersionKind(gvk) | ||||
| 		break | ||||
| 	} | ||||
|  | ||||
| 	return p.Delegate.PrintObj(obj, w) | ||||
| } | ||||
|  | ||||
| // ToPrinter returns a printer (not threadsafe!) that has been wrapped | ||||
| func (p *TypeSetterPrinter) ToPrinter(delegate ResourcePrinter) ResourcePrinter { | ||||
| 	if p == nil { | ||||
| 		return delegate | ||||
| 	} | ||||
|  | ||||
| 	p.Delegate = delegate | ||||
| 	return p | ||||
| } | ||||
|  | ||||
| // WrapToPrinter wraps the common ToPrinter method | ||||
| func (p *TypeSetterPrinter) WrapToPrinter(delegate ResourcePrinter, err error) (ResourcePrinter, error) { | ||||
| 	if err != nil { | ||||
| 		return delegate, err | ||||
| 	} | ||||
| 	if p == nil { | ||||
| 		return delegate, nil | ||||
| 	} | ||||
|  | ||||
| 	p.Delegate = delegate | ||||
| 	return p, nil | ||||
| } | ||||
		Reference in New Issue
	
	Block a user
	 David Eads
					David Eads