-
Notifications
You must be signed in to change notification settings - Fork 59
/
azure.go
71 lines (63 loc) · 1.8 KB
/
azure.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
package metadata
import (
"encoding/json"
"k8s.io/klog/v2"
"net/http"
)
const (
azureEndpoint = "http://169.254.169.254/metadata/instance"
)
type azureIp struct {
Private string `json:"privateIpAddress"`
Public string `json:"publicIpAddress"`
}
type azureInterface struct {
Ipv4 struct {
IpAddress []azureIp `json:"ipAddress"`
}
}
type azureInstanceMetadata struct {
Compute struct {
Region string `json:"location"`
Id string `json:"vmID"`
Type string `json:"vmSize"`
Zone string `json:"zone"`
SubscriptionId string `json:"subscriptionId"`
}
Network struct {
Interface []azureInterface `json:"interface"`
}
}
func getAzureMetadata() *CloudMetadata {
r, _ := http.NewRequest(http.MethodGet, azureEndpoint, nil)
r.Header.Add("Metadata", "True")
q := r.URL.Query()
q.Add("format", "json")
q.Add("api-version", "2021-05-01")
r.URL.RawQuery = q.Encode()
resp, err := httpCallWithTimeout(r)
if err != nil {
klog.Errorln(err)
return nil
}
defer resp.Body.Close()
instanceMd := &azureInstanceMetadata{}
decoder := json.NewDecoder(resp.Body)
if err := decoder.Decode(instanceMd); err != nil {
klog.Errorln("failed to unmarshall response of Azure metadata service:", err)
return nil
}
md := &CloudMetadata{
Provider: CloudProviderAzure,
AccountId: instanceMd.Compute.SubscriptionId,
InstanceId: instanceMd.Compute.Id,
InstanceType: instanceMd.Compute.Type,
Region: instanceMd.Compute.Region,
AvailabilityZone: instanceMd.Compute.Zone,
}
if len(instanceMd.Network.Interface) > 0 && len(instanceMd.Network.Interface[0].Ipv4.IpAddress) > 0 {
md.LocalIPv4 = instanceMd.Network.Interface[0].Ipv4.IpAddress[0].Private
md.PublicIPv4 = instanceMd.Network.Interface[0].Ipv4.IpAddress[0].Public
}
return md
}