Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch resource history annotations instead of update #818

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pkg/kapp/clusterapply/add_or_update_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package clusterapply

import (
"encoding/json"
"fmt"
"time"

Expand All @@ -12,6 +13,7 @@
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 (
Expand Down Expand Up @@ -262,12 +264,16 @@
return true, nil
}

_, err = c.identifiedResources.Update(latestResWithHistoryUpdated)
latestResourceAnnotations := latestResWithHistoryUpdated.Annotations()
appliedResAnnValJsonStr, err := json.Marshal(latestResourceAnnotations[ctldiff.AppliedResAnnKey])

Check warning on line 268 in pkg/kapp/clusterapply/add_or_update_change.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: var appliedResAnnValJsonStr should be appliedResAnnValJSONStr (revive)
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
})
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/kapp/diff/resource_with_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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, "", ""
Expand Down Expand Up @@ -197,15 +197,15 @@ 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{},
Path: ctlres.NewPathFromStrings([]string{"metadata", "annotations", debugAppliedResDiffAnnKey}),
},
ctlres.FieldRemoveMod{
ResourceMatcher: ctlres.AllMatcher{},
Path: ctlres.NewPathFromStrings([]string{"metadata", "annotations", appliedResDiffMD5AnnKey}),
Path: ctlres.NewPathFromStrings([]string{"metadata", "annotations", AppliedResDiffMD5AnnKey}),
},
ctlres.FieldRemoveMod{
ResourceMatcher: ctlres.AllMatcher{},
Expand Down
Loading