Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/clusterawsadm/ami/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 0 additions & 50 deletions cmd/clusterawsadm/ami/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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))
})
}
}
3 changes: 2 additions & 1 deletion pkg/cloud/services/ec2/ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"fmt"
"sort"
"strings"
"text/template"
"time"

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/cloud/services/ec2/ami_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down