Skip to content

Commit 137abf7

Browse files
committed
policy: Use 'insertOldIfNotExists() in addDependentOnEntry()
Keep the oldest changed value in 'addDependentOnEntry()' by using 'insertOldIfNotExists()' rather than modifying 'changes.Old' directly. Changes in tests are due to the way 'insertOldIfNotExists()' filters out new entries (not adding an 'old' entry if the key exists in 'changes.Adds'). Test changes are in the 'deletes' field due to the test internally setting a 'deletes' key for each key in 'changes.Old'. Old key values are used when reverting a failed endpoint update, prior to this fix that revert may have produced incorrect results. Signed-off-by: Jarno Rajahalme <jarno@isovalent.com>
1 parent 3ed3085 commit 137abf7

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

pkg/policy/mapstate.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -638,9 +638,7 @@ func (ms *mapState) AddDependent(owner Key, dependent Key, changes ChangeState)
638638
// addDependentOnEntry adds 'dependent' to the set of dependent keys of 'e'.
639639
func (ms *mapState) addDependentOnEntry(owner Key, e MapStateEntry, dependent Key, changes ChangeState) {
640640
if _, exists := e.dependents[dependent]; !exists {
641-
if changes.Old != nil {
642-
changes.Old[owner] = e
643-
}
641+
changes.insertOldIfNotExists(owner, e)
644642
e.AddDependent(dependent)
645643
ms.insert(owner, e)
646644
}
@@ -830,9 +828,7 @@ func (ms *mapState) addKeyWithChanges(key Key, entry MapStateEntry, changes Chan
830828
}
831829

832830
// Save old value before any changes, if desired
833-
if changes.Old != nil {
834-
changes.insertOldIfNotExists(key, oldEntry)
835-
}
831+
changes.insertOldIfNotExists(key, oldEntry)
836832

837833
// Compare for datapath equalness before merging, as the old entry is updated in
838834
// place!
@@ -1376,8 +1372,11 @@ var visibilityDerivedFromLabels = labels.LabelArray{
13761372

13771373
var visibilityDerivedFrom = labels.LabelArrayList{visibilityDerivedFromLabels}
13781374

1379-
// insertIfNotExists only inserts `key=value` if `key` does not exist in keys already
1380-
// returns 'true' if 'key=entry' was added to 'keys'
1375+
// insertIfNotExists only inserts an entry in 'changes.Old' if 'key' does not exist in there already
1376+
// and 'key' does not already exist in 'changes.Adds'. This prevents recording "old" values for
1377+
// newly added keys. When an entry is updated, we are called before the key is added to
1378+
// 'changes.Adds' so we'll record the old value as expected.
1379+
// Returns 'true' if an old entry was added.
13811380
func (changes *ChangeState) insertOldIfNotExists(key Key, entry MapStateEntry) bool {
13821381
if changes == nil || changes.Old == nil {
13831382
return false

pkg/policy/mapstate_test.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2494,10 +2494,7 @@ func TestMapState_AccumulateMapChangesOnVisibilityKeys(t *testing.T) {
24942494
ingressL3OnlyKey(236): {},
24952495
HttpIngressKey(236): {},
24962496
},
2497-
deletes: Keys{
2498-
ingressL3OnlyKey(235): {}, // changed dependents
2499-
ingressL3OnlyKey(236): {}, // changed dependents
2500-
},
2497+
deletes: Keys{},
25012498
}, {
25022499
continued: true,
25032500
name: "test-2b - Adding Bar also selecting 235",
@@ -2520,9 +2517,7 @@ func TestMapState_AccumulateMapChangesOnVisibilityKeys(t *testing.T) {
25202517
ingressL3OnlyKey(237): {},
25212518
HttpIngressKey(237): {},
25222519
},
2523-
deletes: Keys{
2524-
ingressL3OnlyKey(237): {}, // changed dependents
2525-
},
2520+
deletes: Keys{},
25262521
}, {
25272522
continued: true,
25282523
name: "test-2c - Deleting 235 from Foo, remains on Bar and no deletes",

0 commit comments

Comments
 (0)