Skip to content

Commit

Permalink
fix bugs and uts
Browse files Browse the repository at this point in the history
  • Loading branch information
paulyufan2 committed Dec 12, 2024
1 parent bf09112 commit 8077cd5
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 174 deletions.
76 changes: 53 additions & 23 deletions cns/middlewares/k8sSwiftV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/netip"
"net"

"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/configuration"
Expand Down Expand Up @@ -238,9 +239,8 @@ func (k *K8sSWIFTv2Middleware) getIPConfig(ctx context.Context, podInfo cns.PodI
return nil, errors.Wrap(err, "failed to parse mtpnc subnetAddressSpace prefix")
}
podIPInfos = append(podIPInfos, podIPInfo)
// // for windows scenario, it is required to add default route with gatewayIP from CNS
// k.addDefaultRoute(&podIPInfo)
// logger.Printf("default route windows are %v", podIPInfo.Routes)
// for windows scenario, it is required to add default route with gatewayIP from CNS
k.addDefaultRoute(&podIPInfo, interfaceInfo.GatewayIP)
}
}
}
Expand All @@ -252,70 +252,100 @@ func (k *K8sSWIFTv2Middleware) Type() cns.SWIFTV2Mode {
return cns.K8sSWIFTV2
}

// always pick up .1 as the default gateway for each IP address
func (k *K8sSWIFTv2Middleware) getWindowsGateway(cidr string) (string, error) {
ip, _, err := net.ParseCIDR(cidr)
if err != nil {
return "", errors.Wrap(err, "failed to parse cidr")
}
ip = ip.To4()
ip[3] = 1

return ip.String(), nil
}

// Linux always use fixed gateway IP for infraVNETCIDRs, podCIDRs and serviceCIDRs
// Windows uses .1 as the gateway IP for each CIDR
func (k *K8sSWIFTv2Middleware) addRoutes(cidrs []string, gatewayIP string) []cns.Route {
routes := make([]cns.Route, len(cidrs))
for i, cidr := range cidrs {
if gatewayIP == "" {
gatewayIP, _ = k.getWindowsGateway(cidr)
}
routes[i] = cns.Route{
IPAddress: cidr,
GatewayIPAddress: gatewayIP,
}
}

return routes
}

func (k *K8sSWIFTv2Middleware) SetInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.Route, error) {
var routes []cns.Route
// CNS gets node, pod and service CIDRs from configuration env and parse them to get the v4 and v6 IPs
func (k *K8sSWIFTv2Middleware) getCidrs(podIPInfo *cns.PodIpInfo) (v4IPs, v6IPs []string, err error) {

Check failure on line 285 in cns/middlewares/k8sSwiftV2.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, ubuntu-latest)

unused-parameter: parameter 'podIPInfo' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 285 in cns/middlewares/k8sSwiftV2.go

View workflow job for this annotation

GitHub Actions / Lint (1.22.x, windows-latest)

unused-parameter: parameter 'podIPInfo' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 285 in cns/middlewares/k8sSwiftV2.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, ubuntu-latest)

unused-parameter: parameter 'podIPInfo' seems to be unused, consider removing or renaming it as _ (revive)

Check failure on line 285 in cns/middlewares/k8sSwiftV2.go

View workflow job for this annotation

GitHub Actions / Lint (1.23.x, windows-latest)

unused-parameter: parameter 'podIPInfo' seems to be unused, consider removing or renaming it as _ (revive)
v4IPs = []string{}
v6IPs = []string{}

// Get and parse infraVNETCIDRs from env
infraVNETCIDRs, err := configuration.InfraVNETCIDRs()
if err != nil {
return nil, errors.Wrapf(err, "failed to get infraVNETCIDRs from env")
return nil, nil, errors.Wrapf(err, "failed to get infraVNETCIDRs from env")
}
infraVNETCIDRsv4, infraVNETCIDRsv6, err := utils.ParseCIDRs(infraVNETCIDRs)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse infraVNETCIDRs")
return nil, nil, errors.Wrapf(err, "failed to parse infraVNETCIDRs")
}

