Skip to content

Commit b32f48f

Browse files
committed
separate pod cidr add
1 parent e3982fb commit b32f48f

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

cns/middlewares/k8sSwiftV2.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,29 @@ func (k *K8sSWIFTv2Middleware) Type() cns.SWIFTV2Mode {
250250
return cns.K8sSWIFTV2
251251
}
252252

253-
// CNS gets node, pod and service CIDRs from configuration env and parse them to get the v4 and v6 IPs
253+
// CNS gets pod CIDRs from configuration env and parse them to get the v4 and v6 IPs
254+
// Containerd reassigns the IP to the adapter and kernel configures the pod cidr route by default, so windows swiftv2 does not require pod cidr
255+
func (k *K8sSWIFTv2Middleware) GetPodCidrs() (v4IPs, v6IPs []string, err error) {
256+
v4IPs = []string{}
257+
v6IPs = []string{}
258+
259+
// Get and parse podCIDRs from env
260+
podCIDRs, err := configuration.PodCIDRs()
261+
if err != nil {
262+
return nil, nil, errors.Wrapf(err, "failed to get podCIDRs from env")
263+
}
264+
podCIDRsV4, podCIDRv6, err := utils.ParseCIDRs(podCIDRs)
265+
if err != nil {
266+
return nil, nil, errors.Wrapf(err, "failed to parse podCIDRs")
267+
}
268+
269+
v4IPs = append(v4IPs, podCIDRsV4...)
270+
v6IPs = append(v6IPs, podCIDRv6...)
271+
272+
return v4IPs, v6IPs, nil
273+
}
274+
275+
// CNS gets node and service CIDRs from configuration env and parse them to get the v4 and v6 IPs
254276
func (k *K8sSWIFTv2Middleware) GetCidrs() (v4IPs, v6IPs []string, err error) {
255277
v4IPs = []string{}
256278
v6IPs = []string{}
@@ -265,16 +287,6 @@ func (k *K8sSWIFTv2Middleware) GetCidrs() (v4IPs, v6IPs []string, err error) {
265287
return nil, nil, errors.Wrapf(err, "failed to parse infraVNETCIDRs")
266288
}
267289

268-
// Get and parse podCIDRs from env
269-
podCIDRs, err := configuration.PodCIDRs()
270-
if err != nil {
271-
return nil, nil, errors.Wrapf(err, "failed to get podCIDRs from env")
272-
}
273-
podCIDRsV4, podCIDRv6, err := utils.ParseCIDRs(podCIDRs)
274-
if err != nil {
275-
return nil, nil, errors.Wrapf(err, "failed to parse podCIDRs")
276-
}
277-
278290
// Get and parse serviceCIDRs from env
279291
serviceCIDRs, err := configuration.ServiceCIDRs()
280292
if err != nil {
@@ -286,11 +298,9 @@ func (k *K8sSWIFTv2Middleware) GetCidrs() (v4IPs, v6IPs []string, err error) {
286298
}
287299

288300
v4IPs = append(v4IPs, infraVNETCIDRsv4...)
289-
v4IPs = append(v4IPs, podCIDRsV4...)
290301
v4IPs = append(v4IPs, serviceCIDRsV4...)
291302

292303
v6IPs = append(v6IPs, infraVNETCIDRsv6...)
293-
v6IPs = append(v6IPs, podCIDRv6...)
294304
v6IPs = append(v6IPs, serviceCIDRsV6...)
295305

296306
return v4IPs, v6IPs, nil

cns/middlewares/k8sSwiftV2_linux.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
2929

3030
case cns.InfraNIC:
3131
// Linux uses 169.254.1.1 as the default ipv4 gateway and fe80::1234:5678:9abc as the default ipv6 gateway
32-
infraRoutes, err := k.setInfraRoutes(podIPInfo)
32+
infraRoutes, err := k.getInfraRoutes(podIPInfo)
3333
if err != nil {
3434
return errors.Wrap(err, "failed to set routes for infraNIC interface")
3535
}
@@ -64,7 +64,7 @@ func (k *K8sSWIFTv2Middleware) addRoutes(cidrs []string, gatewayIP string) []cns
6464
return routes
6565
}
6666

67-
func (k *K8sSWIFTv2Middleware) setInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.Route, error) {
67+
func (k *K8sSWIFTv2Middleware) getInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.Route, error) {
6868
var routes []cns.Route
6969

7070
ip, err := netip.ParseAddr(podIPInfo.PodIPConfig.IPAddress)
@@ -74,9 +74,17 @@ func (k *K8sSWIFTv2Middleware) setInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.R
7474

7575
v4IPs, v6IPs, err := k.GetCidrs()
7676
if err != nil {
77-
return nil, errors.Wrap(err, "failed to get CIDRs")
77+
return nil, errors.Wrap(err, "failed to get node and service CIDRs")
7878
}
7979

80+
v4PodIPs, v6PodIPs, err := k.GetPodCidrs()
81+
if err != nil {
82+
return nil, errors.Wrap(err, "failed to get pod CIDRs")
83+
}
84+
85+
v4IPs = append(v4IPs, v4PodIPs...)
86+
v6IPs = append(v6IPs, v6PodIPs...)
87+
8088
if ip.Is4() {
8189
routes = append(routes, k.addRoutes(v4IPs, overlayGatewayv4)...)
8290
} else {

cns/middlewares/k8sSwiftV2_windows.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
2525

2626
// set routes(pod/node/service cidrs) for infraNIC interface
2727
// Swiftv2 Windows does not support IPv6
28-
infraRoutes, err := k.setInfraRoutes(podIPInfo)
28+
infraRoutes, err := k.getInfraRoutes(podIPInfo)
2929
if err != nil {
3030
return errors.Wrap(err, "failed to set routes for infraNIC interface")
3131
}
@@ -94,7 +94,7 @@ func (k *K8sSWIFTv2Middleware) addRoutes(cidrs []string) []cns.Route {
9494
return routes
9595
}
9696

97-
func (k *K8sSWIFTv2Middleware) setInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.Route, error) {
97+
func (k *K8sSWIFTv2Middleware) getInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.Route, error) {
9898
var routes []cns.Route
9999

100100
ip, err := netip.ParseAddr(podIPInfo.PodIPConfig.IPAddress)

cns/middlewares/k8sSwiftV2_windows_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313

1414
func TestSetRoutesSuccess(t *testing.T) {
1515
middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()}
16-
t.Setenv(configuration.EnvPodCIDRs, "10.0.1.10/24")
1716
t.Setenv(configuration.EnvServiceCIDRs, "10.0.0.0/16")
1817
t.Setenv(configuration.EnvInfraVNETCIDRs, "10.240.0.10/16")
1918

@@ -42,10 +41,6 @@ func TestSetRoutesSuccess(t *testing.T) {
4241
},
4342
NICType: cns.InfraNIC,
4443
Routes: []cns.Route{
45-
{
46-
IPAddress: "10.0.1.10/24",
47-
GatewayIPAddress: "10.0.1.1",
48-
},
4944
{
5045
IPAddress: "10.0.0.0/16",
5146
GatewayIPAddress: "10.0.0.1",

0 commit comments

Comments
 (0)