Skip to content

Commit 670bae2

Browse files
committed
Patch resource history annotations instead of update
Signed-off-by: rcmadhankumar <rcmadhankumar@gmail.com>
1 parent 0f793e1 commit 670bae2

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

pkg/kapp/clusterapply/add_or_update_change.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
ctlres "github.com/vmware-tanzu/carvel-kapp/pkg/kapp/resources"
1313
"github.com/vmware-tanzu/carvel-kapp/pkg/kapp/util"
1414
"k8s.io/apimachinery/pkg/api/errors"
15+
"k8s.io/apimachinery/pkg/types"
1516
)
1617

1718
const (
@@ -252,7 +253,7 @@ func (c AddOrUpdateChange) recordAppliedResource(savedRes ctlres.Resource) error
252253
}
253254

254255
// Record last applied change on the latest version of a resource
255-
latestResWithHistoryUpdated, madeAnyModifications, err := latestResWithHistory.RecordLastAppliedResource(applyChange)
256+
annotationStr, madeAnyModifications, err := latestResWithHistory.RecordLastAppliedResource(applyChange)
256257
if err != nil {
257258
return true, fmt.Errorf("Recording last applied resource: %w", err)
258259
}
@@ -262,12 +263,14 @@ func (c AddOrUpdateChange) recordAppliedResource(savedRes ctlres.Resource) error
262263
return true, nil
263264
}
264265

265-
_, err = c.identifiedResources.Update(latestResWithHistoryUpdated)
266+
jsonStr := fmt.Sprintf("{\"metadata\": {\"annotations\": %s }}", annotationStr)
267+
data := []byte(jsonStr)
268+
269+
_, err = c.identifiedResources.Patch(savedRes, types.MergePatchType, data)
266270
if err != nil {
267271
latestResWithHistory = nil // Get again
268272
return false, fmt.Errorf("Saving record of last applied resource: %w", err)
269273
}
270-
271274
return true, nil
272275
})
273276
}

pkg/kapp/diff/resource_with_history.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package diff
55

66
import (
7+
"encoding/json"
78
"fmt"
89
"os"
910

@@ -68,13 +69,13 @@ func (r ResourceWithHistory) AllowsRecordingLastApplied() bool {
6869
return !found
6970
}
7071

71-
func (r ResourceWithHistory) RecordLastAppliedResource(appliedChange Change) (ctlres.Resource, bool, error) {
72+
func (r ResourceWithHistory) RecordLastAppliedResource(appliedChange Change) (string, bool, error) {
7273
// Use compact representation to take as little space as possible
7374
// because annotation value max length is 262144 characters
7475
// (https://github.com/vmware-tanzu/carvel-kapp/issues/48).
7576
appliedResBytes, err := appliedChange.AppliedResource().AsCompactBytes()
7677
if err != nil {
77-
return nil, true, err
78+
return "", true, err
7879
}
7980

8081
diff := appliedChange.OpsDiff()
@@ -84,37 +85,30 @@ func (r ResourceWithHistory) RecordLastAppliedResource(appliedChange Change) (ct
8485
r.resource.Description(), diff.MinimalMD5(), diff.MinimalString())
8586
}
8687

87-
annsMod := ctlres.StringMapAppendMod{
88-
ResourceMatcher: ctlres.AllMatcher{},
89-
Path: ctlres.NewPathFromStrings([]string{"metadata", "annotations"}),
90-
KVs: map[string]string{
91-
appliedResAnnKey: string(appliedResBytes),
92-
appliedResDiffMD5AnnKey: diff.MinimalMD5(),
88+
annsKVS := map[string]string{
89+
appliedResAnnKey: string(appliedResBytes),
90+
appliedResDiffMD5AnnKey: diff.MinimalMD5(),
9391

94-
// Following fields useful for debugging:
95-
// debugAppliedResDiffAnnKey: diff.MinimalString(),
96-
// debugAppliedResDiffFullAnnKey: diff.FullString(),
97-
},
92+
// Following fields useful for debugging:
93+
// debugAppliedResDiffAnnKey: diff.MinimalString(),
94+
// debugAppliedResDiffFullAnnKey: diff.FullString(),
9895
}
9996

10097
const annValMaxLen = 262144
10198

10299
// kapp deploy should work without adding disable annotation when annotation value max length exceed
103100
// (https://github.com/vmware-tanzu/carvel-kapp/issues/410)
104-
for _, annVal := range annsMod.KVs {
101+
for _, annVal := range annsKVS {
105102
if len(annVal) > annValMaxLen {
106-
return nil, false, nil
103+
return "", false, nil
107104
}
108105
}
109106

110-
resultRes := r.resource.DeepCopy()
111-
112-
err = annsMod.Apply(resultRes)
107+
result, err := json.Marshal(annsKVS)
113108
if err != nil {
114-
return nil, true, err
109+
return "", false, err
115110
}
116-
117-
return resultRes, true, nil
111+
return string(result), true, nil
118112
}
119113

120114
func (r ResourceWithHistory) CalculateChange(appliedRes ctlres.Resource) (Change, error) {

0 commit comments

Comments
 (0)