Merge pull request #36071 from juanvallejo/jvallejo/fix-set-image-local-output

Automatic merge from submit-queue

fix local resource output when `-f` not specified

**Release note**:
```release-note
release-note-none
```

`kubectl set image` does not have a `--dry-run` option. Although it offers a
`--local` flag, it does not support server request, limiting input to that of stdin
or that of a local file.

This patch adds a `--dry-run` option to the `kubectl set image` command,
allowing for resources from the server to be selected, without making any
mutations.

cc @ncdc 

Related PR: https://github.com/kubernetes/kubernetes/pull/36174
This commit is contained in:
Kubernetes Submit Queue 2016-12-08 16:20:00 -08:00 committed by GitHub
commit 76e0b1af54
3 changed files with 63 additions and 3 deletions

View File

@ -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",
],
)

View File

@ -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)
}

View File

@ -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+".")
}
}