diff --git a/cmd/clusterawsadm/ami/helper.go b/cmd/clusterawsadm/ami/helper.go index 11271f0d03..0ca0e4c70b 100644 --- a/cmd/clusterawsadm/ami/helper.go +++ b/cmd/clusterawsadm/ami/helper.go @@ -229,7 +229,7 @@ func getAllImages(ec2Client ec2iface.EC2API, ownerID string) (map[string][]*ec2. } func findAMI(imagesMap map[string][]*ec2.Image, baseOS, kubernetesVersion string) (*ec2.Image, error) { - amiNameFormat := "capa-ami-{{.BaseOS}}-{{.K8sVersion}}-*" + amiNameFormat := "capa-ami-{{.BaseOS}}-{{.K8sVersion}}" amiName, err := ec2service.GenerateAmiName(amiNameFormat, baseOS, kubernetesVersion) if err != nil { return nil, errors.Wrapf(err, "failed to process ami format: %q", amiNameFormat) diff --git a/cmd/clusterawsadm/ami/helper_test.go b/cmd/clusterawsadm/ami/helper_test.go index 680ace868d..ac47834d04 100644 --- a/cmd/clusterawsadm/ami/helper_test.go +++ b/cmd/clusterawsadm/ami/helper_test.go @@ -19,10 +19,7 @@ package ami import ( "testing" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/ec2" "github.com/google/go-cmp/cmp" - . "github.com/onsi/gomega" ) func TestAllPatchVersions(t *testing.T) { @@ -54,50 +51,3 @@ func TestAllPatchVersions(t *testing.T) { }) } } - -func TestFindAMI(t *testing.T) { - tests := []struct { - name string - imagesMap map[string][]*ec2.Image - want *ec2.Image - }{ - { - name: "find AMI based on the latest ami format", - imagesMap: map[string][]*ec2.Image{ - "capa-ami-amazon-2-v1.23.5-*": { - { - ImageId: aws.String("capa-ami-amazon-2-v1.25.3-1664536077"), - CreationDate: aws.String("2011-02-08T17:02:31.000Z"), - }, - }, - }, - want: &ec2.Image{ - ImageId: aws.String("capa-ami-amazon-2-v1.25.3-1664536077"), - CreationDate: aws.String("2011-02-08T17:02:31.000Z"), - }, - }, - { - name: "find AMI based on the old ami format", - imagesMap: map[string][]*ec2.Image{ - "capa-ami-amazon-2-1.23.5": { - { - ImageId: aws.String("capa-ami-amazon-2-1.25.3-00-1664536077"), - CreationDate: aws.String("2011-02-08T17:02:31.000Z"), - }, - }, - }, - want: nil, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - g := NewWithT(t) - got, err := findAMI(tt.imagesMap, "amazon-2", "v1.23.5") - if err != nil { - t.Fatalf("error while finding AMI %+v", err) - } - g.Expect(got).Should(Equal(tt.want)) - }) - } -} diff --git a/pkg/cloud/services/ec2/ami.go b/pkg/cloud/services/ec2/ami.go index 75e6d08353..8f46c97ddc 100644 --- a/pkg/cloud/services/ec2/ami.go +++ b/pkg/cloud/services/ec2/ami.go @@ -20,6 +20,7 @@ import ( "bytes" "fmt" "sort" + "strings" "text/template" "time" @@ -76,7 +77,7 @@ type AMILookup struct { // GenerateAmiName will generate an AMI name. func GenerateAmiName(amiNameFormat, baseOS, kubernetesVersion string) (string, error) { - amiNameParameters := AMILookup{baseOS, kubernetesVersion} + amiNameParameters := AMILookup{baseOS, strings.TrimPrefix(kubernetesVersion, "v")} // revert to default if not specified if amiNameFormat == "" { amiNameFormat = DefaultAmiNameFormat diff --git a/pkg/cloud/services/ec2/ami_test.go b/pkg/cloud/services/ec2/ami_test.go index bc0ae2d592..eec279a888 100644 --- a/pkg/cloud/services/ec2/ami_test.go +++ b/pkg/cloud/services/ec2/ami_test.go @@ -265,7 +265,7 @@ func TestGenerateAmiName(t *testing.T) { args: args{ kubernetesVersion: "v1.23.3", }, - want: "capa-ami--?v1.23.3-*", + want: "capa-ami--?1.23.3-*", }, { name: "Should return valid amiName if default AMI name format passed",