From c8449aaa775bfa545f322caac53a3458bcde69fa Mon Sep 17 00:00:00 2001 From: Nikolay Sivko Date: Tue, 7 Jan 2025 14:45:14 +0300 Subject: [PATCH] instance metadata: add support for Alibaba Cloud --- node/metadata/alibaba.go | 43 +++++++++++++++++++++++++++++++++++++++ node/metadata/metadata.go | 8 +++++++- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 node/metadata/alibaba.go diff --git a/node/metadata/alibaba.go b/node/metadata/alibaba.go new file mode 100644 index 0000000..d314b8d --- /dev/null +++ b/node/metadata/alibaba.go @@ -0,0 +1,43 @@ +package metadata + +import ( + "io" + "net/http" + + "k8s.io/klog/v2" +) + +const alibabaInstanceMetadataURL = "http://100.100.100.200/latest/meta-data/" + +func getAlibabaMetadata() *CloudMetadata { + var lastErr error + getVar := func(path string) string { + r, _ := http.NewRequest(http.MethodGet, alibabaInstanceMetadataURL+path, nil) + resp, err := httpCallWithTimeout(r) + if err != nil { + lastErr = err + return "" + } + defer resp.Body.Close() + data, err := io.ReadAll(resp.Body) + if err != nil { + lastErr = err + return "" + } + return string(data) + } + res := &CloudMetadata{ + Provider: CloudProviderAlibaba, + InstanceId: getVar("instance-id"), + Region: getVar("region-id"), + AvailabilityZone: getVar("zone-id"), + AccountId: getVar("owner-account-id"), + InstanceType: getVar("instance/instance-type"), + LocalIPv4: getVar("private-ipv4"), + } + if lastErr != nil { + klog.Warningln(lastErr) + return nil + } + return res +} diff --git a/node/metadata/metadata.go b/node/metadata/metadata.go index 6b39e05..d7234c0 100644 --- a/node/metadata/metadata.go +++ b/node/metadata/metadata.go @@ -20,6 +20,7 @@ const ( CloudProviderAzure CloudProvider = "Azure" CloudProviderHetzner CloudProvider = "Hetzner" CloudProviderDigitalOcean CloudProvider = "DigitalOcean" + CloudProviderAlibaba CloudProvider = "Alibaba" CloudProviderUnknown CloudProvider = "" ) @@ -55,8 +56,11 @@ func getCloudProvider() CloudProvider { } } if vendor, err := os.ReadFile("/sys/class/dmi/id/sys_vendor"); err == nil { - if strings.TrimSpace(string(vendor)) == "Hetzner" { + switch strings.TrimSpace(string(vendor)) { + case "Hetzner": return CloudProviderHetzner + case "Alibaba Cloud": + return CloudProviderAlibaba } } return CloudProviderUnknown @@ -76,6 +80,8 @@ func GetInstanceMetadata() *CloudMetadata { return getHetznerMetadata() case CloudProviderDigitalOcean: return getDigitalOceanMetadata() + case CloudProviderAlibaba: + return getAlibabaMetadata() } return nil }