Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instance metadata: add support for Alibaba Cloud #167

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading