update patch to work with --local and avoid extra requests

pull/8/head
David Eads 2018-08-14 15:53:47 -04:00
parent d0439d417b
commit 562209d632
1 changed files with 11 additions and 28 deletions

View File

@ -193,6 +193,7 @@ func (o *PatchOptions) RunPatch() error {
r := o.builder.
Unstructured().
ContinueOnError().
LocalParam(o.Local).
NamespaceParam(o.namespace).DefaultNamespace().
FilenameParam(o.enforceNamespace, &o.FilenameOptions).
ResourceTypeOrNameArgs(false, o.args...).
@ -208,14 +209,16 @@ func (o *PatchOptions) RunPatch() error {
if err != nil {
return err
}
count++
name, namespace := info.Name, info.Namespace
mapping := info.ResourceMapping()
client, err := o.unstructuredClientForMapping(mapping)
if err != nil {
return err
}
if !o.Local && !o.dryRun {
mapping := info.ResourceMapping()
client, err := o.unstructuredClientForMapping(mapping)
if err != nil {
return err
}
helper := resource.NewHelper(client, mapping)
patchedObj, err := helper.Patch(namespace, name, patchType, patchBytes)
if err != nil {
@ -234,29 +237,20 @@ func (o *PatchOptions) RunPatch() error {
patchedObj = recordedObj
}
}
count++
// After computing whether we changed data, refresh the resource info with the resulting object
if err := info.Refresh(patchedObj, true); err != nil {
return err
}
printer, err := o.ToPrinter(patchOperation(didPatch))
if err != nil {
return err
}
return printer.PrintObj(info.Object, o.Out)
return printer.PrintObj(patchedObj, o.Out)
}
count++
originalObjJS, err := runtime.Encode(unstructured.UnstructuredJSONScheme, info.Object)
if err != nil {
return err
}
originalPatchedObjJS, err := getPatchedJSON(patchType, originalObjJS, patchBytes, mapping.GroupVersionKind, scheme.Scheme)
originalPatchedObjJS, err := getPatchedJSON(patchType, originalObjJS, patchBytes, info.Object.GetObjectKind().GroupVersionKind(), scheme.Scheme)
if err != nil {
return err
}
@ -267,22 +261,11 @@ func (o *PatchOptions) RunPatch() error {
}
didPatch := !reflect.DeepEqual(info.Object, targetObj)
// TODO: if we ever want to go generic, this allows a clean -o yaml without trying to print columns or anything
// rawExtension := &runtime.Unknown{
// Raw: originalPatchedObjJS,
// }
if didPatch {
if err := info.Refresh(targetObj, true); err != nil {
return err
}
}
printer, err := o.ToPrinter(patchOperation(didPatch))
if err != nil {
return err
}
return printer.PrintObj(info.Object, o.Out)
return printer.PrintObj(targetObj, o.Out)
})
if err != nil {
return err