Skip to content

Commit 2dfc05a

Browse files
authored
chores: SWIFT v2 routing changes (#2342)
* chores: chage nodeCIDRs to infraVNETCIDRs * chores: add routes for virtualGW * fix: add /32 prefix * fix: manually fmt.Sprintf /32 prefix with virtual gw
1 parent d1a03ba commit 2dfc05a

File tree

5 files changed

+76
-51
lines changed

5 files changed

+76
-51
lines changed

cns/configuration/env.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ const (
1414
// LabelNodeSwiftV2 is the Node label for Swift V2
1515
LabelNodeSwiftV2 = "kubernetes.azure.com/podnetwork-multi-tenancy-enabled"
1616
// LabelPodSwiftV2 is the Pod label for Swift V2
17-
LabelPodSwiftV2 = "kubernetes.azure.com/pod-network"
18-
EnvPodCIDRs = "POD_CIDRs"
19-
EnvServiceCIDRs = "SERVICE_CIDRs"
20-
EnvNodeCIDRs = "NODE_CIDRs"
17+
LabelPodSwiftV2 = "kubernetes.azure.com/pod-network"
18+
EnvPodCIDRs = "POD_CIDRs"
19+
EnvServiceCIDRs = "SERVICE_CIDRs"
20+
EnvInfraVNETCIDRs = "INFRA_VNET_CIDRs"
2121
)
2222

2323
// ErrNodeNameUnset indicates the the $EnvNodeName variable is unset in the environment.
@@ -29,8 +29,8 @@ var ErrPodCIDRsUnset = errors.Errorf("must declare %s environment variable", Env
2929
// ErrServiceCIDRsUnset indicates the the $EnvServiceCIDRs variable is unset in the environment.
3030
var ErrServiceCIDRsUnset = errors.Errorf("must declare %s environment variable", EnvServiceCIDRs)
3131

32-
// ErrNodeCIDRsUnset indicates the the $EnvNodeCIDRs variable is unset in the environment.
33-
var ErrNodeCIDRsUnset = errors.Errorf("must declare %s environment variable", EnvNodeCIDRs)
32+
// ErrInfraVNETCIDRsUnset indicates the the $EnvInfraVNETCIDRs variable is unset in the environment.
33+
var ErrInfraVNETCIDRsUnset = errors.Errorf("must declare %s environment variable", EnvInfraVNETCIDRs)
3434

3535
// NodeName checks the environment variables for the NODENAME and returns it or an error if unset.
3636
func NodeName() (string, error) {
@@ -62,10 +62,10 @@ func ServiceCIDRs() (string, error) {
6262
return serviceCIDRs, nil
6363
}
6464

65-
func NodeCIDRs() (string, error) {
66-
nodeCIDRs := os.Getenv(EnvNodeCIDRs)
67-
if nodeCIDRs == "" {
68-
return "", ErrNodeCIDRsUnset
65+
func InfraVNETCIDRs() (string, error) {
66+
infraVNETCIDRs := os.Getenv(EnvInfraVNETCIDRs)
67+
if infraVNETCIDRs == "" {
68+
return "", ErrInfraVNETCIDRsUnset
6969
}
70-
return nodeCIDRs, nil
70+
return infraVNETCIDRs, nil
7171
}

cns/configuration/env_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,13 @@ func TestServiceCIDRs(t *testing.T) {
3737
assert.NoError(t, err)
3838
assert.Equal(t, "test", cidr)
3939
}
40+
41+
func TestInfraVNETCIDRs(t *testing.T) {
42+
_, err := InfraVNETCIDRs()
43+
require.Error(t, err)
44+
require.ErrorIs(t, err, ErrInfraVNETCIDRsUnset)
45+
os.Setenv(EnvInfraVNETCIDRs, "test")
46+
cidr, err := InfraVNETCIDRs()
47+
assert.NoError(t, err)
48+
assert.Equal(t, "test", cidr)
49+
}

cns/middlewares/mock/mockSWIFTv2.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ var (
2525
const (
2626
prefixLength = 32
2727
overlayGatewayv4 = "169.254.1.1"
28+
virtualGW = "169.254.2.1"
2829
overlayGatewayV6 = "fe80::1234:5678:9abc"
2930
)
3031

@@ -56,7 +57,7 @@ func (m *SWIFTv2Middleware) SetMTPNCReady() {
5657
func (m *SWIFTv2Middleware) SetEnvVar() {
5758
os.Setenv(configuration.EnvPodCIDRs, "10.0.1.10/24")
5859
os.Setenv(configuration.EnvServiceCIDRs, "10.0.2.10/24")
59-
os.Setenv(configuration.EnvNodeCIDRs, "10.0.3.10/24")
60+
os.Setenv(configuration.EnvInfraVNETCIDRs, "10.0.3.10/24")
6061
}
6162

6263
func (m *SWIFTv2Middleware) UnsetEnvVar() error {
@@ -66,8 +67,8 @@ func (m *SWIFTv2Middleware) UnsetEnvVar() error {
6667
if err := os.Unsetenv(configuration.EnvServiceCIDRs); err != nil {
6768
return fmt.Errorf("failed to unset env var %s : %w", configuration.EnvServiceCIDRs, err)
6869
}
69-
if err := os.Unsetenv(configuration.EnvNodeCIDRs); err != nil {
70-
return fmt.Errorf("failed to unset env var %s : %w", configuration.EnvNodeCIDRs, err)
70+
if err := os.Unsetenv(configuration.EnvInfraVNETCIDRs); err != nil {
71+
return fmt.Errorf("failed to unset env var %s : %w", configuration.EnvInfraVNETCIDRs, err)
7172
}
7273
return nil
7374
}
@@ -130,20 +131,24 @@ func (m *SWIFTv2Middleware) SetRoutes(podIPInfo *cns.PodIpInfo) error {
130131
podIPInfo.Routes = []cns.Route{}
131132
switch podIPInfo.NICType {
132133
case cns.DelegatedVMNIC:
134+
virtualGWRoute := cns.Route{
135+
IPAddress: fmt.Sprintf("%s/%d", virtualGW, prefixLength),
136+
}
133137
// default route via SWIFT v2 interface
134138
route := cns.Route{
135-
IPAddress: "0.0.0.0/0",
139+
IPAddress: "0.0.0.0/0",
140+
GatewayIPAddress: virtualGW,
136141
}
137-
podIPInfo.Routes = []cns.Route{route}
142+
podIPInfo.Routes = []cns.Route{virtualGWRoute, route}
138143
case cns.InfraNIC:
139-
// Get and parse nodeCIDRs from env
140-
nodeCIDRs, err := configuration.NodeCIDRs()
144+
// Get and parse infraVNETCIDRs from env
145+
infraVNETCIDRs, err := configuration.InfraVNETCIDRs()
141146
if err != nil {
142-
return errors.Wrapf(err, "failed to get nodeCIDR from env")
147+
return errors.Wrapf(err, "failed to get infraVNETCIDRs from env")
143148
}
144-
nodeCIDRsv4, nodeCIDRsv6, err := utils.ParseCIDRs(nodeCIDRs)
149+
infraVNETCIDRsv4, infraVNETCIDRsv6, err := utils.ParseCIDRs(infraVNETCIDRs)
145150
if err != nil {
146-
return errors.Wrapf(err, "failed to parse nodeCIDRs")
151+
return errors.Wrapf(err, "failed to parse infraVNETCIDRs")
147152
}
148153

149154
// Get and parse podCIDRs from env
@@ -187,13 +192,13 @@ func (m *SWIFTv2Middleware) SetRoutes(podIPInfo *cns.PodIpInfo) error {
187192
}
188193
podIPInfo.Routes = append(podIPInfo.Routes, serviceCIDRv4Route)
189194
}
190-
// route for IPv4 nodeCIDR traffic
191-
for _, nodeCIDRv4 := range nodeCIDRsv4 {
192-
nodeCIDRv4Route := cns.Route{
193-
IPAddress: nodeCIDRv4,
195+
// route for IPv4 infraVNETCIDR traffic
196+
for _, infraVNETCIDRsv4 := range infraVNETCIDRsv4 {
197+
infraVNETCIDRsv4Route := cns.Route{
198+
IPAddress: infraVNETCIDRsv4,
194199
GatewayIPAddress: overlayGatewayv4,
195200
}
196-
podIPInfo.Routes = append(podIPInfo.Routes, nodeCIDRv4Route)
201+
podIPInfo.Routes = append(podIPInfo.Routes, infraVNETCIDRsv4Route)
197202
}
198203
} else {
199204
// routes for IPv6 podCIDR traffic
@@ -212,13 +217,13 @@ func (m *SWIFTv2Middleware) SetRoutes(podIPInfo *cns.PodIpInfo) error {
212217
}
213218
podIPInfo.Routes = append(podIPInfo.Routes, serviceCIDRv6Route)
214219
}
215-
// route for IPv6 nodeCIDR traffic
216-
for _, nodeCIDRv6 := range nodeCIDRsv6 {
217-
nodeCIDRv6Route := cns.Route{
218-
IPAddress: nodeCIDRv6,
220+
// route for IPv6 infraVNETCIDR traffic
221+
for _, infraVNETCIDRv6 := range infraVNETCIDRsv6 {
222+
infraVNETCIDRv6Route := cns.Route{
223+
IPAddress: infraVNETCIDRv6,
219224
GatewayIPAddress: overlayGatewayV6,
220225
}
221-
podIPInfo.Routes = append(podIPInfo.Routes, nodeCIDRv6Route)
226+
podIPInfo.Routes = append(podIPInfo.Routes, infraVNETCIDRv6Route)
222227
}
223228
}
224229
podIPInfo.SkipDefaultRoutes = true

cns/middlewares/swiftV2.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var (
2626
const (
2727
prefixLength = 32
2828
overlayGatewayv4 = "169.254.1.1"
29+
virtualGW = "169.254.2.1"
2930
overlayGatewayV6 = "fe80::1234:5678:9abc"
3031
)
3132

@@ -114,20 +115,24 @@ func (m *SWIFTv2Middleware) SetRoutes(podIPInfo *cns.PodIpInfo) error {
114115
podIPInfo.Routes = []cns.Route{}
115116
switch podIPInfo.NICType {
116117
case cns.DelegatedVMNIC:
118+
virtualGWRoute := cns.Route{
119+
IPAddress: fmt.Sprintf("%s/%d", virtualGW, prefixLength),
120+
}
117121
// default route via SWIFT v2 interface
118122
route := cns.Route{
119-
IPAddress: "0.0.0.0/0",
123+
IPAddress: "0.0.0.0/0",
124+
GatewayIPAddress: virtualGW,
120125
}
121-
podIPInfo.Routes = []cns.Route{route}
126+
podIPInfo.Routes = []cns.Route{virtualGWRoute, route}
122127
case cns.InfraNIC:
123-
// Get and parse nodeCIDRs from env
124-
nodeCIDRs, err := configuration.NodeCIDRs()
128+
// Get and parse infraVNETCIDRs from env
129+
infraVNETCIDRs, err := configuration.InfraVNETCIDRs()
125130
if err != nil {
126-
return errors.Wrapf(err, "failed to get nodeCIDR from env")
131+
return errors.Wrapf(err, "failed to get infraVNETCIDRs from env")
127132
}
128-
nodeCIDRsv4, nodeCIDRsv6, err := utils.ParseCIDRs(nodeCIDRs)
133+
infraVNETCIDRsv4, infraVNETCIDRsv6, err := utils.ParseCIDRs(infraVNETCIDRs)
129134
if err != nil {
130-
return errors.Wrapf(err, "failed to parse nodeCIDRs")
135+
return errors.Wrapf(err, "failed to parse infraVNETCIDRs")
131136
}
132137

133138
// Get and parse podCIDRs from env
@@ -171,13 +176,13 @@ func (m *SWIFTv2Middleware) SetRoutes(podIPInfo *cns.PodIpInfo) error {
171176
}
172177
podIPInfo.Routes = append(podIPInfo.Routes, serviceCIDRv4Route)
173178
}
174-
// route for IPv4 nodeCIDR traffic
175-
for _, nodeCIDRv4 := range nodeCIDRsv4 {
176-
nodeCIDRv4Route := cns.Route{
177-
IPAddress: nodeCIDRv4,
179+
// route for IPv4 infraVNETCIDR traffic
180+
for _, infraVNETCIDRv4 := range infraVNETCIDRsv4 {
181+
infraVNETCIDRv4Route := cns.Route{
182+
IPAddress: infraVNETCIDRv4,
178183
GatewayIPAddress: overlayGatewayv4,
179184
}
180-
podIPInfo.Routes = append(podIPInfo.Routes, nodeCIDRv4Route)
185+
podIPInfo.Routes = append(podIPInfo.Routes, infraVNETCIDRv4Route)
181186
}
182187
} else {
183188
// routes for IPv6 podCIDR traffic
@@ -196,13 +201,13 @@ func (m *SWIFTv2Middleware) SetRoutes(podIPInfo *cns.PodIpInfo) error {
196201
}
197202
podIPInfo.Routes = append(podIPInfo.Routes, serviceCIDRv6Route)
198203
}
199-
// route for IPv6 nodeCIDR traffic
200-
for _, nodeCIDRv6 := range nodeCIDRsv6 {
201-
nodeCIDRv6Route := cns.Route{
202-
IPAddress: nodeCIDRv6,
204+
// route for IPv6 infraVNETCIDR traffic
205+
for _, infraVNETCIDRv6 := range infraVNETCIDRsv6 {
206+
infraVNETCIDRv6Route := cns.Route{
207+
IPAddress: infraVNETCIDRv6,
203208
GatewayIPAddress: overlayGatewayV6,
204209
}
205-
podIPInfo.Routes = append(podIPInfo.Routes, nodeCIDRv6Route)
210+
podIPInfo.Routes = append(podIPInfo.Routes, infraVNETCIDRv6Route)
206211
}
207212
}
208213
podIPInfo.SkipDefaultRoutes = true

cns/middlewares/swiftV2_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package middlewares
22

33
import (
44
"context"
5+
"fmt"
56
"os"
67
"testing"
78

@@ -30,13 +31,13 @@ var (
3031
func setEnvVar() {
3132
os.Setenv(configuration.EnvPodCIDRs, "10.0.1.10/24,16A0:0010:AB00:001E::2/32")
3233
os.Setenv(configuration.EnvServiceCIDRs, "10.0.0.0/16,16A0:0010:AB00:0000::/32")
33-
os.Setenv(configuration.EnvNodeCIDRs, "10.240.0.1/16,16A0:0020:AB00:0000::/32")
34+
os.Setenv(configuration.EnvInfraVNETCIDRs, "10.240.0.1/16,16A0:0020:AB00:0000::/32")
3435
}
3536

3637
func unsetEnvVar() {
3738
os.Unsetenv(configuration.EnvPodCIDRs)
3839
os.Unsetenv(configuration.EnvServiceCIDRs)
39-
os.Unsetenv(configuration.EnvNodeCIDRs)
40+
os.Unsetenv(configuration.EnvInfraVNETCIDRs)
4041
}
4142

4243
func TestMain(m *testing.M) {
@@ -200,7 +201,11 @@ func TestSetRoutesSuccess(t *testing.T) {
200201
MacAddress: "12:34:56:78:9a:bc",
201202
Routes: []cns.Route{
202203
{
203-
IPAddress: "0.0.0.0/0",
204+
IPAddress: fmt.Sprintf("%s/%d", virtualGW, prefixLength),
205+
},
206+
{
207+
IPAddress: "0.0.0.0/0",
208+
GatewayIPAddress: virtualGW,
204209
},
205210
},
206211
},

0 commit comments

Comments
 (0)