From 52d45cfd4f4694ed2ef453b55a32349b307da82a Mon Sep 17 00:00:00 2001 From: David Eads Date: Mon, 2 Jul 2018 10:00:16 -0400 Subject: [PATCH 1/3] move template printers to genericclioptions --- pkg/kubectl/cmd/cmd_printing_test.go | 6 +++--- pkg/kubectl/cmd/config/flags.go | 4 ++-- pkg/kubectl/cmd/create/flags.go | 4 ++-- pkg/kubectl/cmd/get/get_flags.go | 4 ++-- .../genericclioptions}/jsonpath_flags.go | 12 ++++++------ .../genericclioptions}/jsonpath_flags_test.go | 9 ++++----- .../genericclioptions}/kube_template_flags.go | 8 ++++---- .../genericclioptions}/printers/jsonpath.go | 5 ++--- .../genericclioptions}/printers/template.go | 5 ++--- .../genericclioptions}/printers/template_test.go | 0 .../genericclioptions}/template_flags.go | 12 ++++++------ .../genericclioptions}/template_flags_test.go | 9 ++++----- pkg/printers/internalversion/printers_test.go | 10 +++++----- 13 files changed, 42 insertions(+), 46 deletions(-) rename pkg/{printers => kubectl/genericclioptions}/jsonpath_flags.go (89%) rename pkg/{printers => kubectl/genericclioptions}/jsonpath_flags_test.go (96%) rename pkg/{printers => kubectl/genericclioptions}/kube_template_flags.go (92%) rename pkg/{ => kubectl/genericclioptions}/printers/jsonpath.go (94%) rename pkg/{ => kubectl/genericclioptions}/printers/template.go (93%) rename pkg/{ => kubectl/genericclioptions}/printers/template_test.go (100%) rename pkg/{printers => kubectl/genericclioptions}/template_flags.go (91%) rename pkg/{printers => kubectl/genericclioptions}/template_flags_test.go (95%) diff --git a/pkg/kubectl/cmd/cmd_printing_test.go b/pkg/kubectl/cmd/cmd_printing_test.go index 23930e52f5b..49676d480db 100644 --- a/pkg/kubectl/cmd/cmd_printing_test.go +++ b/pkg/kubectl/cmd/cmd_printing_test.go @@ -122,12 +122,12 @@ func TestIllegalPackageSourceCheckerThroughPrintFlags(t *testing.T) { } func TestIllegalPackageSourceCheckerDirectlyThroughPrinters(t *testing.T) { - jsonPathPrinter, err := printers.NewJSONPathPrinter("{ .metadata.name }") + jsonPathPrinter, err := genericprinters.NewJSONPathPrinter("{ .metadata.name }") if err != nil { t.Fatalf("unexpected error: %v", err) } - goTemplatePrinter, err := printers.NewGoTemplatePrinter([]byte("{{ .metadata.name }}")) + goTemplatePrinter, err := genericprinters.NewGoTemplatePrinter([]byte("{{ .metadata.name }}")) if err != nil { t.Fatalf("unexpected error: %v", err) } @@ -140,7 +140,7 @@ func TestIllegalPackageSourceCheckerDirectlyThroughPrinters(t *testing.T) { testCases := []struct { name string expectInternalObjErr bool - printer printers.ResourcePrinter + printer genericprinters.ResourcePrinter obj runtime.Object expectedOutput string }{ diff --git a/pkg/kubectl/cmd/config/flags.go b/pkg/kubectl/cmd/config/flags.go index 5e2f3ed8067..6455bafbf5c 100644 --- a/pkg/kubectl/cmd/config/flags.go +++ b/pkg/kubectl/cmd/config/flags.go @@ -31,7 +31,7 @@ import ( type kubectlConfigPrintFlags struct { JSONYamlPrintFlags *genericclioptions.JSONYamlPrintFlags NamePrintFlags *genericclioptions.NamePrintFlags - TemplateFlags *printers.KubeTemplatePrintFlags + TemplateFlags *genericclioptions.KubeTemplatePrintFlags TypeSetter *genericprinters.TypeSetterPrinter @@ -94,7 +94,7 @@ func newKubeConfigPrintFlags(scheme runtime.ObjectTyper) *kubectlConfigPrintFlag JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(), NamePrintFlags: genericclioptions.NewNamePrintFlags(""), - TemplateFlags: printers.NewKubeTemplatePrintFlags(), + TemplateFlags: genericclioptions.NewKubeTemplatePrintFlags(), TypeSetter: genericprinters.NewTypeSetter(scheme), } diff --git a/pkg/kubectl/cmd/create/flags.go b/pkg/kubectl/cmd/create/flags.go index e1bf63cf465..6e902ea1222 100644 --- a/pkg/kubectl/cmd/create/flags.go +++ b/pkg/kubectl/cmd/create/flags.go @@ -31,7 +31,7 @@ import ( type PrintFlags struct { JSONYamlPrintFlags *genericclioptions.JSONYamlPrintFlags NamePrintFlags *genericclioptions.NamePrintFlags - TemplateFlags *printers.KubeTemplatePrintFlags + TemplateFlags *genericclioptions.KubeTemplatePrintFlags TypeSetter *genericprinters.TypeSetterPrinter @@ -86,7 +86,7 @@ func NewPrintFlags(operation string, scheme runtime.ObjectTyper) *PrintFlags { JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(), NamePrintFlags: genericclioptions.NewNamePrintFlags(operation), - TemplateFlags: printers.NewKubeTemplatePrintFlags(), + TemplateFlags: genericclioptions.NewKubeTemplatePrintFlags(), TypeSetter: genericprinters.NewTypeSetter(scheme), } diff --git a/pkg/kubectl/cmd/get/get_flags.go b/pkg/kubectl/cmd/get/get_flags.go index 6905f0ff4fb..bc05ac05c72 100644 --- a/pkg/kubectl/cmd/get/get_flags.go +++ b/pkg/kubectl/cmd/get/get_flags.go @@ -35,7 +35,7 @@ type PrintFlags struct { NamePrintFlags *genericclioptions.NamePrintFlags CustomColumnsFlags *printers.CustomColumnsPrintFlags HumanReadableFlags *HumanPrintFlags - TemplateFlags *printers.KubeTemplatePrintFlags + TemplateFlags *genericclioptions.KubeTemplatePrintFlags NoHeaders *bool OutputFormat *string @@ -182,7 +182,7 @@ func NewGetPrintFlags() *PrintFlags { JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(), NamePrintFlags: genericclioptions.NewNamePrintFlags(""), - TemplateFlags: printers.NewKubeTemplatePrintFlags(), + TemplateFlags: genericclioptions.NewKubeTemplatePrintFlags(), HumanReadableFlags: NewHumanPrintFlags(), CustomColumnsFlags: printers.NewCustomColumnsPrintFlags(), diff --git a/pkg/printers/jsonpath_flags.go b/pkg/kubectl/genericclioptions/jsonpath_flags.go similarity index 89% rename from pkg/printers/jsonpath_flags.go rename to pkg/kubectl/genericclioptions/jsonpath_flags.go index d95f737620f..c495fcd8b85 100644 --- a/pkg/printers/jsonpath_flags.go +++ b/pkg/kubectl/genericclioptions/jsonpath_flags.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package printers +package genericclioptions import ( "fmt" @@ -23,7 +23,7 @@ import ( "github.com/spf13/cobra" - "k8s.io/kubernetes/pkg/kubectl/genericclioptions" + "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers" ) // templates are logically optional for specifying a format. @@ -55,9 +55,9 @@ func (f *JSONPathPrintFlags) AllowedFormats() []string { // ToPrinter receives an templateFormat and returns a printer capable of // handling --template format printing. // Returns false if the specified templateFormat does not match a template format. -func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter, error) { +func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (printers.ResourcePrinter, error) { if (f.TemplateArgument == nil || len(*f.TemplateArgument) == 0) && len(templateFormat) == 0 { - return nil, genericclioptions.NoCompatiblePrinterError{Options: f, OutputFormat: &templateFormat} + return nil, NoCompatiblePrinterError{Options: f, OutputFormat: &templateFormat} } templateValue := "" @@ -76,7 +76,7 @@ func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter, } if _, supportedFormat := jsonFormats[templateFormat]; !supportedFormat { - return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &templateFormat, AllowedFormats: f.AllowedFormats()} + return nil, NoCompatiblePrinterError{OutputFormat: &templateFormat, AllowedFormats: f.AllowedFormats()} } if len(templateValue) == 0 { @@ -92,7 +92,7 @@ func (f *JSONPathPrintFlags) ToPrinter(templateFormat string) (ResourcePrinter, templateValue = string(data) } - p, err := NewJSONPathPrinter(templateValue) + p, err := printers.NewJSONPathPrinter(templateValue) if err != nil { return nil, fmt.Errorf("error parsing jsonpath %s, %v\n", templateValue, err) } diff --git a/pkg/printers/jsonpath_flags_test.go b/pkg/kubectl/genericclioptions/jsonpath_flags_test.go similarity index 96% rename from pkg/printers/jsonpath_flags_test.go rename to pkg/kubectl/genericclioptions/jsonpath_flags_test.go index 2f5b6c4c7c2..bf0c087bc1b 100644 --- a/pkg/printers/jsonpath_flags_test.go +++ b/pkg/kubectl/genericclioptions/jsonpath_flags_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package printers +package genericclioptions import ( "bytes" @@ -26,7 +26,6 @@ import ( "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/kubernetes/pkg/kubectl/genericclioptions" ) func TestPrinterSupportsExpectedJSONPathFormats(t *testing.T) { @@ -105,12 +104,12 @@ func TestPrinterSupportsExpectedJSONPathFormats(t *testing.T) { p, err := printFlags.ToPrinter(tc.outputFormat) if tc.expectNoMatch { - if !genericclioptions.IsNoCompatiblePrinterError(err) { + if !IsNoCompatiblePrinterError(err) { t.Fatalf("expected no printer matches for output format %q", tc.outputFormat) } return } - if genericclioptions.IsNoCompatiblePrinterError(err) { + if IsNoCompatiblePrinterError(err) { t.Fatalf("expected to match template printer for output format %q", tc.outputFormat) } @@ -184,7 +183,7 @@ func TestJSONPathPrinterDefaultsAllowMissingKeysToTrue(t *testing.T) { outputFormat := "jsonpath" p, err := printFlags.ToPrinter(outputFormat) - if genericclioptions.IsNoCompatiblePrinterError(err) { + if IsNoCompatiblePrinterError(err) { t.Fatalf("expected to match template printer for output format %q", outputFormat) } if err != nil { diff --git a/pkg/printers/kube_template_flags.go b/pkg/kubectl/genericclioptions/kube_template_flags.go similarity index 92% rename from pkg/printers/kube_template_flags.go rename to pkg/kubectl/genericclioptions/kube_template_flags.go index 8b53e60cdc5..345ca418d81 100644 --- a/pkg/printers/kube_template_flags.go +++ b/pkg/kubectl/genericclioptions/kube_template_flags.go @@ -14,12 +14,12 @@ See the License for the specific language governing permissions and limitations under the License. */ -package printers +package genericclioptions import ( "github.com/spf13/cobra" - "k8s.io/kubernetes/pkg/kubectl/genericclioptions" + "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers" ) // KubeTemplatePrintFlags composes print flags that provide both a JSONPath and a go-template printer. @@ -37,8 +37,8 @@ func (f *KubeTemplatePrintFlags) AllowedFormats() []string { return append(f.GoTemplatePrintFlags.AllowedFormats(), f.JSONPathPrintFlags.AllowedFormats()...) } -func (f *KubeTemplatePrintFlags) ToPrinter(outputFormat string) (ResourcePrinter, error) { - if p, err := f.JSONPathPrintFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) { +func (f *KubeTemplatePrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error) { + if p, err := f.JSONPathPrintFlags.ToPrinter(outputFormat); !IsNoCompatiblePrinterError(err) { return p, err } return f.GoTemplatePrintFlags.ToPrinter(outputFormat) diff --git a/pkg/printers/jsonpath.go b/pkg/kubectl/genericclioptions/printers/jsonpath.go similarity index 94% rename from pkg/printers/jsonpath.go rename to pkg/kubectl/genericclioptions/printers/jsonpath.go index 1e2e7b303c8..0bdb3511f1e 100644 --- a/pkg/printers/jsonpath.go +++ b/pkg/kubectl/genericclioptions/printers/jsonpath.go @@ -24,7 +24,6 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/util/jsonpath" - "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers" ) // exists returns true if it would be possible to call the index function @@ -118,8 +117,8 @@ func (j *JSONPathPrinter) PrintObj(obj runtime.Object, w io.Writer) error { // we use reflect.Indirect here in order to obtain the actual value from a pointer. // we need an actual value in order to retrieve the package path for an object. // using reflect.Indirect indiscriminately is valid here, as all runtime.Objects are supposed to be pointers. - if printers.InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj)).Type().PkgPath()) { - return fmt.Errorf(printers.InternalObjectPrinterErr) + if InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj)).Type().PkgPath()) { + return fmt.Errorf(InternalObjectPrinterErr) } var queryObj interface{} = obj diff --git a/pkg/printers/template.go b/pkg/kubectl/genericclioptions/printers/template.go similarity index 93% rename from pkg/printers/template.go rename to pkg/kubectl/genericclioptions/printers/template.go index 678b46e3ba4..5dd807dad99 100644 --- a/pkg/printers/template.go +++ b/pkg/kubectl/genericclioptions/printers/template.go @@ -25,7 +25,6 @@ import ( "text/template" "k8s.io/apimachinery/pkg/runtime" - "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers" ) // GoTemplatePrinter is an implementation of ResourcePrinter which formats data with a Go Template. @@ -61,8 +60,8 @@ func (p *GoTemplatePrinter) AllowMissingKeys(allow bool) { // PrintObj formats the obj with the Go Template. func (p *GoTemplatePrinter) PrintObj(obj runtime.Object, w io.Writer) error { - if printers.InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj)).Type().PkgPath()) { - return fmt.Errorf(printers.InternalObjectPrinterErr) + if InternalObjectPreventer.IsForbidden(reflect.Indirect(reflect.ValueOf(obj)).Type().PkgPath()) { + return fmt.Errorf(InternalObjectPrinterErr) } var data []byte diff --git a/pkg/printers/template_test.go b/pkg/kubectl/genericclioptions/printers/template_test.go similarity index 100% rename from pkg/printers/template_test.go rename to pkg/kubectl/genericclioptions/printers/template_test.go diff --git a/pkg/printers/template_flags.go b/pkg/kubectl/genericclioptions/template_flags.go similarity index 91% rename from pkg/printers/template_flags.go rename to pkg/kubectl/genericclioptions/template_flags.go index ffd7a24dca6..ac835a0643a 100644 --- a/pkg/printers/template_flags.go +++ b/pkg/kubectl/genericclioptions/template_flags.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package printers +package genericclioptions import ( "fmt" @@ -23,7 +23,7 @@ import ( "github.com/spf13/cobra" - "k8s.io/kubernetes/pkg/kubectl/genericclioptions" + "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers" ) // templates are logically optional for specifying a format. @@ -57,9 +57,9 @@ func (f *GoTemplatePrintFlags) AllowedFormats() []string { // ToPrinter receives an templateFormat and returns a printer capable of // handling --template format printing. // Returns false if the specified templateFormat does not match a template format. -func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (ResourcePrinter, error) { +func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (printers.ResourcePrinter, error) { if (f.TemplateArgument == nil || len(*f.TemplateArgument) == 0) && len(templateFormat) == 0 { - return nil, genericclioptions.NoCompatiblePrinterError{Options: f, OutputFormat: &templateFormat} + return nil, NoCompatiblePrinterError{Options: f, OutputFormat: &templateFormat} } templateValue := "" @@ -78,7 +78,7 @@ func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (ResourcePrinter } if _, supportedFormat := templateFormats[templateFormat]; !supportedFormat { - return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &templateFormat, AllowedFormats: f.AllowedFormats()} + return nil, NoCompatiblePrinterError{OutputFormat: &templateFormat, AllowedFormats: f.AllowedFormats()} } if len(templateValue) == 0 { @@ -94,7 +94,7 @@ func (f *GoTemplatePrintFlags) ToPrinter(templateFormat string) (ResourcePrinter templateValue = string(data) } - p, err := NewGoTemplatePrinter([]byte(templateValue)) + p, err := printers.NewGoTemplatePrinter([]byte(templateValue)) if err != nil { return nil, fmt.Errorf("error parsing template %s, %v\n", templateValue, err) } diff --git a/pkg/printers/template_flags_test.go b/pkg/kubectl/genericclioptions/template_flags_test.go similarity index 95% rename from pkg/printers/template_flags_test.go rename to pkg/kubectl/genericclioptions/template_flags_test.go index 3a2400ca879..e1f5ae60e5c 100644 --- a/pkg/printers/template_flags_test.go +++ b/pkg/kubectl/genericclioptions/template_flags_test.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package printers +package genericclioptions import ( "bytes" @@ -26,7 +26,6 @@ import ( "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/kubernetes/pkg/kubectl/genericclioptions" ) func TestPrinterSupportsExpectedTemplateFormats(t *testing.T) { @@ -105,12 +104,12 @@ func TestPrinterSupportsExpectedTemplateFormats(t *testing.T) { p, err := printFlags.ToPrinter(tc.outputFormat) if tc.expectNoMatch { - if !genericclioptions.IsNoCompatiblePrinterError(err) { + if !IsNoCompatiblePrinterError(err) { t.Fatalf("expected no printer matches for output format %q", tc.outputFormat) } return } - if genericclioptions.IsNoCompatiblePrinterError(err) { + if IsNoCompatiblePrinterError(err) { t.Fatalf("expected to match template printer for output format %q", tc.outputFormat) } @@ -178,7 +177,7 @@ func TestTemplatePrinterDefaultsAllowMissingKeysToTrue(t *testing.T) { outputFormat := "template" p, err := printFlags.ToPrinter(outputFormat) - if genericclioptions.IsNoCompatiblePrinterError(err) { + if IsNoCompatiblePrinterError(err) { t.Fatalf("expected to match template printer for output format %q", outputFormat) } if err != nil { diff --git a/pkg/printers/internalversion/printers_test.go b/pkg/printers/internalversion/printers_test.go index 82569ec1b21..318734d6c4e 100644 --- a/pkg/printers/internalversion/printers_test.go +++ b/pkg/printers/internalversion/printers_test.go @@ -338,7 +338,7 @@ func TestUnknownTypePrinting(t *testing.T) { func TestTemplatePanic(t *testing.T) { tmpl := `{{and ((index .currentState.info "foo").state.running.startedAt) .currentState.info.net.state.running.startedAt}}` - printer, err := printers.NewGoTemplatePrinter([]byte(tmpl)) + printer, err := genericprinters.NewGoTemplatePrinter([]byte(tmpl)) if err != nil { t.Fatalf("tmpl fail: %v", err) } @@ -503,7 +503,7 @@ func TestTemplateStrings(t *testing.T) { } // The point of this test is to verify that the below template works. tmpl := `{{if (exists . "status" "containerStatuses")}}{{range .status.containerStatuses}}{{if (and (eq .name "foo") (exists . "state" "running"))}}true{{end}}{{end}}{{end}}` - printer, err := printers.NewGoTemplatePrinter([]byte(tmpl)) + printer, err := genericprinters.NewGoTemplatePrinter([]byte(tmpl)) if err != nil { t.Fatalf("tmpl fail: %v", err) } @@ -535,17 +535,17 @@ func TestPrinters(t *testing.T) { jsonpathPrinter printers.ResourcePrinter ) - templatePrinter, err = printers.NewGoTemplatePrinter([]byte("{{.name}}")) + templatePrinter, err = genericprinters.NewGoTemplatePrinter([]byte("{{.name}}")) if err != nil { t.Fatal(err) } - templatePrinter2, err = printers.NewGoTemplatePrinter([]byte("{{len .items}}")) + templatePrinter2, err = genericprinters.NewGoTemplatePrinter([]byte("{{len .items}}")) if err != nil { t.Fatal(err) } - jsonpathPrinter, err = printers.NewJSONPathPrinter("{.metadata.name}") + jsonpathPrinter, err = genericprinters.NewJSONPathPrinter("{.metadata.name}") if err != nil { t.Fatal(err) } From 70417ca1502fa75ad4b767670ca456b667db33da Mon Sep 17 00:00:00 2001 From: David Eads Date: Mon, 2 Jul 2018 10:05:24 -0400 Subject: [PATCH 2/3] make template printers a recommended printer --- pkg/kubectl/cmd/create/BUILD | 2 - pkg/kubectl/cmd/create/create.go | 8 +- .../cmd/create/create_deployment_test.go | 2 +- pkg/kubectl/cmd/create/create_job.go | 4 +- pkg/kubectl/cmd/create/create_job_test.go | 2 +- pkg/kubectl/cmd/create/create_pdb_test.go | 2 +- .../cmd/create/create_priorityclass_test.go | 2 +- pkg/kubectl/cmd/create/create_role.go | 4 +- pkg/kubectl/cmd/create/create_role_test.go | 10 +- pkg/kubectl/cmd/create/flags.go | 93 ------------------- pkg/kubectl/cmd/replace.go | 9 +- pkg/kubectl/cmd/scale.go | 9 +- pkg/kubectl/genericclioptions/BUILD | 5 + .../genericclioptions/kube_template_flags.go | 15 ++- pkg/kubectl/genericclioptions/print_flags.go | 27 +++++- pkg/kubectl/genericclioptions/printers/BUILD | 12 ++- pkg/printers/BUILD | 8 -- 17 files changed, 70 insertions(+), 144 deletions(-) delete mode 100644 pkg/kubectl/cmd/create/flags.go diff --git a/pkg/kubectl/cmd/create/BUILD b/pkg/kubectl/cmd/create/BUILD index ed89ca3f4ec..fedfef695f5 100644 --- a/pkg/kubectl/cmd/create/BUILD +++ b/pkg/kubectl/cmd/create/BUILD @@ -18,7 +18,6 @@ go_library( "create_secret.go", "create_service.go", "create_serviceaccount.go", - "flags.go", ], importpath = "k8s.io/kubernetes/pkg/kubectl/cmd/create", visibility = ["//build/visible_to:pkg_kubectl_cmd_create_CONSUMERS"], @@ -29,7 +28,6 @@ go_library( "//pkg/kubectl/cmd/util:go_default_library", "//pkg/kubectl/cmd/util/editor:go_default_library", "//pkg/kubectl/genericclioptions:go_default_library", - "//pkg/kubectl/genericclioptions/printers:go_default_library", "//pkg/kubectl/genericclioptions/resource:go_default_library", "//pkg/kubectl/scheme:go_default_library", "//pkg/kubectl/util/i18n:go_default_library", diff --git a/pkg/kubectl/cmd/create/create.go b/pkg/kubectl/cmd/create/create.go index 556e6d15afa..a6bb8b03b8e 100644 --- a/pkg/kubectl/cmd/create/create.go +++ b/pkg/kubectl/cmd/create/create.go @@ -44,7 +44,7 @@ import ( ) type CreateOptions struct { - PrintFlags *PrintFlags + PrintFlags *genericclioptions.PrintFlags RecordFlags *genericclioptions.RecordFlags DryRun bool @@ -79,7 +79,7 @@ var ( func NewCreateOptions(ioStreams genericclioptions.IOStreams) *CreateOptions { return &CreateOptions{ - PrintFlags: NewPrintFlags("created", legacyscheme.Scheme), + PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme), RecordFlags: genericclioptions.NewRecordFlags(), Recorder: genericclioptions.NoopRecorder{}, @@ -336,7 +336,7 @@ func NameFromCommandArgs(cmd *cobra.Command, args []string) (string, error) { // CreateSubcommandOptions is an options struct to support create subcommands type CreateSubcommandOptions struct { // PrintFlags holds options necessary for obtaining a printer - PrintFlags *PrintFlags + PrintFlags *genericclioptions.PrintFlags // Name of resource being created Name string // StructuredGenerator is the resource generator for the object being created @@ -358,7 +358,7 @@ type CreateSubcommandOptions struct { func NewCreateSubcommandOptions(ioStreams genericclioptions.IOStreams) *CreateSubcommandOptions { return &CreateSubcommandOptions{ - PrintFlags: NewPrintFlags("created", legacyscheme.Scheme), + PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme), IOStreams: ioStreams, } } diff --git a/pkg/kubectl/cmd/create/create_deployment_test.go b/pkg/kubectl/cmd/create/create_deployment_test.go index bd42fcfedd0..1df3066eb69 100644 --- a/pkg/kubectl/cmd/create/create_deployment_test.go +++ b/pkg/kubectl/cmd/create/create_deployment_test.go @@ -140,7 +140,7 @@ func TestCreateDeploymentNoImage(t *testing.T) { cmd.Flags().Set("output", "name") options := &DeploymentOpts{ CreateSubcommandOptions: &CreateSubcommandOptions{ - PrintFlags: NewPrintFlags("created", legacyscheme.Scheme), + PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme), DryRun: true, IOStreams: ioStreams, }, diff --git a/pkg/kubectl/cmd/create/create_job.go b/pkg/kubectl/cmd/create/create_job.go index d4f024e470b..1e92754def4 100644 --- a/pkg/kubectl/cmd/create/create_job.go +++ b/pkg/kubectl/cmd/create/create_job.go @@ -45,7 +45,7 @@ var ( ) type CreateJobOptions struct { - PrintFlags *PrintFlags + PrintFlags *genericclioptions.PrintFlags PrintObj func(obj runtime.Object) error @@ -64,7 +64,7 @@ type CreateJobOptions struct { func NewCreateJobOptions(ioStreams genericclioptions.IOStreams) *CreateJobOptions { return &CreateJobOptions{ - PrintFlags: NewPrintFlags("created", legacyscheme.Scheme), + PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme), IOStreams: ioStreams, } } diff --git a/pkg/kubectl/cmd/create/create_job_test.go b/pkg/kubectl/cmd/create/create_job_test.go index d21249495fa..7fa4b9e78cd 100644 --- a/pkg/kubectl/cmd/create/create_job_test.go +++ b/pkg/kubectl/cmd/create/create_job_test.go @@ -85,7 +85,7 @@ func TestCreateJobFromCronJob(t *testing.T) { f := cmdtesting.NewTestFactory() defer f.Cleanup() - printFlags := NewPrintFlags("created", legacyscheme.Scheme) + printFlags := genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme) ioStreams, _, buf, _ := genericclioptions.NewTestIOStreams() cmdOptions := &CreateJobOptions{ diff --git a/pkg/kubectl/cmd/create/create_pdb_test.go b/pkg/kubectl/cmd/create/create_pdb_test.go index 01d6ee99e8d..42e7652c030 100644 --- a/pkg/kubectl/cmd/create/create_pdb_test.go +++ b/pkg/kubectl/cmd/create/create_pdb_test.go @@ -58,7 +58,7 @@ func TestCreatePdb(t *testing.T) { cmd.Flags().Set("dry-run", "true") cmd.Flags().Set("output", outputFormat) - printFlags := NewPrintFlags("created", legacyscheme.Scheme) + printFlags := genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme) printFlags.OutputFormat = &outputFormat options := &PodDisruptionBudgetOpts{ diff --git a/pkg/kubectl/cmd/create/create_priorityclass_test.go b/pkg/kubectl/cmd/create/create_priorityclass_test.go index 2f775be6385..b25aaa7a225 100644 --- a/pkg/kubectl/cmd/create/create_priorityclass_test.go +++ b/pkg/kubectl/cmd/create/create_priorityclass_test.go @@ -59,7 +59,7 @@ func TestCreatePriorityClass(t *testing.T) { cmd.Flags().Set("dry-run", "true") cmd.Flags().Set("output", outputFormat) - printFlags := NewPrintFlags("created", legacyscheme.Scheme) + printFlags := genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme) printFlags.OutputFormat = &outputFormat options := &PriorityClassOpts{ diff --git a/pkg/kubectl/cmd/create/create_role.go b/pkg/kubectl/cmd/create/create_role.go index c9ecd1cb233..1d9a481e7ec 100644 --- a/pkg/kubectl/cmd/create/create_role.go +++ b/pkg/kubectl/cmd/create/create_role.go @@ -112,7 +112,7 @@ type ResourceOptions struct { } type CreateRoleOptions struct { - PrintFlags *PrintFlags + PrintFlags *genericclioptions.PrintFlags Name string Verbs []string @@ -131,7 +131,7 @@ type CreateRoleOptions struct { func NewCreateRoleOptions(ioStreams genericclioptions.IOStreams) *CreateRoleOptions { return &CreateRoleOptions{ - PrintFlags: NewPrintFlags("created", legacyscheme.Scheme), + PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme), IOStreams: ioStreams, } diff --git a/pkg/kubectl/cmd/create/create_role_test.go b/pkg/kubectl/cmd/create/create_role_test.go index 4551bea8541..506e30333dc 100644 --- a/pkg/kubectl/cmd/create/create_role_test.go +++ b/pkg/kubectl/cmd/create/create_role_test.go @@ -372,14 +372,14 @@ func TestComplete(t *testing.T) { "test-missing-name": { params: []string{}, roleOptions: &CreateRoleOptions{ - PrintFlags: NewPrintFlags("created", legacyscheme.Scheme), + PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme), }, expectErr: true, }, "test-duplicate-verbs": { params: []string{roleName}, roleOptions: &CreateRoleOptions{ - PrintFlags: NewPrintFlags("created", legacyscheme.Scheme), + PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme), Name: roleName, Verbs: []string{ "get", @@ -412,7 +412,7 @@ func TestComplete(t *testing.T) { "test-verball": { params: []string{roleName}, roleOptions: &CreateRoleOptions{ - PrintFlags: NewPrintFlags("created", legacyscheme.Scheme), + PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme), Name: roleName, Verbs: []string{ "get", @@ -441,7 +441,7 @@ func TestComplete(t *testing.T) { "test-duplicate-resourcenames": { params: []string{roleName}, roleOptions: &CreateRoleOptions{ - PrintFlags: NewPrintFlags("created", legacyscheme.Scheme), + PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme), Name: roleName, Verbs: []string{"*"}, ResourceNames: []string{"foo", "foo"}, @@ -466,7 +466,7 @@ func TestComplete(t *testing.T) { "test-valid-complete-case": { params: []string{roleName}, roleOptions: &CreateRoleOptions{ - PrintFlags: NewPrintFlags("created", legacyscheme.Scheme), + PrintFlags: genericclioptions.NewPrintFlags("created").WithTypeSetter(legacyscheme.Scheme), Name: roleName, Verbs: []string{"*"}, ResourceNames: []string{"foo"}, diff --git a/pkg/kubectl/cmd/create/flags.go b/pkg/kubectl/cmd/create/flags.go deleted file mode 100644 index 6e902ea1222..00000000000 --- a/pkg/kubectl/cmd/create/flags.go +++ /dev/null @@ -1,93 +0,0 @@ -/* -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 create - -import ( - "github.com/spf13/cobra" - "k8s.io/kubernetes/pkg/kubectl/genericclioptions" - - "k8s.io/apimachinery/pkg/runtime" - genericprinters "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers" - "k8s.io/kubernetes/pkg/printers" -) - -// PrintFlags composes common printer flag structs -// used across all create commands, and provides a method -// of retrieving a known printer based on flag values provided. -type PrintFlags struct { - JSONYamlPrintFlags *genericclioptions.JSONYamlPrintFlags - NamePrintFlags *genericclioptions.NamePrintFlags - TemplateFlags *genericclioptions.KubeTemplatePrintFlags - - TypeSetter *genericprinters.TypeSetterPrinter - - OutputFormat *string -} - -func (f *PrintFlags) AllowedFormats() []string { - return append(append(f.JSONYamlPrintFlags.AllowedFormats(), f.NamePrintFlags.AllowedFormats()...), - f.TemplateFlags.AllowedFormats()...) -} - -func (f *PrintFlags) Complete(successTemplate string) error { - return f.NamePrintFlags.Complete(successTemplate) -} - -func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) { - outputFormat := "" - if f.OutputFormat != nil { - outputFormat = *f.OutputFormat - } - - if p, err := f.JSONYamlPrintFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) { - return f.TypeSetter.WrapToPrinter(p, err) - } - - if p, err := f.NamePrintFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) { - return f.TypeSetter.WrapToPrinter(p, err) - } - - if p, err := f.TemplateFlags.ToPrinter(outputFormat); !genericclioptions.IsNoCompatiblePrinterError(err) { - return f.TypeSetter.WrapToPrinter(p, err) - } - - return nil, genericclioptions.NoCompatiblePrinterError{OutputFormat: &outputFormat, AllowedFormats: f.AllowedFormats()} -} - -func (f *PrintFlags) AddFlags(cmd *cobra.Command) { - f.JSONYamlPrintFlags.AddFlags(cmd) - f.NamePrintFlags.AddFlags(cmd) - f.TemplateFlags.AddFlags(cmd) - - if f.OutputFormat != nil { - cmd.Flags().StringVarP(f.OutputFormat, "output", "o", *f.OutputFormat, "Output format. One of: json|yaml|wide|name|custom-columns=...|custom-columns-file=...|go-template=...|go-template-file=...|jsonpath=...|jsonpath-file=... See custom columns [http://kubernetes.io/docs/user-guide/kubectl-overview/#custom-columns], golang template [http://golang.org/pkg/text/template/#pkg-overview] and jsonpath template [http://kubernetes.io/docs/user-guide/jsonpath].") - } -} - -func NewPrintFlags(operation string, scheme runtime.ObjectTyper) *PrintFlags { - outputFormat := "" - - return &PrintFlags{ - OutputFormat: &outputFormat, - - JSONYamlPrintFlags: genericclioptions.NewJSONYamlPrintFlags(), - NamePrintFlags: genericclioptions.NewNamePrintFlags(operation), - TemplateFlags: genericclioptions.NewKubeTemplatePrintFlags(), - - TypeSetter: genericprinters.NewTypeSetter(scheme), - } -} diff --git a/pkg/kubectl/cmd/replace.go b/pkg/kubectl/cmd/replace.go index 7c94ddf4973..d44f7625c59 100644 --- a/pkg/kubectl/cmd/replace.go +++ b/pkg/kubectl/cmd/replace.go @@ -89,15 +89,8 @@ type ReplaceOptions struct { } func NewReplaceOptions(streams genericclioptions.IOStreams) *ReplaceOptions { - outputFormat := "" - return &ReplaceOptions{ - // TODO(juanvallejo): figure out why we only support the "name" outputFormat in this command - // we only support "-o name" for this command, so only register the name printer - PrintFlags: &genericclioptions.PrintFlags{ - OutputFormat: &outputFormat, - NamePrintFlags: genericclioptions.NewNamePrintFlags("replaced"), - }, + PrintFlags: genericclioptions.NewPrintFlags("replaced"), DeleteFlags: NewDeleteFlags("to use to replace the resource."), IOStreams: streams, diff --git a/pkg/kubectl/cmd/scale.go b/pkg/kubectl/cmd/scale.go index 1f8c4f2577c..a883456dd27 100644 --- a/pkg/kubectl/cmd/scale.go +++ b/pkg/kubectl/cmd/scale.go @@ -93,15 +93,8 @@ type ScaleOptions struct { } func NewScaleOptions(ioStreams genericclioptions.IOStreams) *ScaleOptions { - outputFormat := "" - return &ScaleOptions{ - // TODO(juanvallejo): figure out why we only support the "name" outputFormat in this command - // we only support "-o name" for this command, so only register the name printer - PrintFlags: &genericclioptions.PrintFlags{ - OutputFormat: &outputFormat, - NamePrintFlags: genericclioptions.NewNamePrintFlags("scaled"), - }, + PrintFlags: genericclioptions.NewPrintFlags("scaled"), RecordFlags: genericclioptions.NewRecordFlags(), CurrentReplicas: -1, Recorder: genericclioptions.NoopRecorder{}, diff --git a/pkg/kubectl/genericclioptions/BUILD b/pkg/kubectl/genericclioptions/BUILD index 3e65891717a..ae8e7e6fa65 100644 --- a/pkg/kubectl/genericclioptions/BUILD +++ b/pkg/kubectl/genericclioptions/BUILD @@ -11,9 +11,12 @@ go_library( "filename_flags.go", "io_options.go", "json_yaml_flags.go", + "jsonpath_flags.go", + "kube_template_flags.go", "name_flags.go", "print_flags.go", "record_flags.go", + "template_flags.go", ], importpath = "k8s.io/kubernetes/pkg/kubectl/genericclioptions", visibility = ["//visibility:public"], @@ -57,7 +60,9 @@ go_test( name = "go_default_test", srcs = [ "json_yaml_flags_test.go", + "jsonpath_flags_test.go", "name_flags_test.go", + "template_flags_test.go", ], embed = [":go_default_library"], deps = [ diff --git a/pkg/kubectl/genericclioptions/kube_template_flags.go b/pkg/kubectl/genericclioptions/kube_template_flags.go index 345ca418d81..56140c3c361 100644 --- a/pkg/kubectl/genericclioptions/kube_template_flags.go +++ b/pkg/kubectl/genericclioptions/kube_template_flags.go @@ -26,18 +26,25 @@ import ( // This is necessary if dealing with cases that require support both both printers, since both sets of flags // require overlapping flags. type KubeTemplatePrintFlags struct { - *GoTemplatePrintFlags - *JSONPathPrintFlags + GoTemplatePrintFlags *GoTemplatePrintFlags + JSONPathPrintFlags *JSONPathPrintFlags AllowMissingKeys *bool TemplateArgument *string } func (f *KubeTemplatePrintFlags) AllowedFormats() []string { + if f == nil { + return []string{} + } return append(f.GoTemplatePrintFlags.AllowedFormats(), f.JSONPathPrintFlags.AllowedFormats()...) } func (f *KubeTemplatePrintFlags) ToPrinter(outputFormat string) (printers.ResourcePrinter, error) { + if f == nil { + return nil, NoCompatiblePrinterError{} + } + if p, err := f.JSONPathPrintFlags.ToPrinter(outputFormat); !IsNoCompatiblePrinterError(err) { return p, err } @@ -47,6 +54,10 @@ func (f *KubeTemplatePrintFlags) ToPrinter(outputFormat string) (printers.Resour // AddFlags receives a *cobra.Command reference and binds // flags related to template printing to it func (f *KubeTemplatePrintFlags) AddFlags(c *cobra.Command) { + if f == nil { + return + } + if f.TemplateArgument != nil { c.Flags().StringVar(f.TemplateArgument, "template", *f.TemplateArgument, "Template string or path to template file to use when -o=go-template, -o=go-template-file. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview].") c.MarkFlagFilename("template") diff --git a/pkg/kubectl/genericclioptions/print_flags.go b/pkg/kubectl/genericclioptions/print_flags.go index 44b76c313c4..8ed6e22efe9 100644 --- a/pkg/kubectl/genericclioptions/print_flags.go +++ b/pkg/kubectl/genericclioptions/print_flags.go @@ -55,8 +55,9 @@ func IsNoCompatiblePrinterError(err error) bool { // used across all commands, and provides a method // of retrieving a known printer based on flag values provided. type PrintFlags struct { - JSONYamlPrintFlags *JSONYamlPrintFlags - NamePrintFlags *NamePrintFlags + JSONYamlPrintFlags *JSONYamlPrintFlags + NamePrintFlags *NamePrintFlags + TemplatePrinterFlags *KubeTemplatePrintFlags TypeSetterPrinter *printers.TypeSetterPrinter @@ -68,7 +69,11 @@ func (f *PrintFlags) Complete(successTemplate string) error { } func (f *PrintFlags) AllowedFormats() []string { - return append(f.JSONYamlPrintFlags.AllowedFormats(), f.NamePrintFlags.AllowedFormats()...) + ret := []string{} + ret = append(ret, f.JSONYamlPrintFlags.AllowedFormats()...) + ret = append(ret, f.NamePrintFlags.AllowedFormats()...) + ret = append(ret, f.TemplatePrinterFlags.AllowedFormats()...) + return ret } func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) { @@ -76,6 +81,10 @@ func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) { if f.OutputFormat != nil { outputFormat = *f.OutputFormat } + // for backwards compatibility we want to support a --template argument given, even when no --output format is provided + if f.TemplatePrinterFlags != nil && f.TemplatePrinterFlags.TemplateArgument != nil && len(*f.TemplatePrinterFlags.TemplateArgument) > 0 && len(outputFormat) == 0 { + outputFormat = "go-template" + } if f.JSONYamlPrintFlags != nil { if p, err := f.JSONYamlPrintFlags.ToPrinter(outputFormat); !IsNoCompatiblePrinterError(err) { @@ -89,12 +98,19 @@ func (f *PrintFlags) ToPrinter() (printers.ResourcePrinter, error) { } } + if f.TemplatePrinterFlags != nil { + if p, err := f.TemplatePrinterFlags.ToPrinter(outputFormat); !IsNoCompatiblePrinterError(err) { + return f.TypeSetterPrinter.WrapToPrinter(p, err) + } + } + return nil, NoCompatiblePrinterError{OutputFormat: f.OutputFormat, AllowedFormats: f.AllowedFormats()} } func (f *PrintFlags) AddFlags(cmd *cobra.Command) { f.JSONYamlPrintFlags.AddFlags(cmd) f.NamePrintFlags.AddFlags(cmd) + f.TemplatePrinterFlags.AddFlags(cmd) if f.OutputFormat != nil { cmd.Flags().StringVarP(f.OutputFormat, "output", "o", *f.OutputFormat, fmt.Sprintf("Output format. One of: %s.", strings.Join(f.AllowedFormats(), "|"))) @@ -119,7 +135,8 @@ func NewPrintFlags(operation string) *PrintFlags { return &PrintFlags{ OutputFormat: &outputFormat, - JSONYamlPrintFlags: NewJSONYamlPrintFlags(), - NamePrintFlags: NewNamePrintFlags(operation), + JSONYamlPrintFlags: NewJSONYamlPrintFlags(), + NamePrintFlags: NewNamePrintFlags(operation), + TemplatePrinterFlags: NewKubeTemplatePrintFlags(), } } diff --git a/pkg/kubectl/genericclioptions/printers/BUILD b/pkg/kubectl/genericclioptions/printers/BUILD index 681bef193cf..7893ac02b35 100644 --- a/pkg/kubectl/genericclioptions/printers/BUILD +++ b/pkg/kubectl/genericclioptions/printers/BUILD @@ -6,8 +6,10 @@ go_library( "discard.go", "interface.go", "json.go", + "jsonpath.go", "name.go", "sourcechecker.go", + "template.go", "typesetter.go", ], importpath = "k8s.io/kubernetes/pkg/kubectl/genericclioptions/printers", @@ -17,14 +19,22 @@ go_library( "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library", + "//staging/src/k8s.io/client-go/util/jsonpath:go_default_library", "//vendor/github.com/ghodss/yaml:go_default_library", ], ) go_test( name = "go_default_test", - srcs = ["sourcechecker_test.go"], + srcs = [ + "sourcechecker_test.go", + "template_test.go", + ], embed = [":go_default_library"], + deps = [ + "//staging/src/k8s.io/api/core/v1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library", + ], ) filegroup( diff --git a/pkg/printers/BUILD b/pkg/printers/BUILD index b9478446e82..8026a11b25e 100644 --- a/pkg/printers/BUILD +++ b/pkg/printers/BUILD @@ -13,12 +13,7 @@ go_library( "customcolumn_flags.go", "humanreadable.go", "interface.go", - "jsonpath.go", - "jsonpath_flags.go", - "kube_template_flags.go", "tabwriter.go", - "template.go", - "template_flags.go", ], importpath = "k8s.io/kubernetes/pkg/printers", deps = [ @@ -61,9 +56,6 @@ go_test( "customcolumn_flags_test.go", "customcolumn_test.go", "humanreadable_test.go", - "jsonpath_flags_test.go", - "template_flags_test.go", - "template_test.go", ], embed = [":go_default_library"], deps = [ From 25a4932653821361b266e1fe30017300fac9f825 Mon Sep 17 00:00:00 2001 From: juanvallejo Date: Mon, 2 Jul 2018 14:16:20 -0400 Subject: [PATCH 3/3] add --template tests for commands Adds --template printing test-cmd tests for the following commands: kubectl annotate, kubectl apply, kubectl autoscale, kubectl convert, kubectl create, kubectl expose, kubectl get, kubectl label, kubectl patch. --- hack/make-rules/test-cmd-util.sh | 210 +++++++++++++++++++++++++++++++ pkg/kubectl/cmd/config/BUILD | 3 - pkg/kubectl/cmd/config/flags.go | 101 --------------- pkg/kubectl/cmd/config/view.go | 4 +- 4 files changed, 212 insertions(+), 106 deletions(-) delete mode 100644 pkg/kubectl/cmd/config/flags.go diff --git a/hack/make-rules/test-cmd-util.sh b/hack/make-rules/test-cmd-util.sh index 8e7598c7227..93c0b76714f 100755 --- a/hack/make-rules/test-cmd-util.sh +++ b/hack/make-rules/test-cmd-util.sh @@ -1710,6 +1710,208 @@ run_kubectl_request_timeout_tests() { set +o errexit } +run_template_output_tests() { + set -o nounset + set -o errexit + + kube::log::status "Testing --template support on commands" + ### Test global request timeout option + # Pre-condition: no POD exists + create_and_use_new_namespace + kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" '' + # Command + # check that create supports --template output + kubectl create "${kube_flags[@]}" -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml + # Post-condition: valid-pod POD is created + kubectl get "${kube_flags[@]}" pods -o json + kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'valid-pod:' + + # check that patch command supports --template output + output_message=$(kubectl "${kube_flags[@]}" patch --dry-run pods/valid-pod -p '{"patched":"value3"}' --type=merge --template="{{ .metadata.name }}:") + kube::test::if_has_string "${output_message}" 'valid-pod:' + + # check that label command supports --template output + output_message=$(kubectl "${kube_flags[@]}" label --dry-run pods/valid-pod label=value --template="{{ .metadata.name }}:") + kube::test::if_has_string "${output_message}" 'valid-pod:' + + # check that annotate command supports --template output + output_message=$(kubectl "${kube_flags[@]}" annotate --dry-run pods/valid-pod annotation=value --template="{{ .metadata.name }}:") + kube::test::if_has_string "${output_message}" 'valid-pod:' + + # check that apply command supports --template output + output_message=$(kubectl "${kube_flags[@]}" apply --dry-run -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml --template="{{ .metadata.name }}:") + kube::test::if_has_string "${output_message}" 'valid-pod:' + + # check that create command supports --template output + output_message=$(kubectl "${kube_flags[@]}" create -f test/fixtures/doc-yaml/admin/limitrange/valid-pod.yaml --dry-run --template="{{ .metadata.name }}:") + kube::test::if_has_string "${output_message}" 'valid-pod:' + + # check that autoscale command supports --template output + output_message=$(kubectl "${kube_flags[@]}" autoscale --max=2 -f hack/testdata/scale-deploy-1.yaml --dry-run --template="{{ .metadata.name }}:") + kube::test::if_has_string "${output_message}" 'scale-1:' + + # check that expose command supports --template output + output_message=$(kubectl "${kube_flags[@]}" expose -f hack/testdata/redis-slave-replicaset.yaml --save-config --port=80 --target-port=8000 --dry-run --template="{{ .metadata.name }}:") + kube::test::if_has_string "${output_message}" 'redis-slave:' + + # check that convert command supports --template output + output_message=$(kubectl "${kube_flags[@]}" convert -f hack/testdata/deployment-revision1.yaml --output-version=apps/v1beta1 --template="{{ .metadata.name }}:") + kube::test::if_has_string "${output_message}" 'nginx:' + + # check that run command supports --template output + output_message=$(kubectl "${kube_flags[@]}" run --dry-run --template="{{ .metadata.name }}:" pi --image=perl --restart=OnFailure -- perl -Mbignum=bpi -wle 'print bpi(2000)') + kube::test::if_has_string "${output_message}" 'pi:' + + # check that taint command supports --template output + output_message=$(kubectl "${kube_flags[@]}" taint node 127.0.0.1 dedicated=foo:PreferNoSchedule --template="{{ .metadata.name }}:") + kube::test::if_has_string "${output_message}" '127.0.0.1:' + # untaint node + kubectl taint node 127.0.0.1 dedicated- + + # check that "apply set-last-applied" command supports --template output + kubectl "${kube_flags[@]}" create -f test/e2e/testing-manifests/statefulset/cassandra/controller.yaml + output_message=$(kubectl "${kube_flags[@]}" apply set-last-applied -f test/e2e/testing-manifests/statefulset/cassandra/controller.yaml --dry-run --create-annotation --template="{{ .metadata.name }}:") + kube::test::if_has_string "${output_message}" 'cassandra:' + + # check that "auth reconcile" command supports --template output + output_message=$(kubectl "${kube_flags[@]}" auth reconcile --dry-run -f test/fixtures/pkg/kubectl/cmd/auth/rbac-resource-plus.yaml --template="{{ .metadata.name }}:") + kube::test::if_has_string "${output_message}" 'testing-CR:testing-CRB:testing-RB:testing-R:' + + # check that "config view" command supports --template output + output_message=$(kubectl "${kube_flags[@]}" config view --output=go-template="{{ .kind }}:") + kube::test::if_has_string "${output_message}" 'Config' + + # check that "create clusterrole" command supports --template output + output_message=$(kubectl "${kube_flags[@]}" create clusterrole --template="{{ .metadata.name }}:" --verb get myclusterrole --non-resource-url /logs/ --resource pods) + kube::test::if_has_string "${output_message}" 'myclusterrole:' + + # check that "create clusterrolebinding" command supports --template output + output_message=$(kubectl "${kube_flags[@]}" create clusterrolebinding foo --clusterrole=myclusterrole --template="{{ .metadata.name }}:") + kube::test::if_has_string "${output_message}" 'foo:' + + # check that "create configmap" command supports --template output + output_message=$(kubectl "${kube_flags[@]}" create configmap cm --dry-run --template="{{ .metadata.name }}:") + kube::test::if_has_string "${output_message}" 'cm:' + + # check that "create deployment" command supports --template output + output_message=$(kubectl "${kube_flags[@]}" create deployment deploy --dry-run --image=nginx --template="{{ .metadata.name }}:") + kube::test::if_has_string "${output_message}" 'deploy:' + + # check that "create job" command supports --template output + kubectl create "${kube_flags[@]}" -f - <