Merge pull request #50584 from xilabao/fix-set-output-01

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

fix kubectl set resource/selector/subject output

**What this PR does / why we need it**:
kubectl set resource/selector/subject -o yaml doesn't return the expected format

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes https://github.com/kubernetes/kubectl/issues/51

**Special notes for your reviewer**:

**Release note**:

```release-note
NONE
```
pull/6/head
Kubernetes Submit Queue 2017-09-28 11:05:41 -07:00 committed by GitHub
commit 5c1d748794
3 changed files with 23 additions and 7 deletions

View File

@ -70,7 +70,7 @@ type ResourcesOptions struct {
Err io.Writer
Selector string
ContainerSelector string
ShortOutput bool
Output string
All bool
Record bool
ChangeCause string
@ -130,7 +130,7 @@ func (o *ResourcesOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args
o.Mapper, o.Typer = f.Object()
o.UpdatePodSpecForObject = f.UpdatePodSpecForObject
o.Encoder = f.JSONEncoder()
o.ShortOutput = cmdutil.GetFlagString(cmd, "output") == "name"
o.Output = cmdutil.GetFlagString(cmd, "output")
o.Record = cmdutil.GetRecordFlag(cmd)
o.Local = cmdutil.GetFlagBool(cmd, "local")
o.ChangeCause = f.Command(cmd, false)
@ -254,7 +254,12 @@ func (o *ResourcesOptions) Run() error {
}
}
info.Refresh(obj, true)
cmdutil.PrintSuccess(o.Mapper, o.ShortOutput, o.Out, info.Mapping.Resource, info.Name, false, "resource requirements updated")
shortOutput := o.Output == "name"
if len(o.Output) > 0 && !shortOutput {
return o.PrintObject(o.Cmd, o.Local, o.Mapper, info.Object, o.Out)
}
cmdutil.PrintSuccess(o.Mapper, shortOutput, o.Out, info.Mapping.Resource, info.Name, false, "resource requirements updated")
}
return utilerrors.NewAggregate(allErrs)
}

View File

@ -44,6 +44,7 @@ type SelectorOptions struct {
all bool
record bool
changeCause string
output string
resources []string
selector *metav1.LabelSelector
@ -107,6 +108,7 @@ func (o *SelectorOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args [
o.all = cmdutil.GetFlagBool(cmd, "all")
o.record = cmdutil.GetRecordFlag(cmd)
o.dryrun = cmdutil.GetDryRunFlag(cmd)
o.output = cmdutil.GetFlagString(cmd, "output")
cmdNamespace, enforceNamespace, err := f.DefaultNamespace()
if err != nil {
@ -206,7 +208,12 @@ func (o *SelectorOptions) RunSelector() error {
}
info.Refresh(patched, true)
cmdutil.PrintSuccess(o.mapper, false, o.out, info.Mapping.Resource, info.Name, o.dryrun, "selector updated")
shortOutput := o.output == "name"
if len(o.output) > 0 && !shortOutput {
return o.PrintObject(info.Object)
}
cmdutil.PrintSuccess(o.mapper, shortOutput, o.out, info.Mapping.Resource, info.Name, o.dryrun, "selector updated")
return nil
})
}

View File

@ -65,7 +65,7 @@ type SubjectOptions struct {
Err io.Writer
Selector string
ContainerSelector string
ShortOutput bool
Output string
All bool
DryRun bool
Local bool
@ -113,7 +113,7 @@ func (o *SubjectOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args []
o.Local = cmdutil.GetFlagBool(cmd, "local")
o.Mapper, o.Typer = f.Object()
o.Encoder = f.JSONEncoder()
o.ShortOutput = cmdutil.GetFlagString(cmd, "output") == "name"
o.Output = cmdutil.GetFlagString(cmd, "output")
o.DryRun = cmdutil.GetDryRunFlag(cmd)
o.PrintObject = func(mapper meta.RESTMapper, obj runtime.Object, out io.Writer) error {
return f.PrintObject(cmd, o.Local, mapper, obj, out)
@ -248,7 +248,11 @@ func (o *SubjectOptions) Run(f cmdutil.Factory, fn updateSubjects) error {
}
info.Refresh(obj, true)
cmdutil.PrintSuccess(o.Mapper, o.ShortOutput, o.Out, info.Mapping.Resource, info.Name, false, "subjects updated")
shortOutput := o.Output == "name"
if len(o.Output) > 0 && !shortOutput {
return o.PrintObject(o.Mapper, info.Object, o.Out)
}
cmdutil.PrintSuccess(o.Mapper, shortOutput, o.Out, info.Mapping.Resource, info.Name, false, "subjects updated")
}
return utilerrors.NewAggregate(allErrs)
}