-
Notifications
You must be signed in to change notification settings - Fork 0
[sample #11456] Migrate to range-over-func style iteration in libcalico-go/lib/set #197
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
base: coc-sample-11456-base
Are you sure you want to change the base?
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 |
|---|---|---|
|
|
@@ -316,47 +316,48 @@ func (m *bpfIPSets) ApplyUpdates(_ ipsets.UpdateListener) { | |
| } | ||
| } | ||
|
|
||
| m.dirtyIPSetIDs.Iter(func(setID uint64) error { | ||
| for setID := range m.dirtyIPSetIDs.All() { | ||
|
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. 🧪 Maintainability & Tests — This (and many siblings) implement a drain-like pattern manually:
|
||
| leaveDirty := false | ||
| ipSet := m.getExistingIPSet(setID) | ||
| if ipSet == nil { | ||
| m.lg.WithField("id", setID).Warn("Couldn't find IP set that was marked as dirty.") | ||
| m.resyncScheduled = true | ||
| return set.RemoveItem | ||
| m.dirtyIPSetIDs.Discard(setID) | ||
| continue | ||
| } | ||
|
|
||
| ipSet.PendingRemoves.Iter(func(entry IPSetEntryInterface) error { | ||
| for entry := range ipSet.PendingRemoves.All() { | ||
|
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. |
||
| if debug { | ||
| m.lg.WithFields(log.Fields{"setID": setID, "entry": entry}).Debug("Removing entry from IP set") | ||
| } | ||
| err := m.bpfMap.Delete(entry.AsBytes()) | ||
| if err != nil { | ||
| m.lg.WithFields(log.Fields{"setID": setID, "entry": entry}).WithError(err).Error("Failed to remove IP set entry") | ||
| leaveDirty = true | ||
| return nil | ||
| continue | ||
| } | ||
| numDels++ | ||
| return set.RemoveItem | ||
| }) | ||
| ipSet.PendingRemoves.Discard(entry) | ||
| } | ||
|
|
||
| ipSet.PendingAdds.Iter(func(entry IPSetEntryInterface) error { | ||
| for entry := range ipSet.PendingAdds.All() { | ||
| if debug { | ||
| m.lg.WithFields(log.Fields{"setID": setID, "entry": entry}).Debug("Adding entry to IP set") | ||
| } | ||
| err := m.bpfMap.Update(entry.AsBytes(), DummyValue) | ||
| if err != nil { | ||
| m.lg.WithFields(log.Fields{"setID": setID, "entry": entry}).WithError(err).Error("Failed to add IP set entry") | ||
| leaveDirty = true | ||
| return nil | ||
| continue | ||
| } | ||
| numAdds++ | ||
| return set.RemoveItem | ||
| }) | ||
| ipSet.PendingAdds.Discard(entry) | ||
| } | ||
|
|
||
| if leaveDirty { | ||
| m.lg.WithField("setID", setID).Debug("IP set still dirty, queueing resync") | ||
| m.resyncScheduled = true | ||
| return nil | ||
| continue | ||
| } | ||
|
|
||
| if ipSet.Deleted { | ||
|
|
@@ -365,8 +366,8 @@ func (m *bpfIPSets) ApplyUpdates(_ ipsets.UpdateListener) { | |
| } | ||
|
|
||
| m.lg.WithField("setID", setID).Debug("IP set is now clean") | ||
| return set.RemoveItem | ||
| }) | ||
| m.dirtyIPSetIDs.Discard(setID) | ||
| } | ||
|
|
||
| duration := time.Since(startTime) | ||
| if numDels > 0 || numAdds > 0 { | ||
|
|
@@ -448,10 +449,9 @@ func (m *bpfIPSet) ReplaceMembers(members []string, protoIPSetMemberToBPFEntry f | |
| } | ||
|
|
||
| func (m *bpfIPSet) RemoveAll() { | ||
| m.DesiredEntries.Iter(func(entry IPSetEntryInterface) error { | ||
| for entry := range m.DesiredEntries.All() { | ||
| m.RemoveMember(entry) | ||
| return nil | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func (m *bpfIPSet) AddMembers(members []string, protoIPSetMemberToBPFEntry func(uint64, string) IPSetEntryInterface) { | ||
|
|
||
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.
🧪 Maintainability & Tests — Tests have been updated mechanically, but no new tests cover the semantics of range-over-func vs prior Iter (e.g., Discard while iterating, early break). Please add focused unit tests in the set package validating:
These tests will serve as the contract for all the new loops spread across the codebase.