allow kubectl subcmds to process multiple resources

- use resource.Visit() to recursively process resources, as well as, aggregate
errors where possible
This commit is contained in:
Mike Metral
2016-04-14 15:00:40 -07:00
parent 9a871ed554
commit 999c8e211e
19 changed files with 676 additions and 231 deletions

View File

@@ -26,6 +26,7 @@ import (
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/resource"
"k8s.io/kubernetes/pkg/runtime"
utilerrors "k8s.io/kubernetes/pkg/util/errors"
"k8s.io/kubernetes/pkg/watch"
)
@@ -197,19 +198,39 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
return nil
}
b := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)).
NamespaceParam(cmdNamespace).DefaultNamespace().AllNamespaces(allNamespaces).
FilenameParam(enforceNamespace, options.Recursive, options.Filenames...).
SelectorParam(selector).
ExportParam(export).
ResourceTypeOrNameArgs(true, args...).
ContinueOnError().
Latest()
Latest().
Flatten().
Do()
err = r.Err()
if err != nil {
return err
}
printer, generic, err := cmdutil.PrinterForCommand(cmd)
if err != nil {
return err
}
infos := []*resource.Info{}
allErrs := []error{}
err = r.Visit(func(info *resource.Info, err error) error {
if err != nil {
return err
}
infos = append(infos, info)
return nil
})
if err != nil {
allErrs = append(allErrs, err)
}
if generic {
clientConfig, err := f.ClientConfig()
if err != nil {
@@ -217,11 +238,7 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
}
singular := false
r := b.Flatten().Do()
infos, err := r.IntoSingular(&singular).Infos()
if err != nil {
return err
}
r.IntoSingular(&singular)
// the outermost object will be converted to the output-version, but inner
// objects can use their mappings
@@ -237,10 +254,6 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
return printer.PrintObj(obj, out)
}
infos, err := b.Flatten().Do().Infos()
if err != nil {
return err
}
objs := make([]runtime.Object, len(infos))
for ix := range infos {
objs[ix] = infos[ix].Object
@@ -262,7 +275,8 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
for ix := range infos {
objs[ix], err = infos[ix].Mapping.ConvertToVersion(infos[ix].Object, version.String())
if err != nil {
return err
allErrs = append(allErrs, err)
continue
}
}
@@ -291,19 +305,21 @@ func RunGet(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []string
if printer == nil || lastMapping == nil || mapping == nil || mapping.Resource != lastMapping.Resource {
printer, err = f.PrinterForMapping(cmd, mapping, allNamespaces)
if err != nil {
return err
allErrs = append(allErrs, err)
continue
}
lastMapping = mapping
}
if _, found := printer.(*kubectl.HumanReadablePrinter); found {
if err := printer.PrintObj(original, w); err != nil {
return err
allErrs = append(allErrs, err)
}
continue
}
if err := printer.PrintObj(original, w); err != nil {
return err
allErrs = append(allErrs, err)
continue
}
}
return nil
return utilerrors.NewAggregate(allErrs)
}