From cbe479039b04095f1af606166cbd6d2ad5b0e723 Mon Sep 17 00:00:00 2001 From: juanvallejo Date: Thu, 8 Dec 2016 15:26:37 -0500 Subject: [PATCH] Add json,yaml output format support kubectl create This patch adds the ability to specify an output format other than "name" to `kubectl create ...`. It can be used in conjunction with the `--dry-run` option. Converts unstructured objects into known types in order to support all `--output` values. --- pkg/kubectl/cmd/apply.go | 10 ++++++++-- pkg/kubectl/cmd/create.go | 7 ++++++- pkg/kubectl/cmd/util/printing.go | 19 +++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/pkg/kubectl/cmd/apply.go b/pkg/kubectl/cmd/apply.go index 6a8f95a4c18..277b95bf39e 100644 --- a/pkg/kubectl/cmd/apply.go +++ b/pkg/kubectl/cmd/apply.go @@ -100,7 +100,6 @@ func NewCmdApply(f cmdutil.Factory, out, errOut io.Writer) *cobra.Command { Example: apply_example, Run: func(cmd *cobra.Command, args []string) { cmdutil.CheckErr(validateArgs(cmd, args)) - cmdutil.CheckErr(cmdutil.ValidateOutputArgs(cmd)) cmdutil.CheckErr(validatePruneAll(options.Prune, cmdutil.GetFlagBool(cmd, "all"), options.Selector)) cmdutil.CheckErr(RunApply(f, cmd, out, errOut, &options)) }, @@ -164,7 +163,6 @@ func parsePruneResources(gvks []string) ([]pruneResource, error) { } func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, options *ApplyOptions) error { - shortOutput := cmdutil.GetFlagString(cmd, "output") == "name" schema, err := f.Validator(cmdutil.GetFlagBool(cmd, "validate"), cmdutil.GetFlagString(cmd, "schema-cache-dir")) if err != nil { return err @@ -197,6 +195,8 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti } dryRun := cmdutil.GetFlagBool(cmd, "dry-run") + output := cmdutil.GetFlagString(cmd, "output") + shortOutput := output == "name" encoder := f.JSONEncoder() decoder := f.Decoder(false) @@ -253,6 +253,9 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti } count++ + if len(output) > 0 && !shortOutput { + return cmdutil.PrintResourceInfoForCommand(cmd, info, f, out) + } cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, dryRun, "created") return nil } @@ -304,6 +307,9 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti } } count++ + if len(output) > 0 && !shortOutput { + return cmdutil.PrintResourceInfoForCommand(cmd, info, f, out) + } cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, dryRun, "configured") return nil }) diff --git a/pkg/kubectl/cmd/create.go b/pkg/kubectl/cmd/create.go index e4d6272f520..a3ef8e57e7f 100644 --- a/pkg/kubectl/cmd/create.go +++ b/pkg/kubectl/cmd/create.go @@ -130,6 +130,7 @@ func RunCreate(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opt } dryRun := cmdutil.GetFlagBool(cmd, "dry-run") + output := cmdutil.GetFlagString(cmd, "output") count := 0 err = r.Visit(func(info *resource.Info, err error) error { @@ -153,7 +154,11 @@ func RunCreate(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opt } count++ - shortOutput := cmdutil.GetFlagString(cmd, "output") == "name" + + shortOutput := output == "name" + if len(output) > 0 && !shortOutput { + return cmdutil.PrintResourceInfoForCommand(cmd, info, f, out) + } if !shortOutput { f.PrintObjectSpecificMessage(info.Object, out) } diff --git a/pkg/kubectl/cmd/util/printing.go b/pkg/kubectl/cmd/util/printing.go index d660f2361cd..7550c9ef433 100644 --- a/pkg/kubectl/cmd/util/printing.go +++ b/pkg/kubectl/cmd/util/printing.go @@ -23,6 +23,7 @@ import ( "k8s.io/kubernetes/pkg/api/meta" "k8s.io/kubernetes/pkg/kubectl" + "k8s.io/kubernetes/pkg/kubectl/resource" "k8s.io/kubernetes/pkg/runtime/schema" "github.com/spf13/cobra" @@ -133,6 +134,24 @@ func PrinterForCommand(cmd *cobra.Command) (kubectl.ResourcePrinter, bool, error return maybeWrapSortingPrinter(cmd, printer), generic, nil } +// PrintResourceInfoForCommand receives a *cobra.Command and a *resource.Info and +// attempts to print an info object based on the specified output format. If the +// object passed is non-generic, it attempts to print the object using a HumanReadablePrinter. +// Requires that printer flags have been added to cmd (see AddPrinterFlags). +func PrintResourceInfoForCommand(cmd *cobra.Command, info *resource.Info, f Factory, out io.Writer) error { + printer, generic, err := PrinterForCommand(cmd) + if err != nil { + return err + } + if !generic || printer == nil { + printer, err = f.PrinterForMapping(cmd, nil, false) + if err != nil { + return err + } + } + return printer.PrintObj(info.Object, out) +} + func maybeWrapSortingPrinter(cmd *cobra.Command, printer kubectl.ResourcePrinter) kubectl.ResourcePrinter { sorting, err := cmd.Flags().GetString("sort-by") if err != nil {