-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load AWS EC2 Instance Types dynamically
- Loading branch information
Showing
8 changed files
with
235 additions
and
114 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
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
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,131 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package aws | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"github.com/aws/aws-sdk-go/aws/endpoints" | ||
"io/ioutil" | ||
"k8s.io/klog" | ||
"net/http" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
type response struct { | ||
Products map[string]product `json:"products"` | ||
} | ||
|
||
type product struct { | ||
Attributes productAttributes `json:"attributes"` | ||
} | ||
|
||
type productAttributes struct { | ||
InstanceType string `json:"instanceType"` | ||
VCPU string `json:"vcpu"` | ||
Memory string `json:"memory"` | ||
GPU string `json:"gpu"` | ||
} | ||
|
||
// GenerateEC2InstanceTypes returns a map of ec2 resources | ||
func GenerateEC2InstanceTypes() (map[string]*InstanceType, error) { | ||
instanceTypes := make(map[string]*InstanceType) | ||
|
||
resolver := endpoints.DefaultResolver() | ||
partitions := resolver.(endpoints.EnumPartitions).Partitions() | ||
|
||
for _, p := range partitions { | ||
for _, r := range p.Regions() { | ||
url := "https://pricing.us-east-1.amazonaws.com/offers/v1.0/aws/AmazonEC2/current/" + r.ID() + "/index.json" | ||
klog.V(1).Infof("fetching %s\n", url) | ||
res, err := http.Get(url) | ||
if err != nil { | ||
klog.Warningf("Error fetching %s skipping...\n", url) | ||
continue | ||
} | ||
|
||
defer res.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
klog.Warningf("Error parsing %s skipping...\n", url) | ||
continue | ||
} | ||
|
||
var unmarshalled = response{} | ||
err = json.Unmarshal(body, &unmarshalled) | ||
if err != nil { | ||
klog.Warningf("Error unmarshalling %s, skip...\n", url) | ||
continue | ||
} | ||
|
||
for _, product := range unmarshalled.Products { | ||
attr := product.Attributes | ||
if attr.InstanceType != "" { | ||
instanceTypes[attr.InstanceType] = &InstanceType{ | ||
InstanceType: attr.InstanceType, | ||
} | ||
if attr.Memory != "" && attr.Memory != "NA" { | ||
instanceTypes[attr.InstanceType].MemoryMb = parseMemory(attr.Memory) | ||
} | ||
if attr.VCPU != "" { | ||
instanceTypes[attr.InstanceType].VCPU = parseCPU(attr.VCPU) | ||
} | ||
if attr.GPU != "" { | ||
instanceTypes[attr.InstanceType].GPU = parseCPU(attr.GPU) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if len(instanceTypes) == 0 { | ||
return nil, errors.New("unable to load EC2 Instance Type list") | ||
} | ||
|
||
return instanceTypes, nil | ||
} | ||
|
||
// GetStaticEC2InstanceTypes return pregenerated ec2 instance type list | ||
func GetStaticEC2InstanceTypes() map[string]*InstanceType { | ||
return InstanceTypes | ||
} | ||
|
||
func parseMemory(memory string) int64 { | ||
reg, err := regexp.Compile("[^0-9\\.]+") | ||
if err != nil { | ||
klog.Fatal(err) | ||
} | ||
|
||
parsed := strings.TrimSpace(reg.ReplaceAllString(memory, "")) | ||
mem, err := strconv.ParseFloat(parsed, 64) | ||
if err != nil { | ||
klog.Fatal(err) | ||
} | ||
|
||
return int64(mem * float64(1024)) | ||
} | ||
|
||
func parseCPU(cpu string) int64 { | ||
i, err := strconv.ParseInt(cpu, 10, 64) | ||
if err != nil { | ||
klog.Fatal(err) | ||
} | ||
return i | ||
} |
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,75 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package aws | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
func TestGetStaticEC2InstanceTypes(t *testing.T) { | ||
result := GetStaticEC2InstanceTypes() | ||
assert.True(t, len(result) != 0) | ||
} | ||
|
||
func TestParseMemory(t *testing.T) { | ||
expectedResultInMiB := int64(3.75 * 1024) | ||
tests := []struct { | ||
input string | ||
expect int64 | ||
}{ | ||
{ | ||
input: "3.75 GiB", | ||
expect: expectedResultInMiB, | ||
}, | ||
{ | ||
input: "3.75 Gib", | ||
expect: expectedResultInMiB, | ||
}, | ||
{ | ||
input: "3.75GiB", | ||
expect: expectedResultInMiB, | ||
}, | ||
{ | ||
input: "3.75", | ||
expect: expectedResultInMiB, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
got := parseMemory(test.input) | ||
assert.Equal(t, test.expect, got) | ||
} | ||
} | ||
|
||
func TestParseCPU(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expect int64 | ||
}{ | ||
{ | ||
input: strconv.FormatInt(8, 10), | ||
expect: int64(8), | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
got := parseCPU(test.input) | ||
assert.Equal(t, test.expect, got) | ||
} | ||
} |
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
Oops, something went wrong.