Skip to content

Commit

Permalink
policy: Fix mapstate changes error in entry change comparison
Browse files Browse the repository at this point in the history
[ upstream commit 3d3f69c ]

Datapath equalness of the old and new entry must be evaluated before
entries are merged, afterwards the entries will be the same and the key
is not added in ChangeState.Adds, which may lead to a policy map entry
not being updated during incremental updates.

Fixes: #26331

Signed-off-by: Jarno Rajahalme <jarno@isovalent.com>
Signed-off-by: Yutaro Hayakawa <yutaro.hayakawa@isovalent.com>
  • Loading branch information
jrajahalme authored and YutaroHayakawa committed Dec 20, 2023
1 parent 7155461 commit 152199a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pkg/policy/mapstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ func (keys MapState) denyPreferredInsert(newKey Key, newEntry MapStateEntry, ide
func (keys MapState) addKeyWithChanges(key Key, entry MapStateEntry, changes ChangeState) {
// Keep all owners that need this entry so that it is deleted only if all the owners delete their contribution
oldEntry, exists := keys[key]
var datapathEqual bool
if exists {
// Deny entry can only be overridden by another deny entry
if oldEntry.IsDeny && !entry.IsDeny {
Expand All @@ -381,6 +382,9 @@ func (keys MapState) addKeyWithChanges(key Key, entry MapStateEntry, changes Cha
changes.Old.insertIfNotExists(key, oldEntry)
}

// Compare for datapath equalness before merging, as the old entry is updated in
// place!
datapathEqual = oldEntry.DatapathEqual(&entry)
oldEntry.Merge(&entry)
keys[key] = oldEntry
} else {
Expand All @@ -393,7 +397,7 @@ func (keys MapState) addKeyWithChanges(key Key, entry MapStateEntry, changes Cha
}

// Record an incremental Add if desired and entry is new or changed
if changes.Adds != nil && (!exists || !oldEntry.DatapathEqual(&entry)) {
if changes.Adds != nil && (!exists || !datapathEqual) {
changes.Adds[key] = struct{}{}
// Key add overrides any previous delete of the same key
if changes.Deletes != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/policy/mapstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1867,6 +1867,9 @@ func (ds *PolicyTestSuite) TestMapState_AddVisibilityKeys(c *check.C) {
for k, v := range tt.keys {
if v2, ok := old[k]; ok {
if equals, _ := checker.DeepEqual(v2, v); !equals {
if !v.DatapathEqual(&v2) {
wantAdds[k] = struct{}{}
}
wantOld[k] = v2
}
} else {
Expand Down Expand Up @@ -2110,6 +2113,7 @@ func (ds *PolicyTestSuite) TestMapState_AccumulateMapChangesOnVisibilityKeys(c *
},
visAdds: Keys{
HttpIngressKey(0): {},
HttpEgressKey(0): {},
},
visOld: MapState{
// Old value for the modified entry
Expand Down

0 comments on commit 152199a

Please sign in to comment.