mirror of
https://github.com/k3s-io/k3s.git
synced 2024-06-07 19:41:36 +00:00
58 lines
1.4 KiB
Go
58 lines
1.4 KiB
Go
package patch
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
|
|
jsonpatch "github.com/evanphx/json-patch"
|
|
"k8s.io/apimachinery/pkg/types"
|
|
"k8s.io/apimachinery/pkg/util/strategicpatch"
|
|
)
|
|
|
|
func Apply(original, patch []byte) ([]byte, error) {
|
|
style, metadata, err := GetPatchStyle(original, patch)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
switch style {
|
|
case types.JSONPatchType:
|
|
return applyJSONPatch(original, patch)
|
|
case types.MergePatchType:
|
|
return applyMergePatch(original, patch)
|
|
case types.StrategicMergePatchType:
|
|
return applyStrategicMergePatch(original, patch, metadata)
|
|
default:
|
|
return nil, fmt.Errorf("invalid patch")
|
|
}
|
|
}
|
|
|
|
func applyStrategicMergePatch(original, patch []byte, lookup strategicpatch.LookupPatchMeta) ([]byte, error) {
|
|
originalMap := map[string]interface{}{}
|
|
patchMap := map[string]interface{}{}
|
|
if err := json.Unmarshal(original, &originalMap); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := json.Unmarshal(patch, &patchMap); err != nil {
|
|
return nil, err
|
|
}
|
|
patchedMap, err := strategicpatch.StrategicMergeMapPatchUsingLookupPatchMeta(originalMap, patchMap, lookup)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return json.Marshal(patchedMap)
|
|
}
|
|
|
|
func applyMergePatch(original, patch []byte) ([]byte, error) {
|
|
return jsonpatch.MergePatch(original, patch)
|
|
}
|
|
|
|
func applyJSONPatch(original, patch []byte) ([]byte, error) {
|
|
jsonPatch, err := jsonpatch.DecodePatch(patch)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return jsonPatch.Apply(original)
|
|
}
|