-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathapi.go
414 lines (355 loc) · 13.9 KB
/
api.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package eks
import (
"fmt"
"io/ioutil"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface"
"github.com/aws/aws-sdk-go/service/cloudtrail"
"github.com/aws/aws-sdk-go/service/cloudtrail/cloudtrailiface"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
awseks "github.com/aws/aws-sdk-go/service/eks"
"github.com/aws/aws-sdk-go/service/eks/eksiface"
"github.com/aws/aws-sdk-go/service/elb"
"github.com/aws/aws-sdk-go/service/elb/elbiface"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/aws/aws-sdk-go/service/elbv2/elbv2iface"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/iam/iamiface"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/aws/aws-sdk-go/service/ssm/ssmiface"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/aws/aws-sdk-go/service/sts/stsiface"
"github.com/kris-nova/logger"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/yaml"
"github.com/weaveworks/eksctl/pkg/ami"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/az"
"github.com/weaveworks/eksctl/pkg/cfn/manager"
"github.com/weaveworks/eksctl/pkg/utils"
"github.com/weaveworks/eksctl/pkg/version"
)
// ClusterProvider stores information about the cluster
type ClusterProvider struct {
// core fields used for config and AWS APIs
Provider api.ClusterProvider
// informative fields, i.e. used as outputs
Status *ProviderStatus
}
// ProviderServices stores the used APIs
type ProviderServices struct {
spec *api.ProviderConfig
cfn cloudformationiface.CloudFormationAPI
eks eksiface.EKSAPI
ec2 ec2iface.EC2API
elb elbiface.ELBAPI
elbv2 elbv2iface.ELBV2API
sts stsiface.STSAPI
ssm ssmiface.SSMAPI
iam iamiface.IAMAPI
cloudtrail cloudtrailiface.CloudTrailAPI
}
// CloudFormation returns a representation of the CloudFormation API
func (p ProviderServices) CloudFormation() cloudformationiface.CloudFormationAPI { return p.cfn }
// CloudFormationRoleARN returns, if any, a service role used by CloudFormation to call AWS API on your behalf
func (p ProviderServices) CloudFormationRoleARN() string { return p.spec.CloudFormationRoleARN }
// EKS returns a representation of the EKS API
func (p ProviderServices) EKS() eksiface.EKSAPI { return p.eks }
// EC2 returns a representation of the EC2 API
func (p ProviderServices) EC2() ec2iface.EC2API { return p.ec2 }
// ELB returns a representation of the ELB API
func (p ProviderServices) ELB() elbiface.ELBAPI { return p.elb }
// ELBV2 returns a representation of the ELBV2 API
func (p ProviderServices) ELBV2() elbv2iface.ELBV2API { return p.elbv2 }
// STS returns a representation of the STS API
func (p ProviderServices) STS() stsiface.STSAPI { return p.sts }
// SSM returns a representation of the STS API
func (p ProviderServices) SSM() ssmiface.SSMAPI { return p.ssm }
// IAM returns a representation of the IAM API
func (p ProviderServices) IAM() iamiface.IAMAPI { return p.iam }
// CloudTrail returns a representation of the CloudTrail API
func (p ProviderServices) CloudTrail() cloudtrailiface.CloudTrailAPI { return p.cloudtrail }
// Region returns provider-level region setting
func (p ProviderServices) Region() string { return p.spec.Region }
// Profile returns provider-level profile name
func (p ProviderServices) Profile() string { return p.spec.Profile }
// WaitTimeout returns provider-level duration after which any wait operation has to timeout
func (p ProviderServices) WaitTimeout() time.Duration { return p.spec.WaitTimeout }
// ProviderStatus stores information about the used IAM role and the resulting session
type ProviderStatus struct {
iamRoleARN string
sessionCreds *credentials.Credentials
clusterInfo *clusterInfo
}
// New creates a new setup of the used AWS APIs
func New(spec *api.ProviderConfig, clusterSpec *api.ClusterConfig) *ClusterProvider {
provider := &ProviderServices{
spec: spec,
}
c := &ClusterProvider{
Provider: provider,
}
// Create a new session and save credentials for possible
// later re-use if overriding sessions due to custom URL
s := c.newSession(spec)
provider.cfn = cloudformation.New(s)
provider.eks = awseks.New(s)
provider.ec2 = ec2.New(s)
provider.elb = elb.New(s)
provider.elbv2 = elbv2.New(s)
provider.sts = sts.New(s,
// STS retrier has to be disabled, as it's not very helpful
// (see https://github.com/weaveworks/eksctl/issues/705)
request.WithRetryer(s.Config.Copy(),
&client.DefaultRetryer{
NumMaxRetries: 1,
},
),
)
provider.ssm = ssm.New(s)
provider.iam = iam.New(s)
provider.cloudtrail = cloudtrail.New(s)
c.Status = &ProviderStatus{
sessionCreds: s.Config.Credentials,
}
// override sessions if any custom endpoints specified
if endpoint, ok := os.LookupEnv("AWS_CLOUDFORMATION_ENDPOINT"); ok {
logger.Debug("Setting CloudFormation endpoint to %s", endpoint)
provider.cfn = cloudformation.New(s, s.Config.Copy().WithEndpoint(endpoint))
}
if endpoint, ok := os.LookupEnv("AWS_EKS_ENDPOINT"); ok {
logger.Debug("Setting EKS endpoint to %s", endpoint)
provider.eks = awseks.New(s, s.Config.Copy().WithEndpoint(endpoint))
}
if endpoint, ok := os.LookupEnv("AWS_EC2_ENDPOINT"); ok {
logger.Debug("Setting EC2 endpoint to %s", endpoint)
provider.ec2 = ec2.New(s, s.Config.Copy().WithEndpoint(endpoint))
}
if endpoint, ok := os.LookupEnv("AWS_ELB_ENDPOINT"); ok {
logger.Debug("Setting ELB endpoint to %s", endpoint)
provider.elb = elb.New(s, s.Config.Copy().WithEndpoint(endpoint))
}
if endpoint, ok := os.LookupEnv("AWS_ELBV2_ENDPOINT"); ok {
logger.Debug("Setting ELBV2 endpoint to %s", endpoint)
provider.elbv2 = elbv2.New(s, s.Config.Copy().WithEndpoint(endpoint))
}
if endpoint, ok := os.LookupEnv("AWS_STS_ENDPOINT"); ok {
logger.Debug("Setting STS endpoint to %s", endpoint)
provider.sts = sts.New(s, s.Config.Copy().WithEndpoint(endpoint))
}
if endpoint, ok := os.LookupEnv("AWS_IAM_ENDPOINT"); ok {
logger.Debug("Setting IAM endpoint to %s", endpoint)
provider.iam = iam.New(s, s.Config.Copy().WithEndpoint(endpoint))
}
if endpoint, ok := os.LookupEnv("AWS_CLOUDTRAIL_ENDPOINT"); ok {
logger.Debug("Setting CloudTrail endpoint to %s", endpoint)
provider.cloudtrail = cloudtrail.New(s, s.Config.Copy().WithEndpoint(endpoint))
}
if clusterSpec != nil {
clusterSpec.Metadata.Region = c.Provider.Region()
}
return c
}
// LoadConfigFromFile loads ClusterConfig from configFile
func LoadConfigFromFile(configFile string) (*api.ClusterConfig, error) {
data, err := readConfig(configFile)
if err != nil {
return nil, errors.Wrapf(err, "reading config file %q", configFile)
}
// strict mode is not available in runtime.Decode, so we use the parser
// directly; we don't store the resulting object, this is just the means
// of detecting any unknown keys
// NOTE: we must use sigs.k8s.io/yaml, as it behaves differently from
// github.com/ghodss/yaml, which didn't handle nested structs well
if err := yaml.UnmarshalStrict(data, &api.ClusterConfig{}); err != nil {
return nil, errors.Wrapf(err, "loading config file %q", configFile)
}
obj, err := runtime.Decode(scheme.Codecs.UniversalDeserializer(), data)
if err != nil {
return nil, errors.Wrapf(err, "loading config file %q", configFile)
}
cfg, ok := obj.(*api.ClusterConfig)
if !ok {
return nil, fmt.Errorf("expected to decode object of type %T; got %T", &api.ClusterConfig{}, cfg)
}
return cfg, nil
}
func readConfig(configFile string) ([]byte, error) {
if configFile == "-" {
return ioutil.ReadAll(os.Stdin)
}
return ioutil.ReadFile(configFile)
}
// IsSupportedRegion check if given region is supported
func (c *ClusterProvider) IsSupportedRegion() bool {
for _, supportedRegion := range api.SupportedRegions() {
if c.Provider.Region() == supportedRegion {
return true
}
}
return false
}
// GetCredentialsEnv returns the AWS credentials for env usage
func (c *ClusterProvider) GetCredentialsEnv() ([]string, error) {
creds, err := c.Status.sessionCreds.Get()
if err != nil {
return nil, errors.Wrap(err, "getting effective credentials")
}
return []string{
fmt.Sprintf("AWS_ACCESS_KEY_ID=%s", creds.AccessKeyID),
fmt.Sprintf("AWS_SECRET_ACCESS_KEY=%s", creds.SecretAccessKey),
fmt.Sprintf("AWS_SESSION_TOKEN=%s", creds.SessionToken),
}, nil
}
// CheckAuth checks the AWS authentication
func (c *ClusterProvider) CheckAuth() error {
input := &sts.GetCallerIdentityInput{}
output, err := c.Provider.STS().GetCallerIdentity(input)
if err != nil {
return errors.Wrap(err, "checking AWS STS access – cannot get role ARN for current session")
}
if output == nil || output.Arn == nil {
return fmt.Errorf("unexpected response from AWS STS")
}
c.Status.iamRoleARN = *output.Arn
logger.Debug("role ARN for the current session is %q", c.Status.iamRoleARN)
return nil
}
// EnsureAMI ensures that the node AMI is set and is available
func EnsureAMI(provider api.ClusterProvider, version string, ng *api.NodeGroup) error {
if api.IsAMI(ng.AMI) {
return ami.Use(provider.EC2(), ng)
}
var resolver ami.Resolver
switch ng.AMI {
case api.NodeImageResolverAuto:
resolver = ami.NewAutoResolver(provider.EC2())
case api.NodeImageResolverAutoSSM:
resolver = ami.NewSSMResolver(provider.SSM())
case api.NodeImageResolverStatic:
resolver = ami.NewStaticResolver()
default:
resolver = ami.NewMultiResolver(
ami.NewSSMResolver(provider.SSM()),
ami.NewAutoResolver(provider.EC2()),
)
}
instanceType := selectInstanceType(ng)
id, err := resolver.Resolve(provider.Region(), version, instanceType, ng.AMIFamily)
if err != nil {
return errors.Wrap(err, "unable to determine AMI to use")
}
if id == "" {
return ami.NewErrFailedResolution(provider.Region(), version, instanceType, ng.AMIFamily)
}
ng.AMI = id
// Check the AMI is available and populate RootDevice information
return ami.Use(provider.EC2(), ng)
}
// selectInstanceType determines which instanceType is relevant for selecting an AMI
// If the nodegroup has mixed instances it will prefer a GPU instance type over a general class one
// This is to make sure that the AMI that is selected later is valid for all the types
func selectInstanceType(ng *api.NodeGroup) string {
if api.HasMixedInstances(ng) {
for _, instanceType := range ng.InstancesDistribution.InstanceTypes {
if utils.IsGPUInstanceType(instanceType) {
return instanceType
}
}
return ng.InstancesDistribution.InstanceTypes[0]
}
return ng.InstanceType
}
func errTooFewAvailabilityZones(azs []string) error {
return fmt.Errorf("only %d zones specified %v, %d are required (can be non-unique)", len(azs), azs, az.MinRequiredAvailabilityZones)
}
// SetAvailabilityZones sets the given (or chooses) the availability zones
func (c *ClusterProvider) SetAvailabilityZones(spec *api.ClusterConfig, given []string) error {
if count := len(given); count != 0 {
if count < az.MinRequiredAvailabilityZones {
return errTooFewAvailabilityZones(given)
}
spec.AvailabilityZones = given
return nil
}
if count := len(spec.AvailabilityZones); count != 0 {
if count < az.MinRequiredAvailabilityZones {
return errTooFewAvailabilityZones(spec.AvailabilityZones)
}
return nil
}
logger.Debug("determining availability zones")
azSelector := az.NewSelectorWithDefaults(c.Provider.EC2())
if c.Provider.Region() == api.RegionUSEast1 {
azSelector = az.NewSelectorWithMinRequired(c.Provider.EC2())
}
zones, err := azSelector.SelectZones(c.Provider.Region())
if err != nil {
return errors.Wrap(err, "getting availability zones")
}
logger.Info("setting availability zones to %v", zones)
spec.AvailabilityZones = zones
return nil
}
func (c *ClusterProvider) newSession(spec *api.ProviderConfig) *session.Session {
// we might want to use bits from kops, although right now it seems like too many thing we
// don't want yet
// https://github.com/kubernetes/kops/blob/master/upup/pkg/fi/cloudup/awsup/aws_cloud.go#L179
config := aws.NewConfig().WithCredentialsChainVerboseErrors(true)
if c.Provider.Region() != "" {
config = config.WithRegion(c.Provider.Region()).WithSTSRegionalEndpoint(endpoints.RegionalSTSEndpoint)
}
config = request.WithRetryer(config, newLoggingRetryer())
if logger.Level >= api.AWSDebugLevel {
config = config.WithLogLevel(aws.LogDebug |
aws.LogDebugWithHTTPBody |
aws.LogDebugWithRequestRetries |
aws.LogDebugWithRequestErrors |
aws.LogDebugWithEventStreamBody)
config = config.WithLogger(aws.LoggerFunc(func(args ...interface{}) {
logger.Debug(fmt.Sprintln(args...))
}))
}
// Create the options for the session
opts := session.Options{
Config: *config,
SharedConfigState: session.SharedConfigEnable,
Profile: spec.Profile,
AssumeRoleTokenProvider: stscreds.StdinTokenProvider,
}
stscreds.DefaultDuration = 30 * time.Minute
s := session.Must(session.NewSessionWithOptions(opts))
s.Handlers.Build.PushFrontNamed(request.NamedHandler{
Name: "eksctlUserAgent",
Fn: request.MakeAddToUserAgentHandler(
"eksctl", version.String()),
})
if spec.Region == "" {
if api.IsSetAndNonEmptyString(s.Config.Region) {
// set cluster config region, based on session config
spec.Region = *s.Config.Region
} else {
// if session config doesn't have region set, make recursive call forcing default region
logger.Debug("no region specified in flags or config, setting to %s", api.DefaultRegion)
spec.Region = api.DefaultRegion
return c.newSession(spec)
}
}
return s
}
// NewStackManager returns a new stack manager
func (c *ClusterProvider) NewStackManager(spec *api.ClusterConfig) *manager.StackCollection {
return manager.NewStackCollection(c.Provider, spec)
}