From 3482fd8e13029f8e9784c0b74187baf3ed324edf Mon Sep 17 00:00:00 2001 From: rcmadhankumar Date: Fri, 15 Sep 2023 16:42:46 +0530 Subject: [PATCH 1/2] Patch resource history annotations instead of update --- pkg/kapp/clusterapply/add_or_update_change.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/kapp/clusterapply/add_or_update_change.go b/pkg/kapp/clusterapply/add_or_update_change.go index c569b8d29..e77a9ef06 100644 --- a/pkg/kapp/clusterapply/add_or_update_change.go +++ b/pkg/kapp/clusterapply/add_or_update_change.go @@ -4,6 +4,7 @@ package clusterapply import ( + "encoding/json" "fmt" "time" @@ -12,6 +13,7 @@ import ( ctlres "github.com/vmware-tanzu/carvel-kapp/pkg/kapp/resources" "github.com/vmware-tanzu/carvel-kapp/pkg/kapp/util" "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/types" ) const ( @@ -262,7 +264,10 @@ func (c AddOrUpdateChange) recordAppliedResource(savedRes ctlres.Resource) error return true, nil } - _, err = c.identifiedResources.Update(latestResWithHistoryUpdated) + jsonStr, err := json.Marshal(latestResWithHistoryUpdated.Annotations()) + data := []byte(fmt.Sprintf("{\"metadata\": {\"annotations\": %s }}", jsonStr)) + + _, err = c.identifiedResources.Patch(savedRes, types.MergePatchType, data) if err != nil { latestResWithHistory = nil // Get again return false, fmt.Errorf("Saving record of last applied resource: %w", err) From 5d670e0a354e167f2188822ea015df6bc477f838 Mon Sep 17 00:00:00 2001 From: rcmadhankumar Date: Tue, 19 Sep 2023 16:56:44 +0530 Subject: [PATCH 2/2] fix --- pkg/kapp/clusterapply/add_or_update_change.go | 7 ++++--- pkg/kapp/diff/resource_with_history.go | 16 ++++++++-------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/pkg/kapp/clusterapply/add_or_update_change.go b/pkg/kapp/clusterapply/add_or_update_change.go index e77a9ef06..e6a55e62f 100644 --- a/pkg/kapp/clusterapply/add_or_update_change.go +++ b/pkg/kapp/clusterapply/add_or_update_change.go @@ -264,15 +264,16 @@ func (c AddOrUpdateChange) recordAppliedResource(savedRes ctlres.Resource) error return true, nil } - jsonStr, err := json.Marshal(latestResWithHistoryUpdated.Annotations()) - data := []byte(fmt.Sprintf("{\"metadata\": {\"annotations\": %s }}", jsonStr)) + latestResourceAnnotations := latestResWithHistoryUpdated.Annotations() + appliedResAnnValJsonStr, err := json.Marshal(latestResourceAnnotations[ctldiff.AppliedResAnnKey]) + jsonStr := fmt.Sprintf("{\"metadata\": {\"annotations\": { \"%s\" : \"%s\", \"%s\": %s}}}", ctldiff.AppliedResDiffMD5AnnKey, latestResourceAnnotations[ctldiff.AppliedResDiffMD5AnnKey], ctldiff.AppliedResAnnKey, appliedResAnnValJsonStr) + data := []byte(jsonStr) _, err = c.identifiedResources.Patch(savedRes, types.MergePatchType, data) if err != nil { latestResWithHistory = nil // Get again return false, fmt.Errorf("Saving record of last applied resource: %w", err) } - return true, nil }) } diff --git a/pkg/kapp/diff/resource_with_history.go b/pkg/kapp/diff/resource_with_history.go index 34115aa7f..4fa960841 100644 --- a/pkg/kapp/diff/resource_with_history.go +++ b/pkg/kapp/diff/resource_with_history.go @@ -11,8 +11,8 @@ import ( ) const ( - appliedResAnnKey = "kapp.k14s.io/original" - appliedResDiffMD5AnnKey = "kapp.k14s.io/original-diff-md5" + AppliedResAnnKey = "kapp.k14s.io/original" + AppliedResDiffMD5AnnKey = "kapp.k14s.io/original-diff-md5" // Following fields useful for debugging: debugAppliedResDiffAnnKey = "kapp.k14s.io/original-diff" @@ -88,8 +88,8 @@ func (r ResourceWithHistory) RecordLastAppliedResource(appliedChange Change) (ct ResourceMatcher: ctlres.AllMatcher{}, Path: ctlres.NewPathFromStrings([]string{"metadata", "annotations"}), KVs: map[string]string{ - appliedResAnnKey: string(appliedResBytes), - appliedResDiffMD5AnnKey: diff.MinimalMD5(), + AppliedResAnnKey: string(appliedResBytes), + AppliedResDiffMD5AnnKey: diff.MinimalMD5(), // Following fields useful for debugging: // debugAppliedResDiffAnnKey: diff.MinimalString(), @@ -131,8 +131,8 @@ func (r ResourceWithHistory) CalculateChange(appliedRes ctlres.Resource) (Change } func (r ResourceWithHistory) recalculateLastAppliedChange() ([]Change, string, string) { - lastAppliedResBytes := r.resource.Annotations()[appliedResAnnKey] - lastAppliedDiffMD5 := r.resource.Annotations()[appliedResDiffMD5AnnKey] + lastAppliedResBytes := r.resource.Annotations()[AppliedResAnnKey] + lastAppliedDiffMD5 := r.resource.Annotations()[AppliedResDiffMD5AnnKey] if len(lastAppliedResBytes) == 0 || len(lastAppliedDiffMD5) == 0 { return nil, "", "" @@ -197,7 +197,7 @@ func (ResourceWithoutHistory) removeAppliedResAnnKeysMods() []ctlres.ResourceMod return []ctlres.ResourceMod{ ctlres.FieldRemoveMod{ ResourceMatcher: ctlres.AllMatcher{}, - Path: ctlres.NewPathFromStrings([]string{"metadata", "annotations", appliedResAnnKey}), + Path: ctlres.NewPathFromStrings([]string{"metadata", "annotations", AppliedResAnnKey}), }, ctlres.FieldRemoveMod{ ResourceMatcher: ctlres.AllMatcher{}, @@ -205,7 +205,7 @@ func (ResourceWithoutHistory) removeAppliedResAnnKeysMods() []ctlres.ResourceMod }, ctlres.FieldRemoveMod{ ResourceMatcher: ctlres.AllMatcher{}, - Path: ctlres.NewPathFromStrings([]string{"metadata", "annotations", appliedResDiffMD5AnnKey}), + Path: ctlres.NewPathFromStrings([]string{"metadata", "annotations", AppliedResDiffMD5AnnKey}), }, ctlres.FieldRemoveMod{ ResourceMatcher: ctlres.AllMatcher{},