forked from eksctl-io/eksctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation.go
1031 lines (884 loc) · 32.9 KB
/
validation.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 v1alpha5
import (
"fmt"
"net"
"regexp"
"strconv"
"strings"
instanceutils "github.com/weaveworks/eksctl/pkg/utils/instance"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/kris-nova/logger"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"github.com/weaveworks/eksctl/pkg/utils/taints"
"k8s.io/apimachinery/pkg/util/validation"
kubeletapis "k8s.io/kubelet/pkg/apis"
)
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-blockdevicemapping-ebs.html
const (
MinThroughput = DefaultNodeVolumeThroughput
MaxThroughput = 1000
MinIO1Iops = DefaultNodeVolumeIO1IOPS
MaxIO1Iops = 64000
MinGP3Iops = DefaultNodeVolumeGP3IOPS
MaxGP3Iops = 16000
)
var (
// ErrClusterEndpointNoAccess indicates the config prevents API access
ErrClusterEndpointNoAccess = errors.New("Kubernetes API access must have one of public or private clusterEndpoints enabled")
// ErrClusterEndpointPrivateOnly warns private-only access requires changes
// to AWS resource configuration in order to effectively use clients in the VPC
ErrClusterEndpointPrivateOnly = errors.New("warning, having public access disallowed will subsequently interfere with some " +
"features of eksctl. This will require running subsequent eksctl (and Kubernetes) " +
"commands/API calls from within the VPC. Running these in the VPC requires making " +
"updates to some AWS resources. See: " +
"https://docs.aws.amazon.com/eks/latest/userguide/cluster-endpoint.html#private-access " +
"for more details")
)
// NOTE: we don't use k8s.io/apimachinery/pkg/util/sets here to keep API package free of dependencies
type nameSet map[string]struct{}
func (s nameSet) checkUnique(path, name string) (bool, error) {
if _, notUnique := s[name]; notUnique {
return false, fmt.Errorf("%s %q is not unique", path, name)
}
s[name] = struct{}{}
return true, nil
}
func setNonEmpty(field string) error {
return fmt.Errorf("%s must be set and non-empty", field)
}
// ValidateClusterConfig checks compatible fields of a given ClusterConfig
func ValidateClusterConfig(cfg *ClusterConfig) error {
if IsDisabled(cfg.IAM.WithOIDC) && len(cfg.IAM.ServiceAccounts) > 0 {
return fmt.Errorf("iam.withOIDC must be enabled explicitly for iam.serviceAccounts to be created")
}
saNames := nameSet{}
for i, sa := range cfg.IAM.ServiceAccounts {
path := fmt.Sprintf("iam.serviceAccounts[%d]", i)
if sa.Name == "" {
return fmt.Errorf("%s.name must be set", path)
}
if ok, err := saNames.checkUnique("<namespace>/<name> of "+path, sa.NameString()); !ok {
return err
}
if !sa.WellKnownPolicies.HasPolicy() && len(sa.AttachPolicyARNs) == 0 && sa.AttachPolicy == nil && sa.AttachRoleARN == "" {
return fmt.Errorf("%[1]s.wellKnownPolicies, %[1]s.attachPolicyARNs,%[1]s.attachRoleARN or %[1]s.attachPolicy must be set", path)
}
}
if err := cfg.validateKubernetesNetworkConfig(); err != nil {
return err
}
// names must be unique across both managed and unmanaged nodegroups
ngNames := nameSet{}
validateNg := func(ng *NodeGroupBase, path string) error {
if ng.Name == "" {
return fmt.Errorf("%s.name must be set", path)
}
if _, err := ngNames.checkUnique(path+".name", ng.Name); err != nil {
return err
}
if cfg.PrivateCluster.Enabled && !ng.PrivateNetworking {
return fmt.Errorf("%s.privateNetworking must be enabled for a fully-private cluster", path)
}
return nil
}
if err := validateIdentityProviders(cfg.IdentityProviders); err != nil {
return err
}
for i, ng := range cfg.NodeGroups {
path := fmt.Sprintf("nodeGroups[%d]", i)
if err := validateNg(ng.NodeGroupBase, path); err != nil {
return err
}
}
for i, ng := range cfg.ManagedNodeGroups {
path := fmt.Sprintf("managedNodeGroups[%d]", i)
if err := validateNg(ng.NodeGroupBase, path); err != nil {
return err
}
}
if cfg.HasClusterCloudWatchLogging() {
for i, logType := range cfg.CloudWatch.ClusterLogging.EnableTypes {
isUnknown := true
for _, knownLogType := range SupportedCloudWatchClusterLogTypes() {
if logType == knownLogType {
isUnknown = false
}
}
if isUnknown {
return fmt.Errorf("log type %q (cloudWatch.clusterLogging.enableTypes[%d]) is unknown", logType, i)
}
}
}
if cfg.VPC != nil && len(cfg.VPC.ExtraCIDRs) > 0 {
cidrs, err := validateCIDRs(cfg.VPC.ExtraCIDRs)
if err != nil {
return err
}
cfg.VPC.ExtraCIDRs = cidrs
}
if cfg.VPC != nil && len(cfg.VPC.PublicAccessCIDRs) > 0 {
cidrs, err := validateCIDRs(cfg.VPC.PublicAccessCIDRs)
if err != nil {
return err
}
cfg.VPC.PublicAccessCIDRs = cidrs
}
if cfg.SecretsEncryption != nil && cfg.SecretsEncryption.KeyARN == "" {
return errors.New("field secretsEncryption.keyARN is required for enabling secrets encryption")
}
// manageSharedNodeSecurityGroupRules cannot be disabled if using eksctl managed security groups
if cfg.VPC != nil && cfg.VPC.SharedNodeSecurityGroup == "" && IsDisabled(cfg.VPC.ManageSharedNodeSecurityGroupRules) {
return errors.New("vpc.manageSharedNodeSecurityGroupRules must be enabled when using ekstcl-managed security groups")
}
return nil
}
// ValidateClusterEndpointConfig checks the endpoint configuration for potential issues
func (c *ClusterConfig) ValidateClusterEndpointConfig() error {
if !c.HasClusterEndpointAccess() {
return ErrClusterEndpointNoAccess
}
endpts := c.VPC.ClusterEndpoints
if noAccess(endpts) {
return ErrClusterEndpointNoAccess
}
return nil
}
// ValidatePrivateCluster validates the private cluster config
func (c *ClusterConfig) ValidatePrivateCluster() error {
if c.PrivateCluster.Enabled {
if c.VPC != nil && c.VPC.ID != "" && len(c.VPC.Subnets.Private) == 0 {
return errors.New("vpc.subnets.private must be specified in a fully-private cluster when a pre-existing VPC is supplied")
}
if additionalEndpoints := c.PrivateCluster.AdditionalEndpointServices; len(additionalEndpoints) > 0 {
if err := ValidateAdditionalEndpointServices(additionalEndpoints); err != nil {
return errors.Wrap(err, "invalid value in privateCluster.additionalEndpointServices")
}
}
if c.VPC != nil && c.VPC.ClusterEndpoints == nil {
c.VPC.ClusterEndpoints = &ClusterEndpoints{}
}
// public access is initially enabled to allow running operations that access the Kubernetes API
c.VPC.ClusterEndpoints.PublicAccess = Enabled()
c.VPC.ClusterEndpoints.PrivateAccess = Enabled()
}
return nil
}
// validateKubernetesNetworkConfig validates the network config
func (c *ClusterConfig) validateKubernetesNetworkConfig() error {
if c.KubernetesNetworkConfig != nil {
serviceIP := c.KubernetesNetworkConfig.ServiceIPv4CIDR
if _, _, err := net.ParseCIDR(serviceIP); serviceIP != "" && err != nil {
return errors.Wrap(err, "invalid IPv4 CIDR for kubernetesNetworkConfig.serviceIPv4CIDR")
}
}
return nil
}
// NoAccess returns true if neither public are private cluster endpoint access is enabled and false otherwise
func noAccess(ces *ClusterEndpoints) bool {
return !(*ces.PublicAccess || *ces.PrivateAccess)
}
// PrivateOnly returns true if public cluster endpoint access is disabled and private cluster endpoint access is enabled, and false otherwise
func PrivateOnly(ces *ClusterEndpoints) bool {
return !*ces.PublicAccess && *ces.PrivateAccess
}
func validateNodeGroupBase(np NodePool, path string) error {
ng := np.BaseNodeGroup()
if ng.VolumeSize == nil {
errCantSet := func(field string) error {
return fmt.Errorf("%s.%s cannot be set without %s.volumeSize", path, field, path)
}
if IsSetAndNonEmptyString(ng.VolumeName) {
return errCantSet("volumeName")
}
if IsEnabled(ng.VolumeEncrypted) {
return errCantSet("volumeEncrypted")
}
if IsSetAndNonEmptyString(ng.VolumeKmsKeyID) {
return errCantSet("volumeKmsKeyID")
}
}
if err := validateVolumeOpts(ng, path); err != nil {
return err
}
if ng.VolumeEncrypted == nil || IsDisabled(ng.VolumeEncrypted) {
if IsSetAndNonEmptyString(ng.VolumeKmsKeyID) {
return fmt.Errorf("%s.volumeKmsKeyID can not be set without %s.volumeEncrypted enabled explicitly", path, path)
}
}
if ng.MaxPodsPerNode < 0 {
return fmt.Errorf("%s.maxPodsPerNode cannot be negative", path)
}
if IsEnabled(ng.DisablePodIMDS) && ng.IAM != nil {
fmtFieldConflictErr := func(_ string) error {
return fmt.Errorf("%s.disablePodIMDS and %s.iam.withAddonPolicies cannot be set at the same time", path, path)
}
if err := validateNodeGroupIAMWithAddonPolicies(ng.IAM.WithAddonPolicies, fmtFieldConflictErr); err != nil {
return err
}
}
if len(ng.AvailabilityZones) > 0 && len(ng.Subnets) > 0 {
return fmt.Errorf("only one of %[1]s.subnets or %[1]s.availabilityZones should be set", path)
}
if ng.Placement != nil {
if ng.Placement.GroupName == "" {
return fmt.Errorf("%s.placement.groupName must be set and non-empty", path)
}
}
if IsEnabled(ng.EFAEnabled) {
if len(ng.AvailabilityZones) > 1 || len(ng.Subnets) > 1 {
return fmt.Errorf("%s.efaEnabled nodegroups must have only one subnet or one availability zone", path)
}
}
if ng.AMIFamily != "" && !isSupportedAMIFamily(ng.AMIFamily) {
return fmt.Errorf("AMI Family %s is not supported - use one of: %s", ng.AMIFamily, strings.Join(supportedAMIFamilies(), ", "))
}
if ng.SSH != nil {
if enableSSM := ng.SSH.EnableSSM; enableSSM != nil {
if !*enableSSM {
return errors.New("SSM agent is now built into EKS AMIs and cannot be disabled")
}
logger.Warning("SSM is now enabled by default; `ssh.enableSSM` is deprecated and will be removed in a future release")
}
}
if instanceutils.IsGPUInstanceType(SelectInstanceType(np)) && ng.AMIFamily != NodeImageFamilyAmazonLinux2 {
return errors.Errorf("GPU instance types are not supported for %s", ng.AMIFamily)
}
return nil
}
func validateVolumeOpts(ng *NodeGroupBase, path string) error {
if ng.VolumeType != nil {
if ng.VolumeIOPS != nil && !(*ng.VolumeType == NodeVolumeTypeIO1 || *ng.VolumeType == NodeVolumeTypeGP3) {
return fmt.Errorf("%s.volumeIOPS is only supported for %s and %s volume types", path, NodeVolumeTypeIO1, NodeVolumeTypeGP3)
}
if *ng.VolumeType == NodeVolumeTypeIO1 {
if ng.VolumeIOPS != nil && !(*ng.VolumeIOPS >= MinIO1Iops && *ng.VolumeIOPS <= MaxIO1Iops) {
return fmt.Errorf("value for %s.volumeIOPS must be within range %d-%d", path, MinIO1Iops, MaxIO1Iops)
}
}
if ng.VolumeThroughput != nil && *ng.VolumeType != NodeVolumeTypeGP3 {
return fmt.Errorf("%s.volumeThroughput is only supported for %s volume type", path, NodeVolumeTypeGP3)
}
}
if ng.VolumeType == nil || *ng.VolumeType == NodeVolumeTypeGP3 {
if ng.VolumeIOPS != nil && !(*ng.VolumeIOPS >= MinGP3Iops && *ng.VolumeIOPS <= MaxGP3Iops) {
return fmt.Errorf("value for %s.volumeIOPS must be within range %d-%d", path, MinGP3Iops, MaxGP3Iops)
}
if ng.VolumeThroughput != nil && !(*ng.VolumeThroughput >= MinThroughput && *ng.VolumeThroughput <= MaxThroughput) {
return fmt.Errorf("value for %s.volumeThroughput must be within range %d-%d", path, MinThroughput, MaxThroughput)
}
}
return nil
}
func validateIdentityProvider(idP IdentityProvider) error {
switch idP := (idP.Inner).(type) {
case *OIDCIdentityProvider:
if idP.Name == "" {
return setNonEmpty("name")
}
if idP.ClientID == "" {
return setNonEmpty("clientID")
}
if idP.IssuerURL == "" {
return setNonEmpty("issuerURL")
}
}
return nil
}
func validateIdentityProviders(idPs []IdentityProvider) error {
for k, idP := range idPs {
if err := validateIdentityProvider(idP); err != nil {
return errors.Wrapf(err, "identityProviders[%d] is invalid", k)
}
}
return nil
}
type unsupportedFieldError struct {
ng *NodeGroupBase
path string
field string
}
func (ue *unsupportedFieldError) Error() string {
return fmt.Sprintf("%s is not supported for %s nodegroups (path=%s.%s)", ue.field, ue.ng.AMIFamily, ue.path, ue.field)
}
// IsInvalidNameArg checks whether the name contains invalid characters
func IsInvalidNameArg(name string) bool {
re := regexp.MustCompile(`[^a-zA-Z0-9\-]+`)
return re.MatchString(name)
}
// errInvalidName error when invalid characters for a name is provided
func ErrInvalidName(name string) error {
return fmt.Errorf("validation for %s failed, name must satisfy regular expression pattern: [a-zA-Z][-a-zA-Z0-9]*", name)
}
func validateNodeGroupName(name string) error {
if name != "" && IsInvalidNameArg(name) {
return ErrInvalidName(name)
}
return nil
}
// ValidateNodeGroup checks compatible fields of a given nodegroup
func ValidateNodeGroup(i int, ng *NodeGroup) error {
path := fmt.Sprintf("nodeGroups[%d]", i)
if err := validateNodeGroupBase(ng, path); err != nil {
return err
}
if err := validateNodeGroupName(ng.Name); err != nil {
return err
}
if ng.IAM != nil {
if err := validateNodeGroupIAM(ng.IAM, ng.IAM.InstanceProfileARN, "instanceProfileARN", path); err != nil {
return err
}
if err := validateNodeGroupIAM(ng.IAM, ng.IAM.InstanceRoleARN, "instanceRoleARN", path); err != nil {
return err
}
if attachPolicyARNs := ng.IAM.AttachPolicyARNs; len(attachPolicyARNs) > 0 {
for _, policyARN := range attachPolicyARNs {
if _, err := arn.Parse(policyARN); err != nil {
return errors.Wrapf(err, "invalid ARN %q in %s.iam.attachPolicyARNs", policyARN, path)
}
}
}
}
if err := validateTaints(ng.Taints); err != nil {
return err
}
if err := validateNodeGroupLabels(ng.Labels); err != nil {
return err
}
if ng.SSH != nil {
if err := validateNodeGroupSSH(ng.SSH); err != nil {
return err
}
}
if ng.Bottlerocket != nil && ng.AMIFamily != NodeImageFamilyBottlerocket {
return fmt.Errorf(`bottlerocket config can only be used with amiFamily "Bottlerocket" but found %s (path=%s.bottlerocket)`,
ng.AMIFamily, path)
}
if IsWindowsImage(ng.AMIFamily) || ng.AMIFamily == NodeImageFamilyBottlerocket {
fieldNotSupported := func(field string) error {
return &unsupportedFieldError{
ng: ng.NodeGroupBase,
path: path,
field: field,
}
}
if ng.KubeletExtraConfig != nil {
return fieldNotSupported("kubeletExtraConfig")
}
if ng.AMIFamily == NodeImageFamilyBottlerocket && ng.PreBootstrapCommands != nil {
return fieldNotSupported("preBootstrapCommands")
}
if ng.OverrideBootstrapCommand != nil {
return fieldNotSupported("overrideBootstrapCommand")
}
} else if err := validateNodeGroupKubeletExtraConfig(ng.KubeletExtraConfig); err != nil {
return err
}
if ng.AMIFamily == NodeImageFamilyBottlerocket && ng.Bottlerocket != nil {
err := checkBottlerocketSettings(ng.Bottlerocket.Settings, path)
if err != nil {
return err
}
}
if err := validateInstancesDistribution(ng); err != nil {
return err
}
if err := validateCPUCredits(ng); err != nil {
return err
}
if err := validateASGSuspendProcesses(ng); err != nil {
return err
}
if ng.ContainerRuntime != nil {
if *ng.ContainerRuntime == ContainerRuntimeContainerD && ng.AMIFamily != NodeImageFamilyAmazonLinux2 {
// check if it's dockerd or containerd
return fmt.Errorf("%s as runtime is only support for AL2 ami family", ContainerRuntimeContainerD)
}
if *ng.ContainerRuntime != ContainerRuntimeDockerD && *ng.ContainerRuntime != ContainerRuntimeContainerD {
return fmt.Errorf("only %s and %s are supported for container runtime", ContainerRuntimeContainerD, ContainerRuntimeDockerD)
}
}
return nil
}
// validateNodeGroupLabels uses proper Kubernetes label validation,
// it's designed to make sure users don't pass weird labels to the
// nodes, which would prevent kubelets to startup properly
func validateNodeGroupLabels(labels map[string]string) error {
// compact version based on:
// - https://github.com/kubernetes/kubernetes/blob/v1.13.2/cmd/kubelet/app/options/options.go#L257-L267
// - https://github.com/kubernetes/kubernetes/blob/v1.13.2/pkg/kubelet/apis/well_known_labels.go
// we cannot import those packages because they break other dependencies
unknownKubernetesLabels := []string{}
for label := range labels {
labelParts := strings.Split(label, "/")
if len(labelParts) > 2 {
return fmt.Errorf("node label key %q is of invalid format, can only use one '/' separator", label)
}
if errs := validation.IsQualifiedName(label); len(errs) > 0 {
return fmt.Errorf("label %q is invalid - %v", label, errs)
}
if errs := validation.IsValidLabelValue(labels[label]); len(errs) > 0 {
return fmt.Errorf("label %q has invalid value %q - %v", label, labels[label], errs)
}
if len(labelParts) == 2 {
namespace := labelParts[0]
if isKubernetesLabel(namespace) && !kubeletapis.IsKubeletLabel(label) {
unknownKubernetesLabels = append(unknownKubernetesLabels, label)
}
}
}
if len(unknownKubernetesLabels) > 0 {
return fmt.Errorf("unknown 'kubernetes.io' or 'k8s.io' labels were specified: %v", unknownKubernetesLabels)
}
return nil
}
func isKubernetesLabel(namespace string) bool {
for _, domain := range []string{"kubernetes.io", "k8s.io"} {
if namespace == domain || strings.HasSuffix(namespace, "."+domain) {
return true
}
}
return false
}
func validateNodeGroupIAMWithAddonPolicies(
policies NodeGroupIAMAddonPolicies,
fmtFieldConflictErr func(conflictingField string) error,
) error {
prefix := "withAddonPolicies."
if IsEnabled(policies.AutoScaler) {
return fmtFieldConflictErr(prefix + "autoScaler")
}
if IsEnabled(policies.ExternalDNS) {
return fmtFieldConflictErr(prefix + "externalDNS")
}
if IsEnabled(policies.CertManager) {
return fmtFieldConflictErr(prefix + "certManager")
}
if IsEnabled(policies.ImageBuilder) {
return fmtFieldConflictErr(prefix + "imageBuilder")
}
if IsEnabled(policies.AppMesh) {
return fmtFieldConflictErr(prefix + "appMesh")
}
if IsEnabled(policies.AppMeshPreview) {
return fmtFieldConflictErr(prefix + "appMeshPreview")
}
if IsEnabled(policies.EBS) {
return fmtFieldConflictErr(prefix + "ebs")
}
if IsEnabled(policies.FSX) {
return fmtFieldConflictErr(prefix + "fsx")
}
if IsEnabled(policies.EFS) {
return fmtFieldConflictErr(prefix + "efs")
}
if IsEnabled(policies.AWSLoadBalancerController) {
return fmtFieldConflictErr(prefix + "albIngress")
}
if IsEnabled(policies.XRay) {
return fmtFieldConflictErr(prefix + "xRay")
}
if IsEnabled(policies.CloudWatch) {
return fmtFieldConflictErr(prefix + "cloudWatch")
}
return nil
}
func validateNodeGroupIAM(iam *NodeGroupIAM, value, fieldName, path string) error {
if value != "" {
fmtFieldConflictErr := func(conflictingField string) error {
return fmt.Errorf("%s.iam.%s and %s.iam.%s cannot be set at the same time", path, fieldName, path, conflictingField)
}
if iam.InstanceRoleName != "" {
return fmtFieldConflictErr("instanceRoleName")
}
if len(iam.AttachPolicyARNs) != 0 {
return fmtFieldConflictErr("attachPolicyARNs")
}
if iam.InstanceRolePermissionsBoundary != "" {
return fmtFieldConflictErr("instanceRolePermissionsBoundary")
}
if err := validateNodeGroupIAMWithAddonPolicies(iam.WithAddonPolicies, fmtFieldConflictErr); err != nil {
return err
}
}
return nil
}
// ValidateManagedNodeGroup validates a ManagedNodeGroup and sets some defaults
func ValidateManagedNodeGroup(ng *ManagedNodeGroup, index int) error {
switch ng.AMIFamily {
case NodeImageFamilyAmazonLinux2, NodeImageFamilyBottlerocket, NodeImageFamilyUbuntu1804, NodeImageFamilyUbuntu2004:
default:
return errors.Errorf("%q is not supported for managed nodegroups", ng.AMIFamily)
}
path := fmt.Sprintf("managedNodeGroups[%d]", index)
if err := validateNodeGroupBase(ng, path); err != nil {
return err
}
if ng.IAM != nil {
if err := validateNodeGroupIAM(ng.IAM, ng.IAM.InstanceRoleARN, "instanceRoleARN", path); err != nil {
return err
}
errNotSupported := func(field string) error {
return fmt.Errorf("%s is not supported for Managed Nodes (%s.%s)", field, path, field)
}
if ng.IAM.InstanceProfileARN != "" {
return errNotSupported("instanceProfileARN")
}
}
// TODO fix error messages to not use CLI flags
if ng.MinSize == nil {
if ng.DesiredCapacity == nil {
defaultNodeCount := DefaultNodeCount
ng.MinSize = &defaultNodeCount
} else {
ng.MinSize = ng.DesiredCapacity
}
} else if ng.DesiredCapacity != nil && *ng.DesiredCapacity < *ng.MinSize {
return fmt.Errorf("cannot use --nodes-min=%d and --nodes=%d at the same time", *ng.MinSize, *ng.DesiredCapacity)
}
// Ensure MaxSize is set, as it is required by the ASG CFN resource
if ng.MaxSize == nil {
if ng.DesiredCapacity == nil {
ng.MaxSize = ng.MinSize
} else {
ng.MaxSize = ng.DesiredCapacity
}
} else if ng.DesiredCapacity != nil && *ng.DesiredCapacity > *ng.MaxSize {
return fmt.Errorf("cannot use --nodes-max=%d and --nodes=%d at the same time", *ng.MaxSize, *ng.DesiredCapacity)
} else if *ng.MaxSize < *ng.MinSize {
return fmt.Errorf("cannot use --nodes-min=%d and --nodes-max=%d at the same time", *ng.MinSize, *ng.MaxSize)
}
if ng.DesiredCapacity == nil {
ng.DesiredCapacity = ng.MinSize
}
if ng.UpdateConfig != nil {
if ng.UpdateConfig.MaxUnavailable == nil && ng.UpdateConfig.MaxUnavailablePercentage == nil {
return fmt.Errorf("invalid UpdateConfig: maxUnavailable or maxUnavailablePercentage must be defined")
}
if ng.UpdateConfig.MaxUnavailable != nil && ng.UpdateConfig.MaxUnavailablePercentage != nil {
return fmt.Errorf("cannot use maxUnavailable=%d and maxUnavailablePercentage=%d at the same time", *ng.UpdateConfig.MaxUnavailable, *ng.UpdateConfig.MaxUnavailablePercentage)
}
if aws.IntValue(ng.UpdateConfig.MaxUnavailable) > aws.IntValue(ng.MaxSize) {
return fmt.Errorf("maxUnavailable=%d cannot be greater than maxSize=%d", *ng.UpdateConfig.MaxUnavailable, *ng.MaxSize)
}
}
if IsEnabled(ng.SecurityGroups.WithLocal) || IsEnabled(ng.SecurityGroups.WithShared) {
return errors.Errorf("securityGroups.withLocal and securityGroups.withShared are not supported for managed nodegroups (%s.securityGroups)", path)
}
if ng.InstanceType != "" {
if len(ng.InstanceTypes) > 0 {
return errors.Errorf("only one of instanceType or instanceTypes can be specified (%s)", path)
}
if !ng.InstanceSelector.IsZero() {
return errors.Errorf("cannot set instanceType when instanceSelector is specified (%s)", path)
}
}
if ng.AMIFamily == NodeImageFamilyBottlerocket {
fieldNotSupported := func(field string) error {
return &unsupportedFieldError{
ng: ng.NodeGroupBase,
path: path,
field: field,
}
}
if ng.PreBootstrapCommands != nil {
return fieldNotSupported("preBootstrapCommands")
}
if ng.OverrideBootstrapCommand != nil {
return fieldNotSupported("overrideBootstrapCommand")
}
}
if err := validateTaints(ng.Taints); err != nil {
return err
}
switch {
case ng.LaunchTemplate != nil:
if ng.LaunchTemplate.ID == "" {
return errors.Errorf("launchTemplate.id is required if launchTemplate is set (%s.%s)", path, "launchTemplate")
}
if ng.LaunchTemplate.Version != nil {
// TODO support `latest` and `default`
versionNumber, err := strconv.ParseInt(*ng.LaunchTemplate.Version, 10, 64)
if err != nil {
return errors.Wrap(err, "invalid launch template version")
}
if versionNumber < 1 {
return errors.Errorf("launchTemplate.version must be >= 1 (%s.%s)", path, "launchTemplate.version")
}
}
if ng.InstanceType != "" || ng.AMI != "" || IsEnabled(ng.SSH.Allow) || IsEnabled(ng.SSH.EnableSSM) || len(ng.SSH.SourceSecurityGroupIDs) > 0 ||
ng.VolumeSize != nil || len(ng.PreBootstrapCommands) > 0 || ng.OverrideBootstrapCommand != nil ||
len(ng.SecurityGroups.AttachIDs) > 0 || ng.InstanceName != "" || ng.InstancePrefix != "" || ng.MaxPodsPerNode != 0 ||
IsEnabled(ng.DisableIMDSv1) || IsEnabled(ng.DisablePodIMDS) || ng.Placement != nil {
incompatibleFields := []string{
"instanceType", "ami", "ssh.allow", "ssh.enableSSM", "ssh.sourceSecurityGroupIds", "securityGroups",
"volumeSize", "instanceName", "instancePrefix", "maxPodsPerNode", "disableIMDSv1",
"disablePodIMDS", "preBootstrapCommands", "overrideBootstrapCommand", "placement",
}
return errors.Errorf("cannot set %s in managedNodeGroup when a launch template is supplied", strings.Join(incompatibleFields, ", "))
}
case ng.AMI != "":
if !IsAMI(ng.AMI) {
return errors.Errorf("invalid AMI %q (%s.%s)", ng.AMI, path, "ami")
}
if ng.AMIFamily != NodeImageFamilyAmazonLinux2 {
return errors.Errorf("cannot set amiFamily to %s when using a custom AMI", ng.AMIFamily)
}
if ng.OverrideBootstrapCommand == nil {
return errors.Errorf("%s.overrideBootstrapCommand is required when using a custom AMI (%s.ami)", path, path)
}
notSupportedWithCustomAMIErr := func(field string) error {
return errors.Errorf("%s.%s is not supported when using a custom AMI (%s.ami)", path, field, path)
}
if ng.MaxPodsPerNode != 0 {
return notSupportedWithCustomAMIErr("maxPodsPerNode")
}
if ng.SSH != nil && IsEnabled(ng.SSH.EnableSSM) {
return notSupportedWithCustomAMIErr("enableSSM")
}
if ng.ReleaseVersion != "" {
return notSupportedWithCustomAMIErr("releaseVersion")
}
case ng.OverrideBootstrapCommand != nil:
return errors.Errorf("%s.overrideBootstrapCommand can only be set when a custom AMI (%s.ami) is specified", path, path)
}
return nil
}
func validateInstancesDistribution(ng *NodeGroup) error {
hasInstanceSelector := ng.InstanceSelector != nil && !ng.InstanceSelector.IsZero()
if ng.InstancesDistribution == nil && !hasInstanceSelector {
return nil
}
if ng.InstanceType != "" && ng.InstanceType != "mixed" {
makeError := func(featureStr string) error {
return errors.Errorf(`instanceType should be "mixed" or unset when using the %s feature`, featureStr)
}
if ng.InstancesDistribution != nil {
return makeError("instances distribution")
}
return makeError("instance selector")
}
if ng.InstancesDistribution == nil {
return nil
}
distribution := ng.InstancesDistribution
if len(distribution.InstanceTypes) == 0 && !hasInstanceSelector {
return fmt.Errorf("at least two instance types have to be specified for mixed nodegroups")
}
if !hasInstanceSelector {
uniqueInstanceTypes := make(map[string]struct{})
for _, instanceType := range distribution.InstanceTypes {
uniqueInstanceTypes[instanceType] = struct{}{}
}
if len(uniqueInstanceTypes) > 20 {
return fmt.Errorf("mixed nodegroups should have between 1 and 20 different instance types")
}
}
if distribution.OnDemandBaseCapacity != nil && *distribution.OnDemandBaseCapacity < 0 {
return fmt.Errorf("onDemandBaseCapacity should be 0 or more")
}
if distribution.OnDemandPercentageAboveBaseCapacity != nil && (*distribution.OnDemandPercentageAboveBaseCapacity < 0 || *distribution.OnDemandPercentageAboveBaseCapacity > 100) {
return fmt.Errorf("percentageAboveBase should be between 0 and 100")
}
if distribution.SpotInstancePools != nil && (*distribution.SpotInstancePools < 1 || *distribution.SpotInstancePools > 20) {
return fmt.Errorf("spotInstancePools should be between 1 and 20")
}
if distribution.SpotInstancePools != nil && distribution.SpotAllocationStrategy != nil && *distribution.SpotAllocationStrategy == SpotAllocationStrategyCapacityOptimized {
return fmt.Errorf("spotInstancePools cannot be specified when also specifying spotAllocationStrategy: %s", SpotAllocationStrategyCapacityOptimized)
}
if distribution.SpotInstancePools != nil && distribution.SpotAllocationStrategy != nil && (*distribution.SpotAllocationStrategy == SpotAllocationStrategyCapacityOptimized || *distribution.SpotAllocationStrategy == SpotAllocationStrategyCapacityOptimizedPrioritized) {
return fmt.Errorf("spotInstancePools cannot be specified when also specifying spotAllocationStrategy: %s", SpotAllocationStrategyCapacityOptimizedPrioritized)
}
if distribution.SpotAllocationStrategy != nil {
if !isSpotAllocationStrategySupported(*distribution.SpotAllocationStrategy) {
return fmt.Errorf("spotAllocationStrategy should be one of: %v", strings.Join(supportedSpotAllocationStrategies(), ", "))
}
}
return nil
}
func validateCPUCredits(ng *NodeGroup) error {
isTInstance := false
instanceTypes := []string{ng.InstanceType}
if ng.CPUCredits == nil {
return nil
}
if ng.InstancesDistribution != nil {
instanceTypes = ng.InstancesDistribution.InstanceTypes
}
for _, instanceType := range instanceTypes {
if strings.HasPrefix(instanceType, "t") {
isTInstance = true
}
}
if !isTInstance {
return fmt.Errorf("cpuCredits option set for nodegroup, but it has no t2/t3 instance types")
}
if strings.ToLower(*ng.CPUCredits) != "unlimited" && strings.ToLower(*ng.CPUCredits) != "standard" {
return fmt.Errorf("cpuCredits option accepts only one of 'standard' or 'unlimited'")
}
return nil
}
func validateASGSuspendProcesses(ng *NodeGroup) error {
// Processes list taken from here: https://docs.aws.amazon.com/autoscaling/ec2/APIReference/API_SuspendProcesses.html
for _, proc := range ng.ASGSuspendProcesses {
switch proc {
case
"Launch",
"Terminate",
"AddToLoadBalancer",
"AlarmNotification",
"AZRebalance",
"HealthCheck",
"InstanceRefresh",
"ReplaceUnhealthy",
"ScheduledActions":
continue
default:
return fmt.Errorf("asgSuspendProcesses contains invalid process name '%s'", proc)
}
}
return nil
}
func validateNodeGroupSSH(SSH *NodeGroupSSH) error {
numSSHFlagsEnabled := countEnabledFields(
SSH.PublicKeyPath,
SSH.PublicKey,
SSH.PublicKeyName)
if numSSHFlagsEnabled > 1 {
return errors.New("only one of publicKeyName, publicKeyPath or publicKey can be specified for SSH per node-group")
}
return nil
}
func countEnabledFields(fields ...*string) int {
count := 0
for _, flag := range fields {
if flag != nil && *flag != "" {
count++
}
}
return count
}
func validateNodeGroupKubeletExtraConfig(kubeletConfig *InlineDocument) error {
if kubeletConfig == nil {
return nil
}
var kubeletForbiddenFields = map[string]struct{}{
"kind": {},
"apiVersion": {},
"address": {},
"clusterDomain": {},
"authentication": {},
"authorization": {},
"serverTLSBootstrap": {},
}
for k := range *kubeletConfig {
if _, exists := kubeletForbiddenFields[k]; exists {
return fmt.Errorf("cannot override %q in kubelet config, as it's critical to eksctl functionality", k)
}
}
return nil
}
func isSupportedAMIFamily(imageFamily string) bool {
for _, image := range supportedAMIFamilies() {
if imageFamily == image {
return true
}
}
return false
}
// IsWindowsImage reports whether the AMI family is for Windows
func IsWindowsImage(imageFamily string) bool {
switch imageFamily {
case NodeImageFamilyWindowsServer2019CoreContainer,
NodeImageFamilyWindowsServer2019FullContainer,
NodeImageFamilyWindowsServer2004CoreContainer:
return true
default:
return false
}
}
func validateCIDRs(cidrs []string) ([]string, error) {
var validCIDRs []string
for _, cidr := range cidrs {
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
return nil, err
}
validCIDRs = append(validCIDRs, ipNet.String())
}
return validCIDRs, nil
}
func validateTaints(ngTaints []NodeGroupTaint) error {
for _, t := range ngTaints {
if err := taints.Validate(corev1.Taint{
Key: t.Key,
Value: t.Value,
Effect: t.Effect,
}); err != nil {
return err
}
}
return nil
}
// ReservedProfileNamePrefix defines the Fargate profile name prefix reserved
// for AWS, and which therefore, cannot be used by users. AWS' API should
// reject the creation of profiles starting with this prefix, but we eagerly
// validate this client-side.
const ReservedProfileNamePrefix = "eks-"
// Validate validates this FargateProfile object.
func (fp FargateProfile) Validate() error {
if fp.Name == "" {
return errors.New("invalid Fargate profile: empty name")
}
if strings.HasPrefix(fp.Name, ReservedProfileNamePrefix) {
return fmt.Errorf("invalid Fargate profile %q: name should NOT start with %q", fp.Name, ReservedProfileNamePrefix)
}
if len(fp.Selectors) == 0 {
return fmt.Errorf("invalid Fargate profile %q: no profile selector", fp.Name)
}
for i, selector := range fp.Selectors {
if err := selector.Validate(); err != nil {
return errors.Wrapf(err, "invalid Fargate profile %q: invalid profile selector at index #%v", fp.Name, i)
}
}
return nil
}
// Validate validates this FargateProfileSelector object.
func (fps FargateProfileSelector) Validate() error {
if fps.Namespace == "" {
return errors.New("empty namespace")
}
return nil
}
func checkBottlerocketSettings(doc *InlineDocument, path string) error {
if doc == nil {
return nil
}