forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.go
208 lines (183 loc) · 5.78 KB
/
image.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
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package instances
import (
"fmt"
"sort"
"github.com/juju/errors"
"github.com/juju/loggo"
"github.com/juju/utils/v2/arch"
"github.com/juju/juju/core/constraints"
"github.com/juju/juju/environs/imagemetadata"
)
var logger = loggo.GetLogger("juju.environs.instances")
// InstanceConstraint constrains the possible instances that may be
// chosen by the environment provider.
type InstanceConstraint struct {
Region string
Series string
Arches []string
Constraints constraints.Value
// Optional filtering criteria not supported by all providers. These
// attributes are not specified by the user as a constraint but rather
// passed in by the provider implementation to restrict the choice of
// available images.
// Storage specifies a list of storage types, in order of preference.
// eg ["ssd", "ebs"] means find images with ssd storage, but if none
// exist, find those with ebs instead.
Storage []string
}
// String returns a human readable form of this InstanceConstraint.
func (ic *InstanceConstraint) String() string {
return fmt.Sprintf(
"{region: %s, series: %s, arches: %s, constraints: %s, storage: %s}",
ic.Region,
ic.Series,
ic.Arches,
ic.Constraints,
ic.Storage,
)
}
// InstanceSpec holds an instance type name and the chosen image info.
type InstanceSpec struct {
InstanceType InstanceType
Image Image
// order is used to sort InstanceSpec based on the input InstanceTypes.
order int
}
// FindInstanceSpec returns an InstanceSpec satisfying the supplied InstanceConstraint.
// possibleImages contains a list of images matching the InstanceConstraint.
// allInstanceTypes provides information on every known available instance type (name, memory, cpu cores etc) on
// which instances can be run. The InstanceConstraint is used to filter allInstanceTypes and then a suitable image
// compatible with the matching instance types is returned.
func FindInstanceSpec(possibleImages []Image, ic *InstanceConstraint, allInstanceTypes []InstanceType) (*InstanceSpec, error) {
logger.Debugf("instance constraints %+v", ic)
if len(possibleImages) == 0 {
return nil, errors.Errorf("no metadata for %q images in %s with arches %s",
ic.Series, ic.Region, ic.Arches)
}
logger.Debugf("matching constraints %v against possible image metadata %+v", ic, possibleImages)
matchingTypes, err := MatchingInstanceTypes(allInstanceTypes, ic.Region, ic.Constraints)
if err != nil {
return nil, err
}
if len(matchingTypes) == 0 {
return nil, errors.Errorf("no instance types found matching constraint: %s", ic)
}
// We check for exact matches (all attributes matching), and also for
// partial matches (instance type specifies attribute, but image does
// not). Exact matches always take precedence.
var exactSpecs, partialSpecs []*InstanceSpec
for _, itype := range matchingTypes {
for _, image := range possibleImages {
specs := &partialSpecs
switch image.match(itype) {
case exactMatch:
specs = &exactSpecs
fallthrough
case partialMatch:
*specs = append(*specs, &InstanceSpec{
InstanceType: itype,
Image: image,
order: len(*specs),
})
}
}
}
specs := exactSpecs
if len(specs) == 0 {
specs = partialSpecs
}
if len(specs) > 0 {
sort.Sort(byArch(specs))
logger.Infof("find instance - using %v image with id: %v", specs[0].Image.Arch, specs[0].Image.Id)
return specs[0], nil
}
names := make([]string, len(matchingTypes))
for i, itype := range matchingTypes {
names[i] = itype.Name
}
return nil, errors.Errorf("no %q images in %s matching instance types %v", ic.Series, ic.Region, names)
}
// byArch sorts InstanceSpecs first by descending word-size, then
// alphabetically by name, and choose the first spec in the sequence.
type byArch []*InstanceSpec
func (a byArch) Len() int {
return len(a)
}
func (a byArch) Less(i, j int) bool {
iArchName := a[i].Image.Arch
jArchName := a[j].Image.Arch
iArch := arch.Info[iArchName]
jArch := arch.Info[jArchName]
// Wider word-size first.
switch {
case iArch.WordSize > jArch.WordSize:
return true
case iArch.WordSize < jArch.WordSize:
return false
}
// Alphabetically by arch name.
switch {
case iArchName < jArchName:
return true
case iArchName > jArchName:
return false
}
// If word-size and name the same, keep stable.
return a[i].order < a[j].order
}
func (a byArch) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
// Image holds the attributes that vary amongst relevant images for
// a given series in a given region.
type Image struct {
Id string
Arch string
// The type of virtualisation supported by this image.
VirtType string
}
type imageMatch int
const (
nonMatch imageMatch = iota
exactMatch
partialMatch
)
// match returns true if the image can run on the supplied instance type.
func (image Image) match(itype InstanceType) imageMatch {
if !image.matchArch(itype.Arches) {
return nonMatch
}
if itype.VirtType == nil || image.VirtType == *itype.VirtType {
return exactMatch
}
if image.VirtType == "" {
// Image doesn't specify virtualisation type. We allow it
// to match, but prefer exact matches.
return partialMatch
}
return nonMatch
}
func (image Image) matchArch(arches []string) bool {
for _, arch := range arches {
if arch == image.Arch {
return true
}
}
return false
}
// ImageMetadataToImages converts an array of ImageMetadata pointers (as
// returned by imagemetadata.Fetch) to an array of Image objects (as required
// by instances.FindInstanceSpec).
func ImageMetadataToImages(inputs []*imagemetadata.ImageMetadata) []Image {
result := make([]Image, len(inputs))
for index, input := range inputs {
result[index] = Image{
Id: input.Id,
VirtType: input.VirtType,
Arch: input.Arch,
}
}
return result
}