Skip to content

Commit

Permalink
Optimise ops diff
Browse files Browse the repository at this point in the history
Signed-off-by: Praveen Rewar <[email protected]>
  • Loading branch information
praveenrewar committed Jan 18, 2024
1 parent 9a4672c commit 436ba8c
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 36 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/cppforlife/cobrautil v0.0.0-20221130162803-acdfead391ef
github.com/cppforlife/color v1.9.1-0.20200716202919-6706ac40b835
github.com/cppforlife/go-cli-ui v0.0.0-20220425131040-94f26b16bc14
github.com/cppforlife/go-patch v0.2.0
github.com/cppforlife/go-patch v0.0.0-20240118020416-2147782e467b
github.com/google/go-cmp v0.5.9
github.com/hashicorp/go-version v1.6.0
github.com/k14s/difflib v0.0.0-20201117154628-0c031775bf57
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ github.com/cppforlife/color v1.9.1-0.20200716202919-6706ac40b835/go.mod h1:dYeVs
github.com/cppforlife/go-cli-ui v0.0.0-20200505234325-512793797f05/go.mod h1:I0qrzCmuPWYI6kAOvkllYjaW2aovclWbJ96+v+YyHb0=
github.com/cppforlife/go-cli-ui v0.0.0-20220425131040-94f26b16bc14 h1:MjRdR01xh0sfkeS3OOBv+MYkYsrbHuTDc4rfBnVdFaI=
github.com/cppforlife/go-cli-ui v0.0.0-20220425131040-94f26b16bc14/go.mod h1:AlgTssDlstr4mf92TR4DPITLfl5+7wEY4cKStCmeeto=
github.com/cppforlife/go-patch v0.2.0 h1:Y14MnCQjDlbw7WXT4k+u6DPAA9XnygN4BfrSpI/19RU=
github.com/cppforlife/go-patch v0.2.0/go.mod h1:67a7aIi94FHDZdoeGSJRRFDp66l9MhaAG1yGxpUoFD8=
github.com/cppforlife/go-patch v0.0.0-20240118020416-2147782e467b h1:+8LQctLhaj+63L/37l8IK/5Q3odN6RzWlglonUwrKok=
github.com/cppforlife/go-patch v0.0.0-20240118020416-2147782e467b/go.mod h1:67a7aIi94FHDZdoeGSJRRFDp66l9MhaAG1yGxpUoFD8=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand Down
32 changes: 1 addition & 31 deletions pkg/kapp/diff/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package diff
import (
"github.com/cppforlife/go-patch/patch"
ctlres "github.com/vmware-tanzu/carvel-kapp/pkg/kapp/resources"
"gopkg.in/yaml.v2"
)

type ChangeOp string
Expand Down Expand Up @@ -147,36 +146,7 @@ func (d *ChangeImpl) OpsDiff() OpsDiff {
}

func (d *ChangeImpl) calculateOpsDiff() OpsDiff {
var existingObj interface{}
var newObj interface{}

if d.existingRes != nil {
existingBytes, err := d.existingRes.AsYAMLBytes()
if err != nil {
panic("yamling existingRes") // TODO panic
}

err = yaml.Unmarshal(existingBytes, &existingObj)
if err != nil {
panic("unyamling existingRes") // TODO panic
}
}

if d.newRes != nil {
newBytes, err := d.newRes.AsYAMLBytes()
if err != nil {
panic("yamling newRes") // TODO panic
}

err = yaml.Unmarshal(newBytes, &newObj)
if err != nil {
panic("unyamling newRes") // TODO panic
}
} else if d.IsIgnored() {
newObj = existingObj // show as no changes
}

return OpsDiff(patch.Diff{Left: existingObj, Right: newObj}.Calculate())
return OpsDiff(patch.Diff{Left: d.existingRes.UnstructuredObject(), Right: d.newRes.UnstructuredObject()}.Calculate())
}

func (d *ChangeImpl) newResHasExistsAnnotation() bool {
Expand Down
4 changes: 4 additions & 0 deletions pkg/kapp/resources/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ type Resource interface {
MarkTransient(bool)
Transient() bool

UnstructuredObject() map[string]interface{}

unstructured() unstructured.Unstructured // private
unstructuredPtr() *unstructured.Unstructured // private
setUnstructured(unstructured.Unstructured) // private
Expand Down Expand Up @@ -280,6 +282,8 @@ func (r *ResourceImpl) Debug(title string) {
func (r *ResourceImpl) SetOrigin(origin string) { r.origin = origin }
func (r *ResourceImpl) Origin() string { return r.origin }

func (r *ResourceImpl) UnstructuredObject() map[string]interface{} { return r.un.Object }

func (r *ResourceImpl) unstructured() unstructured.Unstructured { return r.un }
func (r *ResourceImpl) unstructuredPtr() *unstructured.Unstructured { return &r.un }
func (r *ResourceImpl) setUnstructured(un unstructured.Unstructured) { r.un = un }
72 changes: 71 additions & 1 deletion vendor/github.com/cppforlife/go-patch/patch/diff.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ github.com/cppforlife/go-cli-ui/errors
github.com/cppforlife/go-cli-ui/ui
github.com/cppforlife/go-cli-ui/ui/table
github.com/cppforlife/go-cli-ui/ui/test
# github.com/cppforlife/go-patch v0.2.0
# github.com/cppforlife/go-patch v0.0.0-20240118020416-2147782e467b
## explicit
github.com/cppforlife/go-patch/patch
# github.com/davecgh/go-spew v1.1.1
Expand Down

0 comments on commit 436ba8c

Please sign in to comment.