// Get and parse podCIDRs from env
podCIDRs, err := configuration.PodCIDRs()
if err != nil {
return nil, errors.Wrapf(err, "failed to get podCIDRs from env")
return nil, nil, errors.Wrapf(err, "failed to get podCIDRs from env")
}
podCIDRsV4, podCIDRv6, err := utils.ParseCIDRs(podCIDRs)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse podCIDRs")
return nil, nil, errors.Wrapf(err, "failed to parse podCIDRs")
}

// Get and parse serviceCIDRs from env
serviceCIDRs, err := configuration.ServiceCIDRs()
if err != nil {
return nil, errors.Wrapf(err, "failed to get serviceCIDRs from env")
return nil, nil, errors.Wrapf(err, "failed to get serviceCIDRs from env")
}
serviceCIDRsV4, serviceCIDRsV6, err := utils.ParseCIDRs(serviceCIDRs)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse serviceCIDRs")
return nil, nil, errors.Wrapf(err, "failed to parse serviceCIDRs")
}

v4IPs = append(v4IPs, infraVNETCIDRsv4...)
v4IPs = append(v4IPs, podCIDRsV4...)
v4IPs = append(v4IPs, serviceCIDRsV4...)

v6IPs = append(v6IPs, infraVNETCIDRsv6...)
v6IPs = append(v6IPs, podCIDRv6...)
v6IPs = append(v6IPs, serviceCIDRsV6...)

return v4IPs, v6IPs, nil
}

func (k *K8sSWIFTv2Middleware) SetInfraRoutes(podIPInfo *cns.PodIpInfo, gwv4, gwv6 string) ([]cns.Route, error) {
var routes []cns.Route

ip, err := netip.ParseAddr(podIPInfo.PodIPConfig.IPAddress)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse podIPConfig IP address %s", podIPInfo.PodIPConfig.IPAddress)
}

