-
Notifications
You must be signed in to change notification settings - Fork 59
/
hetzner.go
43 lines (38 loc) · 1.11 KB
/
hetzner.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
package metadata
import (
"net/http"
"gopkg.in/yaml.v2"
"k8s.io/klog/v2"
)
const hetznerInstanceMetadataURL = "http://169.254.169.254/hetzner/v1/metadata"
type hetznerInstanceMetadata struct {
AvailabilityZone string `yaml:"availability-zone"`
InstanceId string `yaml:"instance-id"`
PublicIPv4 string `yaml:"public-ipv4"`
LocalIPv4 string `yaml:"local-ipv4"`
Region string `yaml:"region"`
}
func getHetznerMetadata() *CloudMetadata {
r, _ := http.NewRequest(http.MethodGet, hetznerInstanceMetadataURL, nil)
resp, err := httpCallWithTimeout(r)
if err != nil {
klog.Errorln(err)
return nil
}
defer resp.Body.Close()
md := &hetznerInstanceMetadata{}
decoder := yaml.NewDecoder(resp.Body)
if err := decoder.Decode(md); err != nil {
klog.Errorln("failed to unmarshall response of Hetzner metadata service:", err)
return nil
}
res := &CloudMetadata{
Provider: CloudProviderHetzner,
InstanceId: md.InstanceId,
Region: md.Region,
AvailabilityZone: md.AvailabilityZone,
LocalIPv4: md.LocalIPv4,
PublicIPv4: md.PublicIPv4,
}
return res
}