Commit 6eaab223 authored by juanvallejo's avatar juanvallejo

add --dry-run opt to `kubectl set image`

This patch adds a `--dry-run` option to `kubectl set image...`
parent 45a436ac
...@@ -36,7 +36,10 @@ go_library( ...@@ -36,7 +36,10 @@ go_library(
go_test( go_test(
name = "go_default_test", name = "go_default_test",
srcs = ["set_image_test.go"], srcs = [
"set_image_test.go",
"set_test.go",
],
data = [ data = [
"//examples:config", "//examples:config",
], ],
...@@ -50,5 +53,6 @@ go_test( ...@@ -50,5 +53,6 @@ go_test(
"//pkg/kubectl/cmd/testing:go_default_library", "//pkg/kubectl/cmd/testing:go_default_library",
"//pkg/kubectl/cmd/util:go_default_library", "//pkg/kubectl/cmd/util:go_default_library",
"//pkg/kubectl/resource:go_default_library", "//pkg/kubectl/resource:go_default_library",
"//vendor:github.com/spf13/cobra",
], ],
) )
...@@ -42,9 +42,11 @@ type ImageOptions struct { ...@@ -42,9 +42,11 @@ type ImageOptions struct {
Selector string Selector string
Out io.Writer Out io.Writer
Err io.Writer Err io.Writer
DryRun bool
ShortOutput bool ShortOutput bool
All bool All bool
Record bool Record bool
Output string
ChangeCause string ChangeCause string
Local bool Local bool
Cmd *cobra.Command Cmd *cobra.Command
...@@ -104,6 +106,7 @@ func NewCmdImage(f cmdutil.Factory, out, err io.Writer) *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().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.") cmd.Flags().BoolVar(&options.Local, "local", false, "If true, set image will NOT contact api-server but run locally.")
cmdutil.AddRecordFlag(cmd) cmdutil.AddRecordFlag(cmd)
cmdutil.AddDryRunFlag(cmd)
return cmd return cmd
} }
...@@ -115,6 +118,8 @@ func (o *ImageOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st ...@@ -115,6 +118,8 @@ func (o *ImageOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []st
o.Record = cmdutil.GetRecordFlag(cmd) o.Record = cmdutil.GetRecordFlag(cmd)
o.ChangeCause = f.Command() o.ChangeCause = f.Command()
o.PrintObject = f.PrintObject o.PrintObject = f.PrintObject
o.DryRun = cmdutil.GetDryRunFlag(cmd)
o.Output = cmdutil.GetFlagString(cmd, "output")
o.Cmd = cmd o.Cmd = cmd
cmdNamespace, enforceNamespace, err := f.DefaultNamespace() cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
...@@ -198,7 +203,7 @@ func (o *ImageOptions) Run() error { ...@@ -198,7 +203,7 @@ func (o *ImageOptions) Run() error {
continue continue
} }
if o.Local { if o.PrintObject != nil && (o.Local || o.DryRun) {
return o.PrintObject(o.Cmd, o.Mapper, info.Object, o.Out) return o.PrintObject(o.Cmd, o.Mapper, info.Object, o.Out)
} }
...@@ -220,7 +225,11 @@ func (o *ImageOptions) Run() error { ...@@ -220,7 +225,11 @@ func (o *ImageOptions) Run() error {
} }
info.Refresh(obj, true) 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) return utilerrors.NewAggregate(allErrs)
} }
......
/*
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+".")
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment