Skip to content

Commit 47cbdbc

Browse files
committed
changes
1 parent dbd474a commit 47cbdbc

File tree

7 files changed

+30
-177
lines changed

7 files changed

+30
-177
lines changed

azure-ipam/ipam.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (p *IPAMPlugin) CmdAdd(args *cniSkel.CmdArgs) error {
8181
p.logger.Debug("Making request to CNS")
8282
// if this fails, the caller plugin should execute again with cmdDel before returning error.
8383
// https://www.cni.dev/docs/spec/#delegated-plugin-execution-procedure
84-
resp, err := p.cnsClient.RequestIPs(context.TODO(), req) // need to add interfaces to this response
84+
resp, err := p.cnsClient.RequestIPs(context.TODO(), req)
8585
if err != nil {
8686
if cnscli.IsUnsupportedAPI(err) {
8787
p.logger.Error("Failed to request IPs using RequestIPs from CNS, going to try RequestIPAddress", zap.Error(err), zap.Any("request", req))
@@ -113,10 +113,9 @@ func (p *IPAMPlugin) CmdAdd(args *cniSkel.CmdArgs) error {
113113
}
114114
}
115115
p.logger.Debug("Received CNS IP config response", zap.Any("response", resp))
116-
// resp.PodIPInfo
117116

118117
// Get Pod IP and gateway IP from ip config response
119-
podIPNet, gatewayIP, err := ipconfig.ProcessIPConfigsResp(resp) // need to get interfaces out of the response and add it here
118+
podIPNet, gatewayIP, err := ipconfig.ProcessIPConfigsResp(resp)
120119
if err != nil {
121120
p.logger.Error("Failed to interpret CNS IPConfigResponse", zap.Error(err), zap.Any("response", resp))
122121
return cniTypes.NewError(ErrProcessIPConfigResponse, err.Error(), "failed to interpret CNS IPConfigResponse")
@@ -133,35 +132,29 @@ func (p *IPAMPlugin) CmdAdd(args *cniSkel.CmdArgs) error {
133132
}
134133

135134
ipConfig.Gateway = (*gatewayIP)[i]
136-
p.logger.Debug("Gatewayv4", zap.String("Gatewayv4", ipConfig.Gateway.String()))
137135
} else {
138136
ipConfig.Address = net.IPNet{
139137
IP: net.ParseIP(ipNet.Addr().String()),
140138
Mask: net.CIDRMask(ipNet.Bits(), 128), // nolint
141139
}
142140

143141
ipConfig.Gateway = (*gatewayIP)[i]
144-
p.logger.Debug("Gatewayv6", zap.String("Gatewayv6", ipConfig.Gateway.String()))
145-
//ipConfig.Gateway = net.ParseIP("fd00:aec6:6946:1::")
146-
if ipConfig.Gateway == nil {
147-
//ipConfig.Gateway = net.ParseIP("fd00:aec6:6946:1::")
148-
//p.logger.Debug("DummyGatewayv6", zap.String("Gatewayv6", ipConfig.Gateway.String()))
149-
}
150142
}
151143
cniResult.IPs[i] = ipConfig
152144
}
153145

154-
p.logger.Info("MACAddress:", zap.Any("MACAddress", resp.PodIPInfo[0].MacAddress))
155-
156146
cniResult.Interfaces = make([]*types100.Interface, 1)
157-
interface_test := &types100.Interface{
158-
Name: "eth1",
159-
//Mac: "00-0D-3A-6F-11-DE",
160-
Mac: resp.PodIPInfo[0].MacAddress,
147+
interfaceMap := make(map[string]bool)
148+
cniResult.Interfaces = make([]*types100.Interface, 0, len(resp.PodIPInfo))
149+
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
156+
}
161157
}
162-
cniResult.Interfaces[0] = interface_test
163-
164-
p.logger.Info("Created CNIResult:", zap.Any("result", cniResult))
165158

