Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
a580917
feat: update contracts to support swift 2
Sep 8, 2023
22badf9
add comments
Sep 18, 2023
a0afdfb
rename AddressType to NICType
Sep 22, 2023
f8baa47
update contract names and comments
Sep 25, 2023
6fdc198
address comments
Oct 3, 2023
5ad7eea
feat: update invokers to support swift 2
Sep 8, 2023
9062e83
address comments
Sep 18, 2023
f407551
address comments
Sep 22, 2023
a9d8de7
refactor cns invoker per comments
Sep 22, 2023
f20a188
update invokers based on contract change
Sep 25, 2023
6c356e5
update test
Sep 25, 2023
426e723
update with contract changes
Sep 27, 2023
28875e8
fix linter errs
Sep 27, 2023
e37ae64
fix naming
Sep 27, 2023
d8fd006
fix linter
Sep 28, 2023
6c0987b
fix linter
Sep 28, 2023
50f82f1
address comments
Oct 3, 2023
c12d952
update tests
Oct 3, 2023
27e9e77
add tests
Oct 4, 2023
73b0a9d
address nit comments
Oct 5, 2023
26cf642
add comments
Oct 9, 2023
ae09219
address comments
Oct 9, 2023
af30d9a
fix casing
Oct 11, 2023
b09b2ca
address comments
Oct 11, 2023
cd97880
feat: update invokers to support swift 2
Sep 8, 2023
fbb1fb6
address comments
Oct 3, 2023
b45906d
feat: update invokers to support swift 2
Sep 8, 2023
053dfe6
feat: update invokers to support swift 2
Sep 8, 2023
20db747
feat: update endpoint clients for swift 2
Sep 8, 2023
abbd281
address comments
Sep 22, 2023
3b59f99
fix lint errs
Sep 25, 2023
5e844a7
update endpoint clients based on contract changes
Sep 25, 2023
cd09e90
update tests
Sep 25, 2023
859a8f2
only skip adding default route
Sep 27, 2023
618a260
modify AddEndpoints per comments
Oct 5, 2023
67d35b9
address comments
Oct 9, 2023
ead5b7b
update deleteendpoint
Oct 10, 2023
ea11100
enter ns before moving interface back to vm ns
Oct 11, 2023
98ed253
update delete endpoint test
Oct 13, 2023
9b76bfb
add namespace interface for testing
Oct 18, 2023
d5fc773
fix lint
Oct 18, 2023
62ecb8a
fix lint
Oct 19, 2023
85e3eb6
add comment
Oct 20, 2023
89aa427
add extra delete endpoint test
Oct 20, 2023
0d13798
update test
Oct 23, 2023
f6984ad
feat: update invokers to support swift 2
Sep 8, 2023
9feaccb
address comments
Oct 3, 2023
1976879
address comments
Sep 22, 2023
984cfe5
feat: refactor endpoint create/delete flow for swift 2
Sep 8, 2023
9c5406c
address comments
Sep 18, 2023
165cacd
address comments
Sep 22, 2023
f1cb36c
address linter
Sep 25, 2023
f9d7c19
update based on contract changes
Sep 25, 2023
88278a0
update with contract changes
Sep 27, 2023
091b8e4
add more tests and address comments
Oct 4, 2023
10ba52f
modify AddEndpoints per comments
Oct 5, 2023
ff5f7a7
update test for invoker add and endpoint client add failure
Oct 9, 2023
13bd2e1
address comments
Oct 9, 2023
bbdb5ea
fix lint
Oct 11, 2023
9db3feb
update windows tests
Oct 11, 2023
f2563b3
update refactor with namespace interface
Oct 18, 2023
4f2de54
fix lint
Oct 20, 2023
3cb718b
rebasing fixes
Oct 23, 2023
6e14593
address comments
Nov 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions cni/network/invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,18 @@ type IPAMAddConfig struct {
}

type IPAMAddResult struct {
ipv4Result *cniTypesCurr.Result
ipv6Result *cniTypesCurr.Result
// Splitting defaultInterfaceInfo from secondaryInterfacesInfo so we don't need to loop for default CNI result every time
defaultInterfaceInfo InterfaceInfo
secondaryInterfacesInfo []InterfaceInfo
// ncResponse is used for Swift 1.0 multitenancy
ncResponse *cns.GetNetworkContainerResponse
hostSubnetPrefix net.IPNet
ipv6Enabled bool
}

