/
configfile.go
1095 lines (909 loc) · 31.6 KB
/
configfile.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 cmdutils
import (
"encoding/csv"
"fmt"
"reflect"
"strconv"
"strings"
"golang.org/x/exp/slices"
"github.com/kris-nova/logger"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/apimachinery/pkg/util/sets"
"github.com/weaveworks/eksctl/pkg/actions/irsa"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils/filter"
"github.com/weaveworks/eksctl/pkg/eks"
"github.com/weaveworks/eksctl/pkg/utils/names"
utilstrings "github.com/weaveworks/eksctl/pkg/utils/strings"
)
// AddConfigFileFlag adds common --config-file flag
func AddConfigFileFlag(fs *pflag.FlagSet, path *string) {
fs.StringVarP(path, "config-file", "f", "", "load configuration from a file (or stdin if set to '-')")
}
// ClusterConfigLoader is an interface that loaders should implement
type ClusterConfigLoader interface {
Load() error
}
type commonClusterConfigLoader struct {
*Cmd
flagsIncompatibleWithConfigFile sets.Set[string]
flagsIncompatibleWithoutConfigFile sets.Set[string]
validateWithConfigFile func() error
validateWithoutConfigFile func() error
}
var (
defaultFlagsIncompatibleWithConfigFile = []string{
"name",
"region",
"version",
"cluster",
"namepace",
}
defaultFlagsIncompatibleWithoutConfigFile = []string{
"only",
"include",
"exclude",
"only-missing",
}
commonCreateFlagsIncompatibleWithDryRun = []string{
"cfn-disable-rollback",
"cfn-role-arn",
"install-neuron-plugin",
"install-nvidia-plugin",
"profile",
"timeout",
}
commonNGFlagsIncompatibleWithConfigFile = []string{
"managed",
"spot",
"instance-types",
"nodes",
"nodes-min",
"nodes-max",
"node-type",
"node-volume-size",
"node-volume-type",
"max-pods-per-node",
"node-ami",
"node-ami-family",
"ssh-access",
"ssh-public-key",
"enable-ssm",
"node-private-networking",
"node-security-groups",
"node-labels",
"node-zones",
"asg-access",
"external-dns-access",
"full-ecr-access",
"instance-name",
"instance-prefix",
}
)
func newCommonClusterConfigLoader(cmd *Cmd) *commonClusterConfigLoader {
nilValidatorFunc := func() error { return nil }
return &commonClusterConfigLoader{
Cmd: cmd,
validateWithConfigFile: nilValidatorFunc,
flagsIncompatibleWithConfigFile: sets.New[string](defaultFlagsIncompatibleWithConfigFile...),
validateWithoutConfigFile: nilValidatorFunc,
flagsIncompatibleWithoutConfigFile: sets.New[string](defaultFlagsIncompatibleWithoutConfigFile...),
}
}
// Load ClusterConfig or use flags
func (l *commonClusterConfigLoader) Load() error {
if err := api.Register(); err != nil {
return err
}
if l.ClusterConfigFile == "" {
if flagName, found := findChangedFlag(l.CobraCommand, sets.List(l.flagsIncompatibleWithoutConfigFile)); found {
return errors.Errorf("cannot use --%s unless a config file is specified via --config-file/-f", flagName)
}
return l.validateWithoutConfigFile()
}
var err error
// The reference to ClusterConfig should only be reassigned if ClusterConfigFile is specified
// because other parts of the code store the pointer locally and access it directly instead of via
// the Cmd reference
if l.ClusterConfig, err = eks.LoadConfigFromFile(l.ClusterConfigFile); err != nil {
return err
}
meta := l.ClusterConfig.Metadata
if meta == nil {
return ErrMustBeSet("metadata")
}
if flagName, found := findChangedFlag(l.CobraCommand, sets.List(l.flagsIncompatibleWithConfigFile)); found {
return ErrCannotUseWithConfigFile(fmt.Sprintf("--%s", flagName))
}
if l.flagsIncompatibleWithConfigFile.Has("name") && l.NameArg != "" {
return ErrCannotUseWithConfigFile("name argument")
}
if meta.Name == "" {
return ErrMustBeSet("metadata.name")
}
if meta.Region == "" {
return ErrMustBeSet("metadata.region")
}
l.ProviderConfig.Region = meta.Region
return l.validateWithConfigFile()
}
func findChangedFlag(cmd *cobra.Command, flagNames []string) (string, bool) {
for _, f := range flagNames {
if flag := cmd.Flag(f); flag != nil && flag.Changed {
return f, true
}
}
return "", false
}
func validateMetadataWithoutConfigFile(cmd *Cmd) error {
meta := cmd.ClusterConfig.Metadata
if meta.Name != "" && cmd.NameArg != "" {
return ErrClusterFlagAndArg(cmd, meta.Name, cmd.NameArg)
}
if cmd.NameArg != "" {
meta.Name = cmd.NameArg
}
if meta.Name == "" {
return ErrMustBeSet(ClusterNameFlag(cmd))
}
return nil
}
func (l *commonClusterConfigLoader) validateMetadataWithoutConfigFile() error {
return validateMetadataWithoutConfigFile(l.Cmd)
}
// NewMetadataLoader handles loading of clusterConfigFile vs using flags for all commands that require only
// metadata fields, e.g. `eksctl delete cluster` or `eksctl utils update-kube-proxy` and other similar
// commands that do simple operations against existing clusters
func NewMetadataLoader(cmd *Cmd) ClusterConfigLoader {
l := newCommonClusterConfigLoader(cmd)
l.validateWithoutConfigFile = l.validateMetadataWithoutConfigFile
return l
}
// NewCreateClusterLoader will load config or use flags for 'eksctl create cluster'
func NewCreateClusterLoader(cmd *Cmd, ngFilter *filter.NodeGroupFilter, ng *api.NodeGroup, params *CreateClusterCmdParams) ClusterConfigLoader {
l := newCommonClusterConfigLoader(cmd)
ngFilter.SetExcludeAll(params.WithoutNodeGroup)
clusterFlagsIncompatibleWithConfigFile := []string{
"tags",
"zones",
"fargate",
"vpc-private-subnets",
"vpc-public-subnets",
"vpc-cidr",
"vpc-nat-mode",
"vpc-from-kops-cluster",
}
l.flagsIncompatibleWithConfigFile.Insert(append(clusterFlagsIncompatibleWithConfigFile, commonNGFlagsIncompatibleWithConfigFile...)...)
l.flagsIncompatibleWithoutConfigFile.Insert("install-vpc-controllers")
validateDryRun := func() error {
if !params.DryRun {
return nil
}
flagsIncompatibleWithDryRun := append([]string{
"authenticator-role-arn",
"auto-kubeconfig",
"install-vpc-controllers",
"kubeconfig",
"set-kubeconfig-context",
"write-kubeconfig",
}, commonCreateFlagsIncompatibleWithDryRun...)
return validateDryRunOptions(l.CobraCommand, flagsIncompatibleWithDryRun)
}
l.validateWithConfigFile = func() error {
clusterConfig := l.ClusterConfig
ipv6Enabled := clusterConfig.IPv6Enabled()
if clusterConfig.VPC == nil {
clusterConfig.VPC = api.NewClusterVPC(ipv6Enabled)
}
if clusterConfig.VPC.NAT == nil && !ipv6Enabled {
clusterConfig.VPC.NAT = api.DefaultClusterNAT()
}
if clusterConfig.VPC.NAT != nil && api.IsEmpty(clusterConfig.VPC.NAT.Gateway) {
*clusterConfig.VPC.NAT.Gateway = api.ClusterSingleNAT
}
if err := validateUnsetNodeGroups(l.ClusterConfig); err != nil {
return err
}
hasEndpointAccess := func() bool {
clusterEndpoints := clusterConfig.VPC.ClusterEndpoints
return clusterEndpoints != nil && (clusterEndpoints.PublicAccess != nil || clusterEndpoints.PrivateAccess != nil)
}
if clusterConfig.IsFullyPrivate() {
if hasEndpointAccess() {
return errors.New("vpc.clusterEndpoints cannot be set for a fully-private cluster (privateCluster.enabled) as the endpoint access defaults to private-only")
}
}
if clusterConfig.IsControlPlaneOnOutposts() {
if len(clusterConfig.ManagedNodeGroups) > 0 {
return errors.New("Managed Nodegroups are not supported on Outposts")
}
if hasEndpointAccess() {
clusterEndpoints := clusterConfig.VPC.ClusterEndpoints
const msg = "the cluster defaults to private-endpoint-only access"
if api.IsEnabled(clusterEndpoints.PublicAccess) {
return fmt.Errorf("clusterEndpoints.publicAccess cannot be enabled for a cluster on Outposts; %s", msg)
}
if api.IsDisabled(clusterEndpoints.PrivateAccess) {
return fmt.Errorf("clusterEndpoints.privateAccess cannot be disabled for a cluster on Outposts; %s", msg)
}
}
clusterConfig.VPC.ClusterEndpoints = &api.ClusterEndpoints{
PrivateAccess: api.Enabled(),
PublicAccess: api.Disabled(),
}
} else {
api.SetClusterEndpointAccessDefaults(clusterConfig.VPC)
}
if clusterConfig.HasAnySubnets() && len(clusterConfig.AvailabilityZones) != 0 {
return errors.New("vpc.subnets and availabilityZones cannot be set at the same time")
}
if clusterConfig.GitOps != nil {
fluxCfg := clusterConfig.GitOps.Flux
if fluxCfg != nil {
if fluxCfg.GitProvider == "" {
return ErrMustBeSet("gitops.flux.gitProvider")
}
if len(fluxCfg.Flags) == 0 {
return ErrMustBeSet("gitops.flux.flags")
}
}
}
if clusterConfig.IAM != nil && len(clusterConfig.IAM.PodIdentityAssociations) > 0 {
addonNames := []string{}
for _, addon := range clusterConfig.Addons {
addonNames = append(addonNames, addon.Name)
}
if !slices.Contains(addonNames, api.PodIdentityAgentAddon) {
suggestion := fmt.Sprintf("please add %q addon to the config file", api.PodIdentityAgentAddon)
return api.ErrPodIdentityAgentNotInstalled(suggestion)
}
if err := validatePodIdentityAssociationsForConfig(clusterConfig, true); err != nil {
return err
}
}
return validateDryRun()
}
l.validateWithoutConfigFile = func() error {
meta := l.ClusterConfig.Metadata
meta.Region = cmd.ProviderConfig.Region
// generate cluster name or use either flag or argument
if names.ForCluster(meta.Name, l.NameArg) == "" {
return ErrClusterFlagAndArg(l.Cmd, meta.Name, l.NameArg)
}
meta.Name = names.ForCluster(meta.Name, l.NameArg)
if l.ClusterConfig.Status != nil {
return fmt.Errorf("status fields are read-only")
}
if err := validateZonesAndNodeZones(l.CobraCommand); err != nil {
return fmt.Errorf("validation for --zones and --node-zones failed: %w", err)
}
if err := validateManagedNGFlags(l.CobraCommand, params.Managed); err != nil {
return err
}
// prevent creation of invalid config object with irrelevant nodegroup
// that may or may not be constructed correctly
if !params.WithoutNodeGroup {
if params.Managed {
l.ClusterConfig.ManagedNodeGroups = []*api.ManagedNodeGroup{makeManagedNodegroup(ng, params.CreateManagedNGOptions)}
} else {
l.ClusterConfig.NodeGroups = []*api.NodeGroup{ng}
}
}
api.SetClusterEndpointAccessDefaults(l.ClusterConfig.VPC)
if params.Fargate {
l.ClusterConfig.SetDefaultFargateProfile()
// A Fargate-only cluster should have no nodegroups if the `managed` flag wasn't explicitly provided.
if !l.CobraCommand.Flag("managed").Changed {
l.ClusterConfig.ManagedNodeGroups = nil
l.ClusterConfig.NodeGroups = nil
}
}
for _, ng := range l.ClusterConfig.NodeGroups {
// generate nodegroup name or use flag
ng.Name = names.ForNodeGroup(ng.Name, "")
if err := normalizeNodeGroup(ng, l); err != nil {
return err
}
}
for _, ng := range l.ClusterConfig.ManagedNodeGroups {
if err := validateUnsupportedCLIFeatures(ng); err != nil {
return err
}
ng.Name = names.ForNodeGroup(ng.Name, "")
normalizeBaseNodeGroup(ng, l.CobraCommand)
}
return validateDryRun()
}
return l
}
func validateAuthConfigMapFlag(cmd *cobra.Command, options *NodeGroupOptions) error {
if f := cmd.Flag(updateAuthConfigMapFlagName); f != nil && f.Changed {
deprecationMsg := fmt.Sprintf("--%s is deprecated and will be removed soon", updateAuthConfigMapFlagName)
if *options.UpdateAuthConfigMap {
logger.Warning("%s; the recommended way to authorize nodes is by creating EKS access entries", deprecationMsg)
} else {
logger.Warning("%s; eksctl now uses EKS Access Entries to authorize nodes if it is enabled on the cluster", deprecationMsg)
logger.Warning("access entry for nodegroup(s) will not be added if cluster's authenticationMode is set to API_AND_CONFIG_MAP or CONFIG_MAP")
}
} else {
options.UpdateAuthConfigMap = nil
}
return nil
}
func validateZonesAndNodeZones(cmd *cobra.Command) error {
if nodeZonesFlag := cmd.Flag("node-zones"); nodeZonesFlag != nil && nodeZonesFlag.Changed {
zonesFlag := cmd.Flag("zones")
if zonesFlag == nil || !zonesFlag.Changed {
return errors.New("zones must be defined if node-zones is provided and must be a superset of node-zones")
}
nodeZones := strings.Split(strings.Trim(nodeZonesFlag.Value.String(), "[]"), ",")
zones := strings.Split(strings.Trim(zonesFlag.Value.String(), "[]"), ",")
for _, zone := range nodeZones {
if !utilstrings.Contains(zones, zone) {
return fmt.Errorf("node-zones %s must be a subset of zones %s; %q was not found in zones", nodeZones, zones, zone)
}
}
}
return nil
}
func validateDryRunOptions(cmd *cobra.Command, incompatibleFlags []string) error {
if flagName, found := findChangedFlag(cmd, incompatibleFlags); found {
msg := fmt.Sprintf("cannot use --%s with --dry-run as this option cannot be represented in ClusterConfig", flagName)
if flagName == "profile" {
msg = fmt.Sprintf("%s: set the AWS_PROFILE environment variable instead", msg)
}
return errors.New(msg)
}
return nil
}
const updateAuthConfigMapFlagName = "update-auth-configmap"
// NewCreateNodeGroupLoader will load config or use flags for 'eksctl create nodegroup'
func NewCreateNodeGroupLoader(cmd *Cmd, ng *api.NodeGroup, ngFilter *filter.NodeGroupFilter, options *NodeGroupOptions) ClusterConfigLoader {
l := newCommonClusterConfigLoader(cmd)
l.flagsIncompatibleWithConfigFile.Insert(commonNGFlagsIncompatibleWithConfigFile...)
validateDryRun := func() error {
if !options.DryRun {
return nil
}
// Filters (--include / --exclude) cannot be represented in ClusterConfig, however, they affect the output, so they're allowed
flagsIncompatibleWithDryRun := append([]string{
updateAuthConfigMapFlagName,
}, commonCreateFlagsIncompatibleWithDryRun...)
return validateDryRunOptions(l.CobraCommand, flagsIncompatibleWithDryRun)
}
l.validateWithConfigFile = func() error {
if err := validateUnsetNodeGroups(l.ClusterConfig); err != nil {
return err
}
if err := ngFilter.AppendGlobs(l.Include, l.Exclude, l.ClusterConfig.GetAllNodeGroupNames()); err != nil {
return err
}
if err := validateAuthConfigMapFlag(l.CobraCommand, options); err != nil {
return err
}
return validateDryRun()
}
l.validateWithoutConfigFile = func() error {
if l.ClusterConfig.Metadata.Name == "" {
return ErrMustBeSet(ClusterNameFlag(cmd))
}
if err := validateManagedNGFlags(l.CobraCommand, options.Managed); err != nil {
return err
}
if err := validateUnmanagedNGFlags(l.CobraCommand, options); err != nil {
return err
}
if err := validateAuthConfigMapFlag(l.CobraCommand, options); err != nil {
return err
}
if options.Managed {
l.ClusterConfig.ManagedNodeGroups = []*api.ManagedNodeGroup{makeManagedNodegroup(ng, options.CreateManagedNGOptions)}
} else {
l.ClusterConfig.NodeGroups = []*api.NodeGroup{ng}
}
// Validate both filtered and unfiltered nodegroups
if options.Managed {
for _, ng := range l.ClusterConfig.ManagedNodeGroups {
if err := validateUnsupportedCLIFeatures(ng); err != nil {
return err
}
ngName := names.ForNodeGroup(ng.Name, l.NameArg)
if ngName == "" {
return ErrFlagAndArg("--name", ng.Name, l.NameArg)
}
ng.Name = ngName
normalizeBaseNodeGroup(ng, l.CobraCommand)
}
} else {
for _, ng := range l.ClusterConfig.NodeGroups {
// generate nodegroup name or use either flag or argument
ngName := names.ForNodeGroup(ng.Name, l.NameArg)
if ngName == "" {
return ErrFlagAndArg("--name", ng.Name, l.NameArg)
}
ng.Name = ngName
if err := normalizeNodeGroup(ng, l); err != nil {
return err
}
}
}
return validateDryRun()
}
return l
}
func validateUnsetNodeGroups(clusterConfig *api.ClusterConfig) error {
for i, ng := range clusterConfig.NodeGroups {
if ng == nil {
return fmt.Errorf("invalid ClusterConfig: nodeGroups[%d] is not set", i)
}
}
for i, ng := range clusterConfig.ManagedNodeGroups {
if ng == nil {
return fmt.Errorf("invalid ClusterConfig: managedNodeGroups[%d] is not set", i)
}
}
return nil
}
func makeManagedNodegroup(nodeGroup *api.NodeGroup, options CreateManagedNGOptions) *api.ManagedNodeGroup {
ngBase := *nodeGroup.NodeGroupBase
if ngBase.SecurityGroups != nil {
ngBase.SecurityGroups = &api.NodeGroupSGs{
AttachIDs: ngBase.SecurityGroups.AttachIDs,
}
}
return &api.ManagedNodeGroup{
NodeGroupBase: &ngBase,
Spot: options.Spot,
InstanceTypes: options.InstanceTypes,
}
}
func validateUnsupportedCLIFeatures(ng *api.ManagedNodeGroup) error {
return nil
}
func validateManagedNGFlags(cmd *cobra.Command, managed bool) error {
if managed {
return nil
}
flagsValidOnlyWithMNG := []string{"spot", "instance-types"}
if flagName, found := findChangedFlag(cmd, flagsValidOnlyWithMNG); found {
return errors.Errorf("--%s is only valid with managed nodegroups (--managed)", flagName)
}
return nil
}
func validateUnmanagedNGFlags(cmd *cobra.Command, options *NodeGroupOptions) error {
if !options.Managed {
return nil
}
flagsValidOnlyWithUnmanagedNG := []string{"version"}
if flagName, found := findChangedFlag(cmd, flagsValidOnlyWithUnmanagedNG); found {
return fmt.Errorf("--%s is only valid with unmanaged nodegroups", flagName)
}
return nil
}
func normalizeNodeGroup(ng *api.NodeGroup, l *commonClusterConfigLoader) error {
if flag := l.CobraCommand.Flag("ssh-public-key"); flag != nil && flag.Changed {
if *ng.SSH.PublicKeyPath == "" {
return fmt.Errorf("--ssh-public-key must be non-empty string")
}
if flag := l.CobraCommand.Flag("ssh-access"); flag == nil || !flag.Changed {
ng.SSH.Allow = api.Enabled()
}
} else {
ng.SSH.PublicKeyPath = nil
}
if *ng.VolumeType == api.NodeVolumeTypeIO1 {
return fmt.Errorf("%s volume type is not supported via flag --node-volume-type, please use a config file", api.NodeVolumeTypeIO1)
}
normalizeBaseNodeGroup(ng, l.CobraCommand)
return nil
}
func normalizeBaseNodeGroup(np api.NodePool, cmd *cobra.Command) {
ng := np.BaseNodeGroup()
flags := cmd.Flags()
if !flags.Changed("instance-selector-gpus") {
ng.InstanceSelector.GPUs = nil
}
if !flags.Changed("enable-ssm") {
ng.SSH.EnableSSM = nil
}
}
// NewDeleteAndDrainNodeGroupLoader will load config or use flags for 'eksctl delete nodegroup'
func NewDeleteAndDrainNodeGroupLoader(cmd *Cmd, ng *api.NodeGroup, ngFilter *filter.NodeGroupFilter) ClusterConfigLoader {
l := newCommonClusterConfigLoader(cmd)
l.validateWithConfigFile = func() error {
if err := validateUnsetNodeGroups(l.ClusterConfig); err != nil {
return err
}
return ngFilter.AppendGlobs(l.Include, l.Exclude, l.ClusterConfig.GetAllNodeGroupNames())
}
l.flagsIncompatibleWithoutConfigFile.Insert(
"approve",
)
l.validateWithoutConfigFile = func() error {
if l.ClusterConfig.Metadata.Name == "" {
return ErrMustBeSet(ClusterNameFlag(cmd))
}
if ng.Name != "" && l.NameArg != "" {
return ErrFlagAndArg("--name", ng.Name, l.NameArg)
}
if l.NameArg != "" {
ng.Name = l.NameArg
}
if ng.Name == "" {
return ErrMustBeSet("--name")
}
if flag := l.CobraCommand.Flag("parallel"); flag != nil && flag.Changed {
if val, _ := strconv.Atoi(flag.Value.String()); val > 25 || val < 1 {
return fmt.Errorf("--parallel value must be of range 1-25")
}
}
ngFilter.AppendIncludeNames(ng.Name)
l.Plan = false
return nil
}
return l
}
// NewUtilsEnableLoggingLoader will load config or use flags for 'eksctl utils update-cluster-logging'
func NewUtilsEnableLoggingLoader(cmd *Cmd) ClusterConfigLoader {
l := newCommonClusterConfigLoader(cmd)
l.flagsIncompatibleWithConfigFile.Insert(
"enable-types",
"disable-types",
)
l.validateWithoutConfigFile = l.validateMetadataWithoutConfigFile
return l
}
// NewUtilsEnableEndpointAccessLoader will load config or use flags for 'eksctl utils update-cluster-endpoints'.
func NewUtilsEnableEndpointAccessLoader(cmd *Cmd, privateAccess, publicAccess bool) ClusterConfigLoader {
l := newCommonClusterConfigLoader(cmd)
l.flagsIncompatibleWithConfigFile.Insert(
"private-access",
"public-access",
)
l.validateWithoutConfigFile = func() error {
if err := l.validateMetadataWithoutConfigFile(); err != nil {
return err
}
if cmd.ClusterConfig.VPC.ClusterEndpoints == nil {
cmd.ClusterConfig.VPC.ClusterEndpoints = api.ClusterEndpointAccessDefaults()
}
if flag := l.CobraCommand.Flag("private-access"); flag != nil && flag.Changed {
cmd.ClusterConfig.VPC.ClusterEndpoints.PrivateAccess = &privateAccess
} else {
cmd.ClusterConfig.VPC.ClusterEndpoints.PrivateAccess = nil
}
if flag := l.CobraCommand.Flag("public-access"); flag != nil && flag.Changed {
cmd.ClusterConfig.VPC.ClusterEndpoints.PublicAccess = &publicAccess
} else {
cmd.ClusterConfig.VPC.ClusterEndpoints.PublicAccess = nil
}
return nil
}
l.validateWithConfigFile = func() error {
if l.ClusterConfig.VPC == nil {
l.ClusterConfig.VPC = api.NewClusterVPC(false)
}
api.SetClusterEndpointAccessDefaults(l.ClusterConfig.VPC)
return nil
}
return l
}
// NewUtilsAssociateIAMOIDCProviderLoader will load config or use flags for 'eksctl utils associal-iam-oidc-provider'
func NewUtilsAssociateIAMOIDCProviderLoader(cmd *Cmd) ClusterConfigLoader {
l := newCommonClusterConfigLoader(cmd)
l.validateWithoutConfigFile = func() error {
l.ClusterConfig.IAM.WithOIDC = api.Enabled()
return l.validateMetadataWithoutConfigFile()
}
l.validateWithConfigFile = func() error {
if l.ClusterConfig.IAM == nil || api.IsDisabled(l.ClusterConfig.IAM.WithOIDC) {
return fmt.Errorf("'iam.withOIDC' is not enabled in %q", l.ClusterConfigFile)
}
return nil
}
return l
}
// NewUtilsPublicAccessCIDRsLoader loads config or uses flags for `eksctl utils set-public-access-cidrs <cidrs>`
func NewUtilsPublicAccessCIDRsLoader(cmd *Cmd) ClusterConfigLoader {
l := newCommonClusterConfigLoader(cmd)
l.validateWithConfigFile = func() error {
if cmd.NameArg != "" {
return fmt.Errorf("config file and CIDR list argument %s", IncompatibleFlags)
}
if l.ClusterConfig.VPC == nil || l.ClusterConfig.VPC.PublicAccessCIDRs == nil {
return errors.New("field vpc.publicAccessCIDRs is required")
}
return nil
}
l.validateWithoutConfigFile = func() error {
if cmd.NameArg == "" {
return errors.New("a comma-separated CIDR list is required")
}
cidrs, err := parseList(cmd.NameArg)
if err != nil {
return err
}
l.ClusterConfig.VPC.PublicAccessCIDRs = cidrs
return nil
}
return l
}
func parseList(arg string) ([]string, error) {
reader := strings.NewReader(arg)
csvReader := csv.NewReader(reader)
return csvReader.Read()
}
// NewCreateIAMServiceAccountLoader will load config or use flags for 'eksctl create iamserviceaccount'
func NewCreateIAMServiceAccountLoader(cmd *Cmd, saFilter *filter.IAMServiceAccountFilter) ClusterConfigLoader {
l := newCommonClusterConfigLoader(cmd)
l.flagsIncompatibleWithConfigFile.Insert(
"policy-arn",
)
l.validateWithConfigFile = func() error {
if l.ClusterConfig.IAM == nil || l.ClusterConfig.IAM.ServiceAccounts == nil {
return fmt.Errorf("'iam.serviceAccounts' is not defined in %q", l.ClusterConfigFile)
}
return saFilter.AppendGlobs(l.Include, l.Exclude, l.ClusterConfig.IAM.ServiceAccounts)
}
l.validateWithoutConfigFile = func() error {
if l.ClusterConfig.Metadata.Name == "" {
return ErrMustBeSet(ClusterNameFlag(cmd))
}
if len(l.ClusterConfig.IAM.ServiceAccounts) != 1 {
return fmt.Errorf("unexpected number of service accounts")
}
serviceAccount := l.ClusterConfig.IAM.ServiceAccounts[0]
if serviceAccount.Name != "" && l.NameArg != "" {
return ErrFlagAndArg("--name", serviceAccount.Name, l.NameArg)
}
if l.NameArg != "" {
serviceAccount.Name = l.NameArg
}
if serviceAccount.Name == "" {
return ErrMustBeSet("--name")
}
if len(serviceAccount.AttachPolicyARNs) == 0 && serviceAccount.AttachRoleARN == "" {
return ErrMustBeSet("--attach-policy-arn or --attach-role-arn")
}
if serviceAccount.AttachRoleARN != "" && (*serviceAccount.RoleOnly || serviceAccount.RoleName != "") {
return fmt.Errorf("cannot provide --role-name or --role-only when --attach-role-arn is configured")
}
if serviceAccount.AttachRoleARN != "" && (len(serviceAccount.AttachPolicyARNs) != 0 || serviceAccount.AttachPolicy != nil) {
return fmt.Errorf("cannot provide --attach-role-arn and specify polices to attach")
}
return nil
}
return l
}
// NewGetIAMServiceAccountLoader will load config or use flags for 'eksctl get iamserviceaccount'
func NewGetIAMServiceAccountLoader(cmd *Cmd, options *irsa.GetOptions) ClusterConfigLoader {
l := newCommonClusterConfigLoader(cmd)
l.validateWithConfigFile = func() error {
if api.IsDisabled(l.ClusterConfig.IAM.WithOIDC) {
return fmt.Errorf("'iam.withOIDC' is not enabled in %q", l.ClusterConfigFile)
}
return nil
}
l.validateWithoutConfigFile = func() error {
if l.ClusterConfig.Metadata.Name == "" {
return ErrMustBeSet(ClusterNameFlag(cmd))
}
if options.Name != "" && l.NameArg != "" {
return ErrFlagAndArg("--name", options.Name, l.NameArg)
}
if options.Name == "" && cmd.NameArg != "" {
options.Name = cmd.NameArg
}
l.Plan = false
return nil
}
return l
}
// NewDeleteIAMServiceAccountLoader will load config or use flags for 'eksctl delete iamserviceaccount'
func NewDeleteIAMServiceAccountLoader(cmd *Cmd, sa *api.ClusterIAMServiceAccount, saFilter *filter.IAMServiceAccountFilter) ClusterConfigLoader {
l := newCommonClusterConfigLoader(cmd)
l.validateWithConfigFile = func() error {
if l.ClusterConfig.IAM == nil || api.IsDisabled(l.ClusterConfig.IAM.WithOIDC) {
return fmt.Errorf("'iam.withOIDC' is not enabled in %q", l.ClusterConfigFile)
}
return saFilter.AppendGlobs(l.Include, l.Exclude, l.ClusterConfig.IAM.ServiceAccounts)
}
l.flagsIncompatibleWithoutConfigFile.Insert(
"approve",
)
l.validateWithoutConfigFile = func() error {
sa.AttachPolicyARNs = []string{""} // force to pass general validation
if l.ClusterConfig.Metadata.Name == "" {
return ErrMustBeSet(ClusterNameFlag(cmd))
}
if sa.Name != "" && l.NameArg != "" {
return ErrFlagAndArg("--name", sa.Name, l.NameArg)
}
if l.NameArg != "" {
sa.Name = l.NameArg
}
if sa.Name == "" {
return ErrMustBeSet("--name")
}
l.Plan = false
return nil
}
return l
}
// NewUpdateNodegroupLoader will load config or use flags for 'eksctl update nodegroup'.
func NewUpdateNodegroupLoader(cmd *Cmd) ClusterConfigLoader {
l := newCommonClusterConfigLoader(cmd)
l.validateWithConfigFile = func() error {
length := len(l.ClusterConfig.ManagedNodeGroups)
if length < 1 {
return ErrMustBeSet("managedNodeGroups field")
}
for _, ng := range l.ClusterConfig.ManagedNodeGroups {
logger.Info("validating nodegroup %q", ng.Name)
var unsupportedFields []string
var err error
if unsupportedFields, err = validateSupportedConfigFields(*ng.NodeGroupBase, []string{"Name"}, unsupportedFields); err != nil {
return err
}
if unsupportedFields, err = validateSupportedConfigFields(*ng, []string{"NodeGroupBase", "UpdateConfig"}, unsupportedFields); err != nil {
return err
}
if len(unsupportedFields) > 0 {
logger.Warning("unchanged fields for nodegroup %s: the following fields remain unchanged; they are not supported by `eksctl update nodegroup`: %s", ng.Name, strings.Join(unsupportedFields[:], ", "))
}
}
return nil
}
l.validateWithoutConfigFile = func() error {
if cmd.ClusterConfigFile == "" {
return ErrMustBeSet("--config-file")
}
return nil
}
return l
}
// NewGetNodegroupLoader loads config file and validates command for `eksctl get nodegroup`.
func NewGetNodegroupLoader(cmd *Cmd, ng *api.NodeGroup) ClusterConfigLoader {
l := newCommonClusterConfigLoader(cmd)
l.validateWithoutConfigFile = func() error {
meta := cmd.ClusterConfig.Metadata
if meta.Name == "" {
return ErrMustBeSet(ClusterNameFlag(cmd))
}
if ng.Name != "" && cmd.NameArg != "" {
return ErrFlagAndArg("--name", ng.Name, cmd.NameArg)
}
if cmd.NameArg != "" {
ng.Name = cmd.NameArg
}
// prevent creation of invalid config object with unnamed nodegroup
if ng.Name != "" {
cmd.ClusterConfig.NodeGroups = append(cmd.ClusterConfig.NodeGroups, ng)
}
return nil
}
return l
}
// NewGetLabelsLoader loads config file and validates command for `eksctl get labels`.
func NewGetLabelsLoader(cmd *Cmd, ngName string) ClusterConfigLoader {
l := newCommonClusterConfigLoader(cmd)
l.validateWithoutConfigFile = func() error {
meta := cmd.ClusterConfig.Metadata
if meta.Name == "" {
return ErrMustBeSet(ClusterNameFlag(cmd))
}
if ngName == "" {
return ErrMustBeSet("--nodegroup")
}
if cmd.NameArg != "" {
return ErrUnsupportedNameArg()
}
return nil
}
return l
}
// NewGetClusterLoader will load config or use flags for 'eksctl get cluster(s)'
func NewGetClusterLoader(cmd *Cmd) ClusterConfigLoader {
l := newCommonClusterConfigLoader(cmd)
return l
}
// NewGetAddonsLoader loads config file and validates command for `eksctl get addon`.
func NewGetAddonsLoader(cmd *Cmd) ClusterConfigLoader {
l := newCommonClusterConfigLoader(cmd)
l.validateWithoutConfigFile = func() error {
meta := cmd.ClusterConfig.Metadata
if meta.Name == "" {
return ErrMustBeSet(ClusterNameFlag(cmd))
}
if cmd.NameArg != "" {
return ErrUnsupportedNameArg()
}
return nil
}