Merge pull request #36148 from kargakis/edit-list

Automatic merge from submit-queue

kubectl: make edit work with lists again

@kubernetes/kubectl this is fixing https://github.com/kubernetes/kubernetes/issues/20519 and slightly changes the behavior of --recursive when the directory that is being edited has files with errors. Previously since `edit` was working on an object basis, bad objects would be skipped and the editor would load the next object. We want to load multiple objects in the same list and it's impossible to load invalid objects in a list so --recursive will not work if there is any error in the directory. I think this is an acceptable trade-off.

Review here: https://github.com/kubernetes/kubernetes/pull/36148/files?w=1
This commit is contained in:
Kubernetes Submit Queue 2016-11-03 17:27:13 -07:00 committed by GitHub
commit 6ac5887e8a
2 changed files with 160 additions and 157 deletions

View File

@ -865,6 +865,7 @@ __EOF__
[ "$(EDITOR=cat kubectl edit pod/valid-pod | grep 'name: valid-pod')" ] [ "$(EDITOR=cat kubectl edit pod/valid-pod | grep 'name: valid-pod')" ]
[ "$(EDITOR=cat kubectl edit --windows-line-endings pod/valid-pod | file - | grep CRLF)" ] [ "$(EDITOR=cat kubectl edit --windows-line-endings pod/valid-pod | file - | grep CRLF)" ]
[ ! "$(EDITOR=cat kubectl edit --windows-line-endings=false pod/valid-pod | file - | grep CRLF)" ] [ ! "$(EDITOR=cat kubectl edit --windows-line-endings=false pod/valid-pod | file - | grep CRLF)" ]
[ "$(EDITOR=cat kubectl edit ns | grep 'kind: List')" ]
### Label POD YAML file locally without effecting the live pod. ### Label POD YAML file locally without effecting the live pod.
# Pre-condition: name is valid-pod # Pre-condition: name is valid-pod
@ -1377,8 +1378,10 @@ __EOF__
echo -e '#!/bin/bash\nsed -i "s/image: busybox/image: prom\/busybox/g" $1' > /tmp/tmp-editor.sh echo -e '#!/bin/bash\nsed -i "s/image: busybox/image: prom\/busybox/g" $1' > /tmp/tmp-editor.sh
chmod +x /tmp/tmp-editor.sh chmod +x /tmp/tmp-editor.sh
output_message=$(! EDITOR=/tmp/tmp-editor.sh kubectl edit -f hack/testdata/recursive/pod --recursive 2>&1 "${kube_flags[@]}") output_message=$(! EDITOR=/tmp/tmp-editor.sh kubectl edit -f hack/testdata/recursive/pod --recursive 2>&1 "${kube_flags[@]}")
# Post-condition: busybox0 & busybox1 PODs are edited, and since busybox2 is malformed, it should error # Post-condition: busybox0 & busybox1 PODs are not edited, and since busybox2 is malformed, it should error
kube::test::get_object_assert pods "{{range.items}}{{$image_field}}:{{end}}" 'prom/busybox:prom/busybox:' # The reason why busybox0 & busybox1 PODs are not edited is because the editor tries to load all objects in
# a list but since it contains invalid objects, it will never open.
kube::test::get_object_assert pods "{{range.items}}{{$image_field}}:{{end}}" 'busybox:busybox:'
kube::test::if_has_string "${output_message}" "Object 'Kind' is missing" kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
# cleaning # cleaning
rm /tmp/tmp-editor.sh rm /tmp/tmp-editor.sh

View File

@ -143,12 +143,16 @@ func runEdit(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args
return err return err
} }
infos, err := r.Infos()
if err != nil {
return err
}
var ( var (
windowsLineEndings = cmdutil.GetFlagBool(cmd, "windows-line-endings") windowsLineEndings = cmdutil.GetFlagBool(cmd, "windows-line-endings")
edit = editor.NewDefaultEditor(f.EditorEnvs()) edit = editor.NewDefaultEditor(f.EditorEnvs())
) )
err = r.Visit(func(info *resource.Info, err error) error {
var ( var (
results = editResults{} results = editResults{}
original = []byte{} original = []byte{}
@ -157,9 +161,7 @@ func runEdit(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args
) )
containsError := false containsError := false
for { for {
infos := []*resource.Info{info}
originalObj, err := resource.AsVersionedObject(infos, false, defaultVersion, encoder) originalObj, err := resource.AsVersionedObject(infos, false, defaultVersion, encoder)
if err != nil { if err != nil {
return err return err
@ -224,7 +226,7 @@ func runEdit(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args
file: file, file: file,
} }
containsError = true containsError = true
fmt.Fprintln(out, results.addError(errors.NewInvalid(api.Kind(""), "", field.ErrorList{field.Invalid(nil, "The edited file failed validation", fmt.Sprintf("%v", err))}), info)) fmt.Fprintln(out, results.addError(errors.NewInvalid(api.Kind(""), "", field.ErrorList{field.Invalid(nil, "The edited file failed validation", fmt.Sprintf("%v", err))}), infos[0]))
continue continue
} }
@ -316,8 +318,6 @@ func runEdit(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args
containsError = true containsError = true
} }
} }
})
return err
} }
func getPrinter(cmd *cobra.Command) (*editPrinterOptions, error) { func getPrinter(cmd *cobra.Command) (*editPrinterOptions, error) {