From c88c081f472be1665214133a533c083b1c0bce8a Mon Sep 17 00:00:00 2001 From: wxdao Date: Wed, 14 Jul 2021 01:35:08 +0800 Subject: [PATCH] Add preconditions Signed-off-by: wxdao --- pkg/kube/client.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/kube/client.go b/pkg/kube/client.go index 253f180b6..8c0a79102 100644 --- a/pkg/kube/client.go +++ b/pkg/kube/client.go @@ -41,6 +41,7 @@ import ( "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/jsonmergepatch" + "k8s.io/apimachinery/pkg/util/mergepatch" "k8s.io/apimachinery/pkg/util/strategicpatch" "k8s.io/apimachinery/pkg/watch" "k8s.io/cli-runtime/pkg/genericclioptions" @@ -454,7 +455,13 @@ func createPatch(target *resource.Info, current runtime.Object) ([]byte, types.P if isUnstructured || isCRD { // fall back to generic JSON merge patch - patch, err := jsonmergepatch.CreateThreeWayJSONMergePatch(oldData, newData, currentData) + // from https://github.com/kubernetes/kubectl/blob/b83b2ec7d15f286720bccf7872b5c72372cb8e80/pkg/cmd/apply/patcher.go#L129 + preconditions := []mergepatch.PreconditionFunc{mergepatch.RequireKeyUnchanged("apiVersion"), + mergepatch.RequireKeyUnchanged("kind"), mergepatch.RequireMetadataKeyUnchanged("name")} + patch, err := jsonmergepatch.CreateThreeWayJSONMergePatch(oldData, newData, currentData, preconditions...) + if err != nil && mergepatch.IsPreconditionFailed(err) { + err = fmt.Errorf("%s", "At least one of apiVersion, kind and name was changed") + } return patch, types.MergePatchType, err }