Skip to content

Commit df130cd

Browse files
committed
merge from master
1 parent 3a1fb2b commit df130cd

File tree

14 files changed

+766
-315
lines changed

14 files changed

+766
-315
lines changed

azure-ipam/ipam.go

Lines changed: 26 additions & 2 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)
84+
resp, err := p.cnsClient.RequestIPs(context.TODO(), req) // need to add interfaces to this response
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,9 +113,10 @@ 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
116117

117118
// Get Pod IP and gateway IP from ip config response
118-
podIPNet, err := ipconfig.ProcessIPConfigsResp(resp)
119+
podIPNet, gatewayIP, err := ipconfig.ProcessIPConfigsResp(resp) // need to get interfaces out of the response and add it here
119120
if err != nil {
120121
p.logger.Error("Failed to interpret CNS IPConfigResponse", zap.Error(err), zap.Any("response", resp))
121122
return cniTypes.NewError(ErrProcessIPConfigResponse, err.Error(), "failed to interpret CNS IPConfigResponse")
@@ -130,15 +131,38 @@ func (p *IPAMPlugin) CmdAdd(args *cniSkel.CmdArgs) error {
130131
IP: net.ParseIP(ipNet.Addr().String()),
131132
Mask: net.CIDRMask(ipNet.Bits(), 32), // nolint
132133
}
134+
135+
ipConfig.Gateway = (*gatewayIP)[i]
136+
p.logger.Debug("Gatewayv4", zap.String("Gatewayv4", ipConfig.Gateway.String()))
133137
} else {
134138
ipConfig.Address = net.IPNet{
135139
IP: net.ParseIP(ipNet.Addr().String()),
136140
Mask: net.CIDRMask(ipNet.Bits(), 128), // nolint
137141
}
142+
143+
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+
}
138150
}
139151
cniResult.IPs[i] = ipConfig
140152
}
141153

