forked from openshift/library-go
/
json_patch_helpers.go
33 lines (30 loc) · 1.03 KB
/
json_patch_helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package resourceapply
import (
"fmt"
patch "github.com/evanphx/json-patch"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
)
// JSONPatch generates a JSON patch between original and modified objects and return the JSON as a string.
// In case of error, the returned string will contain the error messages.
func JSONPatch(original, modified runtime.Object) string {
if original == nil {
return "original object is nil"
}
if modified == nil {
return "modified object is nil"
}
originalJSON, err := runtime.Encode(unstructured.UnstructuredJSONScheme, original)
if err != nil {
return fmt.Sprintf("unable to decode original to JSON: %v", err)
}
modifiedJSON, err := runtime.Encode(unstructured.UnstructuredJSONScheme, modified)
if err != nil {
return fmt.Sprintf("unable to decode modified to JSON: %v", err)
}
patchBytes, err := patch.CreateMergePatch(originalJSON, modifiedJSON)
if err != nil {
return fmt.Sprintf("unable to create JSON patch: %v", err)
}
return string(patchBytes)
}