From 03af5233bd00f5a9aa7540d5d69bc610eb040657 Mon Sep 17 00:00:00 2001 From: Janet Kuo Date: Thu, 8 Jun 2017 15:50:57 -0700 Subject: [PATCH] Make kubectl apply add change-cause before patching --- hack/make-rules/test-cmd-util.sh | 6 ++++-- pkg/kubectl/cmd/apply.go | 27 +++++++++++---------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/hack/make-rules/test-cmd-util.sh b/hack/make-rules/test-cmd-util.sh index e17cf4d7ff..97e1159686 100644 --- a/hack/make-rules/test-cmd-util.sh +++ b/hack/make-rules/test-cmd-util.sh @@ -2898,16 +2898,18 @@ run_daemonset_history_tests() { kube::test::get_object_assert daemonsets "{{range.items}}{{$id_field}}:{{end}}" '' # Command # Create a DaemonSet (revision 1) - kubectl apply -f hack/testdata/rollingupdate-daemonset.yaml "${kube_flags[@]}" + kubectl apply -f hack/testdata/rollingupdate-daemonset.yaml --record "${kube_flags[@]}" + kube::test::get_object_assert controllerrevisions "{{range.items}}{{$annotations_field}}:{{end}}" ".*rollingupdate-daemonset.yaml --record.*" # Rollback to revision 1 - should be no-op kubectl rollout undo daemonset --to-revision=1 "${kube_flags[@]}" kube::test::get_object_assert daemonset "{{range.items}}{{$daemonset_image_field0}}:{{end}}" "${IMAGE_DAEMONSET_R1}:" kube::test::get_object_assert daemonset "{{range.items}}{{$container_len}}{{end}}" "1" # Update the DaemonSet (revision 2) - kubectl apply -f hack/testdata/rollingupdate-daemonset-rv2.yaml "${kube_flags[@]}" + kubectl apply -f hack/testdata/rollingupdate-daemonset-rv2.yaml --record "${kube_flags[@]}" kube::test::wait_object_assert daemonset "{{range.items}}{{$daemonset_image_field0}}:{{end}}" "${IMAGE_DAEMONSET_R2}:" kube::test::wait_object_assert daemonset "{{range.items}}{{$daemonset_image_field1}}:{{end}}" "${IMAGE_DAEMONSET_R2_2}:" kube::test::get_object_assert daemonset "{{range.items}}{{$container_len}}{{end}}" "2" + kube::test::wait_object_assert controllerrevisions "{{range.items}}{{$annotations_field}}:{{end}}" ".*rollingupdate-daemonset-rv2.yaml --record.*" # Rollback to revision 1 with dry-run - should be no-op kubectl rollout undo daemonset --dry-run=true "${kube_flags[@]}" kube::test::get_object_assert daemonset "{{range.items}}{{$daemonset_image_field0}}:{{end}}" "${IMAGE_DAEMONSET_R2}:" diff --git a/pkg/kubectl/cmd/apply.go b/pkg/kubectl/cmd/apply.go index 7b8ccb76a9..8c8096e7ea 100644 --- a/pkg/kubectl/cmd/apply.go +++ b/pkg/kubectl/cmd/apply.go @@ -240,6 +240,17 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti visitedNamespaces.Insert(info.Namespace) } + // Add change-cause annotation to resource info if it should be recorded + if cmdutil.ShouldRecord(cmd, info) { + recordInObj := info.VersionedObject + if info.VersionedObject == nil { + recordInObj = info.Object + } + if err := cmdutil.RecordChangeCause(recordInObj, f.Command(cmd, false)); err != nil { + glog.V(4).Infof("error recording current command: %v", err) + } + } + // Get the modified configuration of the object. Embed the result // as an annotation in the modified configuration, so that it will appear // in the patch sent to the server. @@ -258,12 +269,6 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti return cmdutil.AddSourceToErr("creating", info.Source, err) } - if cmdutil.ShouldRecord(cmd, info) { - if err := cmdutil.RecordChangeCause(info.Object, f.Command(cmd, false)); err != nil { - return cmdutil.AddSourceToErr("creating", info.Source, err) - } - } - if !dryRun { // Then create the resource and skip the three-way merge if err := createAndRefresh(info); err != nil { @@ -313,16 +318,6 @@ func RunApply(f cmdutil.Factory, cmd *cobra.Command, out, errOut io.Writer, opti return cmdutil.AddSourceToErr(fmt.Sprintf("applying patch:\n%s\nto:\n%v\nfor:", patchBytes, info), info.Source, err) } - if cmdutil.ShouldRecord(cmd, info) { - if patch, patchType, err := cmdutil.ChangeResourcePatch(info, f.Command(cmd, true)); err == nil { - if recordedObject, err := helper.Patch(info.Namespace, info.Name, patchType, patch); err != nil { - glog.V(4).Infof("error recording reason: %v", err) - } else { - patchedObject = recordedObject - } - } - } - info.Refresh(patchedObject, true) if uid, err := info.Mapping.UID(info.Object); err != nil {