From 81f37bd770f22cafea20d80c4765161ec0cb8d10 Mon Sep 17 00:00:00 2001 From: juanvallejo Date: Thu, 4 May 2017 16:23:48 -0400 Subject: [PATCH] improve output of --list-pods This patch ensures that headers are printed for each list of pods that is printed in each node, as long as the --no-headers printer flag has not been specified by the user. It also ensures that the entire `--dry-run` output of `--evacuate` is only printed once, rather than once per node on the cluster. --- pkg/cmd/admin/node/evacuate.go | 14 +++++++++----- pkg/cmd/admin/node/listpods.go | 10 ++++++++++ pkg/cmd/admin/node/node.go | 4 +++- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/pkg/cmd/admin/node/evacuate.go b/pkg/cmd/admin/node/evacuate.go index 1f2553118c3a..82ac5bec8160 100644 --- a/pkg/cmd/admin/node/evacuate.go +++ b/pkg/cmd/admin/node/evacuate.go @@ -27,6 +27,8 @@ type EvacuateOptions struct { DryRun bool Force bool GracePeriod int64 + + printPodHeaders bool } // NewEvacuateOptions creates a new EvacuateOptions with default values. @@ -36,6 +38,8 @@ func NewEvacuateOptions(nodeOptions *NodeOptions) *EvacuateOptions { DryRun: false, Force: false, GracePeriod: 30, + + printPodHeaders: true, } } @@ -49,6 +53,11 @@ func (e *EvacuateOptions) AddFlags(cmd *cobra.Command) { } func (e *EvacuateOptions) Run() error { + if e.DryRun { + listpodsOp := ListPodsOptions{Options: e.Options, printPodHeaders: e.printPodHeaders} + return listpodsOp.Run() + } + nodes, err := e.Options.GetNodes() if err != nil { return err @@ -66,11 +75,6 @@ func (e *EvacuateOptions) Run() error { } func (e *EvacuateOptions) RunEvacuate(node *kapi.Node) error { - if e.DryRun { - listpodsOp := ListPodsOptions{Options: e.Options} - return listpodsOp.Run() - } - // We do *not* automatically mark the node unschedulable to perform evacuation. // Rationale: If we unschedule the node and later the operation is unsuccessful (stopped by user, network error, etc.), // we may not be able to recover in some cases to mark the node back to schedulable. To avoid these cases, we recommend diff --git a/pkg/cmd/admin/node/listpods.go b/pkg/cmd/admin/node/listpods.go index bf74aa27eaeb..97c9b557790e 100644 --- a/pkg/cmd/admin/node/listpods.go +++ b/pkg/cmd/admin/node/listpods.go @@ -17,6 +17,8 @@ import ( type ListPodsOptions struct { Options *NodeOptions + + printPodHeaders bool } func (l *ListPodsOptions) AddFlags(cmd *cobra.Command) { @@ -71,6 +73,14 @@ func (l *ListPodsOptions) runListPods(node *kapi.Node, printer kprinters.Resourc } fmt.Fprint(l.Options.ErrWriter, "\nListing matched pods on node: ", node.ObjectMeta.Name, "\n\n") + if p, ok := printer.(*kprinters.HumanReadablePrinter); ok { + if l.printPodHeaders { + p.EnsurePrintHeaders() + } + p.PrintObj(pods, l.Options.Writer) + return err + } + printer.PrintObj(pods, l.Options.Writer) return err diff --git a/pkg/cmd/admin/node/node.go b/pkg/cmd/admin/node/node.go index 9e2de13e07f8..68f944ca10a1 100644 --- a/pkg/cmd/admin/node/node.go +++ b/pkg/cmd/admin/node/node.go @@ -58,7 +58,7 @@ func NewCommandManageNode(f *clientcmd.Factory, commandName, fullName string, ou opts := &NodeOptions{} schedulableOp := &SchedulableOptions{Options: opts} evacuateOp := NewEvacuateOptions(opts) - listpodsOp := &ListPodsOptions{Options: opts} + listpodsOp := &ListPodsOptions{Options: opts, printPodHeaders: true} cmd := &cobra.Command{ Use: commandName, @@ -91,8 +91,10 @@ func NewCommandManageNode(f *clientcmd.Factory, commandName, fullName string, ou schedulableOp.Schedulable = schedulable err = schedulableOp.Run() } else if evacuate { + evacuateOp.printPodHeaders = !kcmdutil.GetFlagBool(c, "no-headers") err = evacuateOp.Run() } else if listpods { + listpodsOp.printPodHeaders = !kcmdutil.GetFlagBool(c, "no-headers") err = listpodsOp.Run() } kcmdutil.CheckErr(err)