-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
api.go
490 lines (425 loc) · 15.5 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
package eks
import (
"context"
"fmt"
"io"
"os"
"strings"
"time"
"k8s.io/apimachinery/pkg/util/sets"
awsv2 "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
"github.com/aws/aws-sdk-go-v2/service/cloudtrail"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/kris-nova/logger"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/runtime"
k8sclient "k8s.io/client-go/kubernetes"
"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/awsapi"
"github.com/weaveworks/eksctl/pkg/az"
"github.com/weaveworks/eksctl/pkg/cfn/manager"
"github.com/weaveworks/eksctl/pkg/credentials"
"github.com/weaveworks/eksctl/pkg/kubernetes"
"github.com/weaveworks/eksctl/pkg/utils/kubeconfig"
"github.com/weaveworks/eksctl/pkg/utils/nodes"
)
// ClusterProvider stores information about the cluster
type ClusterProvider struct {
// KubeProvider offers helper methods to handle Kubernetes operations
KubeProvider
// core fields used for config and AWS APIs
AWSProvider api.ClusterProvider
// informative fields, i.e. used as outputs
Status *ProviderStatus
}
// KubernetesProvider provides helper methods to handle Kubernetes operations.
type KubernetesProvider struct {
WaitTimeout time.Duration
RoleARN string
Signer api.STSPresigner
}
// KubeProvider is an interface with helper funcs for k8s and EKS that are part of ClusterProvider
//
//go:generate counterfeiter -o fakes/fake_kube_provider.go . KubeProvider
type KubeProvider interface {
NewRawClient(clusterInfo kubeconfig.ClusterInfo) (*kubernetes.RawClient, error)
NewStdClientSet(clusterInfo kubeconfig.ClusterInfo) (k8sclient.Interface, error)
ServerVersion(rawClient *kubernetes.RawClient) (string, error)
WaitForControlPlane(meta *api.ClusterMeta, clientSet *kubernetes.RawClient, waitTimeout time.Duration) error
}
// ProviderServices stores the used APIs
type ProviderServices struct {
spec *api.ProviderConfig
asg awsapi.ASG
cloudtrail awsapi.CloudTrail
cloudwatchlogs awsapi.CloudWatchLogs
*ServicesV2
}
// 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 }
// CloudFormationDisableRollback returns whether stacks should not rollback on failure
func (p ProviderServices) CloudFormationDisableRollback() bool {
return p.spec.CloudFormationDisableRollback
}
// ASG returns a representation of the AutoScaling API
func (p ProviderServices) ASG() awsapi.ASG { return p.asg }
// CloudTrail returns a representation of the CloudTrail API
func (p ProviderServices) CloudTrail() awsapi.CloudTrail { return p.cloudtrail }
// CloudWatchLogs returns a representation of the CloudWatchLogs API.
func (p ProviderServices) CloudWatchLogs() awsapi.CloudWatchLogs {
return p.cloudwatchlogs
}
// Region returns provider-level region setting
func (p ProviderServices) Region() string { return p.spec.Region }
// Profile returns the provider-level AWS profile.
func (p ProviderServices) Profile() api.Profile { 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 }
// ClusterInfo provides information about the cluster.
type ClusterInfo struct {
Cluster *ekstypes.Cluster
}
// ProviderStatus stores information about the used IAM role and the resulting session
type ProviderStatus struct {
IAMRoleARN string
ClusterInfo *ClusterInfo
}
// New creates a new setup of the used AWS APIs
func New(
ctx context.Context,
spec *api.ProviderConfig,
clusterSpec *api.ClusterConfig,
) (*ClusterProvider, error) {
return newHelper(ctx, spec, clusterSpec, newAWSProvider)
}
func newHelper(
ctx context.Context,
spec *api.ProviderConfig,
clusterSpec *api.ClusterConfig,
awsProviderBuilder func(*api.ProviderConfig, AWSConfigurationLoader) (api.ClusterProvider, error),
) (*ClusterProvider, error) {
provider, err := awsProviderBuilder(spec, &ConfigurationLoader{})
if err != nil {
return nil, err
}
c := &ClusterProvider{
AWSProvider: provider,
Status: &ProviderStatus{},
}
stsOutput, err := c.checkAuth(ctx)
if err != nil {
return nil, err
}
// c.Status.IAMRoleARN is later needed by the kubeProvider
c.Status.IAMRoleARN = *stsOutput.Arn
logger.Debug("role ARN for the current session is %q", c.Status.IAMRoleARN)
if clusterSpec != nil {
clusterSpec.Metadata.AccountID = *stsOutput.Account
clusterSpec.Metadata.Region = c.AWSProvider.Region()
}
kubeProvider := &KubernetesProvider{
WaitTimeout: spec.WaitTimeout,
RoleARN: c.Status.IAMRoleARN,
Signer: provider.STSPresigner(),
}
c.KubeProvider = kubeProvider
return c, nil
}
func newAWSProvider(spec *api.ProviderConfig, configurationLoader AWSConfigurationLoader) (api.ClusterProvider, error) {
var (
credentialsCacheFilePath string
err error
)
provider := &ProviderServices{
spec: spec,
}
cacheCredentials := os.Getenv(credentials.EksctlGlobalEnableCachingEnvName) != ""
if cacheCredentials {
credentialsCacheFilePath, err = credentials.GetCacheFilePath()
if err != nil {
return nil, fmt.Errorf("error getting cache file path: %w", err)
}
}
cfg, err := newV2Config(spec, credentialsCacheFilePath, configurationLoader)
if err != nil {
return nil, err
}
if cfg.Region == "" {
return nil, fmt.Errorf("AWS Region must be set, please set the AWS Region in AWS config file or as environment variable")
}
if spec.Region == "" {
spec.Region = cfg.Region
}
provider.ServicesV2 = &ServicesV2{
config: cfg,
}
provider.asg = autoscaling.NewFromConfig(cfg)
provider.cloudwatchlogs = cloudwatchlogs.NewFromConfig(cfg)
provider.cloudtrail = cloudtrail.NewFromConfig(cfg)
if endpoint, ok := os.LookupEnv("AWS_CLOUDTRAIL_ENDPOINT"); ok {
logger.Debug("Setting CloudTrail endpoint to %s", endpoint)
provider.cloudtrail = cloudtrail.NewFromConfig(cfg, func(o *cloudtrail.Options) {
o.BaseEndpoint = &endpoint
})
}
return provider, nil
}
// ParseConfig parses data into a ClusterConfig
func ParseConfig(data []byte) (*api.ClusterConfig, error) {
// 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, err
}
obj, err := runtime.Decode(scheme.Codecs.UniversalDeserializer(), data)
if err != nil {
return nil, err
}
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
}
// 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)
}
clusterConfig, err := ParseConfig(data)
if err != nil {
return nil, errors.Wrapf(err, "loading config file %q", configFile)
}
return clusterConfig, nil
}
func readConfig(configFile string) ([]byte, error) {
if configFile == "-" {
return io.ReadAll(os.Stdin)
}
return os.ReadFile(configFile)
}
// IsSupportedRegion check if given region is supported
func (c *ClusterProvider) IsSupportedRegion() bool {
for _, supportedRegion := range api.SupportedRegions() {
if c.AWSProvider.Region() == supportedRegion {
return true
}
}
return false
}
// GetCredentialsEnv returns the AWS credentials for env usage
func (c *ClusterProvider) GetCredentialsEnv(ctx context.Context) ([]string, error) {
creds, err := c.AWSProvider.CredentialsProvider().Retrieve(ctx)
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(ctx context.Context) (*sts.GetCallerIdentityOutput, error) {
output, err := c.AWSProvider.STS().GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{})
if err != nil {
return nil, errors.Wrap(err, "checking AWS STS access – cannot get role ARN for current session")
}
if output == nil || output.Arn == nil {
return nil, fmt.Errorf("unexpected response from AWS STS")
}
return output, nil
}
// ResolveAMI ensures that the node AMI is set and is available
func ResolveAMI(ctx context.Context, provider api.ClusterProvider, version string, np api.NodePool) error {
var resolver ami.Resolver
ng := np.BaseNodeGroup()
switch ng.AMI {
case api.NodeImageResolverAuto:
resolver = ami.NewAutoResolver(provider.EC2())
case api.NodeImageResolverAutoSSM:
resolver = ami.NewSSMResolver(provider.SSM())
case "":
resolver = ami.NewMultiResolver(
ami.NewSSMResolver(provider.SSM()),
ami.NewAutoResolver(provider.EC2()),
)
default:
return errors.Errorf("invalid AMI value: %q", ng.AMI)
}
instanceType := api.SelectInstanceType(np)
id, err := resolver.Resolve(ctx, 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
return nil
}
// SetAvailabilityZones sets the given (or chooses) the availability zones
// Returns whether azs were set randomly or provided by a user.
// CheckInstanceAvailability is only run if azs were provided by the user. Random
// selection already performs this check and makes sure AZs support all given instances.
func SetAvailabilityZones(ctx context.Context, spec *api.ClusterConfig, given []string, ec2API awsapi.EC2, region string) (bool, error) {
if count := len(given); count != 0 {
if count < api.MinRequiredAvailabilityZones {
return false, api.ErrTooFewAvailabilityZones(given)
}
spec.AvailabilityZones = given
return true, nil
}
if count := len(spec.AvailabilityZones); count != 0 {
if count < api.MinRequiredAvailabilityZones {
return false, api.ErrTooFewAvailabilityZones(spec.AvailabilityZones)
}
return true, nil
}
logger.Debug("determining availability zones")
zones, err := az.GetAvailabilityZones(ctx, ec2API, region, spec)
if err != nil {
return false, errors.Wrap(err, "getting availability zones")
}
logger.Info("setting availability zones to %v", zones)
spec.AvailabilityZones = zones
return false, nil
}
// CheckInstanceAvailability verifies that if any instances are provided in any node groups
// that those instances are available in the selected AZs.
func CheckInstanceAvailability(ctx context.Context, spec *api.ClusterConfig, ec2API awsapi.EC2) error {
logger.Debug("determining instance availability in zones")
// This map will use either globally configured AZs or, if set, the AZ defined by the nodegroup.
// map["ng-1"]["c2.large"]=[]string{"us-west-1a", "us-west-1b"}
instanceMap := make(map[string]map[string][]string)
uniqueInstances := sets.New[string]()
pool := nodes.ToNodePools(spec)
for _, ng := range pool {
if _, ok := instanceMap[ng.BaseNodeGroup().Name]; !ok {
instanceMap[ng.BaseNodeGroup().Name] = make(map[string][]string)
}
for _, instanceType := range ng.InstanceTypeList() {
if instanceType == "mixed" {
continue
}
uniqueInstances.Insert(instanceType)
if len(ng.BaseNodeGroup().AvailabilityZones) > 0 {
instanceMap[ng.BaseNodeGroup().Name][instanceType] = ng.BaseNodeGroup().AvailabilityZones
} else {
instanceMap[ng.BaseNodeGroup().Name][instanceType] = spec.AvailabilityZones
}
}
}
// Do an early exit if we don't have anything.
if uniqueInstances.Len() == 0 {
// nothing to do
return nil
}
var instanceTypeOfferings []ec2types.InstanceTypeOffering
p := ec2.NewDescribeInstanceTypeOfferingsPaginator(ec2API, &ec2.DescribeInstanceTypeOfferingsInput{
Filters: []ec2types.Filter{
{
Name: awsv2.String("instance-type"),
Values: sets.List(uniqueInstances),
},
},
LocationType: ec2types.LocationTypeAvailabilityZone,
MaxResults: awsv2.Int32(100),
})
for p.HasMorePages() {
output, err := p.NextPage(ctx)
if err != nil {
return fmt.Errorf("unable to list offerings for instance types %w", err)
}
instanceTypeOfferings = append(instanceTypeOfferings, output.InstanceTypeOfferings...)
}
// construct a map so instance types can easily be checked
// map["c2.large"]["us-east-1a"]=struct{}{}
offers := make(map[string]map[string]struct{})
for _, offer := range instanceTypeOfferings {
if _, ok := offers[string(offer.InstanceType)]; !ok {
offers[string(offer.InstanceType)] = map[string]struct{}{
awsv2.ToString(offer.Location): {},
}
} else {
offers[string(offer.InstanceType)][awsv2.ToString(offer.Location)] = struct{}{}
}
}
// check if the instance type is available in at least one of the offered zones
// per nodegroup.
for k, v := range instanceMap {
var (
notAvailableIn []string
available bool
)
for instance, azs := range v {
if zones, ok := offers[instance]; ok {
for _, az := range azs {
if _, ok := zones[az]; ok {
available = true
break
} else {
notAvailableIn = append(notAvailableIn, az)
}
}
}
if !available {
return fmt.Errorf("none of the provided AZs %q support instance type %s in nodegroup %s", strings.Join(notAvailableIn, ","), instance, k)
}
}
}
return nil
}
// ValidateLocalZones validates that the specified local zones exist.
func ValidateLocalZones(ctx context.Context, ec2API awsapi.EC2, localZones []string, region string) error {
output, err := ec2API.DescribeAvailabilityZones(ctx, &ec2.DescribeAvailabilityZonesInput{
ZoneNames: localZones,
Filters: []ec2types.Filter{
{
Name: awsv2.String("region-name"),
Values: []string{region},
},
{
Name: awsv2.String("state"),
Values: []string{string(ec2types.AvailabilityZoneStateAvailable)},
},
},
})
if err != nil {
return fmt.Errorf("error describing availability zones: %w", err)
}
if len(output.AvailabilityZones) != len(localZones) {
return fmt.Errorf("failed to find all local zones; expected to find %d available local zones but found only %d", len(localZones), len(output.AvailabilityZones))
}
for _, z := range output.AvailabilityZones {
if *z.ZoneType != "local-zone" {
return fmt.Errorf("non local-zone %q specified in localZones", *z.ZoneName)
}
}
return nil
}
// NewStackManager returns a new stack manager
func (c *ClusterProvider) NewStackManager(spec *api.ClusterConfig) manager.StackManager {
return manager.NewStackCollection(c.AWSProvider, spec)
}
// LoadClusterIntoSpecFromStack uses stack information to load the cluster
// configuration into the spec
// At the moment VPC and KubernetesNetworkConfig are respected
func (c *ClusterProvider) LoadClusterIntoSpecFromStack(ctx context.Context, spec *api.ClusterConfig, stack *manager.Stack) error {
if err := c.LoadClusterVPC(ctx, spec, stack); err != nil {
return err
}
return c.RefreshClusterStatus(ctx, spec)
}