From 5d0909da88f63271657633120d7a0106a21a1831 Mon Sep 17 00:00:00 2001 From: Dmitriy Kalinin Date: Sat, 5 Dec 2020 21:42:20 -0500 Subject: [PATCH] rebase new resource against existing res on cluster --- pkg/kapp/diff/change_factory.go | 11 +++++++++-- pkg/kapp/diff/resource_with_history.go | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/kapp/diff/change_factory.go b/pkg/kapp/diff/change_factory.go index 22cee496f..88ea72efd 100644 --- a/pkg/kapp/diff/change_factory.go +++ b/pkg/kapp/diff/change_factory.go @@ -19,10 +19,17 @@ func NewChangeFactory(rebaseMods []ctlres.ResourceModWithMultiple, } func (f ChangeFactory) NewChangeAgainstLastApplied(existingRes, newRes ctlres.Resource) (Change, error) { + // Retain original copy of existing resource and use it + // for rebasing last applied resource and new resource. + existingResForRebasing := existingRes + if existingRes != nil { + // If we have copy of last applied resource (assuming it's still "valid"), + // use it as an existing resource to provide "smart" diff instead of + // diffing against resource that is actually stored on cluster. lastAppliedRes := f.NewResourceWithHistory(existingRes).LastAppliedResource() if lastAppliedRes != nil { - rebasedLastAppliedRes, err := NewRebasedResource(existingRes, lastAppliedRes, f.rebaseMods).Resource() + rebasedLastAppliedRes, err := NewRebasedResource(existingResForRebasing, lastAppliedRes, f.rebaseMods).Resource() if err != nil { return nil, err } @@ -46,7 +53,7 @@ func (f ChangeFactory) NewChangeAgainstLastApplied(existingRes, newRes ctlres.Re newRes = historylessNewRes } - rebasedNewRes, err := NewRebasedResource(existingRes, newRes, f.rebaseMods).Resource() + rebasedNewRes, err := NewRebasedResource(existingResForRebasing, newRes, f.rebaseMods).Resource() if err != nil { return nil, err } diff --git a/pkg/kapp/diff/resource_with_history.go b/pkg/kapp/diff/resource_with_history.go index 732479a18..8092624e0 100644 --- a/pkg/kapp/diff/resource_with_history.go +++ b/pkg/kapp/diff/resource_with_history.go @@ -41,6 +41,8 @@ func (r ResourceWithHistory) HistorylessResource() (ctlres.Resource, error) { return resourceWithoutHistory{r.resource}.Resource() } +// LastAppliedResource will return "last applied" resource that was saved +// iff it still matches actually saved resource on the cluster (noted at the time of saving). func (r ResourceWithHistory) LastAppliedResource() ctlres.Resource { recalculatedLastAppliedChanges, expectedDiffMD5, expectedDiff := r.recalculateLastAppliedChange()