Skip to content

Commit

Permalink
Fix dumping OVS flows of a NetworkPolicy (#3335)
Browse files Browse the repository at this point in the history
Fixes #3306.

This commit fixes the nil pointer dereference error while dumping
ovsflows of NetworkPolicy, and adds a test to validate the changes.

Signed-off-by: Pulkit Jain <jainpu@vmware.com>
  • Loading branch information
jainpulkit22 committed Feb 25, 2022
1 parent 74a05c5 commit 7692426
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/agent/openflow/network_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,8 @@ type policyRuleConjunction struct {
serviceClause *clause
actionFlows []binding.Flow
metricFlows []binding.Flow
// NetworkPolicy reference information for debugging usage.
// NetworkPolicy reference information for debugging usage, its value can be nil
// for conjunctions that are not built for a specific NetworkPolicy, e.g. DNS packetin Conjunction.
npRef *v1beta2.NetworkPolicyReference
ruleTableID uint8
}
Expand Down Expand Up @@ -1455,6 +1456,11 @@ func (c *client) GetNetworkPolicyFlowKeys(npName, npNamespace string) []string {

for _, conjObj := range c.policyCache.List() {
conj := conjObj.(*policyRuleConjunction)
// If the NetworkPolicyReference in the policyRuleConjunction is nil then that entry in client's
// policyCache should be ignored because here we need to dump flows of NetworkPolicy.
if conj.npRef == nil {
continue
}
if conj.npRef.Name == npName && conj.npRef.Namespace == npNamespace {
// There can be duplicated flows added due to conjunctive matches
// shared by multiple policy rules (clauses).
Expand Down
5 changes: 5 additions & 0 deletions pkg/agent/openflow/network_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ func TestInstallPolicyRuleFlows(t *testing.T) {
c.networkConfig = &config.NetworkConfig{IPv4Enabled: true}
c.ipProtocols = []binding.Protocol{binding.ProtocolIP}
defaultAction := crdv1alpha1.RuleActionAllow
// Create a policyRuleConjunction for the dns response interception flows
// to ensure nil NetworkPolicyReference is handled correctly by GetNetworkPolicyFlowKeys.
dnsID := uint32(1)
require.NoError(t, c.NewDNSpacketInConjunction(dnsID))

ruleID1 := uint32(101)
rule1 := &types.PolicyRule{
Direction: v1beta2.DirectionOut,
Expand Down

0 comments on commit 7692426

Please sign in to comment.