From 3709ab43eaa0dea7411601989a7cae17a60f38d0 Mon Sep 17 00:00:00 2001 From: rcmadhankumar Date: Fri, 22 Sep 2023 10:59:51 +0530 Subject: [PATCH] fix --- pkg/kapp/clusterapply/add_or_update_change.go | 7 ++-- pkg/kapp/diff/resource_with_history.go | 34 ++++++++----------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/pkg/kapp/clusterapply/add_or_update_change.go b/pkg/kapp/clusterapply/add_or_update_change.go index a12e76f19..bdc53cb28 100644 --- a/pkg/kapp/clusterapply/add_or_update_change.go +++ b/pkg/kapp/clusterapply/add_or_update_change.go @@ -4,7 +4,6 @@ package clusterapply import ( - "encoding/json" "fmt" "time" @@ -254,7 +253,7 @@ func (c AddOrUpdateChange) recordAppliedResource(savedRes ctlres.Resource) error } // Record last applied change on the latest version of a resource - latestResWithHistoryUpdated, madeAnyModifications, err := latestResWithHistory.RecordLastAppliedResource(applyChange) + annotationStr, madeAnyModifications, err := latestResWithHistory.RecordLastAppliedResource(applyChange) if err != nil { return true, fmt.Errorf("Recording last applied resource: %w", err) } @@ -264,9 +263,7 @@ func (c AddOrUpdateChange) recordAppliedResource(savedRes ctlres.Resource) error return true, nil } - 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) + jsonStr := fmt.Sprintf("{\"metadata\": {\"annotations\": %s }}", annotationStr) data := []byte(jsonStr) _, err = c.identifiedResources.Patch(savedRes, types.MergePatchType, data) diff --git a/pkg/kapp/diff/resource_with_history.go b/pkg/kapp/diff/resource_with_history.go index 4fa960841..1dfc84739 100644 --- a/pkg/kapp/diff/resource_with_history.go +++ b/pkg/kapp/diff/resource_with_history.go @@ -4,6 +4,7 @@ package diff import ( + "encoding/json" "fmt" "os" @@ -68,13 +69,13 @@ func (r ResourceWithHistory) AllowsRecordingLastApplied() bool { return !found } -func (r ResourceWithHistory) RecordLastAppliedResource(appliedChange Change) (ctlres.Resource, bool, error) { +func (r ResourceWithHistory) RecordLastAppliedResource(appliedChange Change) (string, bool, error) { // Use compact representation to take as little space as possible // because annotation value max length is 262144 characters // (https://github.com/vmware-tanzu/carvel-kapp/issues/48). appliedResBytes, err := appliedChange.AppliedResource().AsCompactBytes() if err != nil { - return nil, true, err + return "", true, err } diff := appliedChange.OpsDiff() @@ -84,37 +85,30 @@ func (r ResourceWithHistory) RecordLastAppliedResource(appliedChange Change) (ct r.resource.Description(), diff.MinimalMD5(), diff.MinimalString()) } - annsMod := ctlres.StringMapAppendMod{ - ResourceMatcher: ctlres.AllMatcher{}, - Path: ctlres.NewPathFromStrings([]string{"metadata", "annotations"}), - KVs: map[string]string{ - AppliedResAnnKey: string(appliedResBytes), - AppliedResDiffMD5AnnKey: diff.MinimalMD5(), + annsKVS := map[string]string{ + AppliedResAnnKey: string(appliedResBytes), + AppliedResDiffMD5AnnKey: diff.MinimalMD5(), - // Following fields useful for debugging: - // debugAppliedResDiffAnnKey: diff.MinimalString(), - // debugAppliedResDiffFullAnnKey: diff.FullString(), - }, + // Following fields useful for debugging: + // debugAppliedResDiffAnnKey: diff.MinimalString(), + // debugAppliedResDiffFullAnnKey: diff.FullString(), } const annValMaxLen = 262144 // kapp deploy should work without adding disable annotation when annotation value max length exceed // (https://github.com/vmware-tanzu/carvel-kapp/issues/410) - for _, annVal := range annsMod.KVs { + for _, annVal := range annsKVS { if len(annVal) > annValMaxLen { - return nil, false, nil + return "", false, nil } } - resultRes := r.resource.DeepCopy() - - err = annsMod.Apply(resultRes) + result, err := json.Marshal(annsKVS) if err != nil { - return nil, true, err + return "", false, err } - - return resultRes, true, nil + return string(result), true, nil } func (r ResourceWithHistory) CalculateChange(appliedRes ctlres.Resource) (Change, error) {