Skip to content

Commit d0829b1

Browse files
authored
Merge pull request #7879 from cPu1/move-bare-cluster-validation
Refactor: move bare cluster validation to NewCreateClusterLoader
2 parents fdf8ee9 + deebe8b commit d0829b1

File tree

7 files changed

+158
-142
lines changed

7 files changed

+158
-142
lines changed

pkg/apis/eksctl.io/v1alpha5/bare_cluster.go

Lines changed: 0 additions & 22 deletions
This file was deleted.

pkg/apis/eksctl.io/v1alpha5/bare_cluster_validation_test.go

Lines changed: 0 additions & 112 deletions
This file was deleted.

pkg/apis/eksctl.io/v1alpha5/validation.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,6 @@ func setNonEmpty(field string) error {
8484

8585
// ValidateClusterConfig checks compatible fields of a given ClusterConfig
8686
func ValidateClusterConfig(cfg *ClusterConfig) error {
87-
if err := validateBareCluster(cfg); err != nil {
88-
return err
89-
}
9087
if IsDisabled(cfg.IAM.WithOIDC) && len(cfg.IAM.ServiceAccounts) > 0 {
9188
return fmt.Errorf("iam.withOIDC must be enabled explicitly for iam.serviceAccounts to be created")
9289
}

pkg/ctl/cmdutils/configfile.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmdutils
33
import (
44
"encoding/csv"
55
"fmt"
6+
"io"
67
"reflect"
78
"strconv"
89
"strings"
@@ -35,6 +36,7 @@ type ClusterConfigLoader interface {
3536

3637
type commonClusterConfigLoader struct {
3738
*Cmd
39+
configReader io.Reader
3840

3941
flagsIncompatibleWithConfigFile sets.Set[string]
4042
flagsIncompatibleWithoutConfigFile sets.Set[string]
@@ -125,7 +127,7 @@ func (l *commonClusterConfigLoader) Load() error {
125127
// The reference to ClusterConfig should only be reassigned if ClusterConfigFile is specified
126128
// because other parts of the code store the pointer locally and access it directly instead of via
127129
// the Cmd reference
128-
if l.ClusterConfig, err = eks.LoadConfigFromFile(l.ClusterConfigFile); err != nil {
130+
if l.ClusterConfig, err = eks.LoadConfigWithReader(l.ClusterConfigFile, l.configReader); err != nil {
129131
return err
130132
}
131133
meta := l.ClusterConfig.Metadata
@@ -199,6 +201,7 @@ func NewMetadataLoader(cmd *Cmd) ClusterConfigLoader {
199201
// NewCreateClusterLoader will load config or use flags for 'eksctl create cluster'
200202
func NewCreateClusterLoader(cmd *Cmd, ngFilter *filter.NodeGroupFilter, ng *api.NodeGroup, params *CreateClusterCmdParams) ClusterConfigLoader {
201203
l := newCommonClusterConfigLoader(cmd)
204+
l.configReader = params.ConfigReader
202205

203206
ngFilter.SetExcludeAll(params.WithoutNodeGroup)
204207

@@ -309,6 +312,10 @@ func NewCreateClusterLoader(cmd *Cmd, ngFilter *filter.NodeGroupFilter, ng *api.
309312
}
310313
}
311314

315+
if err := validateBareCluster(clusterConfig); err != nil {
316+
return err
317+
}
318+
312319
shallCreatePodIdentityAssociations := func(cfg *api.ClusterConfig) bool {
313320
if cfg.IAM != nil && len(cfg.IAM.PodIdentityAssociations) > 0 {
314321
return true
@@ -448,6 +455,22 @@ func validateDryRunOptions(cmd *cobra.Command, incompatibleFlags []string) error
448455
return nil
449456
}
450457

458+
// validateBareCluster validates a cluster for unsupported fields if VPC CNI is disabled.
459+
func validateBareCluster(clusterConfig *api.ClusterConfig) error {
460+
if !clusterConfig.AddonsConfig.DisableDefaultAddons || slices.ContainsFunc(clusterConfig.Addons, func(addon *api.Addon) bool {
461+
return addon.Name == api.VPCCNIAddon
462+
}) {
463+
return nil
464+
}
465+
if clusterConfig.HasNodes() || clusterConfig.IsFargateEnabled() || clusterConfig.Karpenter != nil || clusterConfig.HasGitOpsFluxConfigured() ||
466+
(clusterConfig.IAM != nil && (len(clusterConfig.IAM.ServiceAccounts) > 0) || len(clusterConfig.IAM.PodIdentityAssociations) > 0) {
467+
return errors.New("fields nodeGroups, managedNodeGroups, fargateProfiles, karpenter, gitops, iam.serviceAccounts, " +
468+
"and iam.podIdentityAssociations are not supported during cluster creation in a cluster without VPC CNI; please remove these fields " +
469+
"and add them back after cluster creation is successful")
470+
}
471+
return nil
472+
}
473+
451474
const updateAuthConfigMapFlagName = "update-auth-configmap"
452475

453476
// NewCreateNodeGroupLoader will load config or use flags for 'eksctl create nodegroup'

pkg/ctl/cmdutils/configfile_test.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ package cmdutils
33
import (
44
"path/filepath"
55

6+
"github.com/aws/aws-sdk-go-v2/aws"
67
. "github.com/onsi/ginkgo/v2"
78
. "github.com/onsi/gomega"
89
"github.com/spf13/cobra"
910
"github.com/spf13/pflag"
1011

12+
clusterutils "github.com/weaveworks/eksctl/integration/utilities/cluster"
1113
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
1214
"github.com/weaveworks/eksctl/pkg/ctl/cmdutils/filter"
1315
)
@@ -471,6 +473,123 @@ var _ = Describe("cmdutils configfile", func() {
471473
testClusterEndpointAccessDefaults("test_data/cluster-with-vpc-private-access.yaml", true, true)
472474
})
473475
})
476+
477+
type bareClusterEntry struct {
478+
updateClusterConfig func(*api.ClusterConfig)
479+
expectErr bool
480+
}
481+
482+
DescribeTable("Bare Cluster validation", func(e bareClusterEntry) {
483+
cmd := &Cmd{
484+
CobraCommand: newCmd(),
485+
ClusterConfigFile: "-",
486+
ClusterConfig: api.NewClusterConfig(),
487+
ProviderConfig: api.ProviderConfig{},
488+
}
489+
clusterConfig := api.NewClusterConfig()
490+
clusterConfig.Metadata.Name = "cluster"
491+
clusterConfig.Metadata.Region = api.DefaultRegion
492+
clusterConfig.AddonsConfig.DisableDefaultAddons = true
493+
clusterConfig.Addons = []*api.Addon{
494+
{
495+
Name: api.CoreDNSAddon,
496+
},
497+
}
498+
e.updateClusterConfig(clusterConfig)
499+
err := NewCreateClusterLoader(cmd, filter.NewNodeGroupFilter(), nil, &CreateClusterCmdParams{
500+
ConfigReader: clusterutils.Reader(clusterConfig),
501+
}).Load()
502+
if e.expectErr {
503+
Expect(err).To(MatchError("fields nodeGroups, managedNodeGroups, fargateProfiles, karpenter, gitops, iam.serviceAccounts, " +
504+
"and iam.podIdentityAssociations are not supported during cluster creation in a cluster without VPC CNI; please remove these fields " +
505+
"and add them back after cluster creation is successful"))
506+
} else {
507+
Expect(err).NotTo(HaveOccurred())
508+
}
509+
},
510+
Entry("nodeGroups", bareClusterEntry{
511+
updateClusterConfig: func(c *api.ClusterConfig) {
512+
ng := api.NewNodeGroup()
513+
ng.Name = "ng"
514+
ng.DesiredCapacity = aws.Int(1)
515+
c.NodeGroups = []*api.NodeGroup{ng}
516+
},
517+
expectErr: true,
518+
}),
519+
Entry("managedNodeGroups", bareClusterEntry{
520+
updateClusterConfig: func(c *api.ClusterConfig) {
521+
ng := api.NewManagedNodeGroup()
522+
ng.Name = "mng"
523+
ng.DesiredCapacity = aws.Int(1)
524+
c.ManagedNodeGroups = []*api.ManagedNodeGroup{ng}
525+
},
526+
expectErr: true,
527+
}),
528+
Entry("fargateProfiles", bareClusterEntry{
529+
updateClusterConfig: func(c *api.ClusterConfig) {
530+
c.FargateProfiles = []*api.FargateProfile{
531+
{
532+
Name: "test",
533+
Selectors: []api.FargateProfileSelector{
534+
{
535+
Namespace: "default",
536+
},
537+
},
538+
},
539+
}
540+
},
541+
expectErr: true,
542+
}),
543+
Entry("gitops", bareClusterEntry{
544+
updateClusterConfig: func(c *api.ClusterConfig) {
545+
c.GitOps = &api.GitOps{
546+
Flux: &api.Flux{
547+
GitProvider: "github",
548+
Flags: api.FluxFlags{
549+
"owner": "aws",
550+
},
551+
},
552+
}
553+
},
554+
expectErr: true,
555+
}),
556+
Entry("karpenter", bareClusterEntry{
557+
updateClusterConfig: func(c *api.ClusterConfig) {
558+
c.Karpenter = &api.Karpenter{}
559+
},
560+
expectErr: true,
561+
}),
562+
Entry("iam.serviceAccounts", bareClusterEntry{
563+
updateClusterConfig: func(c *api.ClusterConfig) {
564+
c.IAM.WithOIDC = api.Enabled()
565+
c.IAM.ServiceAccounts = []*api.ClusterIAMServiceAccount{
566+
{
567+
ClusterIAMMeta: api.ClusterIAMMeta{
568+
Name: "test",
569+
Namespace: "test",
570+
},
571+
AttachPolicyARNs: []string{"arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"},
572+
},
573+
}
574+
},
575+
expectErr: true,
576+
}),
577+
Entry("iam.podIdentityAssociations", bareClusterEntry{
578+
updateClusterConfig: func(c *api.ClusterConfig) {
579+
c.Addons = append(c.Addons, &api.Addon{Name: api.PodIdentityAgentAddon})
580+
c.IAM.PodIdentityAssociations = []api.PodIdentityAssociation{
581+
{
582+
Namespace: "test",
583+
PermissionPolicyARNs: []string{"arn:aws:iam::aws:policy/AmazonEKS_CNI_Policy"},
584+
},
585+
}
586+
},
587+
expectErr: true,
588+
}),
589+
Entry("no unsupported field set", bareClusterEntry{
590+
updateClusterConfig: func(c *api.ClusterConfig) {},
591+
}),
592+
)
474593
})
475594

476595
Describe("SetLabelLoader", func() {

pkg/ctl/cmdutils/create_cluster.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cmdutils
22

33
import (
4+
"io"
5+
46
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
57
)
68

@@ -21,6 +23,8 @@ type CreateClusterCmdParams struct {
2123
DryRun bool
2224
CreateNGOptions
2325
CreateManagedNGOptions
26+
27+
ConfigReader io.Reader
2428
}
2529

2630
// NodeGroupOptions holds options for creating nodegroups.

0 commit comments

Comments
 (0)