-
Notifications
You must be signed in to change notification settings - Fork 38.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Strip .meta.managedFields for kubectl edit command #91946
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ import ( | |
corev1 "k8s.io/api/core/v1" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
@@ -72,6 +73,8 @@ type EditOptions struct { | |
ApplyAnnotation bool | ||
ChangeCause string | ||
|
||
managedFields map[types.UID][]metav1.ManagedFieldsEntry | ||
|
||
genericclioptions.IOStreams | ||
|
||
Recorder genericclioptions.Recorder | ||
|
@@ -262,6 +265,10 @@ func (o *EditOptions) Run() error { | |
} | ||
|
||
if !containsError { | ||
if err := o.extractManagedFields(originalObj); err != nil { | ||
return preservedFile(err, results.file, o.ErrOut) | ||
} | ||
|
||
if err := o.editPrinterOptions.PrintObj(originalObj, w); err != nil { | ||
return preservedFile(err, results.file, o.ErrOut) | ||
} | ||
|
@@ -279,6 +286,7 @@ func (o *EditOptions) Run() error { | |
if err != nil { | ||
return preservedFile(err, results.file, o.ErrOut) | ||
} | ||
|
||
// If we're retrying the loop because of an error, and no change was made in the file, short-circuit | ||
if containsError && bytes.Equal(cmdutil.StripComments(editedDiff), cmdutil.StripComments(edited)) { | ||
return preservedFile(fmt.Errorf("%s", "Edit cancelled, no valid changes were saved."), file, o.ErrOut) | ||
|
@@ -334,10 +342,19 @@ func (o *EditOptions) Run() error { | |
results.header.reasons = append(results.header.reasons, editReason{head: fmt.Sprintf("The edited file had a syntax error: %v", err)}) | ||
continue | ||
} | ||
|
||
// not a syntax error as it turns out... | ||
containsError = false | ||
updatedVisitor := resource.InfoListVisitor(updatedInfos) | ||
|
||
// we need to add back managedFields to both updated and original object | ||
if err := o.restoreManagedFields(updatedInfos); err != nil { | ||
return preservedFile(err, file, o.ErrOut) | ||
} | ||
if err := o.restoreManagedFields(infos); err != nil { | ||
return preservedFile(err, file, o.ErrOut) | ||
} | ||
|
||
// need to make sure the original namespace wasn't changed while editing | ||
if err := updatedVisitor.Visit(resource.RequireNamespace(o.CmdNamespace)); err != nil { | ||
return preservedFile(err, file, o.ErrOut) | ||
|
@@ -437,6 +454,49 @@ func (o *EditOptions) Run() error { | |
} | ||
} | ||
|
||
func (o *EditOptions) extractManagedFields(obj runtime.Object) error { | ||
o.managedFields = make(map[types.UID][]metav1.ManagedFieldsEntry) | ||
if meta.IsListType(obj) { | ||
err := meta.EachListItem(obj, func(obj runtime.Object) error { | ||
uid, mf, err := clearManagedFields(obj) | ||
if err != nil { | ||
return err | ||
} | ||
o.managedFields[uid] = mf | ||
return nil | ||
}) | ||
return err | ||
} | ||
uid, mf, err := clearManagedFields(obj) | ||
if err != nil { | ||
return err | ||
} | ||
o.managedFields[uid] = mf | ||
return nil | ||
} | ||
|
||
func clearManagedFields(obj runtime.Object) (types.UID, []metav1.ManagedFieldsEntry, error) { | ||
metaObjs, err := meta.Accessor(obj) | ||
if err != nil { | ||
return "", nil, err | ||
} | ||
mf := metaObjs.GetManagedFields() | ||
metaObjs.SetManagedFields(nil) | ||
return metaObjs.GetUID(), mf, nil | ||
} | ||
|
||
func (o *EditOptions) restoreManagedFields(infos []*resource.Info) error { | ||
for _, info := range infos { | ||
metaObjs, err := meta.Accessor(info.Object) | ||
if err != nil { | ||
return err | ||
} | ||
mf := o.managedFields[metaObjs.GetUID()] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I recommend detecting if the user tried to add something here and failing with a message if so, e.g. "you can't use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, there's a |
||
metaObjs.SetManagedFields(mf) | ||
} | ||
return nil | ||
} | ||
|
||
func (o *EditOptions) visitToApplyEditPatch(originalInfos []*resource.Info, patchVisitor resource.Visitor) error { | ||
err := patchVisitor.Visit(func(info *resource.Info, incomingErr error) error { | ||
editObjUID, err := meta.NewAccessor().UID(info.Object) | ||
|
@@ -590,6 +650,7 @@ func (o *EditOptions) visitToPatch(originalInfos []*resource.Info, patchVisitor | |
mergepatch.RequireKeyUnchanged("apiVersion"), | ||
mergepatch.RequireKeyUnchanged("kind"), | ||
mergepatch.RequireMetadataKeyUnchanged("name"), | ||
mergepatch.RequireKeyUnchanged("managedFields"), | ||
} | ||
|
||
// Create the versioned struct from the type defined in the mapping | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A test for extract / clear / restore would be good?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done