From 5b06bfe938c74e765a91fce4fbe8e59ae5aa6b8a Mon Sep 17 00:00:00 2001 From: Dennis Marttinen Date: Wed, 14 Aug 2019 19:32:34 +0300 Subject: [PATCH] Re-encode StrategicMergePatch with the serializer for correct formatting --- pkg/util/patch/patch.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/pkg/util/patch/patch.go b/pkg/util/patch/patch.go index 5eb43466e..e5594ac25 100644 --- a/pkg/util/patch/patch.go +++ b/pkg/util/patch/patch.go @@ -6,6 +6,7 @@ import ( "github.com/weaveworks/ignite/pkg/apis/ignite/scheme" meta "github.com/weaveworks/ignite/pkg/apis/meta/v1alpha1" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/util/strategicpatch" ) @@ -51,7 +52,12 @@ func Apply(original, patch []byte, gvk schema.GroupVersionKind) ([]byte, error) return nil, err } - return strategicpatch.StrategicMergePatch(original, patch, emptyobj) + b, err := strategicpatch.StrategicMergePatch(original, patch, emptyobj) + if err != nil { + return nil, err + } + + return serializerEncode(b) } func ApplyOnFile(filePath string, patch []byte, gvk schema.GroupVersionKind) error { @@ -67,3 +73,15 @@ func ApplyOnFile(filePath string, patch []byte, gvk schema.GroupVersionKind) err return ioutil.WriteFile(filePath, newContent, 0644) } + +// StrategicMergePatch returns an unindented, unorganized JSON byte slice, +// this helper takes that as an input and returns the same JSON re-encoded +// with the serializer so it conforms to a runtime.Object +func serializerEncode(input []byte) (result []byte, err error) { + var obj runtime.Object + if obj, err = serializer.Decode(input, true); err == nil { + result, err = serializer.EncodeJSON(obj) + } + + return +}