Skip to content
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

Fix dumping OVS flows of a NetworkPolicy #3335

Merged
merged 1 commit into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to explain why the reference could be nil in the comment of this field in the struct.

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