mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
Merge pull request #25085 from metral/recursive-edit
Automatic merge from submit-queue enable recursive processing in kubectl edit This PR was split out of https://github.com/kubernetes/kubernetes/pull/23673 per @deads2k's suggestion: https://github.com/kubernetes/kubernetes/pull/23673#discussion_r61291178 It makes use of the recursive processing of a directory in `kubectl edit`
This commit is contained in:
commit
7c355e18a7
@ -1026,6 +1026,19 @@ __EOF__
|
|||||||
kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
|
kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
|
||||||
kube::test::if_has_string "${output_message}" 'error validating data: kind not set'
|
kube::test::if_has_string "${output_message}" 'error validating data: kind not set'
|
||||||
|
|
||||||
|
## Edit multiple busybox PODs by updating the image field of multiple PODs recursively from a directory. tmp-editor.sh is a fake editor
|
||||||
|
# Pre-condition: busybox0 & busybox1 PODs exist
|
||||||
|
kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
|
||||||
|
# Command
|
||||||
|
echo -e '#!/bin/bash\nsed -i "s/image: busybox/image: prom\/busybox/g" $1' > /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[@]}")
|
||||||
|
# Post-condition: busybox0 & busybox1 PODs are edited, and since busybox2 is malformed, it should error
|
||||||
|
kube::test::get_object_assert pods "{{range.items}}{{$image_field}}:{{end}}" 'prom/busybox:prom/busybox:'
|
||||||
|
kube::test::if_has_string "${output_message}" "Object 'Kind' is missing"
|
||||||
|
# cleaning
|
||||||
|
rm /tmp/tmp-editor.sh
|
||||||
|
|
||||||
## Replace multiple busybox PODs recursively from directory of YAML files
|
## Replace multiple busybox PODs recursively from directory of YAML files
|
||||||
# Pre-condition: busybox0 & busybox1 PODs exist
|
# Pre-condition: busybox0 & busybox1 PODs exist
|
||||||
kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
|
kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'busybox0:busybox1:'
|
||||||
|
@ -161,6 +161,7 @@ func RunEdit(f *cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args
|
|||||||
NamespaceParam(cmdNamespace).DefaultNamespace().
|
NamespaceParam(cmdNamespace).DefaultNamespace().
|
||||||
FilenameParam(enforceNamespace, options.Recursive, options.Filenames...).
|
FilenameParam(enforceNamespace, options.Recursive, options.Filenames...).
|
||||||
ResourceTypeOrNameArgs(true, args...).
|
ResourceTypeOrNameArgs(true, args...).
|
||||||
|
ContinueOnError().
|
||||||
Flatten().
|
Flatten().
|
||||||
Latest().
|
Latest().
|
||||||
Do()
|
Do()
|
||||||
@ -169,11 +170,6 @@ 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
|
|
||||||
}
|
|
||||||
|
|
||||||
clientConfig, err := f.ClientConfig()
|
clientConfig, err := f.ClientConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -184,14 +180,14 @@ func RunEdit(f *cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
originalObj, err := resource.AsVersionedObject(infos, false, defaultVersion, encoder)
|
|
||||||
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 (
|
||||||
results = editResults{}
|
results = editResults{}
|
||||||
original = []byte{}
|
original = []byte{}
|
||||||
edited = []byte{}
|
edited = []byte{}
|
||||||
@ -201,22 +197,23 @@ func RunEdit(f *cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args
|
|||||||
containsError := false
|
containsError := false
|
||||||
|
|
||||||
for {
|
for {
|
||||||
// infos mutates over time to be the list of things we've tried and failed to edit
|
infos := []*resource.Info{info}
|
||||||
// this means that our overall list changes over time.
|
originalObj, err := resource.AsVersionedObject(infos, false, defaultVersion, encoder)
|
||||||
objToEdit, err := resource.AsVersionedObject(infos, false, defaultVersion, encoder)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
objToEdit := originalObj
|
||||||
|
|
||||||
// generate the file to edit
|
// generate the file to edit
|
||||||
buf := &bytes.Buffer{}
|
buf := &bytes.Buffer{}
|
||||||
var w io.Writer = buf
|
var w io.Writer = buf
|
||||||
if windowsLineEndings {
|
if windowsLineEndings {
|
||||||
w = crlf.NewCRLFWriter(w)
|
w = crlf.NewCRLFWriter(w)
|
||||||
}
|
}
|
||||||
if err := results.header.writeTo(w); err != nil {
|
|
||||||
return preservedFile(err, results.file, errOut)
|
results.header.writeTo(w)
|
||||||
}
|
|
||||||
if !containsError {
|
if !containsError {
|
||||||
if err := printer.PrintObj(objToEdit, w); err != nil {
|
if err := printer.PrintObj(objToEdit, w); err != nil {
|
||||||
return preservedFile(err, results.file, errOut)
|
return preservedFile(err, results.file, errOut)
|
||||||
@ -432,6 +429,8 @@ func RunEdit(f *cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args
|
|||||||
// loop again and edit the remaining items
|
// loop again and edit the remaining items
|
||||||
infos = results.edit
|
infos = results.edit
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// editReason preserves a message about the reason this file must be edited again
|
// editReason preserves a message about the reason this file must be edited again
|
||||||
|
Loading…
Reference in New Issue
Block a user