@@ -6,9 +6,12 @@ package imds
66import (
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)
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
6377func 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+ }
0 commit comments