-
Notifications
You must be signed in to change notification settings - Fork 491
/
instancetypes.go
363 lines (335 loc) · 10.8 KB
/
instancetypes.go
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package ec2
import (
"math"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/juju/collections/set"
"github.com/juju/errors"
"github.com/juju/utils/v2/arch"
"github.com/juju/juju/core/constraints"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/context"
"github.com/juju/juju/environs/instances"
)
var _ environs.InstanceTypesFetcher = (*environ)(nil)
// InstanceTypes implements InstanceTypesFetcher
func (e *environ) InstanceTypes(ctx context.ProviderCallContext, c constraints.Value) (instances.InstanceTypesWithCostMetadata, error) {
iTypes, err := e.supportedInstanceTypes(ctx)
if err != nil {
return instances.InstanceTypesWithCostMetadata{}, errors.Trace(err)
}
iTypes, err = instances.MatchingInstanceTypes(iTypes, "", c)
if err != nil {
return instances.InstanceTypesWithCostMetadata{}, errors.Trace(err)
}
return instances.InstanceTypesWithCostMetadata{
InstanceTypes: iTypes,
CostUnit: "$USD/hour",
CostDivisor: 1000,
CostCurrency: "USD"}, nil
}
func calculateCPUPower(instType string, clock *float64, vcpu uint64) uint64 {
// T-class instances have burstable CPU. This is not captured
// in the pricing information, so we have to hard-code it. We
// will have to update this list when T3 instances come along.
switch instType {
case "t1.micro":
return 20
case "t2.nano":
return 5
case "t2.micro":
return 10
case "t2.small":
return 20
case "t2.medium":
return 40
case "t2.large":
return 60
}
if clock == nil {
return vcpu * 100
}
// If the information includes a clock speed, we use that
// to estimate the ECUs. The pricing information does not
// include the ECUs, but they're only estimates anyway.
// Amazon moved to "vCPUs" quite some time ago.
// To date, info.ClockSpeed can have the form "Up to <float> GHz" or
// "<float> GHz", so look for a float match.
return uint64(*clock * 1.4 * 100 * float64(vcpu))
}
// virtType returns a virt type that matches our simplestrams metadata
// rather than what the instance type actually reports.
func virtType(info types.InstanceTypeInfo) *string {
// Only very old instance types are restricted to paravirtual.
switch strings.SplitN(string(info.InstanceType), ".", 2)[0] {
case "t1", "m1", "c1", "m2":
return aws.String("paravirtual")
}
return aws.String("hvm")
}
// supportsClassic reports whether the instance type with the given
// name can be run in EC2-Classic.
//
// At the time of writing, we know that the following instance type
// families support only VPC: C4, M4, P2, T2, X1. However, rather
// than hard-coding that list, we assume that any new instance type
// families support VPC only, and so we hard-code the inverse of the
// list at the time of writing.
//
// See:
// http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-vpc.html#vpc-only-instance-types
func supportsClassic(instanceType string) bool {
classicTypes := set.NewStrings(
"c1", "c3",
"cc2",
"cg1",
"cr1",
"d2",
"g2",
"hi1",
"hs1",
"i2",
"m1", "m2", "m3",
"r3",
"t1",
)
parts := strings.SplitN(instanceType, ".", 2)
if len(parts) < 2 {
return false
}
return classicTypes.Contains(strings.ToLower(parts[0]))
}
var archNames = map[types.ArchitectureType]string{
"x86": arch.I386,
"x86_64": arch.AMD64,
"arm": arch.ARM,
"aarch64": arch.ARM64,
}
func archName(in types.ArchitectureType) string {
if archName, ok := archNames[in]; ok {
return archName
}
return string(in)
}
func (e *environ) supportedInstanceTypes(ctx context.ProviderCallContext) ([]instances.InstanceType, error) {
e.instTypesMutex.Lock()
defer e.instTypesMutex.Unlock()
// Use a cached copy if populated as it's mildly
// expensive to fetch each time.
// TODO(wallyworld) - consider using a cache with expiry
if len(e.instTypes) == 0 {
instTypes, err := e.collectSupportedInstanceTypes(ctx)
if err != nil {
return nil, err
}
e.instTypes = instTypes
}
return e.instTypes, nil
}
// collectSupportedInstanceTypes queries several EC2 APIs and combines the
// results into a slice of InstanceType values.
//
// This method must be called while holding the instTypesMutex.
func (e *environ) collectSupportedInstanceTypes(ctx context.ProviderCallContext) ([]instances.InstanceType, error) {
const (
maxOfferingsResults = 1000
maxTypesPage = 100
)
// First get all the zone names for the current region.
zoneResults, err := e.ec2Client.DescribeAvailabilityZones(ctx, &ec2.DescribeAvailabilityZonesInput{
Filters: []types.Filter{makeFilter("region-name", e.cloud.Region)},
})
if err != nil {
return nil, errors.Trace(err)
}
var zoneNames []string
zoneFilter := types.Filter{Name: aws.String("location")}
for _, z := range zoneResults.AvailabilityZones {
// Should never be nil.
if z.ZoneName == nil {
continue
}
zoneNames = append(zoneNames, *z.ZoneName)
zoneFilter.Values = append(zoneFilter.Values, aws.ToString(z.ZoneName))
}
// Query the instance type names for the region and credential in use.
var instTypeNames []types.InstanceType
instanceTypeZones := make(map[types.InstanceType]set.Strings)
var token *string
for {
offeringResults, err := e.ec2Client.DescribeInstanceTypeOfferings(ctx, &ec2.DescribeInstanceTypeOfferingsInput{
LocationType: "availability-zone",
MaxResults: aws.Int32(maxOfferingsResults),
NextToken: token,
Filters: []types.Filter{zoneFilter},
})
if err != nil {
return nil, errors.Trace(err)
}
for _, offering := range offeringResults.InstanceTypeOfferings {
// Should never be empty.
if offering.InstanceType == "" {
continue
}
if _, ok := instanceTypeZones[offering.InstanceType]; !ok {
instanceTypeZones[offering.InstanceType] = set.NewStrings()
}
instanceTypeZones[offering.InstanceType].Add(*offering.Location)
instTypeNames = append(instTypeNames, offering.InstanceType)
}
token = offeringResults.NextToken
if token == nil {
break
}
}
// Populate the costs for the instance types in use.
costs, err := instanceTypeCosts(e.ec2Client, ctx, instTypeNames, zoneNames)
if err != nil {
return nil, errors.Trace(err)
}
// Compose the results.
var allInstanceTypes []instances.InstanceType
for len(instTypeNames) > 0 {
querySize := len(instTypeNames)
if querySize > maxTypesPage {
querySize = maxTypesPage
}
page := instTypeNames[0:querySize]
instTypeNames = instTypeNames[querySize:]
instTypeParams := &ec2.DescribeInstanceTypesInput{
InstanceTypes: page,
}
instTypeResults, err := e.ec2Client.DescribeInstanceTypes(ctx, instTypeParams)
if err != nil {
return nil, errors.Trace(err)
}
for _, info := range instTypeResults.InstanceTypes {
// Should never be empty.
if info.InstanceType == "" {
continue
}
allInstanceTypes = append(
allInstanceTypes, convertEC2InstanceType(info, instanceTypeZones, costs, zoneNames))
}
}
if isVPCIDSet(e.ecfg().vpcID()) {
return allInstanceTypes, nil
}
hasDefaultVPC, err := e.hasDefaultVPC(ctx)
if err != nil {
return nil, errors.Trace(err)
}
if hasDefaultVPC {
return allInstanceTypes, nil
}
// The region has no default VPC, and the user has not specified
// one to use. We filter out any instance types that are not
// supported in EC2-Classic.
var supportedInstanceTypes []instances.InstanceType
for _, instanceType := range allInstanceTypes {
if !supportsClassic(instanceType.Name) {
continue
}
supportedInstanceTypes = append(supportedInstanceTypes, instanceType)
}
return supportedInstanceTypes, nil
}
func convertEC2InstanceType(
info types.InstanceTypeInfo,
instanceTypeZones map[types.InstanceType]set.Strings,
costs map[types.InstanceType]uint64,
zoneNames []string,
) instances.InstanceType {
instCost, ok := costs[info.InstanceType]
if !ok {
instCost = math.MaxUint64
}
instType := instances.InstanceType{
Name: string(info.InstanceType),
VirtType: virtType(info),
Deprecated: info.CurrentGeneration == nil || !*info.CurrentGeneration,
Cost: instCost,
}
if info.VCpuInfo != nil && info.VCpuInfo.DefaultVCpus != nil {
instType.CpuCores = uint64(*info.VCpuInfo.DefaultVCpus)
if info.ProcessorInfo != nil && info.ProcessorInfo.SustainedClockSpeedInGhz != nil {
cpupower := calculateCPUPower(
instType.Name,
info.ProcessorInfo.SustainedClockSpeedInGhz,
uint64(*info.VCpuInfo.DefaultVCpus))
instType.CpuPower = &cpupower
}
}
if info.MemoryInfo != nil && info.MemoryInfo.SizeInMiB != nil {
instType.Mem = uint64(*info.MemoryInfo.SizeInMiB)
}
if info.ProcessorInfo != nil {
for _, instArch := range info.ProcessorInfo.SupportedArchitectures {
// Should never be empty.
if instArch == "" {
continue
}
instType.Arches = append(instType.Arches, archName(instArch))
}
}
instZones, ok := instanceTypeZones[types.InstanceType(instType.Name)]
if !ok {
instType.Deprecated = true
} else {
// If a instance type is available it at least 3 zones (or all of them if < 3)
// then consider it able to be used without explicitly asking for it.
instType.Deprecated = instZones.Size() < 3 && instZones.Size() < len(zoneNames)
}
return instType
}
// instanceTypeCosts queries the latest spot price for the given instance types.
func instanceTypeCosts(ec2Client Client, ctx context.ProviderCallContext, instTypeNames []types.InstanceType, zoneNames []string) (map[types.InstanceType]uint64, error) {
const (
maxResults = 1000
costFactor = 1000
)
var token *string
spParams := &ec2.DescribeSpotPriceHistoryInput{
InstanceTypes: instTypeNames,
MaxResults: aws.Int32(maxResults),
StartTime: aws.Time(time.Now()),
// Only look at Linux results (to reduce total number of results;
// it's only an estimate anyway)
Filters: []types.Filter{makeFilter("product-description", "Linux/UNIX", "Linux/UNIX (Amazon VPC)")},
}
if len(zoneNames) > 0 {
// Just return results for the first availability zone (others are
// probably similar, and we don't need to be too accurate here)
filter := makeFilter("availability-zone", zoneNames[0])
spParams.Filters = append(spParams.Filters, filter)
}
costs := make(map[types.InstanceType]uint64)
for {
spParams.NextToken = token
priceResults, err := ec2Client.DescribeSpotPriceHistory(ctx, spParams)
if err != nil {
return nil, errors.Trace(err)
}
for _, sp := range priceResults.SpotPriceHistory {
if _, ok := costs[sp.InstanceType]; !ok {
price, err := strconv.ParseFloat(*sp.SpotPrice, 32)
if err == nil {
costs[sp.InstanceType] = uint64(costFactor * price)
}
}
}
token = priceResults.NextToken
// token should be nil when there's no more records
// but it never gets set to nil so there's a bug in the api.
if len(priceResults.SpotPriceHistory) < maxResults {
break
}
}
return costs, nil
}