-
Notifications
You must be signed in to change notification settings - Fork 8
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
Add merge function for cluster snapshot #301
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
changelog: | ||
- type: NEW_FEATURE | ||
description: Adds a `Merge` function to Snapshot and ClusterSnapshot so that two snapshots can be merged. | ||
issueLink: https://github.com/solo-io/skv2/issues/302 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,25 @@ func (s Snapshot) Clone(selectors ...GVKSelectorFunc) Snapshot { | |
return clone | ||
} | ||
|
||
// Merges the Snapshot with a Snapshot passed in as an argument. The values | ||
// in the passed in Snapshot will take precedence when there is an object mapped | ||
// to the same gvk and name in both Snapshots. | ||
func (s Snapshot) Merge(toMerge Snapshot) Snapshot { | ||
merged := s.Clone() | ||
for gvk, objectsMap := range toMerge { | ||
if _, ok := merged[gvk]; ok { | ||
for name, object := range objectsMap { | ||
// If there is already an object specified here, the object from toMerge | ||
// will replace it | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this conflict resolution approach seem fine? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but you also need to loop over all of the objects in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you elaborate? That's what I'm doing here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ignore me, I'm an idiot 😆 |
||
merged[gvk][name] = object | ||
} | ||
} else { | ||
merged[gvk] = objectsMap | ||
} | ||
} | ||
return merged | ||
} | ||
|
||
// ClusterSnapshot represents a set of snapshots partitioned by cluster | ||
type ClusterSnapshot map[string]Snapshot | ||
|
||
|
@@ -128,3 +147,19 @@ func (cs ClusterSnapshot) Clone(selectors ...GVKSelectorFunc) ClusterSnapshot { | |
} | ||
return clone | ||
} | ||
|
||
// Merges the ClusterSnapshot with a ClusterSnapshot passed in as an argument. | ||
// If a cluster exists in both ClusterSnapshots, then both Snapshots for the | ||
// cluster is merged; with the passed in ClusterSnapshot's corresponding Snapshot | ||
// taking precedence in case of conflicts. | ||
func (cs ClusterSnapshot) Merge(toMerge ClusterSnapshot) ClusterSnapshot { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment here as well |
||
merged := cs.Clone() | ||
for cluster, snapshot := range toMerge { | ||
if baseSnap, ok := merged[cluster]; ok { | ||
merged[cluster] = baseSnap.Merge(snapshot) | ||
} else { | ||
merged[cluster] = snapshot | ||
} | ||
} | ||
return merged | ||
} |
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.
Can you add a comment to the method about the what it does, and the merge semantics