diff --git a/pkg/kubectl/cmd/set/BUILD b/pkg/kubectl/cmd/set/BUILD index bdb6a15eb92..f5e88629add 100644 --- a/pkg/kubectl/cmd/set/BUILD +++ b/pkg/kubectl/cmd/set/BUILD @@ -36,7 +36,10 @@ go_library( go_test( name = "go_default_test", - srcs = ["set_image_test.go"], + srcs = [ + "set_image_test.go", + "set_test.go", + ], data = [ "//examples:config", ], @@ -50,5 +53,6 @@ go_test( "//pkg/kubectl/cmd/testing:go_default_library", "//pkg/kubectl/cmd/util:go_default_library", "//pkg/kubectl/resource:go_default_library", + "//vendor:github.com/spf13/cobra", ], ) diff --git a/pkg/kubectl/cmd/set/set_image.go b/pkg/kubectl/cmd/set/set_image.go index c303883d90f..be22e0102a4 100644 --- a/pkg/kubectl/cmd/set/set_image.go +++ b/pkg/kubectl/cmd/set/set_image.go @@ -42,9 +42,11 @@ type ImageOptions struct { Selector string Out io.Writer Err io.Writer + DryRun bool ShortOutput bool All bool Record bool + Output string ChangeCause string Local bool Cmd *cobra.Command @@ -104,6 +106,7 @@ func NewCmdImage(f cmdutil.Factory, out, err io.Writer) *cobra.Command { cmd.Flags().StringVarP(&options.Selector, "selector", "l", "", "Selector (label query) to filter on, supports '=', '==', and '!='.") cmd.Flags().BoolVar(&options.Local, "local", false, "If true, set image will NOT contact api-server but run locally.") cmdutil.AddRecordFlag(cmd) + cmdutil.AddDryRunFlag(cmd) return cmd } @@ -115,6 +118,8 @@ func (o *ImageOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st o.Record = cmdutil.GetRecordFlag(cmd) o.ChangeCause = f.Command() o.PrintObject = f.PrintObject + o.DryRun = cmdutil.GetDryRunFlag(cmd) + o.Output = cmdutil.GetFlagString(cmd, "output") o.Cmd = cmd cmdNamespace, enforceNamespace, err := f.DefaultNamespace() @@ -198,7 +203,7 @@ func (o *ImageOptions) Run() error { continue } - if o.Local { + if o.PrintObject != nil && (o.Local || o.DryRun) { return o.PrintObject(o.Cmd, o.Mapper, info.Object, o.Out) } @@ -220,7 +225,11 @@ func (o *ImageOptions) Run() error { } info.Refresh(obj, true) - cmdutil.PrintSuccess(o.Mapper, o.ShortOutput, o.Out, info.Mapping.Resource, info.Name, false, "image updated") + + if len(o.Output) > 0 { + return o.PrintObject(o.Cmd, o.Mapper, obj, o.Out) + } + cmdutil.PrintSuccess(o.Mapper, o.ShortOutput, o.Out, info.Mapping.Resource, info.Name, o.DryRun, "image updated") } return utilerrors.NewAggregate(allErrs) } diff --git a/pkg/kubectl/cmd/set/set_test.go b/pkg/kubectl/cmd/set/set_test.go new file mode 100644 index 00000000000..c0a889c15c5 --- /dev/null +++ b/pkg/kubectl/cmd/set/set_test.go @@ -0,0 +1,47 @@ +/* +Copyright 2016 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 set + +import ( + "bytes" + "testing" + + "github.com/spf13/cobra" + + clientcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util" +) + +func TestLocalAndDryRunFlags(t *testing.T) { + out := &bytes.Buffer{} + errout := &bytes.Buffer{} + f := clientcmdutil.NewFactory(nil) + setCmd := NewCmdSet(f, out, errout) + ensureLocalAndDryRunFlagsOnChildren(t, setCmd, "") +} + +func ensureLocalAndDryRunFlagsOnChildren(t *testing.T, c *cobra.Command, prefix string) { + for _, cmd := range c.Commands() { + name := prefix + cmd.Name() + if localFlag := cmd.Flag("local"); localFlag == nil { + t.Errorf("Command %s does not implement the --local flag", name) + } + if dryRunFlag := cmd.Flag("dry-run"); dryRunFlag == nil { + t.Errorf("Command %s does not implement the --dry-run flag", name) + } + ensureLocalAndDryRunFlagsOnChildren(t, cmd, name+".") + } +}