From 562209d63291cf55fe6eddbb5746776b2485d3c7 Mon Sep 17 00:00:00 2001 From: David Eads Date: Tue, 14 Aug 2018 15:53:47 -0400 Subject: [PATCH] update patch to work with --local and avoid extra requests --- pkg/kubectl/cmd/patch.go | 39 +++++++++++---------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/pkg/kubectl/cmd/patch.go b/pkg/kubectl/cmd/patch.go index 51e967aa11..4533ced3e7 100644 --- a/pkg/kubectl/cmd/patch.go +++ b/pkg/kubectl/cmd/patch.go @@ -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