jsonpatch 详见: https://github.com/chinaran/jsonpatch
K8s Patch在开发 controller/operator 时,经常会对 CR 资源进行更新操作,
可使用 update/apply 进行更新,但有可能会导致冲突 (Conflict)。
如果只是小范围更新,推荐使用 patch 操作。
kubectl patch 可参考官方示例: https://kubernetes.io/docs/reference/kubectl/cheatsheet/#patching-resources
所以基于 https://github.com/mattbaird/jsonpatch,封装了一个更易用的 jsonpatch 库,目前够用。
引用go get -u github.com/chinaran/jsonpatch
示例controller client 可用 client.RawPatch(types.JSONPatchType, patchData)
dynamic client, client set 等参考如下示例:
import ( "context" "github.com/chinaran/jsonpatch" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/dynamic")func JSONPatchResource(dyclient dynamic.NamespaceableResourceInterface) (*unstructured.Unstructured, error) { patchValues := []*jsonpatch.Operation{ // patch annotations jsonpatch.NewAnnotationOperation(jsonpatch.OpReplace, "key-1", "value-1"), // patch labels jsonpatch.NewLabelOperation(jsonpatch.OpReplace, "key-2", "value-2"), // add patch jsonpatch.NewAddOperation("/spec/arrayx/0", "value-3"), // replace patch jsonpatch.NewReplaceOperation("/spec/arrayx", []string{"value-3", "value-4"}), /// remove patch jsonpatch.NewRemoveOperation("/spec/arrayx/0"), } patchBytes, err := jsonpatch.NewOperationData(patchValues...) if err != nil { return nil, err } return dyclient.Namespace("test-ns").Patch(context.TODO(), "test-name", types.JSONPatchType, patchBytes, metav1.PatchOptions{})}