Skip to content

Commit

Permalink
Use github.com/evanphx/json-patch/v5 for patching (#388)
Browse files Browse the repository at this point in the history
Normalize patching on a single library rather than a mix of one lib to
generate patches and another to apply them.

Signed-off-by: Scott Andrews <andrewssc@vmware.com>
  • Loading branch information
scothis committed Jun 23, 2023
1 parent f33ddef commit 4e79a24
Showing 1 changed file with 4 additions and 13 deletions.
17 changes: 4 additions & 13 deletions reconcilers/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
"encoding/json"
"errors"

jsonmergepatch "github.com/evanphx/json-patch/v5"
jsonpatch "gomodules.xyz/jsonpatch/v2"
jsonpatch "github.com/evanphx/json-patch/v5"
"sigs.k8s.io/controller-runtime/pkg/client"
)

Expand All @@ -23,18 +22,14 @@ func NewPatch(base, update client.Object) (*Patch, error) {
if err != nil {
return nil, err
}
patch, err := jsonpatch.CreatePatch(baseBytes, updateBytes)
if err != nil {
return nil, err
}
patchBytes, err := json.Marshal(patch)
patch, err := jsonpatch.CreateMergePatch(baseBytes, updateBytes)
if err != nil {
return nil, err
}

return &Patch{
generation: base.GetGeneration(),
bytes: patchBytes,
bytes: patch,
}, nil
}

Expand All @@ -54,11 +49,7 @@ func (p *Patch) Apply(rebase client.Object) error {
if err != nil {
return err
}
merge, err := jsonmergepatch.DecodePatch(p.bytes)
if err != nil {
return err
}
patchedBytes, err := merge.Apply(rebaseBytes)
patchedBytes, err := jsonpatch.MergePatch(rebaseBytes, p.bytes)
if err != nil {
return err
}
Expand Down

0 comments on commit 4e79a24

Please sign in to comment.