Skip to content

Commit ae97639

Browse files
committed
fix getting subnet cidr by protocol (#4844)
Signed-off-by: zhangzujian <zhangzujian.7@gmail.com>
1 parent ec216d2 commit ae97639

File tree

3 files changed

+77
-16
lines changed

3 files changed

+77
-16
lines changed

pkg/daemon/gateway.go

+8-13
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (c *Controller) getSubnetsNeedNAT(protocol string) ([]string, error) {
101101
for _, subnet := range subnets {
102102
if c.isSubnetNeedNat(subnet, protocol) {
103103
cidrBlock, err := getCidrByProtocol(subnet.Spec.CIDRBlock, protocol)
104-
if err == nil {
104+
if err == nil && cidrBlock != "" {
105105
subnetsNeedNat = append(subnetsNeedNat, cidrBlock)
106106
}
107107
}
@@ -141,7 +141,7 @@ func (c *Controller) getSubnetsDistributedGateway(protocol string) ([]string, er
141141
subnet.Spec.GatewayType == kubeovnv1.GWDistributedType &&
142142
(subnet.Spec.Protocol == kubeovnv1.ProtocolDual || subnet.Spec.Protocol == protocol) {
143143
cidrBlock, err := getCidrByProtocol(subnet.Spec.CIDRBlock, protocol)
144-
if err == nil {
144+
if err == nil && cidrBlock != "" {
145145
result = append(result, cidrBlock)
146146
}
147147
}
@@ -172,7 +172,7 @@ func (c *Controller) getDefaultVpcSubnetsCIDR(protocol string) ([]string, map[st
172172
for _, subnet := range subnets {
173173
if subnet.Spec.Vpc == c.config.ClusterRouter && (subnet.Spec.Vlan == "" || subnet.Spec.LogicalGateway) && subnet.Spec.CIDRBlock != "" {
174174
cidrBlock, err := getCidrByProtocol(subnet.Spec.CIDRBlock, protocol)
175-
if err == nil {
175+
if err == nil && cidrBlock != "" {
176176
ret = append(ret, cidrBlock)
177177
subnetMap[subnet.Name] = cidrBlock
178178
}
@@ -204,22 +204,17 @@ func (c *Controller) getOtherNodes(protocol string) ([]string, error) {
204204
}
205205

206206
func getCidrByProtocol(cidr, protocol string) (string, error) {
207-
var cidrStr string
208207
if err := util.CheckCidrs(cidr); err != nil {
209208
return "", err
210209
}
211210

212-
if util.CheckProtocol(cidr) == kubeovnv1.ProtocolDual {
213-
cidrBlocks := strings.Split(cidr, ",")
214-
if protocol == kubeovnv1.ProtocolIPv4 {
215-
cidrStr = cidrBlocks[0]
216-
} else if protocol == kubeovnv1.ProtocolIPv6 {
217-
cidrStr = cidrBlocks[1]
211+
for _, cidr := range strings.Split(cidr, ",") {
212+
if util.CheckProtocol(cidr) == protocol {
213+
return cidr, nil
218214
}
219-
} else {
220-
cidrStr = cidr
221215
}
222-
return cidrStr, nil
216+
217+
return "", nil
223218
}
224219

225220
func (c *Controller) getEgressNatIPByNode(nodeName string) (map[string]string, error) {

pkg/daemon/gateway_linux.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,17 @@ func (c *Controller) reconcileNatOutGoingPolicyIPset(protocol string) {
205205
return
206206
}
207207

208-
subnetCidrs := make([]string, 0)
208+
subnetCidrs := make([]string, 0, len(subnets))
209209
natPolicyRuleIDs := strset.New()
210210
for _, subnet := range subnets {
211211
cidrBlock, err := getCidrByProtocol(subnet.Spec.CIDRBlock, protocol)
212212
if err != nil {
213213
klog.Errorf("failed to get subnet %s CIDR block by protocol: %v", subnet.Name, err)
214214
continue
215215
}
216-
subnetCidrs = append(subnetCidrs, cidrBlock)
216+
if cidrBlock != "" {
217+
subnetCidrs = append(subnetCidrs, cidrBlock)
218+
}
217219
for _, rule := range subnet.Status.NatOutgoingPolicyRules {
218220
if rule.RuleID == "" {
219221
klog.Errorf("unexpected empty ID for NAT outgoing rule %q of subnet %s", rule.NatOutgoingPolicyRule, subnet.Name)
@@ -1003,6 +1005,9 @@ func (c *Controller) generateNatOutgoingPolicyChainRules(protocol string) ([]uti
10031005
klog.Errorf("failed to get subnet %s cidr block with protocol: %v", subnet.Name, err)
10041006
continue
10051007
}
1008+
if cidrBlock == "" {
1009+
continue
1010+
}
10061011

10071012
ovnNatPolicySubnetChainName := OvnNatOutGoingPolicySubnet + util.GetTruncatedUID(string(subnet.GetUID()))
10081013
natPolicySubnetIptables = append(natPolicySubnetIptables, util.IPTableRule{Table: NAT, Chain: OvnNatOutGoingPolicy, Rule: strings.Fields(fmt.Sprintf(`-s %s -m comment --comment natPolicySubnet-%s -j %s`, cidrBlock, subnet.Name, ovnNatPolicySubnetChainName))})
@@ -1616,7 +1621,7 @@ func (c *Controller) getSubnetsNeedPR(protocol string) (map[policyRouteMeta]stri
16161621
}
16171622
if meta.gateway != "" {
16181623
cidrBlock, err := getCidrByProtocol(subnet.Spec.CIDRBlock, protocol)
1619-
if err == nil {
1624+
if err == nil && cidrBlock != "" {
16201625
subnetsNeedPR[meta] = cidrBlock
16211626
}
16221627
}

pkg/daemon/gateway_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package daemon
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
9+
)
10+
11+
func TestGetCidrByProtocol(t *testing.T) {
12+
cases := []struct {
13+
name string
14+
cidr string
15+
protocol string
16+
wantErr bool
17+
expetced string
18+
}{{
19+
name: "ipv4 only",
20+
cidr: "1.1.1.0/24",
21+
protocol: kubeovnv1.ProtocolIPv4,
22+
expetced: "1.1.1.0/24",
23+
}, {
24+
name: "ipv6 only",
25+
cidr: "2001:db8::/120",
26+
protocol: kubeovnv1.ProtocolIPv6,
27+
expetced: "2001:db8::/120",
28+
}, {
29+
name: "get ipv4 from ipv6",
30+
cidr: "2001:db8::/120",
31+
protocol: kubeovnv1.ProtocolIPv4,
32+
}, {
33+
name: "get ipv4 from dual stack",
34+
cidr: "1.1.1.0/24,2001:db8::/120",
35+
protocol: kubeovnv1.ProtocolIPv4,
36+
expetced: "1.1.1.0/24",
37+
}, {
38+
name: "get ipv6 from ipv4",
39+
cidr: "1.1.1.0/24",
40+
protocol: kubeovnv1.ProtocolIPv6,
41+
}, {
42+
name: "get ipv6 from dual stack",
43+
cidr: "1.1.1.0/24,2001:db8::/120",
44+
protocol: kubeovnv1.ProtocolIPv6,
45+
expetced: "2001:db8::/120",
46+
}, {
47+
name: "invalid cidr",
48+
cidr: "foo bar",
49+
protocol: kubeovnv1.ProtocolIPv4,
50+
wantErr: true,
51+
}}
52+
for _, c := range cases {
53+
t.Run(c.name, func(t *testing.T) {
54+
got, err := getCidrByProtocol(c.cidr, c.protocol)
55+
if (err != nil) != c.wantErr {
56+
t.Errorf("getCidrByProtocol(%q, %q) error = %v, wantErr = %v", c.cidr, c.protocol, err, c.wantErr)
57+
}
58+
require.Equal(t, c.expetced, got)
59+
})
60+
}
61+
}

0 commit comments

Comments
 (0)