Skip to content

Commit

Permalink
Merge pull request #167 from coroot/alibaba_metadata
Browse files Browse the repository at this point in the history
instance metadata: add support for Alibaba Cloud
  • Loading branch information
def authored Jan 7, 2025
2 parents 67a13bb + c8449aa commit 4f95b6b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
43 changes: 43 additions & 0 deletions node/metadata/alibaba.go
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 7 additions & 1 deletion node/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
CloudProviderAzure CloudProvider = "Azure"
CloudProviderHetzner CloudProvider = "Hetzner"
CloudProviderDigitalOcean CloudProvider = "DigitalOcean"
CloudProviderAlibaba CloudProvider = "Alibaba"
CloudProviderUnknown CloudProvider = ""
)

Expand Down Expand Up @@ -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
Expand All @@ -76,6 +80,8 @@ func GetInstanceMetadata() *CloudMetadata {
return getHetznerMetadata()
case CloudProviderDigitalOcean:
return getDigitalOceanMetadata()
case CloudProviderAlibaba:
return getAlibabaMetadata()
}
return nil
}
Expand Down

0 comments on commit 4f95b6b

Please sign in to comment.