Skip to content

Commit fa52034

Browse files
committed
Prefix on NIC v6 support
1 parent 75d1d54 commit fa52034

File tree

10 files changed

+60
-68
lines changed

10 files changed

+60
-68
lines changed

azure-ipam/ipam.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -130,30 +130,42 @@ func (p *IPAMPlugin) CmdAdd(args *cniSkel.CmdArgs) error {
130130
IP: net.ParseIP(ipNet.Addr().String()),
131131
Mask: net.CIDRMask(ipNet.Bits(), 32), // nolint
132132
}
133-
134-
ipConfig.Gateway = (*gatewayIP)[i]
135133
} else {
136134
ipConfig.Address = net.IPNet{
137135
IP: net.ParseIP(ipNet.Addr().String()),
138136
Mask: net.CIDRMask(ipNet.Bits(), 128), // nolint
139137
}
140-
141-
ipConfig.Gateway = (*gatewayIP)[i]
142138
}
139+
ipConfig.Gateway = (*gatewayIP)[i]
143140
cniResult.IPs[i] = ipConfig
144141
}
145142

146-
cniResult.Interfaces = make([]*types100.Interface, 1)
147-
interfaceMap := make(map[string]bool)
148-
cniResult.Interfaces = make([]*types100.Interface, 0, len(resp.PodIPInfo))
143+
cniResult.Interfaces = []*types100.Interface{}
144+
seenInterfaces := map[string]bool{}
145+
149146
for _, podIPInfo := range resp.PodIPInfo {
150-
if _, exists := interfaceMap[podIPInfo.InterfaceName]; !exists {
151-
cniResult.Interfaces = append(cniResult.Interfaces, &types100.Interface{
152-
Name: podIPInfo.InterfaceName, // Populate interface name based on MacAddress
153-
Mac: podIPInfo.MacAddress,
154-
})
155-
interfaceMap[podIPInfo.InterfaceName] = true
147+
if podIPInfo.MacAddress == "" {
148+
continue
149+
}
150+
151+
// Skip if interface already seen
152+
// This is to avoid duplicate interfaces in the result
153+
// which can happen if multiple IPs are assigned to the same interface
154+
// or if multiple interfaces are assigned to the same pod
155+
if seenInterfaces[podIPInfo.MacAddress] {
156+
continue
157+
}
158+
159+
infMac, err := net.ParseMAC(podIPInfo.MacAddress)
160+
if err != nil {
161+
p.logger.Error("Failed to parse interface MAC address", zap.Error(err), zap.String("macAddress", podIPInfo.MacAddress))
162+
return cniTypes.NewError(cniTypes.ErrUnsupportedField, err.Error(), "failed to parse interface MAC address")
156163
}
164+
165+
cniResult.Interfaces = append(cniResult.Interfaces, &types100.Interface{
166+
Mac: infMac.String(),
167+
})
168+
seenInterfaces[podIPInfo.MacAddress] = true
157169
}
158170

159171
// Get versioned result

azure-ipam/ipconfig/ipconfig.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ func ProcessIPConfigsResp(resp *cns.IPConfigsResponse) (*[]netip.Prefix, *[]net.
6969
gatewaysIPs := make([]net.IP, len(resp.PodIPInfo))
7070

7171
for i := range resp.PodIPInfo {
72+
var gatewayIP net.IP
73+
7274
podCIDR := fmt.Sprintf(
7375
"%s/%d",
7476
resp.PodIPInfo[i].PodIPConfig.IPAddress,
@@ -81,21 +83,14 @@ func ProcessIPConfigsResp(resp *cns.IPConfigsResponse) (*[]netip.Prefix, *[]net.
8183
podIPNets[i] = podIPNet
8284

8385
if podIPNet.Addr().Is4() {
84-
gatewayIP := net.ParseIP(resp.PodIPInfo[i].NetworkContainerPrimaryIPConfig.GatewayIPAddress)
85-
86-
if gatewayIP == nil {
87-
return nil, nil, errors.New("cns returned invalid gateway IP address")
88-
}
89-
gatewaysIPs[i] = gatewayIP
86+
gatewayIP = net.ParseIP(resp.PodIPInfo[i].NetworkContainerPrimaryIPConfig.GatewayIPAddress)
9087
} else if podIPNet.Addr().Is6() {
91-
gatewayIP := net.ParseIP(resp.PodIPInfo[i].NetworkContainerPrimaryIPConfig.GatewayIPv6Address)
88+
gatewayIP = net.ParseIP(resp.PodIPInfo[i].NetworkContainerPrimaryIPConfig.GatewayIPv6Address)
89+
}
9290

93-
if gatewayIP == nil {
94-
return nil, nil, errors.New("cns returned invalid gateway IPv6 address")
95-
}
91+
if gatewayIP != nil {
9692
gatewaysIPs[i] = gatewayIP
9793
}
98-
9994
}
10095

10196
return &podIPNets, &gatewaysIPs, nil

cns/NetworkContainerContract.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ type CreateNetworkContainerRequest struct {
129129
EndpointPolicies []NetworkContainerRequestPolicies
130130
NCStatus v1alpha.NCStatus
131131
NetworkInterfaceInfo NetworkInterfaceInfo //nolint // introducing new field for backendnic, to be used later by cni code
132-
IPFamilies map[IPFamily]struct{}
133132
}
134133

135134
func (req *CreateNetworkContainerRequest) Validate() error {

cns/kubecontroller/nodenetworkconfig/conversion_linux.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
//nolint:gocritic //ignore hugeparam
1616
func createNCRequestFromStaticNCHelper(nc v1alpha.NetworkContainer, primaryIPPrefix netip.Prefix, subnet cns.IPSubnet) (*cns.CreateNetworkContainerRequest, error) {
1717
secondaryIPConfigs := map[string]cns.SecondaryIPConfig{}
18-
ipFamilies := map[cns.IPFamily]struct{}{}
1918

2019
// in the case of vnet prefix on swift v2 the primary IP is a /32 and should not be added to secondary IP configs
2120
if !primaryIPPrefix.IsSingleIP() {
@@ -46,13 +45,6 @@ func createNCRequestFromStaticNCHelper(nc v1alpha.NetworkContainer, primaryIPPre
4645
NCVersion: int(nc.Version),
4746
}
4847
}
49-
50-
// adds the IPFamily of the secondary CIDR to the set
51-
if cidrPrefix.Addr().Is4() {
52-
ipFamilies[cns.IPv4Family] = struct{}{}
53-
} else {
54-
ipFamilies[cns.IPv6Family] = struct{}{}
55-
}
5648
}
5749
}
5850

@@ -67,8 +59,7 @@ func createNCRequestFromStaticNCHelper(nc v1alpha.NetworkContainer, primaryIPPre
6759
GatewayIPAddress: nc.DefaultGateway,
6860
GatewayIPv6Address: nc.DefaultGatewayV6,
6961
},
70-
NCStatus: nc.Status,
71-
IPFamilies: ipFamilies,
62+
NCStatus: nc.Status,
7263
NetworkInterfaceInfo: cns.NetworkInterfaceInfo{
7364
MACAddress: nc.MacAddress,
7465
},

cns/restserver/internalapi_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"fmt"
99
"math/rand"
1010
"net"
11-
"net/netip"
1211
"os"
1312
"reflect"
1413
"strconv"
@@ -914,23 +913,12 @@ func generateNetworkContainerRequest(secondaryIps map[string]cns.SecondaryIPConf
914913
ipSubnet.IPAddress = primaryIP
915914
ipSubnet.PrefixLength = subnetPrfixLength
916915
ipConfig.IPSubnet = ipSubnet
917-
918-
ipFamilies := map[cns.IPFamily]struct{}{}
919-
for _, secIPConfig := range secondaryIps {
920-
IP, _ := netip.ParseAddr(secIPConfig.IPAddress)
921-
if IP.Is4() {
922-
ipFamilies[cns.IPv4Family] = struct{}{}
923-
} else {
924-
ipFamilies[cns.IPv6Family] = struct{}{}
925-
}
926-
}
927916

928917
req := cns.CreateNetworkContainerRequest{
929918
NetworkContainerType: dockerContainerType,
930919
NetworkContainerid: ncID,
931920
IPConfiguration: ipConfig,
932921
Version: ncVersion,
933-
IPFamilies: ipFamilies,
934922
}
935923

936924
ncVersionInInt, _ := strconv.Atoi(ncVersion)

cns/restserver/ipam.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ var (
2525
ErrStoreEmpty = errors.New("empty endpoint state store")
2626
ErrParsePodIPFailed = errors.New("failed to parse pod's ip")
2727
ErrNoNCs = errors.New("no NCs found in the CNS internal state")
28-
ErrNoIPFamilies = errors.New("No IP Families found on NCs")
2928
ErrOptManageEndpointState = errors.New("CNS is not set to manage the endpoint state")
3029
ErrEndpointStateNotFound = errors.New("endpoint state could not be found in the statefile")
3130
ErrGetAllNCResponseEmpty = errors.New("failed to get NC responses from statefile")
@@ -107,7 +106,6 @@ func (service *HTTPRestService) requestIPConfigHandlerHelper(ctx context.Context
107106
}
108107

109108
podIPInfoResult = append(podIPInfoResult, podIPInfo...)
110-
111109
return &cns.IPConfigsResponse{
112110
Response: cns.Response{
113111
ReturnCode: types.Success,
@@ -1003,8 +1001,20 @@ func (service *HTTPRestService) AssignAvailableIPConfigs(podInfo cns.PodInfo) ([
10031001

10041002
// Gets the IPFamilies from all NCs and store them in a map. This will be used to determine the number of IPs to return
10051003
for ncID := range service.state.ContainerStatus {
1006-
for ipFamily := range service.state.ContainerStatus[ncID].CreateNetworkContainerRequest.IPFamilies {
1007-
ipFamilies[ipFamily] = struct{}{}
1004+
if len(ipFamilies) == 2 {
1005+
break
1006+
}
1007+
1008+
for _, secIPConfig := range service.state.ContainerStatus[ncID].CreateNetworkContainerRequest.SecondaryIPConfigs {
1009+
if len(ipFamilies) == 2 {
1010+
break
1011+
}
1012+
1013+
if net.ParseIP(secIPConfig.IPAddress).To4() != nil {
1014+
ipFamilies[cns.IPv4Family] = struct{}{}
1015+
} else {
1016+
ipFamilies[cns.IPv6Family] = struct{}{}
1017+
}
10081018
}
10091019
}
10101020

@@ -1034,7 +1044,7 @@ func (service *HTTPRestService) AssignAvailableIPConfigs(podInfo cns.PodInfo) ([
10341044
ipStateFamily = cns.IPv6Family
10351045
}
10361046

1037-
key := ipState.NCID + string(ipStateFamily)
1047+
key := generateAssignedIPKey(ipState.NCID, ipStateFamily)
10381048

10391049
// check if the IP with the same family type exists already
10401050
if _, ncIPFamilyAlreadyMarkedForAssignment := ipsToAssign[key]; ncIPFamilyAlreadyMarkedForAssignment {
@@ -1054,8 +1064,8 @@ func (service *HTTPRestService) AssignAvailableIPConfigs(podInfo cns.PodInfo) ([
10541064
// Checks to make sure we found one IP for each NCxIPFamily
10551065
if len(ipsToAssign) != numberOfIPs {
10561066
for ncID := range service.state.ContainerStatus {
1057-
for ipFamily := range service.state.ContainerStatus[ncID].CreateNetworkContainerRequest.IPFamilies {
1058-
if _, found := ipsToAssign[ncID+string(ipFamily)]; found {
1067+
for ipFamily := range ipFamilies {
1068+
if _, found := ipsToAssign[generateAssignedIPKey(ncID, ipFamily)]; found {
10591069
continue
10601070
}
10611071
return podIPInfo, errors.Errorf("not enough IPs available of type %s for %s, waiting on Azure CNS to allocate more with NC Status: %s",
@@ -1099,6 +1109,10 @@ func (service *HTTPRestService) AssignAvailableIPConfigs(podInfo cns.PodInfo) ([
10991109
return podIPInfo, nil
11001110
}
11011111

1112+
func generateAssignedIPKey(ncID string, ipFamily cns.IPFamily) string {
1113+
return fmt.Sprintf("%s_%s", ncID, string(ipFamily))
1114+
}
1115+
11021116
// If IPConfigs are already assigned to the pod, it returns that else it returns the available ipconfigs.
11031117
func requestIPConfigsHelper(service *HTTPRestService, req cns.IPConfigsRequest) ([]cns.PodIpInfo, error) {
11041118
// check if ipconfigs already assigned to this pod and return if exists or error

cns/restserver/restserver.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ type HTTPRestService struct {
9292
PnpIDByMacAddress map[string]string
9393
imdsClient imdsClient
9494
nodesubnetIPFetcher *nodesubnet.IPFetcher
95-
IPFamilies []cns.IPFamily
9695
}
9796

9897
type CNIConflistGenerator interface {

cns/restserver/util.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,6 @@ func (service *HTTPRestService) saveNetworkContainerGoalState(req cns.CreateNetw
171171
hostVersion = "-1"
172172
}
173173

174-
hostVersion = req.Version
175-
176174
// Remove the auth token before saving the containerStatus to cns json file
177175
createNetworkContainerRequest := req
178176
createNetworkContainerRequest.AuthorizationToken = ""
@@ -848,7 +846,7 @@ func (service *HTTPRestService) populateIPConfigInfoUntransacted(ipConfigStatus
848846
podIPInfo.HostPrimaryIPInfo.Subnet = primaryHostInterface.Subnet
849847
podIPInfo.HostPrimaryIPInfo.Gateway = primaryHostInterface.Gateway
850848
podIPInfo.MacAddress = ncStatus.CreateNetworkContainerRequest.NetworkInterfaceInfo.MACAddress
851-
podIPInfo.NICType = cns.InfraNIC // Update this to DelegatedNIC when it is Prefix on NIC v6
849+
podIPInfo.NICType = cns.InfraNIC
852850

853851
return nil
854852
}

go.mod

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/avast/retry-go/v3 v3.1.1
1313
github.com/avast/retry-go/v4 v4.6.1
1414
github.com/billgraziano/dpapi v0.5.0
15-
github.com/containernetworking/cni v1.2.3
15+
github.com/containernetworking/cni v1.2.2
1616
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
1717
github.com/go-logr/zapr v1.3.0 // indirect
1818
github.com/golang/mock v1.6.0
@@ -25,7 +25,7 @@ require (
2525
github.com/microsoft/ApplicationInsights-Go v0.4.4
2626
github.com/nxadm/tail v1.4.11
2727
github.com/onsi/ginkgo v1.16.5
28-
github.com/onsi/gomega v1.36.0
28+
github.com/onsi/gomega v1.33.1
2929
github.com/patrickmn/go-cache v2.1.0+incompatible
3030
github.com/pkg/errors v0.9.1
3131
github.com/prometheus/client_golang v1.21.1
@@ -202,5 +202,3 @@ retract (
202202
v1.16.15 // typo in the version number.
203203
v1.15.22 // typo in the version number.
204204
)
205-
206-
replace github.com/Azure/azure-container-networking => ./azure-container-networking

go.sum

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,8 @@ github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151X
8888
github.com/containerd/errdefs/pkg v0.3.0/go.mod h1:NJw6s9HwNuRhnjJhM7pylWwMyAkmCQvQ4GpJHEqRLVk=
8989
github.com/containerd/typeurl/v2 v2.2.0 h1:6NBDbQzr7I5LHgp34xAXYF5DOTQDn05X58lsPEmzLso=
9090
github.com/containerd/typeurl/v2 v2.2.0/go.mod h1:8XOOxnyatxSWuG8OfsZXVnAF4iZfedjS/8UHSPJnX4g=
91-
github.com/containernetworking/cni v1.2.3 h1:hhOcjNVUQTnzdRJ6alC5XF+wd9mfGIUaj8FuJbEslXM=
92-
github.com/containernetworking/cni v1.2.3/go.mod h1:DuLgF+aPd3DzcTQTtp/Nvl1Kim23oFKdm2okJzBQA5M=
93-
github.com/containernetworking/plugins v1.6.2 h1:pqP8Mq923TLyef5g97XfJ/xpDeVek4yF8A4mzy9Tc4U=
94-
github.com/containernetworking/plugins v1.6.2/go.mod h1:SP5UG3jDO9LtmfbBJdP+nl3A1atOtbj2MBOYsnaxy64=
91+
github.com/containernetworking/cni v1.2.2 h1:9IbP6KJQQxVKo4hhnm8r50YcVKrJbJu3Dqw+Rbt1vYk=
92+
github.com/containernetworking/cni v1.2.2/go.mod h1:DuLgF+aPd3DzcTQTtp/Nvl1Kim23oFKdm2okJzBQA5M=
9593
github.com/coreos/go-iptables v0.8.0 h1:MPc2P89IhuVpLI7ETL/2tx3XZ61VeICZjYqDEgNsPRc=
9694
github.com/coreos/go-iptables v0.8.0/go.mod h1:Qe8Bv2Xik5FyTXwgIbLAnv2sWSBmvWdFETJConOQ//Q=
9795
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
@@ -299,8 +297,8 @@ github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
299297
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
300298
github.com/onsi/ginkgo v1.12.0 h1:Iw5WCbBcaAAd0fpRb1c9r5YCylv4XDoCSigm1zLevwU=
301299
github.com/onsi/ginkgo v1.12.0/go.mod h1:oUhWkIvk5aDxtKvDDuw8gItl8pKl42LzjC9KZE0HfGg=
302-
github.com/onsi/ginkgo/v2 v2.22.0 h1:Yed107/8DjTr0lKCNt7Dn8yQ6ybuDRQoMGrNFKzMfHg=
303-
github.com/onsi/ginkgo/v2 v2.22.0/go.mod h1:7Du3c42kxCUegi0IImZ1wUQzMBVecgIHjR1C+NkhLQo=
300+
github.com/onsi/ginkgo/v2 v2.19.0 h1:9Cnnf7UHo57Hy3k6/m5k3dRfGTMXGvxhHFvkDTCTpvA=
301+
github.com/onsi/ginkgo/v2 v2.19.0/go.mod h1:rlwLi9PilAFJ8jCg9UE1QP6VBpd6/xj3SRC0d6TU0To=
304302
github.com/onsi/gomega v1.10.0 h1:Gwkk+PTu/nfOwNMtUB/mRUv0X7ewW5dO4AERT1ThVKo=
305303
github.com/onsi/gomega v1.10.0/go.mod h1:Ho0h+IUsWyvy1OpqCwxlQ/21gkhVunqlU8fDGcoTdcA=
306304
github.com/opentracing/opentracing-go v1.2.1-0.20220228012449-10b1cf09e00b h1:FfH+VrHHk6Lxt9HdVS0PXzSXFyS2NbZKXv33FYPol0A=

0 commit comments

Comments
 (0)