if ip.Is4() {
routes = append(routes, k.addRoutes(podCIDRsV4, "10.229.0.1")...)
routes = append(routes, k.addRoutes(serviceCIDRsV4, "10.0.0.1")...)
routes = append(routes, k.addRoutes(infraVNETCIDRsv4, "10.225.0.1")...)
} else {
routes = append(routes, k.addRoutes(podCIDRv6, overlayGatewayV6)...)
routes = append(routes, k.addRoutes(serviceCIDRsV6, overlayGatewayV6)...)
routes = append(routes, k.addRoutes(infraVNETCIDRsv6, overlayGatewayV6)...)
v4IPs, v6IPs, err := k.getCidrs(podIPInfo)
if err != nil {
return nil, errors.Wrap(err, "failed to get CIDRs")
}

defaultRoute := cns.Route{
IPAddress: "0.0.0.0/0",
GatewayIPAddress: "0.0.0.0",
if ip.Is4() {
routes = append(routes, k.addRoutes(v4IPs, gwv4)...)
} else {
routes = append(routes, k.addRoutes(v6IPs, gwv6)...)
}
routes = append(routes, defaultRoute)

return routes, nil
}

5 changes: 3 additions & 2 deletions cns/middlewares/k8sSwiftV2_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
routes = append(routes, virtualGWRoute, route)

case cns.InfraNIC:
infraRoutes, err := k.SetInfraRoutes(podIPInfo)
// Linux uses 169.254.1.1 as the default ipv4 gateway and fe80::1234:5678:9abc as the default ipv6 gateway
infraRoutes, err := k.SetInfraRoutes(podIPInfo, overlayGatewayv4, overlayGatewayV6)
if err != nil {
return errors.Wrap(err, "failed to set routes for infraNIC interface")
}
Expand All @@ -49,4 +50,4 @@ func (k *K8sSWIFTv2Middleware) assignSubnetPrefixLengthFields(_ *cns.PodIpInfo,
return nil
}

func (k *K8sSWIFTv2Middleware) addDefaultRoute(*cns.PodIpInfo) {}
func (k *K8sSWIFTv2Middleware) addDefaultRoute(*cns.PodIpInfo, string) {}
215 changes: 108 additions & 107 deletions cns/middlewares/k8sSwiftV2_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package middlewares

import (
"context"
// "fmt"
"fmt"
"testing"
"reflect"

"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/cns/configuration"
Expand Down Expand Up @@ -242,112 +243,112 @@ func TestGetSWIFTv2IPConfigFailure(t *testing.T) {
assert.Error(t, err, errMTPNCNotReady.Error())
}

// func TestSetRoutesSuccess(t *testing.T) {
// middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()}
// t.Setenv(configuration.EnvPodCIDRs, "10.0.1.10/24,16A0:0010:AB00:001E::2/32")
// t.Setenv(configuration.EnvServiceCIDRs, "10.0.0.0/16,16A0:0010:AB00:0000::/32")
// t.Setenv(configuration.EnvInfraVNETCIDRs, "10.240.0.1/16,16A0:0020:AB00:0000::/32")

// podIPInfo := []cns.PodIpInfo{
// {
// PodIPConfig: cns.IPSubnet{
// IPAddress: "10.0.1.10",
// PrefixLength: 32,
// },
// NICType: cns.InfraNIC,
// },
// {
// PodIPConfig: cns.IPSubnet{
// IPAddress: "2001:0db8:abcd:0015::0",
// PrefixLength: 64,
// },
// NICType: cns.InfraNIC,
// },
// {
// PodIPConfig: cns.IPSubnet{
// IPAddress: "20.240.1.242",
// PrefixLength: 32,
// },
// NICType: cns.DelegatedVMNIC,
// MacAddress: "12:34:56:78:9a:bc",
// },
// }
// desiredPodIPInfo := []cns.PodIpInfo{
// {
// PodIPConfig: cns.IPSubnet{
// IPAddress: "10.0.1.10",
// PrefixLength: 32,
// },
// NICType: cns.InfraNIC,
// Routes: []cns.Route{
// {
// IPAddress: "10.0.1.10/24",
// GatewayIPAddress: overlayGatewayv4,
// },
// {
// IPAddress: "10.0.0.0/16",
// GatewayIPAddress: overlayGatewayv4,
// },
// {
// IPAddress: "10.240.0.1/16",
// GatewayIPAddress: overlayGatewayv4,
// },
// },
// },
// {
// PodIPConfig: cns.IPSubnet{
// IPAddress: "2001:0db8:abcd:0015::0",
// PrefixLength: 64,
// },
// NICType: cns.InfraNIC,
// Routes: []cns.Route{
// {
// IPAddress: "16A0:0010:AB00:001E::2/32",
// GatewayIPAddress: overlayGatewayV6,
// },
// {
// IPAddress: "16A0:0010:AB00:0000::/32",
// GatewayIPAddress: overlayGatewayV6,
// },
// {
// IPAddress: "16A0:0020:AB00:0000::/32",
// GatewayIPAddress: overlayGatewayV6,
// },
// },
// },
// {
// PodIPConfig: cns.IPSubnet{
// IPAddress: "20.240.1.242",
// PrefixLength: 32,
// },
// NICType: cns.DelegatedVMNIC,
// MacAddress: "12:34:56:78:9a:bc",
// Routes: []cns.Route{
// {
// IPAddress: fmt.Sprintf("%s/%d", virtualGW, prefixLength),
// },
// {
// IPAddress: "0.0.0.0/0",
// GatewayIPAddress: virtualGW,
// },
// },
// },
// }
// for i := range podIPInfo {
// ipInfo := &podIPInfo[i]
// err := middleware.setRoutes(ipInfo)
// assert.Equal(t, err, nil)
// if ipInfo.NICType == cns.InfraNIC {
// assert.Equal(t, ipInfo.SkipDefaultRoutes, true)
// } else {
// assert.Equal(t, ipInfo.SkipDefaultRoutes, false)
// }

// }
// for i := range podIPInfo {
// assert.DeepEqual(t, podIPInfo[i].Routes, desiredPodIPInfo[i].Routes)
// }
// }
func TestSetRoutesSuccess(t *testing.T) {
middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()}
t.Setenv(configuration.EnvPodCIDRs, "10.0.1.10/24,16A0:0010:AB00:001E::2/32")
t.Setenv(configuration.EnvServiceCIDRs, "10.0.0.0/16,16A0:0010:AB00:0000::/32")
t.Setenv(configuration.EnvInfraVNETCIDRs, "10.240.0.1/16,16A0:0020:AB00:0000::/32")

podIPInfo := []cns.PodIpInfo{
{
PodIPConfig: cns.IPSubnet{
IPAddress: "10.0.1.10",
PrefixLength: 32,
},
NICType: cns.InfraNIC,
},
{
PodIPConfig: cns.IPSubnet{
IPAddress: "2001:0db8:abcd:0015::0",
PrefixLength: 64,
},
NICType: cns.InfraNIC,
},
{
PodIPConfig: cns.IPSubnet{
IPAddress: "20.240.1.242",
PrefixLength: 32,
},
NICType: cns.DelegatedVMNIC,
MacAddress: "12:34:56:78:9a:bc",
},
}
desiredPodIPInfo := []cns.PodIpInfo{
{
PodIPConfig: cns.IPSubnet{
IPAddress: "10.0.1.10",
PrefixLength: 32,
},
NICType: cns.InfraNIC,
Routes: []cns.Route{
{
IPAddress: "10.0.1.10/24",
GatewayIPAddress: overlayGatewayv4,
},
{
IPAddress: "10.0.0.0/16",
GatewayIPAddress: overlayGatewayv4,
},
{
IPAddress: "10.240.0.1/16",
GatewayIPAddress: overlayGatewayv4,
},
},
},
{
PodIPConfig: cns.IPSubnet{
IPAddress: "2001:0db8:abcd:0015::0",
PrefixLength: 64,
},
NICType: cns.InfraNIC,
Routes: []cns.Route{
{
IPAddress: "16A0:0010:AB00:001E::2/32",
GatewayIPAddress: overlayGatewayV6,
},
{
IPAddress: "16A0:0010:AB00:0000::/32",
GatewayIPAddress: overlayGatewayV6,
},
{
IPAddress: "16A0:0020:AB00:0000::/32",
GatewayIPAddress: overlayGatewayV6,
},
},
},
{
PodIPConfig: cns.IPSubnet{
IPAddress: "20.240.1.242",
PrefixLength: 32,
},
NICType: cns.DelegatedVMNIC,
MacAddress: "12:34:56:78:9a:bc",
Routes: []cns.Route{
{
IPAddress: fmt.Sprintf("%s/%d", virtualGW, prefixLength),
},
{
IPAddress: "0.0.0.0/0",
GatewayIPAddress: virtualGW,
},
},
},
}
for i := range podIPInfo {
ipInfo := &podIPInfo[i]
err := middleware.setRoutes(ipInfo)
assert.Equal(t, err, nil)
if ipInfo.NICType == cns.InfraNIC {
assert.Equal(t, ipInfo.SkipDefaultRoutes, true)
} else {
assert.Equal(t, ipInfo.SkipDefaultRoutes, false)
}
}

for i := range podIPInfo {
reflect.DeepEqual(podIPInfo[i].Routes, desiredPodIPInfo[i].Routes)
}
}

func TestSetRoutesFailure(t *testing.T) {
// Failure due to env var not set
Expand Down
Loading

0 comments on commit 8077cd5

Please sign in to comment.