-
Notifications
You must be signed in to change notification settings - Fork 846
/
ami.go
295 lines (272 loc) · 10.2 KB
/
ami.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
/*
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 amifamily
import (
"context"
"fmt"
"sort"
"strings"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/aws/aws-sdk-go/service/ssm/ssmiface"
"github.com/mitchellh/hashstructure/v2"
"github.com/patrickmn/go-cache"
"github.com/samber/lo"
v1 "k8s.io/api/core/v1"
"knative.dev/pkg/logging"
"github.com/aws/karpenter/pkg/apis/v1beta1"
"github.com/aws/karpenter/pkg/providers/version"
"sigs.k8s.io/karpenter/pkg/cloudprovider"
"sigs.k8s.io/karpenter/pkg/scheduling"
"sigs.k8s.io/karpenter/pkg/utils/pretty"
)
type Provider struct {
sync.Mutex
cache *cache.Cache
ssm ssmiface.SSMAPI
ec2api ec2iface.EC2API
cm *pretty.ChangeMonitor
versionProvider *version.Provider
}
type AMI struct {
Name string
AmiID string
CreationDate string
Requirements scheduling.Requirements
}
type AMIs []AMI
// Sort orders the AMIs by creation date in descending order.
// If creation date is nil or two AMIs have the same creation date, the AMIs will be sorted by ID, which is guaranteed to be unique, in ascending order.
func (a AMIs) Sort() {
sort.Slice(a, func(i, j int) bool {
itime, _ := time.Parse(time.RFC3339, a[i].CreationDate)
jtime, _ := time.Parse(time.RFC3339, a[j].CreationDate)
if itime.Unix() != jtime.Unix() {
return itime.Unix() > jtime.Unix()
}
return a[i].AmiID < a[j].AmiID
})
}
func (a AMIs) String() string {
var sb strings.Builder
ids := lo.Map(a, func(a AMI, _ int) string { return a.AmiID })
if len(a) > 25 {
sb.WriteString(strings.Join(ids[:25], ", "))
sb.WriteString(fmt.Sprintf(" and %d other(s)", len(a)-25))
} else {
sb.WriteString(strings.Join(ids, ", "))
}
return sb.String()
}
// MapToInstanceTypes returns a map of AMIIDs that are the most recent on creationDate to compatible instancetypes
func (a AMIs) MapToInstanceTypes(instanceTypes []*cloudprovider.InstanceType) map[string][]*cloudprovider.InstanceType {
amiIDs := map[string][]*cloudprovider.InstanceType{}
for _, instanceType := range instanceTypes {
for _, ami := range a {
if err := instanceType.Requirements.Compatible(ami.Requirements, scheduling.AllowUndefinedWellKnownLabels); err == nil {
amiIDs[ami.AmiID] = append(amiIDs[ami.AmiID], instanceType)
break
}
}
}
return amiIDs
}
func NewProvider(versionProvider *version.Provider, ssm ssmiface.SSMAPI, ec2api ec2iface.EC2API, cache *cache.Cache) *Provider {
return &Provider{
cache: cache,
ssm: ssm,
ec2api: ec2api,
cm: pretty.NewChangeMonitor(),
versionProvider: versionProvider,
}
}
// Get Returning a list of AMIs with its associated requirements
func (p *Provider) Get(ctx context.Context, nodeClass *v1beta1.EC2NodeClass, options *Options) (AMIs, error) {
p.Lock()
defer p.Unlock()
var err error
var amis AMIs
if len(nodeClass.Spec.AMISelectorTerms) == 0 {
amis, err = p.getDefaultAMIs(ctx, nodeClass, options)
if err != nil {
return nil, err
}
} else {
amis, err = p.getAMIs(ctx, nodeClass.Spec.AMISelectorTerms)
if err != nil {
return nil, err
}
}
amis.Sort()
if p.cm.HasChanged(fmt.Sprintf("amis/%s", nodeClass.Name), amis) {
logging.FromContext(ctx).With("ids", amis, "count", len(amis)).Debugf("discovered amis")
}
return amis, nil
}
func (p *Provider) getDefaultAMIs(ctx context.Context, nodeClass *v1beta1.EC2NodeClass, options *Options) (res AMIs, err error) {
if images, ok := p.cache.Get(lo.FromPtr(nodeClass.Spec.AMIFamily)); ok {
// Ensure what's returned from this function is a deep-copy of AMIs so alterations
// to the data don't affect the original
return append(AMIs{}, images.(AMIs)...), nil
}
amiFamily := GetAMIFamily(nodeClass.Spec.AMIFamily, options)
kubernetesVersion, err := p.versionProvider.Get(ctx)
if err != nil {
return nil, fmt.Errorf("getting kubernetes version %w", err)
}
defaultAMIs := amiFamily.DefaultAMIs(kubernetesVersion)
for _, ami := range defaultAMIs {
if id, err := p.resolveSSMParameter(ctx, ami.Query); err != nil {
logging.FromContext(ctx).With("query", ami.Query).Errorf("discovering amis from ssm, %s", err)
} else {
res = append(res, AMI{AmiID: id, Requirements: ami.Requirements})
}
}
// Resolve Name and CreationDate information into the DefaultAMIs
if err = p.ec2api.DescribeImagesPagesWithContext(ctx, &ec2.DescribeImagesInput{
Filters: []*ec2.Filter{{Name: aws.String("image-id"), Values: aws.StringSlice(lo.Map(res, func(a AMI, _ int) string { return a.AmiID }))}},
MaxResults: aws.Int64(500),
}, func(page *ec2.DescribeImagesOutput, _ bool) bool {
for i := range page.Images {
for j := range res {
if res[j].AmiID == aws.StringValue(page.Images[i].ImageId) {
res[j].Name = aws.StringValue(page.Images[i].Name)
res[j].CreationDate = aws.StringValue(page.Images[i].CreationDate)
}
}
}
return true
}); err != nil {
return nil, fmt.Errorf("describing images, %w", err)
}
p.cache.SetDefault(lo.FromPtr(nodeClass.Spec.AMIFamily), res)
return res, nil
}
func (p *Provider) resolveSSMParameter(ctx context.Context, ssmQuery string) (string, error) {
output, err := p.ssm.GetParameterWithContext(ctx, &ssm.GetParameterInput{Name: aws.String(ssmQuery)})
if err != nil {
return "", fmt.Errorf("getting ssm parameter %q, %w", ssmQuery, err)
}
ami := aws.StringValue(output.Parameter.Value)
return ami, nil
}
func (p *Provider) getAMIs(ctx context.Context, terms []v1beta1.AMISelectorTerm) (AMIs, error) {
filterAndOwnerSets := GetFilterAndOwnerSets(terms)
hash, err := hashstructure.Hash(filterAndOwnerSets, hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true})
if err != nil {
return nil, err
}
if images, ok := p.cache.Get(fmt.Sprintf("%d", hash)); ok {
// Ensure what's returned from this function is a deep-copy of AMIs so alterations
// to the data don't affect the original
return append(AMIs{}, images.(AMIs)...), nil
}
images := map[uint64]AMI{}
for _, filtersAndOwners := range filterAndOwnerSets {
if err = p.ec2api.DescribeImagesPagesWithContext(ctx, &ec2.DescribeImagesInput{
// Don't include filters in the Describe Images call as EC2 API doesn't allow empty filters.
Filters: lo.Ternary(len(filtersAndOwners.Filters) > 0, filtersAndOwners.Filters, nil),
Owners: lo.Ternary(len(filtersAndOwners.Owners) > 0, aws.StringSlice(filtersAndOwners.Owners), nil),
MaxResults: aws.Int64(1000),
}, func(page *ec2.DescribeImagesOutput, _ bool) bool {
for i := range page.Images {
reqs := p.getRequirementsFromImage(page.Images[i])
if !v1beta1.WellKnownArchitectures.Has(reqs.Get(v1.LabelArchStable).Any()) {
continue
}
reqsHash := lo.Must(hashstructure.Hash(reqs.NodeSelectorRequirements(), hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true}))
// If the proposed image is newer, store it so that we can return it
if v, ok := images[reqsHash]; ok {
candidateCreationTime, _ := time.Parse(time.RFC3339, lo.FromPtr(page.Images[i].CreationDate))
existingCreationTime, _ := time.Parse(time.RFC3339, v.CreationDate)
if existingCreationTime == candidateCreationTime && lo.FromPtr(page.Images[i].Name) < v.Name {
continue
}
if candidateCreationTime.Unix() < existingCreationTime.Unix() {
continue
}
}
images[reqsHash] = AMI{
Name: lo.FromPtr(page.Images[i].Name),
AmiID: lo.FromPtr(page.Images[i].ImageId),
CreationDate: lo.FromPtr(page.Images[i].CreationDate),
Requirements: reqs,
}
}
return true
}); err != nil {
return nil, fmt.Errorf("describing images, %w", err)
}
}
p.cache.SetDefault(fmt.Sprintf("%d", hash), AMIs(lo.Values(images)))
return lo.Values(images), nil
}
type FiltersAndOwners struct {
Filters []*ec2.Filter
Owners []string
}
func GetFilterAndOwnerSets(terms []v1beta1.AMISelectorTerm) (res []FiltersAndOwners) {
idFilter := &ec2.Filter{Name: aws.String("image-id")}
for _, term := range terms {
switch {
case term.ID != "":
idFilter.Values = append(idFilter.Values, aws.String(term.ID))
default:
elem := FiltersAndOwners{
Owners: lo.Ternary(term.Owner != "", []string{term.Owner}, []string{}),
}
if term.Name != "" {
// Default owners to self,amazon to ensure Karpenter only discovers cross-account AMIs if the user specifically allows it.
// Removing this default would cause Karpenter to discover publicly shared AMIs passing the name filter.
elem = FiltersAndOwners{
Owners: lo.Ternary(term.Owner != "", []string{term.Owner}, []string{"self", "amazon"}),
}
elem.Filters = append(elem.Filters, &ec2.Filter{
Name: aws.String("name"),
Values: aws.StringSlice([]string{term.Name}),
})
}
for k, v := range term.Tags {
if v == "*" {
elem.Filters = append(elem.Filters, &ec2.Filter{
Name: aws.String("tag-key"),
Values: []*string{aws.String(k)},
})
} else {
elem.Filters = append(elem.Filters, &ec2.Filter{
Name: aws.String(fmt.Sprintf("tag:%s", k)),
Values: []*string{aws.String(v)},
})
}
}
res = append(res, elem)
}
}
if len(idFilter.Values) > 0 {
res = append(res, FiltersAndOwners{Filters: []*ec2.Filter{idFilter}})
}
return res
}
func (p *Provider) getRequirementsFromImage(ec2Image *ec2.Image) scheduling.Requirements {
requirements := scheduling.NewRequirements()
// Always add the architecture of an image as a requirement, irrespective of what's specified in EC2 tags.
architecture := *ec2Image.Architecture
if value, ok := v1beta1.AWSToKubeArchitectures[architecture]; ok {
architecture = value
}
requirements.Add(scheduling.NewRequirement(v1.LabelArchStable, v1.NodeSelectorOpIn, architecture))
return requirements
}