Repository: kubernetes-sigs/cluster-api-provider-aws
Pull request #3812 (2022-11-03 07:34:51)

Revert "Change naming format of AMIs while searching for AMIs"
Reverts kubernetes-sigs/cluster-api-provider-aws#3790


Patch:
diff --git a/cmd/clusterawsadm/ami/helper.go b/cmd/clusterawsadm/ami/helper.go
index 11271f0d0..0ca0e4c70 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/pkg/cloud/services/ec2/ami.go b/pkg/cloud/services/ec2/ami.go
index 75e6d0835..8f46c97dd 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


Test patch:
diff --git a/cmd/clusterawsadm/ami/helper_test.go b/cmd/clusterawsadm/ami/helper_test.go
index 680ace868..ac47834d0 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_test.go b/pkg/cloud/services/ec2/ami_test.go
index bc0ae2d59..eec279a88 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",