-
Notifications
You must be signed in to change notification settings - Fork 34
/
eks-cluster-config-handler.go
1409 lines (1249 loc) · 50.3 KB
/
eks-cluster-config-handler.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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package controller
import (
"context"
"encoding/base64"
"fmt"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/blang/semver"
eksv1 "github.com/rancher/eks-operator/pkg/apis/eks.cattle.io/v1"
awsservices "github.com/rancher/eks-operator/pkg/eks"
"github.com/rancher/eks-operator/pkg/eks/services"
ekscontrollers "github.com/rancher/eks-operator/pkg/generated/controllers/eks.cattle.io/v1"
"github.com/rancher/eks-operator/templates"
"github.com/rancher/eks-operator/utils"
wranglerv1 "github.com/rancher/wrangler/v2/pkg/generated/controllers/core/v1"
"github.com/sirupsen/logrus"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/util/retry"
)
const (
controllerName = "eks-controller"
controllerRemoveName = "eks-controller-remove"
eksConfigCreatingPhase = "creating"
eksConfigNotCreatedPhase = ""
eksConfigActivePhase = "active"
eksConfigUpdatingPhase = "updating"
eksConfigImportingPhase = "importing"
eksClusterConfigKind = "EKSClusterConfig"
)
type Handler struct {
eksCC ekscontrollers.EKSClusterConfigClient
eksEnqueueAfter func(namespace, name string, duration time.Duration)
eksEnqueue func(namespace, name string)
secrets wranglerv1.SecretClient
secretsCache wranglerv1.SecretCache
}
type awsServices struct {
cloudformation services.CloudFormationServiceInterface
eks services.EKSServiceInterface
ec2 services.EC2ServiceInterface
iam services.IAMServiceInterface
}
func Register(
ctx context.Context,
secrets wranglerv1.SecretController,
eks ekscontrollers.EKSClusterConfigController) {
controller := &Handler{
eksCC: eks,
eksEnqueue: eks.Enqueue,
eksEnqueueAfter: eks.EnqueueAfter,
secretsCache: secrets.Cache(),
secrets: secrets,
}
// Register handlers
eks.OnChange(ctx, controllerName, controller.recordError(controller.OnEksConfigChanged))
eks.OnRemove(ctx, controllerRemoveName, controller.OnEksConfigRemoved)
}
func (h *Handler) OnEksConfigChanged(_ string, config *eksv1.EKSClusterConfig) (*eksv1.EKSClusterConfig, error) {
if config == nil {
return nil, nil
}
if config.DeletionTimestamp != nil {
return nil, nil
}
awsSVCs, err := newAWSServices(h.secretsCache, config.Spec)
if err != nil {
return config, fmt.Errorf("error creating new AWS services: %w", err)
}
switch config.Status.Phase {
case eksConfigImportingPhase:
return h.importCluster(config, awsSVCs)
case eksConfigNotCreatedPhase:
return h.create(config, awsSVCs)
case eksConfigCreatingPhase:
return h.waitForCreationComplete(config, awsSVCs)
case eksConfigActivePhase, eksConfigUpdatingPhase:
return h.checkAndUpdate(config, awsSVCs)
}
return config, nil
}
// recordError writes the error return by onChange to the failureMessage field on status. If there is no error, then
// empty string will be written to status
func (h *Handler) recordError(onChange func(key string, config *eksv1.EKSClusterConfig) (*eksv1.EKSClusterConfig, error)) func(key string, config *eksv1.EKSClusterConfig) (*eksv1.EKSClusterConfig, error) {
return func(key string, config *eksv1.EKSClusterConfig) (*eksv1.EKSClusterConfig, error) {
var err error
var message string
config, err = onChange(key, config)
if config == nil {
// EKS config is likely deleting
return config, err
}
if err != nil {
if !strings.Contains(err.Error(), "currently has update") {
// The update is valid in that the controller should retry but there is no actionable resolution as far
// as a user is concerned. An update has either been initiated by the eks-operator or another source
// is already in progress. It is possible an update is not being immediately reflected in the upstream
// cluster state. The config object will reenter the controller and then the controller will wait for
// the update to finish.
message = err.Error()
}
messageNoMeta, err := removeErrorMetadata(message)
if err != nil {
logrus.Errorf("Error removing metadata from failure message: %s", err.Error())
} else {
message = messageNoMeta
}
}
if config.Status.FailureMessage == message {
return config, err
}
if message != "" {
config = config.DeepCopy()
if config.Status.Phase == eksConfigActivePhase {
// can assume an update is failing
config.Status.Phase = eksConfigUpdatingPhase
}
}
config.Status.FailureMessage = message
var recordErr error
config, recordErr = h.eksCC.UpdateStatus(config)
if recordErr != nil {
logrus.Errorf("Error recording ekscc [%s] failure message: %s", config.Name, recordErr.Error())
}
return config, err
}
}
func removeErrorMetadata(message string) (string, error) {
// failure message
type RespMetadata struct {
StatusCode int `json:"statusCode"`
RequestID string `json:"requestID"`
}
type Message struct {
RespMetadata RespMetadata `json:"respMetadata"`
ClusterName string `json:"clusterName"`
Message_ string `json:"message_"` // nolint:revive
NodegroupName string `json:"nodegroupName"`
}
// failure message with no meta
type FailureMessage struct {
ClusterName string `json:"clusterName"`
Message_ string `json:"message_"` // nolint:revive
NodegroupName string `json:"nodegroupName"`
}
// Remove the first line of the message because it usually contains the name of an Amazon EKS error type that
// implements Serializable (ex: ResourceInUseException). That name is unpredictable depending on the error. We
// only need cluster name, message, and node group.
index := strings.Index(message, "{")
if index == -1 {
return "", fmt.Errorf("message body not formatted as expected")
}
message = message[index:]
// unmarshal json error to an object
in := []byte(message)
failureMessage := Message{}
err := yaml.Unmarshal(in, &failureMessage)
if err != nil {
return "", err
}
// add error message fields without metadata to new object
failureMessageNoMeta := FailureMessage{
ClusterName: failureMessage.ClusterName,
Message_: failureMessage.Message_,
NodegroupName: failureMessage.NodegroupName,
}
str := fmt.Sprintf("%#v", failureMessageNoMeta)
return str, nil
}
func (h *Handler) OnEksConfigRemoved(_ string, config *eksv1.EKSClusterConfig) (*eksv1.EKSClusterConfig, error) {
awsSVCs, err := newAWSServices(h.secretsCache, config.Spec)
if err != nil {
return config, fmt.Errorf("error creating new AWS services: %w", err)
}
if config.Spec.Imported {
logrus.Infof("cluster [%s] is imported, will not delete EKS cluster", config.Name)
return config, nil
}
if config.Status.Phase == eksConfigNotCreatedPhase {
// The most likely context here is that the cluster already existed in EKS, so we shouldn't delete it
logrus.Warnf("cluster [%s] never advanced to creating status, will not delete EKS cluster", config.Name)
return config, nil
}
logrus.Infof("deleting cluster [%s]", config.Name)
logrus.Infof("starting node group deletion for config [%s]", config.Spec.DisplayName)
waitingForNodegroupDeletion := true
for waitingForNodegroupDeletion {
waitingForNodegroupDeletion, err = deleteNodeGroups(config, config.Spec.NodeGroups, awsSVCs.eks)
if err != nil {
return config, fmt.Errorf("error deleting nodegroups for config [%s]", config.Spec.DisplayName)
}
time.Sleep(10 * time.Second)
logrus.Infof("waiting for config [%s] node groups to delete", config.Name)
}
if config.Status.ManagedLaunchTemplateID != "" {
logrus.Infof("deleting common launch template for config [%s]", config.Name)
deleteLaunchTemplate(config.Status.ManagedLaunchTemplateID, awsSVCs.ec2)
}
logrus.Infof("starting control plane deletion for config [%s]", config.Name)
_, err = awsSVCs.eks.DeleteCluster(&eks.DeleteClusterInput{
Name: aws.String(config.Spec.DisplayName),
})
if err != nil {
if notFound(err) {
_, err = awsSVCs.eks.DeleteCluster(&eks.DeleteClusterInput{
Name: aws.String(config.Spec.DisplayName),
})
}
if err != nil && !notFound(err) {
return config, fmt.Errorf("error deleting cluster: %v", err)
}
}
if aws.BoolValue(config.Spec.EBSCSIDriver) {
logrus.Infof("deleting ebs csi driver role for config [%s]", config.Name)
if err := deleteStack(awsSVCs.cloudformation, getEBSCSIDriverRoleStackName(config.Spec.DisplayName), getEBSCSIDriverRoleStackName(config.Spec.DisplayName)); err != nil {
return config, fmt.Errorf("error ebs csi driver role stack: %v", err)
}
}
if aws.StringValue(config.Spec.ServiceRole) == "" {
logrus.Infof("deleting service role for config [%s]", config.Name)
if err := deleteStack(awsSVCs.cloudformation, getServiceRoleName(config.Spec.DisplayName), getServiceRoleName(config.Spec.DisplayName)); err != nil {
return config, fmt.Errorf("error deleting service role stack: %v", err)
}
}
if len(config.Spec.Subnets) == 0 {
logrus.Infof("deleting vpc, subnets, and security groups for config [%s]", config.Name)
if err := deleteStack(awsSVCs.cloudformation, getVPCStackName(config.Spec.DisplayName), getVPCStackName(config.Spec.DisplayName)); err != nil {
return config, fmt.Errorf("error deleting vpc stack: %v", err)
}
}
logrus.Infof("deleting node instance role for config [%s]", config.Name)
if err := deleteStack(awsSVCs.cloudformation, fmt.Sprintf("%s-node-instance-role", config.Spec.DisplayName), fmt.Sprintf("%s-node-instance-role", config.Spec.DisplayName)); err != nil {
return config, fmt.Errorf("error deleting worker node stack: %v", err)
}
return config, err
}
func (h *Handler) checkAndUpdate(config *eksv1.EKSClusterConfig, awsSVCs *awsServices) (*eksv1.EKSClusterConfig, error) {
if awsSVCs == nil {
return config, fmt.Errorf("aws services not initialized")
}
if err := validateUpdate(config); err != nil {
// validation failed, will be considered a failing update until resolved
config = config.DeepCopy()
config.Status.Phase = eksConfigUpdatingPhase
var updateErr error
config, updateErr = h.eksCC.UpdateStatus(config)
if updateErr != nil {
return config, updateErr
}
return config, err
}
clusterState, err := awsservices.GetClusterState(&awsservices.GetClusterStatusOpts{
EKSService: awsSVCs.eks,
Config: config,
})
if err != nil {
return config, err
}
if aws.StringValue(clusterState.Cluster.Status) == eks.ClusterStatusUpdating {
// upstream cluster is already updating, must wait until sending next update
logrus.Infof("waiting for cluster [%s] to finish updating", config.Name)
if config.Status.Phase != eksConfigUpdatingPhase {
config = config.DeepCopy()
config.Status.Phase = eksConfigUpdatingPhase
return h.eksCC.UpdateStatus(config)
}
h.eksEnqueueAfter(config.Namespace, config.Name, 30*time.Second)
return config, nil
}
ngs, err := awsSVCs.eks.ListNodegroups(
&eks.ListNodegroupsInput{
ClusterName: aws.String(config.Spec.DisplayName),
})
if err != nil {
return config, err
}
// gather upstream node groups states
nodeGroupStates := make([]*eks.DescribeNodegroupOutput, 0, len(ngs.Nodegroups))
nodegroupARNs := make(map[string]string)
for _, ngName := range ngs.Nodegroups {
ng, err := awsSVCs.eks.DescribeNodegroup(
&eks.DescribeNodegroupInput{
ClusterName: aws.String(config.Spec.DisplayName),
NodegroupName: ngName,
})
if err != nil {
return config, err
}
if status := aws.StringValue(ng.Nodegroup.Status); status == eks.NodegroupStatusUpdating || status == eks.NodegroupStatusDeleting ||
status == eks.NodegroupStatusCreating {
if config.Status.Phase != eksConfigUpdatingPhase {
config = config.DeepCopy()
config.Status.Phase = eksConfigUpdatingPhase
config, err = h.eksCC.UpdateStatus(config)
if err != nil {
return config, err
}
}
logrus.Infof("waiting for cluster [%s] to update nodegroups [%s]", config.Name, aws.StringValue(ngName))
h.eksEnqueueAfter(config.Namespace, config.Name, 30*time.Second)
return config, nil
}
nodeGroupStates = append(nodeGroupStates, ng)
nodegroupARNs[aws.StringValue(ngName)] = aws.StringValue(ng.Nodegroup.NodegroupArn)
}
if config.Status.Phase == eksConfigActivePhase && len(config.Status.TemplateVersionsToDelete) != 0 {
// If there are any launch template versions that need to be cleaned up, we do it now.
awsservices.DeleteLaunchTemplateVersions(awsSVCs.ec2, config.Status.ManagedLaunchTemplateID, aws.StringSlice(config.Status.TemplateVersionsToDelete))
config = config.DeepCopy()
config.Status.TemplateVersionsToDelete = nil
return h.eksCC.UpdateStatus(config)
}
upstreamSpec, clusterARN, err := BuildUpstreamClusterState(config.Spec.DisplayName, config.Status.ManagedLaunchTemplateID, clusterState, nodeGroupStates, awsSVCs.ec2, true)
if err != nil {
return config, err
}
return h.updateUpstreamClusterState(upstreamSpec, config, awsSVCs, clusterARN, nodegroupARNs)
}
func validateUpdate(config *eksv1.EKSClusterConfig) error {
var clusterVersion *semver.Version
if config.Spec.KubernetesVersion != nil {
var err error
clusterVersion, err = semver.New(fmt.Sprintf("%s.0", aws.StringValue(config.Spec.KubernetesVersion)))
if err != nil {
return fmt.Errorf("improper version format for cluster [%s]: %s", config.Name, aws.StringValue(config.Spec.KubernetesVersion))
}
}
errs := make([]string, 0)
nodeGroupNames := make(map[string]struct{}, 0)
// validate nodegroup versions
for _, ng := range config.Spec.NodeGroups {
if _, ok := nodeGroupNames[aws.StringValue(ng.NodegroupName)]; !ok {
nodeGroupNames[aws.StringValue(ng.NodegroupName)] = struct{}{}
} else {
errs = append(errs, fmt.Sprintf("node group names must be unique within the [%s] cluster to avoid duplication", config.Name))
}
if ng.Version == nil {
continue
}
version, err := semver.New(fmt.Sprintf("%s.0", aws.StringValue(ng.Version)))
if err != nil {
errs = append(errs, fmt.Sprintf("improper version format for nodegroup [%s]: %s", aws.StringValue(ng.NodegroupName), aws.StringValue(ng.Version)))
continue
}
if clusterVersion == nil {
continue
}
if clusterVersion.EQ(*version) {
continue
}
if clusterVersion.Minor-version.Minor == 1 {
continue
}
errs = append(errs, fmt.Sprintf("versions for cluster [%s] and nodegroup [%s] not compatible: all nodegroup kubernetes versions"+
"must be equal to or one minor version lower than the cluster kubernetes version", aws.StringValue(config.Spec.KubernetesVersion), aws.StringValue(ng.Version)))
}
if len(errs) != 0 {
return fmt.Errorf(strings.Join(errs, ";"))
}
return nil
}
func (h *Handler) create(config *eksv1.EKSClusterConfig, awsSVCs *awsServices) (*eksv1.EKSClusterConfig, error) {
if awsSVCs == nil {
return config, fmt.Errorf("aws services not initialized")
}
if err := h.validateCreate(config, awsSVCs); err != nil {
return config, err
}
if config.Spec.Imported {
config = config.DeepCopy()
config.Status.Phase = eksConfigImportingPhase
return h.eksCC.UpdateStatus(config)
}
config, err := h.generateAndSetNetworking(config, awsSVCs)
if err != nil {
return config, fmt.Errorf("error generating and setting networking: %w", err)
}
roleARN, err := h.createOrGetServiceRole(config, awsSVCs)
if err != nil {
return config, fmt.Errorf("error creating or getting service role: %w", err)
}
if err := awsservices.CreateCluster(&awsservices.CreateClusterOptions{
EKSService: awsSVCs.eks,
Config: config,
RoleARN: roleARN,
}); err != nil {
if !isClusterConflict(err) {
return config, fmt.Errorf("error creating cluster: %w", err)
}
}
// If a user edits a cluster at the exact right (or wrong) time, then the
// `UpdateStatus` call may produce a conflict and will error. When the
// controller re-enters the create function, it will try to verify that a
// cluster with the same name in EKS does not exist (in the `validateCreate`
// call above). It will find the one that was created and error.
// Therefore, the `RetryOnConflict` will successfully update the status in this situation.
err = retry.RetryOnConflict(retry.DefaultRetry, func() error {
config, err = h.eksCC.Get(config.Namespace, config.Name, metav1.GetOptions{})
if err != nil {
return err
}
config.Status.Phase = eksConfigCreatingPhase
config.Status.FailureMessage = ""
config, err = h.eksCC.UpdateStatus(config)
return err
})
return config, err
}
func (h *Handler) validateCreate(config *eksv1.EKSClusterConfig, awsSVCs *awsServices) error {
if awsSVCs == nil {
return fmt.Errorf("aws services not initialized")
}
// Check for existing eksclusterconfigs with the same display name
eksConfigs, err := h.eksCC.List(config.Namespace, metav1.ListOptions{})
if err != nil {
return fmt.Errorf("cannot list eksclusterconfigs for display name check")
}
for _, c := range eksConfigs.Items {
if c.Spec.DisplayName == config.Spec.DisplayName && c.Name != config.Name {
return fmt.Errorf("cannot create cluster [%s] because an eksclusterconfig exists with the same name", config.Spec.DisplayName)
}
}
// validate nodegroup version
nodeP := map[string]bool{}
if !config.Spec.Imported {
// Check for existing clusters in EKS with the same display name
listOutput, err := awsSVCs.eks.ListClusters(&eks.ListClustersInput{})
if err != nil {
return fmt.Errorf("error listing clusters: %v", err)
}
for _, cluster := range listOutput.Clusters {
if aws.StringValue(cluster) == config.Spec.DisplayName {
return fmt.Errorf("cannot create cluster [%s] because a cluster in EKS exists with the same name", config.Spec.DisplayName)
}
}
cannotBeNilError := "field [%s] cannot be nil for non-import cluster [%s]"
if config.Spec.KubernetesVersion == nil {
return fmt.Errorf(cannotBeNilError, "kubernetesVersion", config.Name)
}
if config.Spec.PrivateAccess == nil {
return fmt.Errorf(cannotBeNilError, "privateAccess", config.Name)
}
if config.Spec.PublicAccess == nil {
return fmt.Errorf(cannotBeNilError, "publicAccess", config.Name)
}
if config.Spec.SecretsEncryption == nil {
return fmt.Errorf(cannotBeNilError, "secretsEncryption", config.Name)
}
if config.Spec.Tags == nil {
return fmt.Errorf(cannotBeNilError, "tags", config.Name)
}
if config.Spec.Subnets == nil {
return fmt.Errorf(cannotBeNilError, "subnets", config.Name)
}
if config.Spec.SecurityGroups == nil {
return fmt.Errorf(cannotBeNilError, "securityGroups", config.Name)
}
if config.Spec.LoggingTypes == nil {
return fmt.Errorf(cannotBeNilError, "loggingTypes", config.Name)
}
if config.Spec.PublicAccessSources == nil {
return fmt.Errorf(cannotBeNilError, "publicAccessSources", config.Name)
}
}
for _, ng := range config.Spec.NodeGroups {
cannotBeNilError := "field [%s] cannot be nil for nodegroup [%s] in non-nil cluster [%s]"
if !config.Spec.Imported {
if ng.LaunchTemplate != nil {
if ng.LaunchTemplate.ID == nil {
return fmt.Errorf(cannotBeNilError, "launchTemplate.ID", *ng.NodegroupName, config.Name)
}
if ng.LaunchTemplate.Version == nil {
return fmt.Errorf(cannotBeNilError, "launchTemplate.Version", *ng.NodegroupName, config.Name)
}
} else {
if ng.Ec2SshKey == nil {
return fmt.Errorf(cannotBeNilError, "ec2SshKey", *ng.NodegroupName, config.Name)
}
if ng.ResourceTags == nil {
return fmt.Errorf(cannotBeNilError, "resourceTags", *ng.NodegroupName, config.Name)
}
if ng.DiskSize == nil {
return fmt.Errorf(cannotBeNilError, "diskSize", *ng.NodegroupName, config.Name)
}
if !aws.BoolValue(ng.RequestSpotInstances) && ng.InstanceType == nil {
return fmt.Errorf(cannotBeNilError, "instanceType", *ng.NodegroupName, config.Name)
}
}
if ng.NodegroupName == nil {
return fmt.Errorf(cannotBeNilError, "name", *ng.NodegroupName, config.Name)
}
if nodeP[*ng.NodegroupName] {
return fmt.Errorf("node group names must be unique within the [%s] cluster to avoid duplication", config.Name)
}
nodeP[*ng.NodegroupName] = true
if ng.Version == nil {
return fmt.Errorf(cannotBeNilError, "version", *ng.NodegroupName, config.Name)
}
if ng.MinSize == nil {
return fmt.Errorf(cannotBeNilError, "minSize", *ng.NodegroupName, config.Name)
}
if ng.MaxSize == nil {
return fmt.Errorf(cannotBeNilError, "maxSize", *ng.NodegroupName, config.Name)
}
if ng.DesiredSize == nil {
return fmt.Errorf(cannotBeNilError, "desiredSize", *ng.NodegroupName, config.Name)
}
if ng.Gpu == nil {
return fmt.Errorf(cannotBeNilError, "gpu", *ng.NodegroupName, config.Name)
}
if ng.Subnets == nil {
return fmt.Errorf(cannotBeNilError, "subnets", *ng.NodegroupName, config.Name)
}
if ng.Tags == nil {
return fmt.Errorf(cannotBeNilError, "tags", *ng.NodegroupName, config.Name)
}
if ng.Labels == nil {
return fmt.Errorf(cannotBeNilError, "labels", *ng.NodegroupName, config.Name)
}
if ng.RequestSpotInstances == nil {
return fmt.Errorf(cannotBeNilError, "requestSpotInstances", *ng.NodegroupName, config.Name)
}
if ng.NodeRole == nil {
logrus.Warnf("nodeRole is not specified for nodegroup [%s] in cluster [%s], the controller will generate it", *ng.NodegroupName, config.Name)
}
if aws.BoolValue(ng.RequestSpotInstances) {
if len(ng.SpotInstanceTypes) == 0 {
return fmt.Errorf("nodegroup [%s] in cluster [%s]: spotInstanceTypes must be specified when requesting spot instances", *ng.NodegroupName, config.Name)
}
if aws.StringValue(ng.InstanceType) != "" {
return fmt.Errorf("nodegroup [%s] in cluster [%s]: instance type should not be specified when requestSpotInstances is specified, use spotInstanceTypes instead",
*ng.NodegroupName, config.Name)
}
}
}
if aws.StringValue(ng.Version) != *config.Spec.KubernetesVersion {
return fmt.Errorf("nodegroup [%s] version must match cluster [%s] version on create", aws.StringValue(ng.NodegroupName), config.Name)
}
}
return nil
}
func (h *Handler) generateAndSetNetworking(config *eksv1.EKSClusterConfig, awsSVCs *awsServices) (*eksv1.EKSClusterConfig, error) {
if awsSVCs == nil {
return nil, fmt.Errorf("aws services not initialized")
}
if len(config.Status.Subnets) != 0 {
// networking fields have already been set
return config, nil
}
if len(config.Spec.Subnets) != 0 {
logrus.Infof("VPC info provided, skipping vpc/subnet/securitygroup creation")
config = config.DeepCopy()
// copy networking fields to status
config.Status.Subnets = config.Spec.Subnets
config.Status.SecurityGroups = config.Spec.SecurityGroups
config.Status.NetworkFieldsSource = "provided"
} else {
logrus.Infof("Bringing up vpc")
stack, err := awsservices.CreateStack(&awsservices.CreateStackOptions{
CloudFormationService: awsSVCs.cloudformation,
StackName: getVPCStackName(config.Spec.DisplayName),
DisplayName: config.Spec.DisplayName,
TemplateBody: templates.VpcTemplate,
Capabilities: []string{},
Parameters: []*cloudformation.Parameter{},
})
if err != nil {
return config, fmt.Errorf("error creating stack with VPC template: %v", err)
}
virtualNetworkString := getParameterValueFromOutput("VpcId", stack.Stacks[0].Outputs)
subnetIDsString := getParameterValueFromOutput("SubnetIds", stack.Stacks[0].Outputs)
if subnetIDsString == "" {
return config, fmt.Errorf("no subnet ids were returned")
}
config = config.DeepCopy()
// copy generated field to status
config.Status.VirtualNetwork = virtualNetworkString
config.Status.Subnets = strings.Split(subnetIDsString, ",")
config.Status.NetworkFieldsSource = "generated"
}
return h.eksCC.UpdateStatus(config)
}
func (h *Handler) createOrGetServiceRole(config *eksv1.EKSClusterConfig, awsSVCs *awsServices) (string, error) {
var roleARN string
if aws.StringValue(config.Spec.ServiceRole) == "" {
logrus.Infof("Creating service role")
stack, err := awsservices.CreateStack(&awsservices.CreateStackOptions{
CloudFormationService: awsSVCs.cloudformation,
StackName: getServiceRoleName(config.Spec.DisplayName),
DisplayName: config.Spec.DisplayName,
TemplateBody: templates.ServiceRoleTemplate,
Capabilities: []string{cloudformation.CapabilityCapabilityIam},
Parameters: nil,
})
if err != nil {
return "", fmt.Errorf("error creating stack with service role template: %v", err)
}
roleARN = getParameterValueFromOutput("RoleArn", stack.Stacks[0].Outputs)
if roleARN == "" {
return "", fmt.Errorf("no RoleARN was returned")
}
} else {
logrus.Infof("Retrieving existing service role")
role, err := awsSVCs.iam.GetRole(&iam.GetRoleInput{
RoleName: config.Spec.ServiceRole,
})
if err != nil {
return "", fmt.Errorf("error getting role: %w", err)
}
roleARN = *role.Role.Arn
}
return roleARN, nil
}
func newAWSServices(secretsCache wranglerv1.SecretCache, spec eksv1.EKSClusterConfigSpec) (*awsServices, error) {
sess, err := newAWSSession(secretsCache, spec)
if err != nil {
return nil, err
}
return &awsServices{
eks: services.NewEKSService(sess),
cloudformation: services.NewCloudFormationService(sess),
iam: services.NewIAMService(sess),
ec2: services.NewEC2Service(sess),
}, nil
}
func newAWSSession(secretsCache wranglerv1.SecretCache, spec eksv1.EKSClusterConfigSpec) (*session.Session, error) {
awsConfig := &aws.Config{}
if region := spec.Region; region != "" {
awsConfig.Region = aws.String(region)
}
ns, id := utils.Parse(spec.AmazonCredentialSecret)
if amazonCredentialSecret := spec.AmazonCredentialSecret; amazonCredentialSecret != "" {
secret, err := secretsCache.Get(ns, id)
if err != nil {
return nil, fmt.Errorf("error getting secret %s/%s: %w", ns, id, err)
}
accessKeyBytes := secret.Data["amazonec2credentialConfig-accessKey"]
secretKeyBytes := secret.Data["amazonec2credentialConfig-secretKey"]
if accessKeyBytes == nil || secretKeyBytes == nil {
return nil, fmt.Errorf("invalid aws cloud credential")
}
accessKey := string(accessKeyBytes)
secretKey := string(secretKeyBytes)
awsConfig.Credentials = credentials.NewStaticCredentials(accessKey, secretKey, "")
}
sess, err := session.NewSession(awsConfig)
if err != nil {
return nil, fmt.Errorf("error getting new aws session: %v", err)
}
return sess, nil
}
func (h *Handler) waitForCreationComplete(config *eksv1.EKSClusterConfig, awsSVCs *awsServices) (*eksv1.EKSClusterConfig, error) {
if awsSVCs == nil {
return config, fmt.Errorf("aws services not initialized")
}
var err error
state, err := awsservices.GetClusterState(&awsservices.GetClusterStatusOpts{
EKSService: awsSVCs.eks,
Config: config,
})
if err != nil {
return config, err
}
if state.Cluster == nil {
return config, fmt.Errorf("no cluster data was returned")
}
if state.Cluster.Status == nil {
return config, fmt.Errorf("no cluster status was returned")
}
status := *state.Cluster.Status
if status == eks.ClusterStatusFailed {
return config, fmt.Errorf("creation failed for cluster named %q with ARN %q",
aws.StringValue(state.Cluster.Name),
aws.StringValue(state.Cluster.Arn))
}
if status == eks.ClusterStatusActive {
if err := h.createCASecret(config, state); err != nil {
return config, err
}
logrus.Infof("cluster [%s] created successfully", config.Name)
config = config.DeepCopy()
config.Status.Phase = eksConfigActivePhase
return h.eksCC.UpdateStatus(config)
}
logrus.Infof("waiting for cluster [%s] to finish creating", config.Name)
h.eksEnqueueAfter(config.Namespace, config.Name, 30*time.Second)
return config, nil
}
// buildUpstreamClusterState
func BuildUpstreamClusterState(name, managedTemplateID string, clusterState *eks.DescribeClusterOutput, nodeGroupStates []*eks.DescribeNodegroupOutput, ec2Service services.EC2ServiceInterface, includeManagedLaunchTemplate bool) (*eksv1.EKSClusterConfigSpec, string, error) {
upstreamSpec := &eksv1.EKSClusterConfigSpec{}
upstreamSpec.Imported = true
upstreamSpec.DisplayName = name
// set kubernetes version
upstreamVersion := aws.StringValue(clusterState.Cluster.Version)
if upstreamVersion == "" {
return nil, "", fmt.Errorf("cannot detect cluster [%s] upstream kubernetes version", name)
}
upstreamSpec.KubernetesVersion = aws.String(upstreamVersion)
// set tags
upstreamSpec.Tags = make(map[string]string)
if len(clusterState.Cluster.Tags) != 0 {
upstreamSpec.Tags = aws.StringValueMap(clusterState.Cluster.Tags)
}
// set public access
if hasPublicAccess := clusterState.Cluster.ResourcesVpcConfig.EndpointPublicAccess; hasPublicAccess == nil || *hasPublicAccess {
upstreamSpec.PublicAccess = aws.Bool(true)
} else {
upstreamSpec.PublicAccess = aws.Bool(false)
}
// set private access
if hasPrivateAccess := clusterState.Cluster.ResourcesVpcConfig.EndpointPrivateAccess; hasPrivateAccess != nil && *hasPrivateAccess {
upstreamSpec.PrivateAccess = aws.Bool(true)
} else {
upstreamSpec.PrivateAccess = aws.Bool(false)
}
// set public access sources
upstreamSpec.PublicAccessSources = make([]string, 0)
if publicAccessSources := aws.StringValueSlice(clusterState.Cluster.ResourcesVpcConfig.PublicAccessCidrs); len(publicAccessSources) > 0 {
upstreamSpec.PublicAccessSources = publicAccessSources
}
// set logging
upstreamSpec.LoggingTypes = make([]string, 0)
if clusterState.Cluster.Logging != nil {
if clusterLogging := clusterState.Cluster.Logging.ClusterLogging; len(clusterLogging) > 0 {
setup := clusterLogging[0]
if aws.BoolValue(setup.Enabled) {
upstreamSpec.LoggingTypes = aws.StringValueSlice(setup.Types)
}
}
}
// set node groups
upstreamSpec.NodeGroups = make([]eksv1.NodeGroup, 0, len(nodeGroupStates))
for _, ng := range nodeGroupStates {
if aws.StringValue(ng.Nodegroup.Status) == eks.NodegroupStatusDeleting {
continue
}
ngToAdd := eksv1.NodeGroup{
NodegroupName: ng.Nodegroup.NodegroupName,
DiskSize: ng.Nodegroup.DiskSize,
Labels: ng.Nodegroup.Labels,
DesiredSize: ng.Nodegroup.ScalingConfig.DesiredSize,
MaxSize: ng.Nodegroup.ScalingConfig.MaxSize,
MinSize: ng.Nodegroup.ScalingConfig.MinSize,
NodeRole: ng.Nodegroup.NodeRole,
Subnets: aws.StringValueSlice(ng.Nodegroup.Subnets),
Tags: ng.Nodegroup.Tags,
RequestSpotInstances: aws.Bool(aws.StringValue(ng.Nodegroup.CapacityType) == eks.CapacityTypesSpot),
}
if aws.StringValue(ng.Nodegroup.Status) != eks.NodegroupStatusUpdating {
ngToAdd.Version = ng.Nodegroup.Version
}
if aws.BoolValue(ngToAdd.RequestSpotInstances) {
ngToAdd.SpotInstanceTypes = ng.Nodegroup.InstanceTypes
}
if ng.Nodegroup.LaunchTemplate != nil {
var version *int64
versionNumber, err := strconv.ParseInt(aws.StringValue(ng.Nodegroup.LaunchTemplate.Version), 10, 64)
if err == nil {
version = aws.Int64(versionNumber)
}
ngToAdd.LaunchTemplate = &eksv1.LaunchTemplate{
ID: ng.Nodegroup.LaunchTemplate.Id,
Name: ng.Nodegroup.LaunchTemplate.Name,
Version: version,
}
if managedTemplateID == aws.StringValue(ngToAdd.LaunchTemplate.ID) {
// If this is a rancher-managed launch template, then we move the data from the launch template to the node group.
launchTemplateRequestOutput, err := awsservices.GetLaunchTemplateVersions(&awsservices.GetLaunchTemplateVersionsOpts{
EC2Service: ec2Service,
LaunchTemplateID: ngToAdd.LaunchTemplate.ID,
Versions: []*string{ng.Nodegroup.LaunchTemplate.Version},
})
if err != nil || len(launchTemplateRequestOutput.LaunchTemplateVersions) == 0 {
if doesNotExist(err) || notFound(err) {
if includeManagedLaunchTemplate {
// In this case, we need to continue rather than error so that we can update the launch template for the nodegroup.
ngToAdd.LaunchTemplate.ID = nil
upstreamSpec.NodeGroups = append(upstreamSpec.NodeGroups, ngToAdd)
continue
}
return nil, "", fmt.Errorf("rancher-managed launch template for node group [%s] in cluster [%s] not found, must create new node group and destroy existing",
aws.StringValue(ngToAdd.NodegroupName),
upstreamSpec.DisplayName,
)
}
return nil, "", fmt.Errorf("error getting launch template info for node group [%s] in cluster [%s]", aws.StringValue(ngToAdd.NodegroupName), upstreamSpec.DisplayName)
}
launchTemplateData := launchTemplateRequestOutput.LaunchTemplateVersions[0].LaunchTemplateData
if len(launchTemplateData.BlockDeviceMappings) == 0 {
return nil, "", fmt.Errorf("launch template for node group [%s] in cluster [%s] is malformed", aws.StringValue(ngToAdd.NodegroupName), upstreamSpec.DisplayName)
}
ngToAdd.DiskSize = launchTemplateData.BlockDeviceMappings[0].Ebs.VolumeSize
ngToAdd.Ec2SshKey = launchTemplateData.KeyName
ngToAdd.ImageID = launchTemplateData.ImageId
ngToAdd.InstanceType = launchTemplateData.InstanceType
ngToAdd.ResourceTags = utils.GetInstanceTags(launchTemplateData.TagSpecifications)
userData := aws.StringValue(launchTemplateData.UserData)
if userData != "" {
decodedUserdata, err := base64.StdEncoding.DecodeString(userData)
if err == nil {
ngToAdd.UserData = aws.String(string(decodedUserdata))
} else {
logrus.Warnf("Could not decode userdata for nodegroup [%s] in cluster[%s]", aws.StringValue(ngToAdd.NodegroupName), name)
}
}
if !includeManagedLaunchTemplate {
ngToAdd.LaunchTemplate = nil
}
}
} else {
// If the node group does not have a launch template, then the following must be pulled from the node group config.
if !aws.BoolValue(ngToAdd.RequestSpotInstances) && len(ng.Nodegroup.InstanceTypes) > 0 {
ngToAdd.InstanceType = ng.Nodegroup.InstanceTypes[0]
}
if ng.Nodegroup.RemoteAccess != nil {
ngToAdd.Ec2SshKey = ng.Nodegroup.RemoteAccess.Ec2SshKey
}
}
if aws.StringValue(ng.Nodegroup.AmiType) == eks.AMITypesAl2X8664Gpu {
ngToAdd.Gpu = aws.Bool(true)
} else if aws.StringValue(ng.Nodegroup.AmiType) == eks.AMITypesAl2X8664 {
ngToAdd.Gpu = aws.Bool(false)
}
upstreamSpec.NodeGroups = append(upstreamSpec.NodeGroups, ngToAdd)
}
// set subnets
upstreamSpec.Subnets = aws.StringValueSlice(clusterState.Cluster.ResourcesVpcConfig.SubnetIds)
// set security groups
upstreamSpec.SecurityGroups = aws.StringValueSlice(clusterState.Cluster.ResourcesVpcConfig.SecurityGroupIds)
upstreamSpec.SecretsEncryption = aws.Bool(len(clusterState.Cluster.EncryptionConfig) != 0)
upstreamSpec.KmsKey = aws.String("")
if len(clusterState.Cluster.EncryptionConfig) > 0 {
upstreamSpec.KmsKey = clusterState.Cluster.EncryptionConfig[0].Provider.KeyArn
}
upstreamSpec.ServiceRole = clusterState.Cluster.RoleArn
if upstreamSpec.ServiceRole == nil {
upstreamSpec.ServiceRole = aws.String("")
}
return upstreamSpec, aws.StringValue(clusterState.Cluster.Arn), nil
}
// updateUpstreamClusterState compares the upstream spec with the config spec, then updates the upstream EKS cluster to
// match the config spec. Function often returns after a single update because once the cluster is in updating phase in EKS,
// no more updates will be accepted until the current update is finished.
func (h *Handler) updateUpstreamClusterState(upstreamSpec *eksv1.EKSClusterConfigSpec, config *eksv1.EKSClusterConfig, awsSVCs *awsServices, clusterARN string, ngARNs map[string]string) (*eksv1.EKSClusterConfig, error) {
if awsSVCs == nil {
return config, fmt.Errorf("aws services not initialized")
}
// check kubernetes version for update
if config.Spec.KubernetesVersion != nil {
updated, err := awsservices.UpdateClusterVersion(&awsservices.UpdateClusterVersionOpts{
EKSService: awsSVCs.eks,
Config: config,
UpstreamClusterSpec: upstreamSpec,
})
if err != nil {
return config, fmt.Errorf("error updating cluster version: %w", err)
}
if updated {
return h.enqueueUpdate(config)
}
}
// check tags for update
if config.Spec.Tags != nil {
updated, err := awsservices.UpdateResourceTags(&awsservices.UpdateResourceTagsOpts{
EKSService: awsSVCs.eks,
Tags: config.Spec.Tags,
UpstreamTags: upstreamSpec.Tags,
ResourceARN: clusterARN,
})
if err != nil {
return config, fmt.Errorf("error updating cluster tags: %w", err)
}
if updated {
return h.enqueueUpdate(config)
}