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