166159
// Get versioned result
167160
versionedCniResult, err := cniResult.GetAsVersion(nwCfg.CNIVersion)

cns/imds/client.go

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@ package imds
66
import (
77
"context"
88
"encoding/json"
9-
"fmt"
10-
"io/ioutil"
119
"net/http"
1210
"net/url"
1311

14-
"github.com/Azure/azure-container-networking/cns/logger"
1512
"github.com/avast/retry-go/v4"
1613
"github.com/pkg/errors"
1714
)
@@ -62,17 +59,6 @@ var (
6259
ErrUnexpectedStatusCode = errors.New("imds returned an unexpected status code")
6360
)
6461

65-
// Define struct for Network Interface
66-
type NetworkInterface struct {
67-
MacAddress string `json:"macAddress"`
68-
NcID string `json:"ncId"`
69-
}
70-
71-
// Define struct for Network
72-
type Network struct {
73-
Interface []NetworkInterface `json:"interface"`
74-
}
75-
7662
// NewClient creates a new imds client
7763
func NewClient(opts ...ClientOption) *Client {
7864
config := clientConfig{
@@ -94,41 +80,15 @@ func (c *Client) GetVMUniqueID(ctx context.Context) (string, error) {
9480
var vmUniqueID string
9581
err := retry.Do(func() error {
9682
computeDoc, err := c.getInstanceComputeMetadata(ctx)
97-
9883
if err != nil {
9984
return errors.Wrap(err, "error getting IMDS compute metadata")
10085
}
101-
102-
// logger.Printf("Complete IMDS call response: %v", computeDoc)
103-
// macaddressData, ok1 := computeDoc["macaddress"].(string)
104-
// if !ok1 {
105-
// return errors.New("unable to parse IMDS macaddress metadata")
106-
// }
107-
// logger.Printf("Complete IMDS call response[network]: %v", macaddressData)
108-
109-
// ncidData, ok2 := computeDoc["ncId"].(string)
110-
// if !ok2 {
111-
// return errors.New("unable to parse IMDS ncid metadata")
112-
// }
113-
// logger.Printf("Complete IMDS call response[network][macaddress]: %v", ncidData)
114-
11586
vmUniqueIDUntyped := computeDoc[vmUniqueIDProperty]
11687
var ok bool
11788
vmUniqueID, ok = vmUniqueIDUntyped.(string)
11889
if !ok {
11990
return errors.New("unable to parse IMDS compute metadata, vmId property is not a string")
12091
}
121-
122-
networkDoc, err := c.getInstanceInterfaceMacaddress(ctx)
123-
124-
if err != nil {
125-
errors.Wrap(err, "error getting IMDS interface metadata")
126-
} else {
127-
for _, int := range networkDoc.Interface {
128-
logger.Printf("Complete IMDS call [macaddress]: %s, [ncId]: %s", int.MacAddress, int.NcID)
129-
}
130-
}
131-
13292
return nil
13393
}, retry.Context(ctx), retry.Attempts(c.config.retryAttempts), retry.DelayType(retry.BackOffDelay))
13494
if err != nil {
@@ -166,52 +126,10 @@ func (c *Client) getInstanceComputeMetadata(ctx context.Context) (map[string]any
166126
return nil, errors.Wrapf(ErrUnexpectedStatusCode, "unexpected status code %d", resp.StatusCode)
167127
}
168128

169-
logger.Printf("Complete IMDS call response body: %v", resp.Body)
170-
171129
var m map[string]any
172130
if err := json.NewDecoder(resp.Body).Decode(&m); err != nil {
173131
return nil, errors.Wrap(err, "error decoding IMDS response as json")
174132
}
175133

176134
return m, nil
177135
}
178-
179-
func (c *Client) getInstanceInterfaceMacaddress(ctx context.Context) (Network, error) {
180-
imdsComputeURL, err := url.JoinPath(c.config.endpoint, "/metadata/instance/network")
181-
if err != nil {
182-
return Network{}, errors.Wrap(err, "unable to build path to IMDS interface metadata")
183-
}
184-
imdsComputeURL = imdsComputeURL + "?" + imdsComputeAPIVersion + "&" + imdsFormatJSON
185-
186-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, imdsComputeURL, http.NoBody)
187-
if err != nil {
188-
return Network{}, errors.Wrap(err, "error building IMDS http request")
189-
}
190-
191-
// IMDS requires the "Metadata: true" header
192-
req.Header.Add(metadataHeaderKey, metadataHeaderValue)
193-
resp, err := c.cli.Do(req)
194-
if err != nil {
195-
return Network{}, errors.Wrap(err, "error querying IMDS")
196-
}
197-
defer resp.Body.Close()
198-
199-
if resp.StatusCode != http.StatusOK {
200-
return Network{}, errors.Wrapf(ErrUnexpectedStatusCode, "unexpected status code %d", resp.StatusCode)
201-
}
202-
203-
body, err := ioutil.ReadAll(resp.Body)
204-
if err != nil {
205-
fmt.Println("Error reading response:", err)
206-
return Network{}, err
207-
}
208-
209-
logger.Printf("Complete IMDS call response body: %v", body)
210-
211-
var m Network
212-
if err := json.Unmarshal(body, &m); err != nil { // .NewDecoder(resp.Body).Decode(&m); err != nil {
213-
return Network{}, errors.Wrap(err, "error decoding IMDS response as json")
214-
}
215-
216-
return m, nil
217-
}

cns/kubecontroller/nodenetworkconfig/conversion.go

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ import (
77
"strings"
88

99
"github.com/Azure/azure-container-networking/cns"
10-
"github.com/Azure/azure-container-networking/cns/logger"
1110
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
12-
"github.com/Azure/azure-container-networking/netlink"
1311
"github.com/pkg/errors"
1412
)
1513

@@ -104,46 +102,5 @@ func CreateNCRequestFromStaticNC(nc v1alpha.NetworkContainer) (*cns.CreateNetwor
104102
return nil, errors.Wrapf(err, "error while creating NC request from static NC")
105103
}
106104

107-
logger.Printf("[CreateNCRequestFromStaticNC] Created NC request %+v", req)
108-
109-
err = assignIPToDelegatedNIC(nc)
110-
111105
return req, err
112106
}
113-
114-
func assignIPToDelegatedNIC(nc v1alpha.NetworkContainer) error {
115-
logger.Printf("[assignIPToDelegatedNIC] Before Assign IP to the Delegated NIC")
116-
117-
// Assign IP to the Delegated NIC
118-
nl := netlink.NewNetlink()
119-
120-
if nl == nil {
121-
logger.Printf("failed to create netlink handle")
122-
return errors.New("failed to create netlink handle")
123-
}
124-
125-
ip, addr, _ := net.ParseCIDR(nc.PrimaryIP)
126-
127-
logger.Printf("[assignIPToDelegatedNIC] ip %s addr %s", ip, addr)
128-
129-
err := nl.AddIPAddress("eth1", ip, addr)
130-
131-
if err != nil {
132-
errors.Wrapf(err, "failed to assign IP to delegated NIC")
133-
}
134-
135-
ipv6, addrv6, _ := net.ParseCIDR(nc.PrimaryIPv6)
136-
137-
logger.Printf("[assignIPToDelegatedNIC] ip %s addr %s", ipv6, addrv6)
138-
139-
if ipv6 != nil {
140-
errv6 := nl.AddIPAddress("eth1", ipv6, addrv6)
141-
142-
if errv6 != nil {
143-
errors.Wrapf(errv6, "failed to assign V6 IP to delegated NIC")
144-
}
145-
}
146-
147-
logger.Printf("[assignIPToDelegatedNIC] After Assign IP to the Delegated NIC")
148-
return err
149-
}

cns/kubecontroller/nodenetworkconfig/conversion_linux.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package nodenetworkconfig
22

33
import (
4-
"fmt"
54
"net/netip"
65
"strconv"
76

@@ -28,20 +27,12 @@ func createNCRequestFromStaticNCHelper(nc v1alpha.NetworkContainer, primaryIPPre
2827
NCVersion: int(nc.Version),
2928
}
3029
}
31-
32-
// adds the IPFamily of the primary CIDR to the set
33-
if primaryIPPrefix.Addr().Is4() {
34-
ipFamilies[cns.IPv4Family] = struct{}{}
35-
} else {
36-
ipFamilies[cns.IPv6Family] = struct{}{}
37-
}
3830
}
3931

