/
eks.go
419 lines (368 loc) · 10.9 KB
/
eks.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
package aws
import (
"context"
"encoding/base64"
"fmt"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/k11n/konstellation/api/v1alpha1"
"github.com/k11n/konstellation/pkg/cloud/types"
"github.com/k11n/konstellation/pkg/utils/async"
)
const (
// By default STS signs the url for 15 minutes so we are creating a
// rfc3339 timestamp with expiration in 14 minutes as part of the token, which
// is used by some clients (client-go) who will refresh the token after 14 mins
TOKEN_EXPIRATION_MINS = 14
URL_TIMEOUT_SECONDS = 60
)
var (
statusMapping = map[string]types.ClusterStatus{
"CREATING": types.StatusCreating,
"ACTIVE": types.StatusActive,
"DELETING": types.StatusDeleting,
"FAILED": types.StatusFailed,
"UPDATING": types.StatusUpdating,
}
)
type EKSService struct {
session *session.Session
EKS *eks.EKS
}
func NewEKSService(s *session.Session) *EKSService {
return &EKSService{
session: s,
EKS: eks.New(s),
}
}
func (s *EKSService) GetAvailableVersions(ctx context.Context) ([]string, error) {
return EKSAvailableVersions, nil
}
func (s *EKSService) ListClusters(ctx context.Context) (clusters []*types.Cluster, err error) {
var clusterNames []*string
err = s.EKS.ListClustersPagesWithContext(ctx, &eks.ListClustersInput{}, func(output *eks.ListClustersOutput, b bool) bool {
for _, clusterName := range output.Clusters {
clusterNames = append(clusterNames, clusterName)
}
return true
})
if err != nil {
return
}
wp := async.NewWorkerPool()
// describe each cluster
for i, _ := range clusterNames {
clusterName := clusterNames[i]
wp.AddTask(func() (interface{}, error) {
descOut, err := s.EKS.DescribeClusterWithContext(ctx, &eks.DescribeClusterInput{
Name: clusterName,
})
if err != nil {
return nil, err
}
if descOut.Cluster.Tags[TagKonstellation] != nil && *descOut.Cluster.Tags[TagKonstellation] == TagValue1 {
return clusterFromEksCluster(descOut.Cluster), nil
}
return nil, nil
})
}
wp.StopWait()
for _, t := range wp.GetTasks() {
if t.Err != nil {
return nil, t.Err
}
if t.Result == nil {
continue
}
clusters = append(clusters, t.Result.(*types.Cluster))
}
return
}
func (s *EKSService) GetCluster(ctx context.Context, name string) (cluster *types.Cluster, err error) {
descOut, err := s.EKS.DescribeClusterWithContext(ctx, &eks.DescribeClusterInput{
Name: &name,
})
if err != nil {
return
}
cluster = clusterFromEksCluster(descOut.Cluster)
return
}
func (s *EKSService) GetAuthToken(ctx context.Context, cluster string, status types.ClusterStatus) (authToken *types.AuthToken, err error) {
var stsSvc *sts.STS
// when fully configured, we should always be using the admin role when providing kube auth token
// normally with EKS only the user who's created the cluster has access
if status == types.StatusActive {
stsSvc = sts.New(s.session)
// get current user and account info
res, err := stsSvc.GetCallerIdentityWithContext(ctx, &sts.GetCallerIdentityInput{})
if err != nil {
return nil, err
}
roleArn := arn.ARN{
Partition: "aws",
Service: "iam",
AccountID: *res.Account,
Resource: fmt.Sprintf("role/kon-%s-admin-role", cluster),
}
callerArn, err := arn.Parse(*res.Arn)
if err != nil {
return nil, err
}
parts := strings.Split(callerArn.Resource, "/")
if len(parts) != 2 {
return nil, fmt.Errorf("unexpected AWS identity: %s, expected user/<name>", callerArn.Resource)
}
userName := parts[1]
creds := stscreds.NewCredentials(s.session, roleArn.String(), func(p *stscreds.AssumeRoleProvider) {
p.RoleSessionName = userName
})
// create new STS with the role
stsSvc = sts.New(s.session, &aws.Config{Credentials: creds})
} else {
stsSvc = sts.New(s.session)
}
req, _ := stsSvc.GetCallerIdentityRequest(&sts.GetCallerIdentityInput{})
req.HTTPRequest.Header.Set("x-k8s-aws-id", cluster)
signedUrl, err := req.Presign(URL_TIMEOUT_SECONDS * time.Second)
if err != nil {
return
}
// fmt.Printf("signed url: %s\n", signedUrl)
encoded := strings.TrimRight(
base64.URLEncoding.EncodeToString([]byte(signedUrl)),
"=",
)
authToken = &types.AuthToken{
Kind: "ExecCredential",
ApiVersion: "client.authentication.k8s.io/v1alpha1",
Spec: make(map[string]interface{}),
}
expTime := time.Now().UTC().Add(TOKEN_EXPIRATION_MINS * time.Minute)
authToken.Status.ExpirationTimestamp = types.RFC3339Time(expTime)
authToken.Status.Token = fmt.Sprintf("k8s-aws-v1.%s", encoded)
return
}
func (s *EKSService) IsNodepoolReady(ctx context.Context, clusterName string, nodepoolName string) (ready bool, err error) {
res, err := s.EKS.DescribeNodegroupWithContext(ctx, &eks.DescribeNodegroupInput{
ClusterName: &clusterName,
NodegroupName: &nodepoolName,
})
if err != nil {
return
}
// https://github.com/aws/aws-sdk-go/blob/ab52e2140da6138c05220ee782cc2bcd85feecee/models/apis/eks/2017-11-01/api-2.json#L1048
ready = *res.Nodegroup.Status == "ACTIVE"
return
}
func (s *EKSService) IsNodepoolDeleted(ctx context.Context, clusterName string, nodepoolName string) (deleted bool, err error) {
res, err := s.EKS.ListNodegroupsWithContext(ctx, &eks.ListNodegroupsInput{
ClusterName: &clusterName,
MaxResults: aws.Int64(DefaultPageSize),
})
if err != nil {
return
}
for _, item := range res.Nodegroups {
if *item == nodepoolName {
return false, nil
}
}
return true, nil
}
func (s *EKSService) DeleteNodeGroupNetworkingResources(ctx context.Context, nodegroup string) error {
ec2Svc := ec2.New(s.session)
sgs, err := ec2Svc.DescribeSecurityGroupsWithContext(ctx, &ec2.DescribeSecurityGroupsInput{
Filters: []*ec2.Filter{
{
Name: aws.String(fmt.Sprintf("tag:%s", TagEKSNodeGroupName)),
Values: []*string{&nodegroup},
},
}})
if err != nil {
return err
}
var groupIds []*string
for _, sg := range sgs.SecurityGroups {
groupIds = append(groupIds, sg.GroupId)
}
if len(groupIds) == 0 {
return nil
}
// find all network interfaces and delete
niRes, err := ec2Svc.DescribeNetworkInterfacesWithContext(ctx, &ec2.DescribeNetworkInterfacesInput{
Filters: []*ec2.Filter{
{
Name: aws.String("group-id"),
Values: groupIds,
},
},
})
if err != nil {
return err
}
isSuccess := true
for _, ni := range niRes.NetworkInterfaces {
// ignore errors
_, err := ec2Svc.DeleteNetworkInterfaceWithContext(ctx, &ec2.DeleteNetworkInterfaceInput{
NetworkInterfaceId: ni.NetworkInterfaceId,
})
if err != nil {
isSuccess = false
}
}
if isSuccess {
// delete security groups
for _, groupId := range groupIds {
ec2Svc.DeleteSecurityGroupWithContext(ctx, &ec2.DeleteSecurityGroupInput{
GroupId: groupId,
})
}
}
return nil
}
func (s *EKSService) CreateNodepool(ctx context.Context, cc *v1alpha1.ClusterConfig, np *v1alpha1.Nodepool) error {
createInput, err := nodepoolSpecToCreateInput(cc, np)
if err != nil {
return err
}
_, err = s.EKS.CreateNodegroupWithContext(ctx, createInput)
return err
}
func (s *EKSService) DeleteNodepool(ctx context.Context, clusterName string, nodePool string) error {
_, err := s.EKS.DeleteNodegroupWithContext(ctx, &eks.DeleteNodegroupInput{
ClusterName: &clusterName,
NodegroupName: &nodePool,
})
return err
}
func (s *EKSService) TagSubnetsForCluster(ctx context.Context, clusterName string, subnetIds []string) error {
// tag VPC subnets if needed
ec2Svc := ec2.New(s.session)
for _, subnet := range subnetIds {
_, err := ec2Svc.CreateTagsWithContext(ctx, &ec2.CreateTagsInput{
Resources: []*string{aws.String(subnet)},
Tags: []*ec2.Tag{
{
Key: aws.String(KubeClusterTag(clusterName)),
Value: aws.String(TagValueShared),
},
},
})
if err != nil {
return err
}
}
return nil
}
func (s *EKSService) UnTagSubnetsForCluster(ctx context.Context, clusterName string, subnetIds []string) error {
// untag VPC subnets if needed
ec2Svc := ec2.New(s.session)
resourcesToTag := make([]*string, 0, len(subnetIds))
for _, subnet := range subnetIds {
resourcesToTag = append(resourcesToTag, &subnet)
}
if len(resourcesToTag) == 0 {
return nil
}
_, err := ec2Svc.DeleteTagsWithContext(ctx, &ec2.DeleteTagsInput{
Resources: resourcesToTag,
Tags: []*ec2.Tag{
{
Key: aws.String(KubeClusterTag(clusterName)),
},
},
})
return err
}
func nodepoolSpecToCreateInput(cc *v1alpha1.ClusterConfig, np *v1alpha1.Nodepool) (cni *eks.CreateNodegroupInput, err error) {
awsStatus := cc.Status.AWS
if awsStatus == nil {
err = fmt.Errorf("Nodepool creation requires ClusterConfig.AWS, which is nil")
return
}
nps := np.Spec
cni = &eks.CreateNodegroupInput{}
cni.SetClusterName(cc.Name)
cni.SetAmiType(nps.AWS.AMIType)
cni.SetDiskSize(int64(nps.DiskSizeGiB))
cni.SetInstanceTypes([]*string{&nps.MachineType})
cni.SetNodeRole(awsStatus.NodeRoleArn)
cni.SetNodegroupName(np.ObjectMeta.Name)
cni.SetScalingConfig(&eks.NodegroupScalingConfig{
MinSize: &nps.MinSize,
MaxSize: &nps.MaxSize,
DesiredSize: &nps.MinSize,
})
var subnetSrc []*v1alpha1.AWSSubnet
if cc.Spec.AWS.Topology == v1alpha1.NetworkTopologyPublicPrivate {
subnetSrc = awsStatus.PrivateSubnets
} else {
subnetSrc = awsStatus.PublicSubnets
}
for _, subnet := range subnetSrc {
cni.Subnets = append(cni.Subnets, &subnet.SubnetId)
}
if nps.AWS.SSHKeypair != "" {
rac := eks.RemoteAccessConfig{
Ec2SshKey: &nps.AWS.SSHKeypair,
}
if !nps.AWS.ConnectFromAnywhere {
groups := make([]*string, 0, len(awsStatus.SecurityGroups))
for _, g := range awsStatus.SecurityGroups {
groups = append(groups, &g)
}
rac.SetSourceSecurityGroups(groups)
}
cni.SetRemoteAccess(&rac)
}
tags := make(map[string]*string)
if nps.Autoscale {
tags[AutoscalerClusterNameTag(cc.Name)] = aws.String(TagValueOwned)
tags[TagAutoscalerEnabled] = aws.String(TagValueTrue)
}
cni.SetTags(tags)
return
}
func clusterFromEksCluster(ec *eks.Cluster) *types.Cluster {
cluster := &types.Cluster{
ID: *ec.Arn,
CloudProvider: "aws",
Name: *ec.Name,
PlatformVersion: *ec.PlatformVersion,
Status: statusMapping[*ec.Status],
Version: *ec.Version,
}
if ec.Endpoint != nil {
cluster.Endpoint = *ec.Endpoint
}
if ec.CertificateAuthority != nil && ec.CertificateAuthority.Data != nil {
var decoded []byte
decoded, err := base64.StdEncoding.DecodeString(*ec.CertificateAuthority.Data)
if err == nil {
cluster.CertificateAuthorityData = decoded
}
}
// consider unactivated clusters as unconfigured
if cluster.Status == types.StatusActive {
if ec.Tags[TagClusterActivated] == nil {
cluster.Status = types.StatusUnconfigured
}
}
return cluster
}
func GetEC2Tag(tags []*ec2.Tag, name string) *ec2.Tag {
for _, tag := range tags {
if *tag.Key == name {
return tag
}
}
return nil
}