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

netpol: fix duplicate default drop acl #3197

Merged
merged 1 commit into from
Sep 11, 2023
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
4 changes: 2 additions & 2 deletions pkg/controller/network_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ func (c *Controller) handleUpdateNp(key string) error {
return err
}

ops, err := c.OVNNbClient.UpdateIngressACLOps(pgName, ingressAllowAsName, ingressExceptAsName, protocol, []netv1.NetworkPolicyPort{}, logEnable, namedPortMap)
ops, err := c.OVNNbClient.UpdateIngressACLOps(pgName, ingressAllowAsName, ingressExceptAsName, protocol, nil, logEnable, namedPortMap)
if err != nil {
klog.Errorf("generate operations that add ingress acls to np %s: %v", key, err)
return err
Expand Down Expand Up @@ -482,7 +482,7 @@ func (c *Controller) handleUpdateNp(key string) error {
return err
}

ops, err := c.OVNNbClient.UpdateEgressACLOps(pgName, egressAllowAsName, egressExceptAsName, protocol, []netv1.NetworkPolicyPort{}, logEnable, namedPortMap)
ops, err := c.OVNNbClient.UpdateEgressACLOps(pgName, egressAllowAsName, egressExceptAsName, protocol, nil, logEnable, namedPortMap)
if err != nil {
klog.Errorf("generate operations that add egress acls to np %s: %v", key, err)
return err
Expand Down
90 changes: 48 additions & 42 deletions pkg/ovs/ovn-nb-acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,32 @@ import (
func (c *OVNNbClient) UpdateIngressACLOps(pgName, asIngressName, asExceptName, protocol string, npp []netv1.NetworkPolicyPort, logEnable bool, namedPortMap map[string]*util.NamedPortInfo) ([]ovsdb.Operation, error) {
acls := make([]*ovnnb.ACL, 0)

ipSuffix := "ip4"
if protocol == kubeovnv1.ProtocolIPv6 {
ipSuffix = "ip6"
}
if strings.HasSuffix(asIngressName, ".0") || strings.HasSuffix(asIngressName, ".all") {
// create the default drop rule for only once
ipSuffix := "ip4"
if protocol == kubeovnv1.ProtocolIPv6 {
ipSuffix = "ip6"
}

/* default drop acl */
allIPMatch := NewAndACLMatch(
NewACLMatch("outport", "==", "@"+pgName, ""),
NewACLMatch(ipSuffix, "", "", ""),
)
options := func(acl *ovnnb.ACL) {
if logEnable {
acl.Log = true
acl.Severity = &ovnnb.ACLSeverityWarning
/* default drop acl */
allIPMatch := NewAndACLMatch(
NewACLMatch("outport", "==", "@"+pgName, ""),
NewACLMatch(ipSuffix, "", "", ""),
)
options := func(acl *ovnnb.ACL) {
if logEnable {
acl.Log = true
acl.Severity = &ovnnb.ACLSeverityWarning
}
}
}

defaultDropACL, err := c.newACLWithoutCheck(pgName, ovnnb.ACLDirectionToLport, util.IngressDefaultDrop, allIPMatch.String(), ovnnb.ACLActionDrop, options)
if err != nil {
return nil, fmt.Errorf("new default drop ingress acl for port group %s: %v", pgName, err)
}
defaultDropACL, err := c.newACLWithoutCheck(pgName, ovnnb.ACLDirectionToLport, util.IngressDefaultDrop, allIPMatch.String(), ovnnb.ACLActionDrop, options)
if err != nil {
return nil, fmt.Errorf("new default drop ingress acl for port group %s: %v", pgName, err)
}

acls = append(acls, defaultDropACL)
acls = append(acls, defaultDropACL)
}

/* allow acl */
matches := newNetworkPolicyACLMatch(pgName, asIngressName, asExceptName, protocol, ovnnb.ACLDirectionToLport, npp, namedPortMap)
Expand All @@ -69,36 +72,39 @@ func (c *OVNNbClient) UpdateIngressACLOps(pgName, asIngressName, asExceptName, p
func (c *OVNNbClient) UpdateEgressACLOps(pgName, asEgressName, asExceptName, protocol string, npp []netv1.NetworkPolicyPort, logEnable bool, namedPortMap map[string]*util.NamedPortInfo) ([]ovsdb.Operation, error) {
acls := make([]*ovnnb.ACL, 0)

ipSuffix := "ip4"
if protocol == kubeovnv1.ProtocolIPv6 {
ipSuffix = "ip6"
}
if strings.HasSuffix(asEgressName, ".0") || strings.HasSuffix(asEgressName, ".all") {
// create the default drop rule for only once
ipSuffix := "ip4"
if protocol == kubeovnv1.ProtocolIPv6 {
ipSuffix = "ip6"
}

/* default drop acl */
allIPMatch := NewAndACLMatch(
NewACLMatch("inport", "==", "@"+pgName, ""),
NewACLMatch(ipSuffix, "", "", ""),
)
options := func(acl *ovnnb.ACL) {
if logEnable {
acl.Log = true
acl.Severity = &ovnnb.ACLSeverityWarning
/* default drop acl */
allIPMatch := NewAndACLMatch(
NewACLMatch("inport", "==", "@"+pgName, ""),
NewACLMatch(ipSuffix, "", "", ""),
)
options := func(acl *ovnnb.ACL) {
if logEnable {
acl.Log = true
acl.Severity = &ovnnb.ACLSeverityWarning
}

if acl.Options == nil {
acl.Options = make(map[string]string)
}
acl.Options["apply-after-lb"] = "true"
}

if acl.Options == nil {
acl.Options = make(map[string]string)
defaultDropACL, err := c.newACLWithoutCheck(pgName, ovnnb.ACLDirectionFromLport, util.EgressDefaultDrop, allIPMatch.String(), ovnnb.ACLActionDrop, options)
if err != nil {
klog.Error(err)
return nil, fmt.Errorf("new default drop egress acl for port group %s: %v", pgName, err)
}
acl.Options["apply-after-lb"] = "true"
}

defaultDropACL, err := c.newACLWithoutCheck(pgName, ovnnb.ACLDirectionFromLport, util.EgressDefaultDrop, allIPMatch.String(), ovnnb.ACLActionDrop, options)
if err != nil {
klog.Error(err)
return nil, fmt.Errorf("new default drop egress acl for port group %s: %v", pgName, err)
acls = append(acls, defaultDropACL)
}

acls = append(acls, defaultDropACL)

/* allow acl */
matches := newNetworkPolicyACLMatch(pgName, asEgressName, asExceptName, protocol, ovnnb.ACLDirectionFromLport, npp, namedPortMap)
for _, m := range matches {
Expand Down
16 changes: 8 additions & 8 deletions pkg/ovs/ovn-nb-acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ func (suite *OvnClientTestSuite) testUpdateIngressACLOps() {
t.Parallel()

pgName := "test_create_v4_ingress_acl_pg"
asIngressName := "test.default.ingress.allow.ipv4"
asExceptName := "test.default.ingress.except.ipv4"
asIngressName := "test.default.ingress.allow.ipv4.all"
asExceptName := "test.default.ingress.except.ipv4.all"
protocol := kubeovnv1.ProtocolIPv4

err := ovnClient.CreatePortGroup(pgName, nil)
Expand All @@ -109,8 +109,8 @@ func (suite *OvnClientTestSuite) testUpdateIngressACLOps() {
t.Parallel()

pgName := "test_create_v6_ingress_acl_pg"
asIngressName := "test.default.ingress.allow.ipv6"
asExceptName := "test.default.ingress.except.ipv6"
asIngressName := "test.default.ingress.allow.ipv6.all"
asExceptName := "test.default.ingress.except.ipv6.all"
protocol := kubeovnv1.ProtocolIPv6

err := ovnClient.CreatePortGroup(pgName, nil)
Expand Down Expand Up @@ -151,8 +151,8 @@ func (suite *OvnClientTestSuite) testUpdateEgressACLOps() {
t.Parallel()

pgName := "test_create_v4_egress_acl_pg"
asEgressName := "test.default.egress.allow.ipv4"
asExceptName := "test.default.egress.except.ipv4"
asEgressName := "test.default.egress.allow.ipv4.all"
asExceptName := "test.default.egress.except.ipv4.all"
protocol := kubeovnv1.ProtocolIPv4

err := ovnClient.CreatePortGroup(pgName, nil)
Expand All @@ -179,8 +179,8 @@ func (suite *OvnClientTestSuite) testUpdateEgressACLOps() {
t.Parallel()

pgName := "test_create_v6_egress_acl_pg"
asEgressName := "test.default.egress.allow.ipv6"
asExceptName := "test.default.egress.except.ipv6"
asEgressName := "test.default.egress.allow.ipv6.all"
asExceptName := "test.default.egress.except.ipv6.all"
protocol := kubeovnv1.ProtocolIPv6

err := ovnClient.CreatePortGroup(pgName, nil)
Expand Down