From 23740d9080dab80a8a2b94e95bb54fee48f31134 Mon Sep 17 00:00:00 2001 From: Janet Kuo Date: Fri, 21 Aug 2015 11:35:53 -0700 Subject: [PATCH 1/2] Enable kubectl label resource type bash completion --- contrib/completions/bash/kubectl | 15 +++++++++++++++ pkg/kubectl/cmd/label.go | 3 +++ 2 files changed, 18 insertions(+) diff --git a/contrib/completions/bash/kubectl b/contrib/completions/bash/kubectl index 8e1a9ccc0cb..afa2f4e7116 100644 --- a/contrib/completions/bash/kubectl +++ b/contrib/completions/bash/kubectl @@ -833,6 +833,21 @@ _kubectl_label() must_have_one_flag=() must_have_one_noun=() + must_have_one_noun+=("componentstatus") + must_have_one_noun+=("endpoints") + must_have_one_noun+=("event") + must_have_one_noun+=("limitrange") + must_have_one_noun+=("namespace") + must_have_one_noun+=("node") + must_have_one_noun+=("persistentvolume") + must_have_one_noun+=("persistentvolumeclaim") + must_have_one_noun+=("pod") + must_have_one_noun+=("podtemplate") + must_have_one_noun+=("replicationcontroller") + must_have_one_noun+=("resourcequota") + must_have_one_noun+=("secret") + must_have_one_noun+=("service") + must_have_one_noun+=("serviceaccount") } _kubectl_annotate() diff --git a/pkg/kubectl/cmd/label.go b/pkg/kubectl/cmd/label.go index 42aba6ac6b1..d0f408e9122 100644 --- a/pkg/kubectl/cmd/label.go +++ b/pkg/kubectl/cmd/label.go @@ -64,6 +64,8 @@ $ kubectl label pods foo bar-` func NewCmdLabel(f *cmdutil.Factory, out io.Writer) *cobra.Command { options := &LabelOptions{} + p := kubectl.NewHumanReadablePrinter(false, false, false, false, []string{}) + validArgs := p.HandledResources() cmd := &cobra.Command{ Use: "label [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--resource-version=version]", @@ -74,6 +76,7 @@ func NewCmdLabel(f *cmdutil.Factory, out io.Writer) *cobra.Command { err := RunLabel(f, out, cmd, args, options) cmdutil.CheckErr(err) }, + ValidArgs: validArgs, } cmdutil.AddPrinterFlags(cmd) cmd.Flags().Bool("overwrite", false, "If true, allow labels to be overwritten, otherwise reject label updates that overwrite existing labels.") From b673920ccf83c9448398243ff5ea3afab989d5fc Mon Sep 17 00:00:00 2001 From: Janet Kuo Date: Mon, 31 Aug 2015 12:04:12 -0700 Subject: [PATCH 2/2] Get printer from factory to retrieve handled resources --- contrib/completions/bash/kubectl | 1 + pkg/kubectl/cmd/cmd_test.go | 5 +++++ pkg/kubectl/cmd/label.go | 8 +++++-- pkg/kubectl/resource_printer.go | 36 ++++++++++++++++++++++++++++++++ pkg/kubectl/sorting_printer.go | 5 +++++ 5 files changed, 53 insertions(+), 2 deletions(-) diff --git a/contrib/completions/bash/kubectl b/contrib/completions/bash/kubectl index afa2f4e7116..2ae95ed94e8 100644 --- a/contrib/completions/bash/kubectl +++ b/contrib/completions/bash/kubectl @@ -848,6 +848,7 @@ _kubectl_label() must_have_one_noun+=("secret") must_have_one_noun+=("service") must_have_one_noun+=("serviceaccount") + must_have_one_noun+=("thirdpartyresource") } _kubectl_annotate() diff --git a/pkg/kubectl/cmd/cmd_test.go b/pkg/kubectl/cmd/cmd_test.go index 5f95a00ca68..acf4fae94a1 100644 --- a/pkg/kubectl/cmd/cmd_test.go +++ b/pkg/kubectl/cmd/cmd_test.go @@ -112,6 +112,11 @@ func (t *testPrinter) PrintObj(obj runtime.Object, out io.Writer) error { return t.Err } +// TODO: implement HandledResources() +func (t *testPrinter) HandledResources() []string { + return []string{} +} + type testDescriber struct { Name, Namespace string Output string diff --git a/pkg/kubectl/cmd/label.go b/pkg/kubectl/cmd/label.go index d0f408e9122..8ffe37ca934 100644 --- a/pkg/kubectl/cmd/label.go +++ b/pkg/kubectl/cmd/label.go @@ -64,8 +64,12 @@ $ kubectl label pods foo bar-` func NewCmdLabel(f *cmdutil.Factory, out io.Writer) *cobra.Command { options := &LabelOptions{} - p := kubectl.NewHumanReadablePrinter(false, false, false, false, []string{}) - validArgs := p.HandledResources() + validArgs := []string{} + p, err := f.Printer(nil, false, false, false, false, []string{}) + cmdutil.CheckErr(err) + if p != nil { + validArgs = p.HandledResources() + } cmd := &cobra.Command{ Use: "label [--overwrite] (-f FILENAME | TYPE NAME) KEY_1=VAL_1 ... KEY_N=VAL_N [--resource-version=version]", diff --git a/pkg/kubectl/resource_printer.go b/pkg/kubectl/resource_printer.go index 3d649a6efc1..a89e05ba90b 100644 --- a/pkg/kubectl/resource_printer.go +++ b/pkg/kubectl/resource_printer.go @@ -101,6 +101,7 @@ func GetPrinter(format, formatArgument string) (ResourcePrinter, bool, error) { type ResourcePrinter interface { // Print receives a runtime object, formats it and prints it to a writer. PrintObj(runtime.Object, io.Writer) error + HandledResources() []string } // ResourcePrinterFunc is a function that can print objects @@ -111,6 +112,11 @@ func (fn ResourcePrinterFunc) PrintObj(obj runtime.Object, w io.Writer) error { return fn(obj, w) } +// TODO: implement HandledResources() +func (fn ResourcePrinterFunc) HandledResources() []string { + return []string{} +} + // VersionedPrinter takes runtime objects and ensures they are converted to a given API version // prior to being passed to a nested printer. type VersionedPrinter struct { @@ -149,6 +155,11 @@ func (p *VersionedPrinter) PrintObj(obj runtime.Object, w io.Writer) error { return fmt.Errorf("the object cannot be converted to any of the versions: %v", p.version) } +// TODO: implement HandledResources() +func (p *VersionedPrinter) HandledResources() []string { + return []string{} +} + // NamePrinter is an implementation of ResourcePrinter which outputs "resource/name" pair of an object. type NamePrinter struct { } @@ -199,6 +210,11 @@ func (p *NamePrinter) PrintObj(obj runtime.Object, w io.Writer) error { return nil } +// TODO: implement HandledResources() +func (p *NamePrinter) HandledResources() []string { + return []string{} +} + // JSONPrinter is an implementation of ResourcePrinter which outputs an object as JSON. type JSONPrinter struct { } @@ -216,6 +232,11 @@ func (p *JSONPrinter) PrintObj(obj runtime.Object, w io.Writer) error { return err } +// TODO: implement HandledResources() +func (p *JSONPrinter) HandledResources() []string { + return []string{} +} + // YAMLPrinter is an implementation of ResourcePrinter which outputs an object as YAML. // The input object is assumed to be in the internal version of an API and is converted // to the given version first. @@ -234,6 +255,11 @@ func (p *YAMLPrinter) PrintObj(obj runtime.Object, w io.Writer) error { return err } +// TODO: implement HandledResources() +func (p *YAMLPrinter) HandledResources() []string { + return []string{} +} + type handlerEntry struct { columns []string printFunc reflect.Value @@ -1214,6 +1240,11 @@ func (p *TemplatePrinter) PrintObj(obj runtime.Object, w io.Writer) error { return nil } +// TODO: implement HandledResources() +func (p *TemplatePrinter) HandledResources() []string { + return []string{} +} + // safeExecute tries to execute the template, but catches panics and returns an error // should the template engine panic. func (p *TemplatePrinter) safeExecute(w io.Writer, obj interface{}) error { @@ -1352,3 +1383,8 @@ func (j *JSONPathPrinter) PrintObj(obj runtime.Object, w io.Writer) error { } return nil } + +// TODO: implement HandledResources() +func (p *JSONPathPrinter) HandledResources() []string { + return []string{} +} diff --git a/pkg/kubectl/sorting_printer.go b/pkg/kubectl/sorting_printer.go index d536914a31c..cb2dc20d561 100644 --- a/pkg/kubectl/sorting_printer.go +++ b/pkg/kubectl/sorting_printer.go @@ -46,6 +46,11 @@ func (s *SortingPrinter) PrintObj(obj runtime.Object, out io.Writer) error { return s.Delegate.PrintObj(obj, out) } +// TODO: implement HandledResources() +func (p *SortingPrinter) HandledResources() []string { + return []string{} +} + func (s *SortingPrinter) sortObj(obj runtime.Object) error { objs, err := runtime.ExtractList(obj) if err != nil {