forked from eksctl-io/eksctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
195 lines (165 loc) · 4.99 KB
/
main.go
File metadata and controls
195 lines (165 loc) · 4.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package main
import (
"context"
"fmt"
"os"
"regexp"
"text/template"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
)
type InstanceInfo struct {
InstanceType string
InstanceStorageSupported bool
EFASupported bool
NvidiaGPUSupported bool
NvidiaGPUType string
NeuronSupported bool
NeuronDeviceType string
CBRSupported bool
CPUArch string
}
const ec2InstancesTemplate = `// / Generated by ` + "`" + `ec2geninfo` + "`" + `
package instance
var InstanceTypesMap = map[string]InstanceInfo{}
func init() {
for _, instance := range InstanceTypes {
InstanceTypesMap[instance.InstanceType] = instance
}
}
type InstanceInfo struct { //nolint
InstanceType string
InstanceStorageSupported bool
EFASupported bool
NvidiaGPUSupported bool
NvidiaGPUType string
NeuronSupported bool
NeuronDeviceType string
CBRSupported bool
CPUArch string
}
var InstanceTypes = []InstanceInfo{
{{- range . }}
{
InstanceType: "{{ .InstanceType }}",
InstanceStorageSupported: {{ .InstanceStorageSupported }},
EFASupported: {{ .EFASupported }},
NvidiaGPUSupported: {{ .NvidiaGPUSupported }},
NvidiaGPUType: "{{ .NvidiaGPUType }}",
NeuronSupported: {{ .NeuronSupported }},
NeuronDeviceType: "{{ .NeuronDeviceType }}",
CBRSupported: {{ .CBRSupported }},
CPUArch: "{{ .CPUArch }}",
},
{{- end }}
}
`
func main() {
err := updateEC2Instances()
if err != nil {
fmt.Fprintf(os.Stderr, "Error updating EC2 instances: %v\n", err)
os.Exit(1)
}
}
func updateEC2Instances() error {
regions := []string{"us-east-1", "us-east-2", "us-west-2"}
instances := make(map[string]InstanceInfo)
for _, region := range regions {
var err error
instances, err = getEC2Instances(region, instances)
if err != nil {
return err
}
}
tmpl, err := template.New("ec2InstancesTemplate").Parse(ec2InstancesTemplate)
if err != nil {
return err
}
file, err := os.Create("pkg/utils/instance/instance_types.go")
if err != nil {
return err
}
defer file.Close()
err = tmpl.Execute(file, instances)
if err != nil {
return err
}
return nil
}
func getEC2Instances(region string, instances map[string]InstanceInfo) (map[string]InstanceInfo, error) {
cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRegion(region))
if err != nil {
return nil, err
}
client := ec2.NewFromConfig(cfg)
input := &ec2.DescribeInstanceTypesInput{
Filters: []types.Filter{
{Name: aws.String("current-generation"), Values: []string{"true"}},
{Name: aws.String("bare-metal"), Values: []string{"false"}},
},
}
paginator := ec2.NewDescribeInstanceTypesPaginator(client, input)
unsupportedRegexp, _ := regexp.Compile("^(p2).*")
for paginator.HasMorePages() {
page, err := paginator.NextPage(context.TODO())
if err != nil {
return nil, err
}
for _, inst := range page.InstanceTypes {
itype := string(inst.InstanceType)
if unsupportedRegexp.MatchString(itype) {
continue
}
efaSupported := inst.NetworkInfo != nil && inst.NetworkInfo.EfaSupported != nil && *inst.NetworkInfo.EfaSupported
nvidiaGPUSupported := false
nvidiaGPUType := ""
if inst.GpuInfo != nil && len(inst.GpuInfo.Gpus) > 0 {
nvidiaGPUSupported = *inst.GpuInfo.Gpus[0].Manufacturer == "NVIDIA"
if nvidiaGPUSupported {
nvidiaGPUType = *inst.GpuInfo.Gpus[0].Name
}
}
neuronSupported := inst.NeuronInfo != nil
neuronDeviceType := ""
if neuronSupported {
for _, acc := range inst.NeuronInfo.NeuronDevices {
neuronDeviceType = *acc.Name
break
}
}
cbrSupported := false
if inst.SupportedUsageClasses != nil {
for _, usageClass := range inst.SupportedUsageClasses {
if usageClass == types.UsageClassTypeCapacityBlock {
cbrSupported = true
break
}
}
}
cpuArch := "unknown"
if inst.ProcessorInfo != nil && inst.ProcessorInfo.SupportedArchitectures != nil {
for _, arch := range inst.ProcessorInfo.SupportedArchitectures {
if arch == types.ArchitectureTypeArm64 || arch == types.ArchitectureTypeArm64Mac {
cpuArch = "arm64"
} else if arch == types.ArchitectureTypeX8664 || arch == types.ArchitectureTypeX8664Mac {
cpuArch = "x86-64"
}
}
}
instances[itype] = InstanceInfo{
InstanceType: itype,
InstanceStorageSupported: inst.InstanceStorageSupported != nil && *inst.InstanceStorageSupported,
EFASupported: efaSupported,
NvidiaGPUSupported: nvidiaGPUSupported,
NvidiaGPUType: nvidiaGPUType,
NeuronSupported: neuronSupported,
NeuronDeviceType: neuronDeviceType,
CBRSupported: cbrSupported,
CPUArch: cpuArch,
}
}
}
return instances, nil
}