-
Notifications
You must be signed in to change notification settings - Fork 110
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
Introduce DiffResources struct #665
Open
criztovyl
wants to merge
6
commits into
carvel-dev:develop
Choose a base branch
from
criztovyl:criztovyl/diff-resources-object
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
581979a
DiffResources: Move versionedResources
criztovyl 19e0c52
DiffResources: Introduce the type
criztovyl c00d6b6
DiffResources: Move setup
criztovyl 5b06840
DiffResources: RenewableResources initial introduction
criztovyl 54e8741
Fix typo
criztovyl 739fb47
Use common short name for DiffResources
criztovyl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright 2020 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package diff | ||
|
||
import ( | ||
"sort" | ||
|
||
ctlconf "github.com/vmware-tanzu/carvel-kapp/pkg/kapp/config" | ||
ctlres "github.com/vmware-tanzu/carvel-kapp/pkg/kapp/resources" | ||
) | ||
|
||
type DiffResources struct { | ||
ExistingResources, NewResources versionedResources | ||
ExistingResourcesGrouped map[string][]ctlres.Resource | ||
|
||
newRs []ctlres.Resource | ||
rules []ctlconf.TemplateRule | ||
} | ||
|
||
func NewDiffResources(existingRs, newRs []ctlres.Resource, rules []ctlconf.TemplateRule) DiffResources { | ||
existingVRs := existingVersionedResources(existingRs) | ||
newVRs := newVersionedResources(newRs) | ||
|
||
existingRsGrouped := newGroupedVersionedResources(existingVRs.Versioned) | ||
|
||
return DiffResources{existingVRs, newVRs, existingRsGrouped, newRs, rules} | ||
} | ||
|
||
type versionedResources struct { | ||
Versioned []ctlres.Resource | ||
NonVersioned []ctlres.Resource | ||
} | ||
|
||
func newVersionedResources(rs []ctlres.Resource) versionedResources { | ||
var result versionedResources | ||
for _, res := range rs { | ||
_, hasVersionedAnn := res.Annotations()[versionedResAnnKey] | ||
_, hasVersionedOrigAnn := res.Annotations()[versionedResOrigAnnKey] | ||
|
||
if hasVersionedAnn { | ||
result.Versioned = append(result.Versioned, res) | ||
if hasVersionedOrigAnn { | ||
result.NonVersioned = append(result.NonVersioned, res.DeepCopy()) | ||
} | ||
} else { | ||
result.NonVersioned = append(result.NonVersioned, res) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
func existingVersionedResources(rs []ctlres.Resource) versionedResources { | ||
var result versionedResources | ||
for _, res := range rs { | ||
// Expect that versioned resources should not be transient | ||
// (Annotations may have been copied from versioned resources | ||
// onto transient resources for non-versioning related purposes). | ||
_, hasVersionedAnn := res.Annotations()[versionedResAnnKey] | ||
|
||
versionedRs := VersionedResource{res: res} | ||
_, version := versionedRs.BaseNameAndVersion() | ||
|
||
if hasVersionedAnn && !res.Transient() && version != "" { | ||
result.Versioned = append(result.Versioned, res) | ||
} else { | ||
result.NonVersioned = append(result.NonVersioned, res) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
func newGroupedVersionedResources(rs []ctlres.Resource) map[string][]ctlres.Resource { | ||
result := map[string][]ctlres.Resource{} | ||
|
||
groupByFunc := func(res ctlres.Resource) string { | ||
_, found := res.Annotations()[versionedResAnnKey] | ||
if found { | ||
return VersionedResource{res, nil}.UniqVersionedKey().String() | ||
} | ||
panic("Expected to find versioned annotation on resource") | ||
} | ||
|
||
for resKey, subRs := range (GroupResources{rs, groupByFunc}).Resources() { | ||
sort.Slice(subRs, func(i, j int) bool { | ||
return VersionedResource{subRs[i], nil}.Version() < VersionedResource{subRs[j], nil}.Version() | ||
}) | ||
result[resKey] = subRs | ||
} | ||
return result | ||
} | ||
|
||
func (d DiffResources) existingResourcesMap() map[string]ctlres.Resource { | ||
result := map[string]ctlres.Resource{} | ||
|
||
for _, res := range d.ExistingResources.NonVersioned { | ||
resKey := ctlres.NewUniqueResourceKey(res).String() | ||
result[resKey] = res | ||
} | ||
|
||
for resKey, res := range d.ExistingResourcesGrouped { | ||
result[resKey] = res[len(res)-1] | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
saving newRs is only a workaround for RenewableResources, I would prefer a different approach, but did not want to spend to much time here for the moment :)