4032
// Add IPs from CIDR block to the secondary IPConfigs
4133
if nc.Type == v1alpha.VNETBlock {
4234

4335
for _, ipAssignment := range nc.IPAssignments {
44-
// Here we would need to check all other assigned CIDR Blocks that aren't the primary.
4536
cidrPrefix, err := netip.ParsePrefix(ipAssignment.IP)
4637
if err != nil {
4738
return nil, errors.Wrapf(err, "invalid CIDR block: %s", ipAssignment.IP)
@@ -65,8 +56,6 @@ func createNCRequestFromStaticNCHelper(nc v1alpha.NetworkContainer, primaryIPPre
6556
}
6657
}
6758

68-
fmt.Printf("IPFamilies found on NC %+v are %+v", nc.ID, ipFamilies)
69-
7059
return &cns.CreateNetworkContainerRequest{
7160
HostPrimaryIP: nc.NodeIP,
7261
SecondaryIPConfigs: secondaryIPConfigs,
@@ -76,7 +65,7 @@ func createNCRequestFromStaticNCHelper(nc v1alpha.NetworkContainer, primaryIPPre
7665
IPConfiguration: cns.IPConfiguration{
7766
IPSubnet: subnet,
7867
GatewayIPAddress: nc.DefaultGateway,
79-
GatewayIPv6Address: nc.DefaultGatewayIPv6,
68+
GatewayIPv6Address: nc.DefaultGatewayV6,
8069
},
8170
NCStatus: nc.Status,
8271
IPFamilies: ipFamilies,

cns/restserver/ipam.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ func (service *HTTPRestService) requestIPConfigHandlerHelper(ctx context.Context
108108

109109
podIPInfoResult = append(podIPInfoResult, podIPInfo...)
110110

111-
logger.Printf("[requestIPConfigHandlerHelper] PodIPInfo before returning to IPAM: %+v", podIPInfoResult)
112-
113111
return &cns.IPConfigsResponse{
114112
Response: cns.Response{
115113
ReturnCode: types.Success,
@@ -1003,7 +1001,7 @@ func (service *HTTPRestService) AssignAvailableIPConfigs(podInfo cns.PodInfo) ([
10031001
// Map used to get the number of IPFamilies across all NCs
10041002
ipFamilies := map[cns.IPFamily]struct{}{}
10051003

1006-
// Gets the IPFamilies from all NCs and stores them in a map. This will be ued to determine the amount of IPs to return
1004+
// Gets the IPFamilies from all NCs and store them in a map. This will be used to determine the number of IPs to return
10071005
for ncID := range service.state.ContainerStatus {
10081006
for ipFamily := range service.state.ContainerStatus[ncID].CreateNetworkContainerRequest.IPFamilies {
10091007
ipFamilies[ipFamily] = struct{}{}
@@ -1012,16 +1010,18 @@ func (service *HTTPRestService) AssignAvailableIPConfigs(podInfo cns.PodInfo) ([
10121010

10131011
// Makes sure we have at least one IPFamily across all NCs
10141012
numOfIPFamilies := len(ipFamilies)
1015-
if numOfIPFamilies == 0 {
1016-
return nil, ErrNoIPFamilies
1013+
1014+
numberOfIPs := numOfNCs
1015+
if numOfIPFamilies != 0 {
1016+
numberOfIPs = numOfIPFamilies
10171017
}
10181018

10191019
service.Lock()
10201020
defer service.Unlock()
10211021
// Creates a slice of PodIpInfo with the size as number of NCs to hold the result for assigned IP configs
1022-
podIPInfo := make([]cns.PodIpInfo, numOfIPFamilies)
1022+
podIPInfo := make([]cns.PodIpInfo, numberOfIPs)
10231023
// This map is used to store whether or not we have found an available IP from an NC when looping through the pool
1024-
ipsToAssign := make(map[cns.IPFamily]cns.IPConfigurationStatus)
1024+
ipsToAssign := make(map[string]cns.IPConfigurationStatus)
10251025

10261026
// Searches for available IPs in the pool
10271027
for _, ipState := range service.PodIPConfigState {
@@ -1034,26 +1034,28 @@ func (service *HTTPRestService) AssignAvailableIPConfigs(podInfo cns.PodInfo) ([
10341034
ipStateFamily = cns.IPv6Family
10351035
}
10361036

1037+
key := ipState.NCID + string(ipStateFamily)
1038+
10371039
// check if the IP with the same family type exists already
1038-
if _, IPFamilyAlreadyMarkedForAssignment := ipsToAssign[ipStateFamily]; IPFamilyAlreadyMarkedForAssignment {
1040+
if _, ncIPFamilyAlreadyMarkedForAssignment := ipsToAssign[key]; ncIPFamilyAlreadyMarkedForAssignment {
10391041
continue
10401042
}
10411043
// Checks if the current IP is available
10421044
if ipState.GetState() != types.Available {
10431045
continue
10441046
}
1045-
ipsToAssign[ipStateFamily] = ipState
1046-
// Once one IP per container is found break out of the loop and stop searching
1047-
if len(ipsToAssign) == numOfIPFamilies {
1047+
ipsToAssign[key] = ipState
1048+
// Once numberOfIPs per container is found break out of the loop and stop searching
1049+
if len(ipsToAssign) == numberOfIPs {
10481050
break
10491051
}
10501052
}
10511053

1052-
// Checks to make sure we found one IP for each IPFamily
1053-
if len(ipsToAssign) != numOfIPFamilies {
1054+
// Checks to make sure we found one IP for each NCxIPFamily
1055+
if len(ipsToAssign) != numberOfIPs {
10541056
for ncID := range service.state.ContainerStatus {
10551057
for ipFamily := range service.state.ContainerStatus[ncID].CreateNetworkContainerRequest.IPFamilies {
1056-
if _, found := ipsToAssign[ipFamily]; found {
1058+
if _, found := ipsToAssign[ncID+string(ipFamily)]; found {
10571059
continue
10581060
}
10591061
return podIPInfo, errors.Errorf("not enough IPs available of type %s for %s, waiting on Azure CNS to allocate more with NC Status: %s",

cns/restserver/util.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -848,10 +848,7 @@ func (service *HTTPRestService) populateIPConfigInfoUntransacted(ipConfigStatus
848848
podIPInfo.HostPrimaryIPInfo.Subnet = primaryHostInterface.Subnet
849849
podIPInfo.HostPrimaryIPInfo.Gateway = primaryHostInterface.Gateway
850850
podIPInfo.MacAddress = ncStatus.CreateNetworkContainerRequest.NetworkInterfaceInfo.MACAddress
851-
fmt.Printf("podIPInfo from NNC: %+v", podIPInfo)
852-
podIPInfo.NICType = cns.InfraNIC
853-
//podIPInfo.MacAddress = "00-0D-3A-07-2C-D1"
854-
fmt.Printf("podIPInfo after adding interface new: %+v", podIPInfo)
851+
podIPInfo.NICType = cns.InfraNIC // Update this to DelegatedNIC when it is Prefix on NIC v6
855852

856853
return nil
857854
}

0 commit comments

Comments
 (0)