154+
p.logger.Info("MACAddress:", zap.Any("MACAddress", resp.PodIPInfo[0].MacAddress))
155+
156+
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,
161+
}
162+
cniResult.Interfaces[0] = interface_test
163+
164+
p.logger.Info("Created CNIResult:", zap.Any("result", cniResult))
165+
142166
// Get versioned result
143167
versionedCniResult, err := cniResult.GetAsVersion(nwCfg.CNIVersion)
144168
if err != nil {

azure-ipam/ipconfig/ipconfig.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ipconfig
33
import (
44
"encoding/json"
55
"fmt"
6+
"net"
67
"net/netip"
78

89
"github.com/Azure/azure-container-networking/cns"
@@ -63,8 +64,9 @@ func CreateIPConfigsReq(args *cniSkel.CmdArgs) (cns.IPConfigsRequest, error) {
6364
return req, nil
6465
}
6566

66-
func ProcessIPConfigsResp(resp *cns.IPConfigsResponse) (*[]netip.Prefix, error) {
67+
func ProcessIPConfigsResp(resp *cns.IPConfigsResponse) (*[]netip.Prefix, *[]net.IP, error) {
6768
podIPNets := make([]netip.Prefix, len(resp.PodIPInfo))
69+
gatewaysIPs := make([]net.IP, len(resp.PodIPInfo))
6870

6971
for i := range resp.PodIPInfo {
7072
podCIDR := fmt.Sprintf(
@@ -74,12 +76,29 @@ func ProcessIPConfigsResp(resp *cns.IPConfigsResponse) (*[]netip.Prefix, error)
7476
)
7577
podIPNet, err := netip.ParsePrefix(podCIDR)
7678
if err != nil {
77-
return nil, errors.Wrapf(err, "cns returned invalid pod CIDR %q", podCIDR)
79+
return nil, nil, errors.Wrapf(err, "cns returned invalid pod CIDR %q", podCIDR)
7880
}
7981
podIPNets[i] = podIPNet
82+
83+
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
90+
} else if podIPNet.Addr().Is6() {
91+
gatewayIP := net.ParseIP(resp.PodIPInfo[i].NetworkContainerPrimaryIPConfig.GatewayIPv6Address)
92+
93+
if gatewayIP == nil {
94+
return nil, nil, errors.New("cns returned invalid gateway IPv6 address")
95+
}
96+
gatewaysIPs[i] = gatewayIP
97+
}
98+
8099
}
81100

82-
return &podIPNets, nil
101+
return &podIPNets, &gatewaysIPs, nil
83102
}
84103

85104
type k8sPodEnvArgs struct {

cns/NetworkContainerContract.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ 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{}
132133
}
133134

134135
func (req *CreateNetworkContainerRequest) Validate() error {
@@ -389,9 +390,10 @@ type NetworkInterfaceInfo struct {
389390

390391
// IPConfiguration contains details about ip config to provision in the VM.
391392
type IPConfiguration struct {
392-
IPSubnet IPSubnet
393-
DNSServers []string
394-
GatewayIPAddress string
393+
IPSubnet IPSubnet
394+
DNSServers []string
395+
GatewayIPAddress string
396+
GatewayIPv6Address string
395397
}
396398

397399
// SecondaryIPConfig contains IP info of SecondaryIP
@@ -746,3 +748,11 @@ type NodeRegisterRequest struct {
746748
NumCores int
747749
NmAgentSupportedApis []string
748750
}
751+
752+
// IPFamily - Enum for determining IPFamily when retrieving IPs from network containers
753+
type IPFamily string
754+
755+
const (
756+
IPv4Family IPFamily = "ipv4"
757+
IPv6Family IPFamily = "ipv6"
758+
)

cns/imds/client.go

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

14+
"github.com/Azure/azure-container-networking/cns/logger"
1215
"github.com/avast/retry-go/v4"
1316
"github.com/pkg/errors"
1417
)
@@ -59,6 +62,17 @@ var (
5962
ErrUnexpectedStatusCode = errors.New("imds returned an unexpected status code")
6063
)
6164

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+
6276
// NewClient creates a new imds client
6377
func NewClient(opts ...ClientOption) *Client {
6478
config := clientConfig{
@@ -80,15 +94,41 @@ func (c *Client) GetVMUniqueID(ctx context.Context) (string, error) {
8094
var vmUniqueID string
8195
err := retry.Do(func() error {
8296
computeDoc, err := c.getInstanceComputeMetadata(ctx)
97+
8398
if err != nil {
8499
return errors.Wrap(err, "error getting IMDS compute metadata")
85100
}
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+
86115
vmUniqueIDUntyped := computeDoc[vmUniqueIDProperty]
87116
var ok bool
88117
vmUniqueID, ok = vmUniqueIDUntyped.(string)
89118
if !ok {
90119
return errors.New("unable to parse IMDS compute metadata, vmId property is not a string")
91120
}
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+
92132
return nil
93133
}, retry.Context(ctx), retry.Attempts(c.config.retryAttempts), retry.DelayType(retry.BackOffDelay))
94134
if err != nil {
@@ -126,10 +166,52 @@ func (c *Client) getInstanceComputeMetadata(ctx context.Context) (map[string]any
126166
return nil, errors.Wrapf(ErrUnexpectedStatusCode, "unexpected status code %d", resp.StatusCode)
127167
}
128168

169+
logger.Printf("Complete IMDS call response body: %v", resp.Body)
170+
129171
var m map[string]any
130172
if err := json.NewDecoder(resp.Body).Decode(&m); err != nil {
131173
return nil, errors.Wrap(err, "error decoding IMDS response as json")
132174
}
133175

134176
return m, nil
135177
}
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"strings"
88

99
"github.com/Azure/azure-container-networking/cns"
10+
"github.com/Azure/azure-container-networking/cns/logger"
1011
"github.com/Azure/azure-container-networking/crd/nodenetworkconfig/api/v1alpha"
12+
"github.com/Azure/azure-container-networking/netlink"
1113
"github.com/pkg/errors"
1214
)
1315

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

107+
logger.Printf("[CreateNCRequestFromStaticNC] Created NC request %+v", req)
108+
109+
err = assignIPToDelegatedNIC(nc)
110+
105111
return req, err
106112
}
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+
}

0 commit comments

Comments
 (0)