Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
paulyufan2 committed Jan 29, 2025
1 parent ed4c09c commit cd629a4
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions cns/middlewares/k8sSwiftV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ func (k *K8sSWIFTv2Middleware) AddRoutes(cidrs []string, gatewayIP string) []cns
return routes
}

// 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) {
// Both Linux and Windows CNS gets infravnet and service CIDRs from configuration env
func (k *K8sSWIFTv2Middleware) GetCidrs() ([]string, []string, error) {

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

View workflow job for this annotation

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

unnamedResult: consider giving a name to these results (gocritic)

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

View workflow job for this annotation

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

unnamedResult: consider giving a name to these results (gocritic)

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

View workflow job for this annotation

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

unnamedResult: consider giving a name to these results (gocritic)

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

View workflow job for this annotation

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

unnamedResult: consider giving a name to these results (gocritic)
v4Cidrs := []string{}
v6Cidrs := []string{}

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

case cns.InfraNIC:
// get service and infravnet routes
// Linux CNS middleware sets the infra routes(infravnet/pod/service cidrs) to infraNIC interface for the podIPInfo used in SWIFT V2 Linux scenario
infraRoutes, err := k.getInfraRoutes(podIPInfo)
if err != nil {
return errors.Wrap(err, "failed to get infra routes for infraNIC interface")
Expand All @@ -50,9 +50,10 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
return nil
}

// 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) {
// Linux CNS gets pod CIDRs from configuration env
// Containerd reassigns the IP to the adapter and kernel configures the pod cidr route by default on Windows VM
// Hence the windows swiftv2 scenario does not require pod cidr
func (k *K8sSWIFTv2Middleware) GetPodCidrs() ([]string, []string, error) {

Check failure on line 56 in cns/middlewares/k8sSwiftV2_linux.go

View workflow job for this annotation

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

unnamedResult: consider giving a name to these results (gocritic)

Check failure on line 56 in cns/middlewares/k8sSwiftV2_linux.go

View workflow job for this annotation

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

unnamedResult: consider giving a name to these results (gocritic)
v4PodCidrs := []string{}
v6PodCidrs := []string{}

Expand All @@ -72,6 +73,8 @@ func (k *K8sSWIFTv2Middleware) GetPodCidrs() (v4IPs, v6IPs []string, err error)
return v4PodCidrs, v6PodCidrs, nil
}

// getInfraRoutes() returns the infra routes including infravnet/pod/service cidrs for the podIPInfo used in SWIFT V2 Linux scenario
// Linux uses 169.254.1.1 as the default ipv4 gateway and fe80::1234:5678:9abc as the default ipv6 gateway
func (k *K8sSWIFTv2Middleware) getInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.Route, error) {
var routes []cns.Route

Expand All @@ -93,7 +96,6 @@ func (k *K8sSWIFTv2Middleware) getInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.R
v4IPs = append(v4IPs, v4PodIPs...)
v6IPs = append(v6IPs, v6PodIPs...)

// Linux uses 169.254.1.1 as the default ipv4 gateway and fe80::1234:5678:9abc as the default ipv6 gateway
if ip.Is4() {
routes = append(routes, k.AddRoutes(v4IPs, overlayGatewayv4)...)
} else {
Expand Down
4 changes: 2 additions & 2 deletions cns/middlewares/k8sSwiftV2_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package middlewares
import (
"context"
"fmt"
"reflect"
"testing"

"github.com/Azure/azure-container-networking/cns"
Expand All @@ -12,6 +11,7 @@ import (
"github.com/Azure/azure-container-networking/cns/middlewares/mock"
"github.com/Azure/azure-container-networking/cns/types"
"github.com/Azure/azure-container-networking/crd/multitenancy/api/v1alpha1"
"github.com/google/go-cmp/cmp"
"gotest.tools/v3/assert"
)

Expand Down Expand Up @@ -346,7 +346,7 @@ func TestSetRoutesSuccess(t *testing.T) {
}

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

Expand Down
10 changes: 5 additions & 5 deletions cns/middlewares/k8sSwiftV2_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (k *K8sSWIFTv2Middleware) setRoutes(podIPInfo *cns.PodIpInfo) error {
}
podIPInfo.Routes = append(podIPInfo.Routes, route)

// set routes(infravnet and service cidrs) for infraNIC interface
// // Linux CNS middleware sets the infra routes(infravnet and service cidrs) to infraNIC interface for the podIPInfo used in SWIFT V2 Windows scenario
infraRoutes, err := k.getInfraRoutes(podIPInfo)
if err != nil {
return errors.Wrap(err, "failed to set routes for infraNIC interface")
Expand Down Expand Up @@ -220,6 +220,9 @@ func GetDefaultDenyBool(mtpnc v1alpha1.MultitenantPodNetworkConfig) bool {
return mtpnc.Status.DefaultDenyACL
}

// getInfraRoutes() returns the infra routes including infravnet/ and service cidrs for the podIPInfo used in SWIFT V2 Windows scenario
// Windows uses default route 0.0.0.0 as the gateway IP for containerd to configure;
// For example, containerd would set route like: ip route 10.0.0.0/16 via 0.0.0.0 dev eth0
func (k *K8sSWIFTv2Middleware) getInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.Route, error) {
var routes []cns.Route

Expand All @@ -228,16 +231,13 @@ func (k *K8sSWIFTv2Middleware) getInfraRoutes(podIPInfo *cns.PodIpInfo) ([]cns.R
return nil, errors.Wrapf(err, "failed to parse podIPConfig IP address %s", podIPInfo.PodIPConfig.IPAddress)
}

// swiftv2 windows does not support ipv6
// TODO: add ipv6 when supported
v4IPs, _, err := k.GetCidrs()
if err != nil {
return nil, errors.Wrap(err, "failed to get CIDRs")
}

if ip.Is4() {
// add routes to podIPInfo for the given CIDRs and gateway IP
// always use default gateway IP for containerd to configure routes;
// containerd will set route with default gateway ip like 10.0.0.0/16 via 0.0.0.0 dev eth0
routes = append(routes, k.AddRoutes(v4IPs, defaultGateway)...)
}

Expand Down
1 change: 1 addition & 0 deletions cns/middlewares/k8sSwiftV2_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func TestSetRoutesSuccess(t *testing.T) {
middleware := K8sSWIFTv2Middleware{Cli: mock.NewClient()}
t.Setenv(configuration.EnvServiceCIDRs, "10.0.0.0/16")
t.Setenv(configuration.EnvInfraVNETCIDRs, "10.240.0.10/16")
t.Setenv(configuration.EnvPodCIDRs, "10.1.0.10/24") // make sure windows swiftv2 does not set pod cidr route

podIPInfo := []cns.PodIpInfo{
{
Expand Down

0 comments on commit cd629a4

Please sign in to comment.