Skip to content

Commit

Permalink
separate pod cidr add
Browse files Browse the repository at this point in the history
  • Loading branch information
paulyufan2 committed Jan 21, 2025
1 parent e3982fb commit b32f48f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
36 changes: 23 additions & 13 deletions cns/middlewares/k8sSwiftV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,29 @@ func (k *K8sSWIFTv2Middleware) Type() cns.SWIFTV2Mode {
return cns.K8sSWIFTV2
}

// CNS gets node, pod and service CIDRs from configuration env and parse them to get the v4 and v6 IPs
// CNS gets pod CIDRs from configuration env and parse them to get the v4 and v6 IPs
// Containerd reassigns the IP to the adapter and kernel configures the pod cidr route by default, so windows swiftv2 does not require pod cidr
func (k *K8sSWIFTv2Middleware) GetPodCidrs() (v4IPs, v6IPs []string, err error) {
v4IPs = []string{}
v6IPs = []string{}

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

v4IPs = append(v4IPs, podCIDRsV4...)
v6IPs = append(v6IPs, podCIDRv6...)

return v4IPs, v6IPs, nil
}

// CNS gets node and service CIDRs from configuration env and parse them to get the v4 and v6 IPs
func (k *K8sSWIFTv2Middleware) GetCidrs() (v4IPs, v6IPs []string, err error) {
v4IPs = []string{}
v6IPs = []string{}
Expand All @@ -265,16 +287,6 @@ func (k *K8sSWIFTv2Middleware) GetCidrs() (v4IPs, v6IPs []string, err error) {
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, nil, errors.Wrapf(err, "failed to get podCIDRs from env")
}
podCIDRsV4, podCIDRv6, err := utils.ParseCIDRs(podCIDRs)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to parse podCIDRs")
}

// Get and parse serviceCIDRs from env
serviceCIDRs, err := configuration.ServiceCIDRs()
if err != nil {
Expand All @@ -286,11 +298,9 @@ func (k *K8sSWIFTv2Middleware) GetCidrs() (v4IPs, v6IPs []string, err error) {
}

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
Expand Down
14 changes: 11 additions & 3 deletions cns/middlewares/k8sSwiftV2_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {

case cns.InfraNIC:
// 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)
infraRoutes, err := k.getInfraRoutes(podIPInfo)
if err != nil {
return errors.Wrap(err, "failed to set routes for infraNIC interface")
}
Expand Down Expand Up @@ -64,7 +64,7 @@ func (k *K8sSWIFTv2Middleware) addRoutes(cidrs []string, gatewayIP string) []cns
return routes
}

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

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

v4IPs, v6IPs, err := k.GetCidrs()
if err != nil {
return nil, errors.Wrap(err, "failed to get CIDRs")
return nil, errors.Wrap(err, "failed to get node and service CIDRs")
}

v4PodIPs, v6PodIPs, err := k.GetPodCidrs()
if err != nil {
return nil, errors.Wrap(err, "failed to get pod CIDRs")
}

v4IPs = append(v4IPs, v4PodIPs...)
v6IPs = append(v6IPs, v6PodIPs...)

if ip.Is4() {
routes = append(routes, k.addRoutes(v4IPs, overlayGatewayv4)...)
} else {
Expand Down
4 changes: 2 additions & 2 deletions cns/middlewares/k8sSwiftV2_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {

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

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

ip, err := netip.ParseAddr(podIPInfo.PodIPConfig.IPAddress)
Expand Down
5 changes: 0 additions & 5 deletions cns/middlewares/k8sSwiftV2_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

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

Expand Down Expand Up @@ -42,10 +41,6 @@ func TestSetRoutesSuccess(t *testing.T) {
},
NICType: cns.InfraNIC,
Routes: []cns.Route{
{
IPAddress: "10.0.1.10/24",
GatewayIPAddress: "10.0.1.1",
},
{
IPAddress: "10.0.0.0/16",
GatewayIPAddress: "10.0.0.1",
Expand Down

0 comments on commit b32f48f

Please sign in to comment.