/
azure.go
149 lines (130 loc) · 4 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
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
/*
Copyright 2020 Elotl Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cloud
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const (
metadataURL = "http://169.254.169.254/metadata/instance"
)
type ComputeMetadata struct {
AZEnvironment string `json:"azEnvironment,omitempty"`
SKU string `json:"sku,omitempty"`
Name string `json:"name,omitempty"`
Zone string `json:"zone,omitempty"`
VMSize string `json:"vmSize,omitempty"`
OSType string `json:"osType,omitempty"`
Location string `json:"location,omitempty"`
PlatformFaultDomain string `json:"platformFaultDomain,omitempty"`
PlatformUpdateDomain string `json:"platformUpdateDomain,omitempty"`
ResourceGroupName string `json:"resourceGroupName,omitempty"`
VMScaleSetName string `json:"vmScaleSetName,omitempty"`
SubscriptionID string `json:"subscriptionId,omitempty"`
}
type NetworkMetadata struct {
Interface []NetworkInterface `json:"interface"`
}
type NetworkInterface struct {
IPv4 NetworkData `json:"ipv4"`
IPv6 NetworkData `json:"ipv6"`
MACAddress string `json:"macAddress"`
}
type NetworkData struct {
IPAddress []IPAddress `json:"ipAddress"`
Subnet []Subnet `json:"subnet"`
}
type IPAddress struct {
PrivateIPAddress string `json:"privateIpAddress"`
PublicIPAddress string `json:"publicIpAddress"`
}
type Subnet struct {
Address string `json:"address"`
Prefix string `json:"prefix"`
}
// InstanceMetadata represents instance information.
type InstanceMetadata struct {
Compute *ComputeMetadata `json:"compute,omitempty"`
Network *NetworkMetadata `json:"network,omitempty"`
}
type AzureCloudInfo struct {
url string
}
func NewAzureCloudInfo(url string) (CloudInfo, error) {
if url == "" {
url = metadataURL
}
az := &AzureCloudInfo{
url: url,
}
_, err := az.getInstanceMetadata()
if err != nil {
return nil, err
}
return az, nil
}
// Get the main IP address (first IP on the main interface).
func (a *AzureCloudInfo) GetMainIPv4Address() (string, error) {
return a.getIPv4Address(0)
}
// Get the IP address (second IP on the main interface) assigned to the pod.
func (a *AzureCloudInfo) GetPodIPv4Address() (string, error) {
return a.getIPv4Address(1)
}
func (a *AzureCloudInfo) getIPv4Address(n int) (string, error) {
metadata, err := a.getInstanceMetadata()
if err != nil {
return "", err
}
if metadata.Network == nil ||
len(metadata.Network.Interface) < 1 ||
len(metadata.Network.Interface[0].IPv4.IPAddress) < n+1 {
return "", fmt.Errorf("IP address #%d not found (addresses: %v)",
n, metadata.Network.Interface[0].IPv4.IPAddress)
}
return metadata.Network.Interface[0].IPv4.IPAddress[n].PrivateIPAddress, nil
}
func (a *AzureCloudInfo) getInstanceMetadata() (*InstanceMetadata, error) {
req, err := http.NewRequest("GET", a.url, nil)
if err != nil {
return nil, err
}
req.Header.Add("Metadata", "True")
req.Header.Add("User-Agent", "elotl/itzo")
q := req.URL.Query()
q.Add("format", "json")
q.Add("api-version", "2019-03-11")
req.URL.RawQuery = q.Encode()
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf(
"getting instance metadata: got response %q", resp.Status)
}
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
metadata := InstanceMetadata{}
err = json.Unmarshal(buf, &metadata)
if err != nil {
return nil, err
}
return &metadata, nil
}