type InterfaceInfo struct {
ipResult *cniTypesCurr.Result
nicType cns.NICType
macAddress net.HardwareAddr
skipDefaultRoutes bool
}
27 changes: 16 additions & 11 deletions cni/network/invoker_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/Azure/azure-container-networking/cni"
"github.com/Azure/azure-container-networking/cni/log"
"github.com/Azure/azure-container-networking/cns"
"github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/ipam"
"github.com/Azure/azure-container-networking/network"
Expand Down Expand Up @@ -46,10 +47,7 @@ func NewAzureIpamInvoker(plugin *NetPlugin, nwInfo *network.NetworkInfo) *AzureI
}

func (invoker *AzureIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, error) {
var (
addResult = IPAMAddResult{}
err error
)
addResult := IPAMAddResult{}

if addConfig.nwCfg == nil {
return addResult, invoker.plugin.Errorf("nil nwCfg passed to CNI ADD, stack: %+v", string(debug.Stack()))
Expand All @@ -60,23 +58,25 @@ func (invoker *AzureIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, er
}

// Call into IPAM plugin to allocate an address pool for the network.
addResult.ipv4Result, err = invoker.plugin.DelegateAdd(addConfig.nwCfg.IPAM.Type, addConfig.nwCfg)

result, err := invoker.plugin.DelegateAdd(addConfig.nwCfg.IPAM.Type, addConfig.nwCfg)
if err != nil && strings.Contains(err.Error(), ipam.ErrNoAvailableAddressPools.Error()) {
invoker.deleteIpamState()
logger.Info("Retry pool allocation after deleting IPAM state")
addResult.ipv4Result, err = invoker.plugin.DelegateAdd(addConfig.nwCfg.IPAM.Type, addConfig.nwCfg)
result, err = invoker.plugin.DelegateAdd(addConfig.nwCfg.IPAM.Type, addConfig.nwCfg)
}

if err != nil {
err = invoker.plugin.Errorf("Failed to allocate pool: %v", err)
return addResult, err
}
if len(result.IPs) > 0 {
addResult.hostSubnetPrefix = result.IPs[0].Address
}

defer func() {
if err != nil {
if len(addResult.ipv4Result.IPs) > 0 {
if er := invoker.Delete(&addResult.ipv4Result.IPs[0].Address, addConfig.nwCfg, nil, addConfig.options); er != nil {
if len(addResult.defaultInterfaceInfo.ipResult.IPs) > 0 {
if er := invoker.Delete(&addResult.defaultInterfaceInfo.ipResult.IPs[0].Address, addConfig.nwCfg, nil, addConfig.options); er != nil {
err = invoker.plugin.Errorf("Failed to clean up IP's during Delete with error %v, after Add failed with error %w", er, err)
}
} else {
Expand All @@ -95,13 +95,18 @@ func (invoker *AzureIPAMInvoker) Add(addConfig IPAMAddConfig) (IPAMAddResult, er
nwCfg6.IPAM.Subnet = invoker.nwInfo.Subnets[1].Prefix.String()
}

addResult.ipv6Result, err = invoker.plugin.DelegateAdd(nwCfg6.IPAM.Type, &nwCfg6)
var ipv6Result *cniTypesCurr.Result
ipv6Result, err = invoker.plugin.DelegateAdd(nwCfg6.IPAM.Type, &nwCfg6)
if err != nil {
err = invoker.plugin.Errorf("Failed to allocate v6 pool: %v", err)
} else {
result.IPs = append(result.IPs, ipv6Result.IPs...)
result.Routes = append(result.Routes, ipv6Result.Routes...)
addResult.ipv6Enabled = true
}
}

addResult.hostSubnetPrefix = addResult.ipv4Result.IPs[0].Address
addResult.defaultInterfaceInfo = InterfaceInfo{ipResult: result, nicType: cns.InfraNIC}

return addResult, err
}
Expand Down
86 changes: 49 additions & 37 deletions cni/network/invoker_azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@ import (
"github.com/stretchr/testify/require"
)

const (
ipv4cidr = "10.0.0.1/24"
v4NetCidr = "10.0.0.0/24"
ipv4cidr2 = "10.0.0.4/24"
ipv6cidr = "2001:0db8:abcd:0015::0/64"
v6NetCidr = "2001:db8:abcd:0012::0/64"
)

type mockDelegatePlugin struct {
add
del
}

type add struct {
resultsIPv4 []*cniTypesCurr.Result
resultsIPv6 []*cniTypesCurr.Result
resultsIPv4Index int
resultsIPv4 [](*cniTypesCurr.Result)
resultsIPv6Index int
resultsIPv6 [](*cniTypesCurr.Result)
errv4 error
errv6 error
}
Expand Down Expand Up @@ -82,8 +90,8 @@ func getCIDRNotationForAddress(ipaddresswithcidr string) *net.IPNet {
return ipnet
}

func getResult(ip string) []*cniTypesCurr.Result {
res := []*cniTypesCurr.Result{
func getSingleResult(ip string) []*cniTypesCurr.Result {
return []*cniTypesCurr.Result{
{
IPs: []*cniTypesCurr.IPConfig{
{
Expand All @@ -92,6 +100,14 @@ func getResult(ip string) []*cniTypesCurr.Result {
},
},
}
}

// getResult will return a slice of IPConfigs
func getResult(ips ...string) *cniTypesCurr.Result {
res := &cniTypesCurr.Result{}
for _, ip := range ips {
res.IPs = append(res.IPs, &cniTypesCurr.IPConfig{Address: *getCIDRNotationForAddress(ip)})
}
return res
}

Expand Down Expand Up @@ -127,46 +143,44 @@ func TestAzureIPAMInvoker_Add(t *testing.T) {
fields fields
args args
want *cniTypesCurr.Result
want1 *cniTypesCurr.Result
wantErr bool
}{
{
name: "happy add ipv4",
fields: fields{
plugin: &mockDelegatePlugin{
add: add{
resultsIPv4: getResult("10.0.0.1/24"),
resultsIPv4: getSingleResult(ipv4cidr),
},
del: del{},
},
nwInfo: getNwInfo("10.0.0.0/24", ""),
nwInfo: getNwInfo(v4NetCidr, ""),
},
args: args{
nwCfg: &cni.NetworkConfig{},
subnetPrefix: getCIDRNotationForAddress("10.0.0.0/24"),
subnetPrefix: getCIDRNotationForAddress(v4NetCidr),
},
want: getResult("10.0.0.1/24")[0],
want: getResult(ipv4cidr),
wantErr: false,
},
{
name: "happy add ipv4+ipv6",
fields: fields{
plugin: &mockDelegatePlugin{
add: add{
resultsIPv4: getResult("10.0.0.1/24"),
resultsIPv6: getResult("2001:0db8:abcd:0015::0/64"),
resultsIPv4: getSingleResult(ipv4cidr),
resultsIPv6: getSingleResult(ipv6cidr),
},
},
nwInfo: getNwInfo("10.0.0.0/24", "2001:db8:abcd:0012::0/64"),
nwInfo: getNwInfo(v4NetCidr, v6NetCidr),
},
args: args{
nwCfg: &cni.NetworkConfig{
IPV6Mode: network.IPV6Nat,
},
subnetPrefix: getCIDRNotationForAddress("10.0.0.0/24"),
subnetPrefix: getCIDRNotationForAddress(v4NetCidr),
},
want: getResult("10.0.0.1/24")[0],
want1: getResult("2001:0db8:abcd:0015::0/64")[0],
want: getResult(ipv4cidr, ipv6cidr),
wantErr: false,
},
{
Expand All @@ -177,34 +191,32 @@ func TestAzureIPAMInvoker_Add(t *testing.T) {
errv4: errors.New("test error"), //nolint:goerr113
},
},
nwInfo: getNwInfo("10.0.0.0/24", ""),
nwInfo: getNwInfo(v4NetCidr, ""),
},
args: args{
nwCfg: &cni.NetworkConfig{},
},
want: nil,
want1: nil,
wantErr: true,
},
{
name: "error on ipv4+ipv6",
fields: fields{
plugin: &mockDelegatePlugin{
add: add{
resultsIPv4: getResult("10.0.0.1/24"),
resultsIPv4: getSingleResult(ipv4cidr),
errv6: errors.New("test v6 error"), //nolint:goerr113
},
},
nwInfo: getNwInfo("10.0.0.0/24", ""),
nwInfo: getNwInfo(v4NetCidr, ""),
},
args: args{
nwCfg: &cni.NetworkConfig{
IPV6Mode: network.IPV6Nat,
},
subnetPrefix: getCIDRNotationForAddress("10.0.0.0/24"),
subnetPrefix: getCIDRNotationForAddress(v4NetCidr),
},
want: getResult("10.0.0.1/24")[0],
want1: nil,
want: getResult(ipv4cidr),
wantErr: true,
},
}
Expand All @@ -226,8 +238,8 @@ func TestAzureIPAMInvoker_Add(t *testing.T) {
require.Nil(err)
}

require.Exactly(tt.want, ipamAddResult.ipv4Result)
require.Exactly(tt.want1, ipamAddResult.ipv6Result)
fmt.Printf("want:%+v\nrest:%+v\n", tt.want, ipamAddResult.defaultInterfaceInfo.ipResult)
require.Exactly(tt.want, ipamAddResult.defaultInterfaceInfo.ipResult)
})
}
}
Expand Down Expand Up @@ -256,10 +268,10 @@ func TestAzureIPAMInvoker_Delete(t *testing.T) {
plugin: &mockDelegatePlugin{
del: del{},
},
nwInfo: getNwInfo("10.0.0.0/24", ""),
nwInfo: getNwInfo(v4NetCidr, ""),
},
args: args{
address: getCIDRNotationForAddress("10.0.0.4/24"),
address: getCIDRNotationForAddress(ipv4cidr2),
nwCfg: &cni.NetworkConfig{
IPAM: cni.IPAM{
Address: "10.0.0.4",
Expand All @@ -273,7 +285,7 @@ func TestAzureIPAMInvoker_Delete(t *testing.T) {
plugin: &mockDelegatePlugin{
del: del{},
},
nwInfo: getNwInfo("10.0.0.0/24", "2001:db8:abcd:0012::0/64"),
nwInfo: getNwInfo(v4NetCidr, v6NetCidr),
},
args: args{
address: getCIDRNotationForAddress("2001:db8:abcd:0015::0/64"),
Expand All @@ -292,7 +304,7 @@ func TestAzureIPAMInvoker_Delete(t *testing.T) {
err: errors.New("error when address is nil"), //nolint:goerr113
},
},
nwInfo: getNwInfo("", "2001:db8:abcd:0012::0/64"),
nwInfo: getNwInfo("", v6NetCidr),
},
args: args{
address: nil,
Expand All @@ -312,13 +324,13 @@ func TestAzureIPAMInvoker_Delete(t *testing.T) {
err: errors.New("error on v4 delete"), //nolint:goerr113
},
},
nwInfo: getNwInfo("10.0.0.0/24", ""),
nwInfo: getNwInfo(v4NetCidr, ""),
},
args: args{
address: getCIDRNotationForAddress("10.0.0.4/24"),
address: getCIDRNotationForAddress(ipv4cidr2),
nwCfg: &cni.NetworkConfig{
IPAM: cni.IPAM{
Address: "10.0.0.4/24",
Address: ipv4cidr2,
},
},
},
Expand All @@ -332,13 +344,13 @@ func TestAzureIPAMInvoker_Delete(t *testing.T) {
err: errors.New("error on v6 delete"), //nolint:goerr113
},
},
nwInfo: getNwInfo("10.0.0.0/24", "2001:db8:abcd:0012::0/64"),
nwInfo: getNwInfo(v4NetCidr, v6NetCidr),
},
args: args{
address: getCIDRNotationForAddress("2001:db8:abcd:0015::0/64"),
nwCfg: &cni.NetworkConfig{
IPAM: cni.IPAM{
Address: "10.0.0.4/24",
Address: ipv4cidr2,
},
},
},
Expand Down Expand Up @@ -393,17 +405,17 @@ func TestRemoveIpamState_Add(t *testing.T) {
fields: fields{
plugin: &mockDelegatePlugin{
add: add{
resultsIPv4: getResult("10.0.0.1/24"),
resultsIPv4: getSingleResult(ipv4cidr),
errv4: ipam.ErrNoAvailableAddressPools,
},
},
nwInfo: getNwInfo("10.0.0.0/24", ""),
nwInfo: getNwInfo(v4NetCidr, ""),
},
args: args{
nwCfg: &cni.NetworkConfig{},
subnetPrefix: getCIDRNotationForAddress("10.0.0.0/24"),
subnetPrefix: getCIDRNotationForAddress(v4NetCidr),
},
want: getResult("10.0.0.1/24")[0],
want: getResult(ipv4cidr),
wantErrMsg: ipam.ErrNoAvailableAddressPools.Error(),
wantErr: true,
},
Expand Down
Loading