Skip to content

Commit

Permalink
fix(app): use jsonpatch to check for changes in status field (argopro…
Browse files Browse the repository at this point in the history
…j#15126)

Signed-off-by: Vlad Fratila <[email protected]>
  • Loading branch information
vladfr committed Aug 25, 2023
1 parent e689618 commit 3f533d8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
5 changes: 3 additions & 2 deletions controller/appcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -1620,9 +1620,10 @@ func (ctrl *ApplicationController) persistAppStatus(orig *appv1.Application, new
}
delete(newAnnotations, appv1.AnnotationKeyRefresh)
}
patch, modified, err := diff.CreateTwoWayMergePatch(

patch, modified, err := argodiff.jsonMergePatch(
&appv1.Application{ObjectMeta: metav1.ObjectMeta{Annotations: orig.GetAnnotations()}, Status: orig.Status},
&appv1.Application{ObjectMeta: metav1.ObjectMeta{Annotations: newAnnotations}, Status: *newStatus}, appv1.Application{})
&appv1.Application{ObjectMeta: metav1.ObjectMeta{Annotations: newAnnotations}, Status: *newStatus})
if err != nil {
logCtx.Errorf("Error constructing app status patch: %v", err)
return
Expand Down
17 changes: 17 additions & 0 deletions util/argo/diff/diff.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package diff

import (
"encoding/json"
"fmt"

jsonpatch "github.com/evanphx/json-patch"
"github.com/go-logr/logr"

k8smanagedfields "k8s.io/apimachinery/pkg/util/managedfields"
Expand Down Expand Up @@ -386,3 +388,18 @@ func safeDeepCopy(obj *unstructured.Unstructured) *unstructured.Unstructured {
}
return obj.DeepCopy()
}

// jsonMergePatch is a wrapper func to calculate the diff between two objects
// instead of bytes.
func jsonMergePatch(orig, new interface{}) ([]byte, bool, error) {
origBytes, err := json.Marshal(orig)
if err != nil {
return nil, false, err
}
newBytes, err := json.Marshal(new)
if err != nil {
return nil, false, err
}
patch, err := jsonpatch.CreateMergePatch(origBytes, newBytes)
return patch, string(patch) != "{}", err
}

0 comments on commit 3f533d8

Please sign in to comment.