-
Notifications
You must be signed in to change notification settings - Fork 244
/
Copy pathutils.go
308 lines (259 loc) · 7.89 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright 2017 Microsoft. All rights reserved.
// MIT License
package common
import (
"context"
"encoding/binary"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"net"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/Azure/azure-container-networking/log"
)
const (
metadataURL = "http://169.254.169.254/metadata/instance?api-version=2017-08-01&format=json"
azCloudUrl = "http://169.254.169.254/metadata/instance/compute/azEnvironment?api-version=2018-10-01&format=text"
httpConnectionTimeout = 7
headerTimeout = 7
RegisterNodeURLFmt = "%s/%s/node/%s%s"
SyncNodeNetworkContainersURLFmt = "%s/%s/node/%s%s"
FiveSeconds = 5 * time.Second
JsonContent = "application/json; charset=UTF-8"
ContentType = "Content-Type"
)
// XmlDocument - Azure host agent XML document format.
type XmlDocument struct {
XMLName xml.Name `xml:"Interfaces"`
Interface []struct {
XMLName xml.Name `xml:"Interface"`
MacAddress string `xml:"MacAddress,attr"`
IsPrimary bool `xml:"IsPrimary,attr"`
IPSubnet []struct {
XMLName xml.Name `xml:"IPSubnet"`
Prefix string `xml:"Prefix,attr"`
IPAddress []struct {
XMLName xml.Name `xml:"IPAddress"`
Address string `xml:"Address,attr"`
IsPrimary bool `xml:"IsPrimary,attr"`
}
}
}
}
// Metadata retrieved from wireserver
type Metadata struct {
Location string `json:"location"`
VMName string `json:"name"`
Offer string `json:"offer"`
OsType string `json:"osType"`
PlacementGroupID string `json:"placementGroupId"`
PlatformFaultDomain string `json:"platformFaultDomain"`
PlatformUpdateDomain string `json:"platformUpdateDomain"`
Publisher string `json:"publisher"`
ResourceGroupName string `json:"resourceGroupName"`
Sku string `json:"sku"`
SubscriptionID string `json:"subscriptionId"`
Tags string `json:"tags"`
OSVersion string `json:"version"`
VMID string `json:"vmId"`
VMSize string `json:"vmSize"`
KernelVersion string
}
// This is how metadata server returns in response for querying metadata
type metadataWrapper struct {
Metadata Metadata `json:"compute"`
}
// Creating http client object to be reused instead of creating one every time.
// This helps make use of the cached tcp connections.
// Clients are safe for concurrent use by multiple goroutines.
var httpClient *http.Client
// InitHttpClient initializes the httpClient object
func InitHttpClient(
connectionTimeoutSec int,
responseHeaderTimeoutSec int) *http.Client {
log.Printf("[Utils] Initializing HTTP client with connection timeout: %d, response header timeout: %d",
connectionTimeoutSec, responseHeaderTimeoutSec)
httpClient = &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: time.Duration(connectionTimeoutSec) * time.Second,
}).DialContext,
ResponseHeaderTimeout: time.Duration(responseHeaderTimeoutSec) * time.Second,
},
}
return httpClient
}
// GetHttpClient returns the singleton httpClient object
func GetHttpClient() *http.Client {
return httpClient
}
// LogNetworkInterfaces logs the host's network interfaces in the default namespace.
func LogNetworkInterfaces() {
interfaces, err := net.Interfaces()
if err != nil {
log.Printf("Failed to query network interfaces, err:%v", err)
return
}
for _, iface := range interfaces {
addrs, _ := iface.Addrs()
log.Printf("[net] Network interface: %+v with IP: %+v", iface, addrs)
}
}
func IpToInt(ip net.IP) uint32 {
if len(ip) == 16 {
return binary.BigEndian.Uint32(ip[12:16])
}
return binary.BigEndian.Uint32(ip)
}
func GetInterfaceSubnetWithSpecificIP(ipAddr string) *net.IPNet {
addrs, err := net.InterfaceAddrs()
if err != nil {
log.Printf("InterfaceAddrs failed with %+v", err)
return nil
}
for _, a := range addrs {
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
if ipnet.IP.String() == ipAddr {
return ipnet
}
}
}
}
return nil
}
func StartProcess(path string, args []string) error {
attr := os.ProcAttr{
Env: os.Environ(),
Files: []*os.File{
os.Stdin,
nil,
nil,
},
}
processArgs := append([]string{path}, args...)
process, err := os.StartProcess(path, processArgs, &attr)
if err == nil {
// Release detaches the process
return process.Release()
}
return err
}
// GetHostMetadata - retrieve VM metadata from wireserver
func GetHostMetadata(fileName string) (Metadata, error) {
content, err := os.ReadFile(fileName)
if err == nil {
var metadata Metadata
if err = json.Unmarshal(content, &metadata); err == nil {
return metadata, nil
}
}
log.Printf("[Telemetry] Request metadata from wireserver")
req, err := http.NewRequest("GET", metadataURL, nil)
if err != nil {
return Metadata{}, err
}
req.Header.Set("Metadata", "True")
client := &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: time.Duration(httpConnectionTimeout) * time.Second,
}).DialContext,
ResponseHeaderTimeout: time.Duration(headerTimeout) * time.Second,
},
}
resp, err := client.Do(req)
if err != nil {
return Metadata{}, err
}
defer resp.Body.Close()
metareport := metadataWrapper{}
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("[Telemetry] Request failed with HTTP error %d", resp.StatusCode)
} else if resp.Body != nil {
err = json.NewDecoder(resp.Body).Decode(&metareport)
if err != nil {
err = fmt.Errorf("[Telemetry] Unable to decode response body due to error: %s", err.Error())
}
} else {
err = fmt.Errorf("[Telemetry] Response body is empty")
}
return metareport.Metadata, err
}
// SaveHostMetadata - save metadata got from wireserver to json file
func SaveHostMetadata(metadata Metadata, fileName string) error {
dataBytes, err := json.Marshal(metadata)
if err != nil {
return fmt.Errorf("[Telemetry] marshal data failed with err %+v", err)
}
if err = os.WriteFile(fileName, dataBytes, 0o644); err != nil {
log.Printf("[Telemetry] Writing metadata to file failed: %v", err)
}
return err
}
func GetAzureCloud(url string) (string, error) {
if url == "" {
url = azCloudUrl
}
log.Printf("GetAzureCloud querying url: %s", url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
req.Header.Set("Metadata", "True")
client := InitHttpClient(httpConnectionTimeout, headerTimeout)
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("Bad http status:%v", resp.Status)
}
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
return strings.TrimSpace(string(bodyBytes)), nil
}
func GetExecutableDirectory() (string, error) {
var (
dir string
ex string
err error
)
ex, err = os.Executable()
if err == nil {
dir = filepath.Dir(ex)
} else {
var exReal string
// If a symlink was used to start the process, depending on the operating system,
// the result might be the symlink or the path it pointed to.
// filepath.EvalSymlinks returns stable results
exReal, err = filepath.EvalSymlinks(ex)
if err == nil {
dir = filepath.Dir(exReal)
}
}
return dir, err
}
func PostCtx(ctx context.Context, cl *http.Client, url, contentType string, body io.Reader) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, body)
if err != nil {
return nil, fmt.Errorf("could not create POST request: %w", err)
}
req.Header.Set("Content-Type", contentType)
var resp *http.Response
resp, err = cl.Do(req)
if err != nil {
// returning response as well
// cause some methods seem to depend on that for error handling
return resp, fmt.Errorf("POST request received response %w", err)
}
return resp, nil
}