Skip to content

Commit

Permalink
improve validation message
Browse files Browse the repository at this point in the history
  • Loading branch information
TiberiuGC committed Apr 3, 2024
1 parent 0f3f294 commit 8965847
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
19 changes: 17 additions & 2 deletions pkg/apis/eksctl.io/v1alpha5/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ var (
}
)

var (
SupportedAmazonLinuxImages = supportedAMIFamiliesForOS(IsAmazonLinuxImage)
SupportedUbuntuImages = supportedAMIFamiliesForOS(IsUbuntuImage)
)

// NOTE: we don't use k8s.io/apimachinery/pkg/util/sets here to keep API package free of dependencies
type nameSet map[string]struct{}

Expand Down Expand Up @@ -1267,8 +1272,8 @@ func ValidateManagedNodeGroup(index int, ng *ManagedNodeGroup) error {
return errors.Errorf("when using a custom AMI, amiFamily needs to be explicitly set via config file or via --node-ami-family flag")
}
if !IsAmazonLinuxImage(ng.AMIFamily) && !IsUbuntuImage(ng.AMIFamily) {
return errors.Errorf("cannot set amiFamily to %s when using a custom AMI for managed nodes, only %s, %s, %s, %s and %s are supported", ng.AMIFamily,
NodeImageFamilyAmazonLinux2023, NodeImageFamilyAmazonLinux2, NodeImageFamilyUbuntu1804, NodeImageFamilyUbuntu2004, NodeImageFamilyUbuntu2204)
return errors.Errorf("cannot set amiFamily to %s when using a custom AMI for managed nodes, only %s are supported", ng.AMIFamily,
strings.Join(append(SupportedAmazonLinuxImages, SupportedUbuntuImages...), ", "))
}
if ng.OverrideBootstrapCommand == nil && ng.AMIFamily != NodeImageFamilyAmazonLinux2023 {
return errors.Errorf("%[1]s.overrideBootstrapCommand is required when using a custom AMI based on %s (%[1]s.ami)", path, ng.AMIFamily)
Expand Down Expand Up @@ -1475,6 +1480,16 @@ func isSupportedAMIFamily(imageFamily string) bool {
return false
}

func supportedAMIFamiliesForOS(isOSImage func(string) bool) []string {
amiFamilies := []string{}
for _, image := range SupportedAMIFamilies() {
if isOSImage(image) {
amiFamilies = append(amiFamilies, image)
}
}
return amiFamilies
}

func IsAmazonLinuxImage(imageFamily string) bool {
switch imageFamily {
case NodeImageFamilyAmazonLinux2023,
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/eksctl.io/v1alpha5/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2070,8 +2070,8 @@ var _ = Describe("ClusterConfig validation", func() {
mng.AMIFamily = api.NodeImageFamilyBottlerocket
mng.OverrideBootstrapCommand = nil
err = api.ValidateManagedNodeGroup(0, mng)
errorMsg := fmt.Sprintf("cannot set amiFamily to %s when using a custom AMI for managed nodes, only %s, %s, %s, %s and %s are supported",
mng.AMIFamily, api.NodeImageFamilyAmazonLinux2023, api.NodeImageFamilyAmazonLinux2, api.NodeImageFamilyUbuntu1804, api.NodeImageFamilyUbuntu2004, api.NodeImageFamilyUbuntu2204)
errorMsg := fmt.Sprintf("cannot set amiFamily to %s when using a custom AMI for managed nodes, only %s are supported", mng.AMIFamily,
strings.Join(append(api.SupportedAmazonLinuxImages, api.SupportedUbuntuImages...), ", "))
Expect(err).To(MatchError(errorMsg))

mng.AMIFamily = api.NodeImageFamilyAmazonLinux2023
Expand Down

0 comments on commit 8965847

Please sign in to comment.