-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from coroot/scaleway_metadata
instance metadata: add support for Scaleway
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package metadata | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"regexp" | ||
"strings" | ||
|
||
"k8s.io/klog/v2" | ||
) | ||
|
||
const scalewayInstanceMetadataURL = "http://169.254.42.42/conf" | ||
|
||
var scalewayAZSuffix = regexp.MustCompile(`-(\d+)$`) | ||
|
||
func getScalewayMetadata() *CloudMetadata { | ||
r, _ := http.NewRequest(http.MethodGet, scalewayInstanceMetadataURL, nil) | ||
resp, err := httpCallWithTimeout(r) | ||
if err != nil { | ||
klog.Warningln(err) | ||
return nil | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode != http.StatusOK { | ||
klog.Warningf("got %s from Scaleway metadata API", resp.Status) | ||
return nil | ||
} | ||
payload, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
klog.Warningln(err) | ||
return nil | ||
} | ||
md := map[string]string{} | ||
for _, l := range strings.Split(string(payload), "\n") { | ||
kv := strings.SplitN(l, "=", 2) | ||
if len(kv) != 2 { | ||
continue | ||
} | ||
md[kv[0]] = kv[1] | ||
} | ||
return &CloudMetadata{ | ||
Provider: CloudProviderScaleway, | ||
InstanceId: md["ID"], | ||
Region: scalewayAZSuffix.ReplaceAllString(md["ZONE"], ""), | ||
AvailabilityZone: md["ZONE"], | ||
InstanceType: md["COMMERCIAL_TYPE"], | ||
} | ||
} |