Skip to content

Commit aeaceb5

Browse files
authored
Merge pull request #3829 from Ankitasw/ami-naming
Change AMI name generation based on new format in image-builder
2 parents 8da9555 + db94e94 commit aeaceb5

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

cmd/clusterawsadm/ami/helper.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,11 @@ func getAllImages(ec2Client ec2iface.EC2API, ownerID string) (map[string][]*ec2.
216216
imagesMap := make(map[string][]*ec2.Image)
217217
for _, image := range out.Images {
218218
arr := strings.Split(aws.StringValue(image.Name), "-")
219-
arr = arr[:len(arr)-2]
219+
if arr[len(arr)-2] == "00" {
220+
arr = arr[:len(arr)-2]
221+
} else {
222+
arr = arr[:len(arr)-1]
223+
}
220224
name := strings.Join(arr, "-")
221225
images, ok := imagesMap[name]
222226
if !ok {
@@ -230,18 +234,30 @@ func getAllImages(ec2Client ec2iface.EC2API, ownerID string) (map[string][]*ec2.
230234

231235
func findAMI(imagesMap map[string][]*ec2.Image, baseOS, kubernetesVersion string) (*ec2.Image, error) {
232236
amiNameFormat := "capa-ami-{{.BaseOS}}-{{.K8sVersion}}"
237+
// Support new AMI format capa-ami-<os-version>-?<k8s-version>-*
233238
amiName, err := ec2service.GenerateAmiName(amiNameFormat, baseOS, kubernetesVersion)
234239
if err != nil {
235240
return nil, errors.Wrapf(err, "failed to process ami format: %q", amiNameFormat)
236241
}
237-
238242
if val, ok := imagesMap[amiName]; ok && val != nil {
239-
latestImage, err := ec2service.GetLatestImage(val)
243+
return latestAMI(val)
244+
} else {
245+
amiName, err = ec2service.GenerateAmiName(amiNameFormat, baseOS, strings.TrimPrefix(kubernetesVersion, "v"))
240246
if err != nil {
241-
return nil, err
247+
return nil, errors.Wrapf(err, "failed to process ami format: %q", amiNameFormat)
248+
}
249+
if val, ok = imagesMap[amiName]; ok && val != nil {
250+
return latestAMI(val)
242251
}
243-
return latestImage, nil
244252
}
245253

246254
return nil, nil
247255
}
256+
257+
func latestAMI(val []*ec2.Image) (*ec2.Image, error) {
258+
latestImage, err := ec2service.GetLatestImage(val)
259+
if err != nil {
260+
return nil, err
261+
}
262+
return latestImage, nil
263+
}

pkg/cloud/services/ec2/ami.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ type AMILookup struct {
7777

7878
// GenerateAmiName will generate an AMI name.
7979
func GenerateAmiName(amiNameFormat, baseOS, kubernetesVersion string) (string, error) {
80-
amiNameParameters := AMILookup{baseOS, strings.TrimPrefix(kubernetesVersion, "v")}
80+
amiNameParameters := AMILookup{baseOS, kubernetesVersion}
8181
// revert to default if not specified
8282
if amiNameFormat == "" {
8383
amiNameFormat = DefaultAmiNameFormat
@@ -106,7 +106,7 @@ func DefaultAMILookup(ec2Client ec2iface.EC2API, ownerID, baseOS, kubernetesVers
106106
baseOS = defaultMachineAMILookupBaseOS
107107
}
108108

109-
amiName, err := GenerateAmiName(amiNameFormat, baseOS, kubernetesVersion)
109+
amiName, err := GenerateAmiName(amiNameFormat, baseOS, strings.TrimPrefix(kubernetesVersion, "v"))
110110
if err != nil {
111111
return nil, errors.Wrapf(err, "failed to process ami format: %q", amiNameFormat)
112112
}

pkg/cloud/services/ec2/ami_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ func TestGenerateAmiName(t *testing.T) {
263263
{
264264
name: "Should return image name even if OS and amiNameFormat is empty",
265265
args: args{
266-
kubernetesVersion: "v1.23.3",
266+
kubernetesVersion: "1.23.3",
267267
},
268268
want: "capa-ami--?1.23.3-*",
269269
},
@@ -285,6 +285,15 @@ func TestGenerateAmiName(t *testing.T) {
285285
},
286286
want: "random-centos-7-?1.23.3-*",
287287
},
288+
{
289+
name: "Should return valid amiName if new AMI name format passed",
290+
args: args{
291+
amiNameFormat: "random-{{.BaseOS}}-{{.K8sVersion}}",
292+
baseOS: "centos-7",
293+
kubernetesVersion: "v1.23.3",
294+
},
295+
want: "random-centos-7-v1.23.3",
296+
},
288297
}
289298
for _, tt := range tests {
290299
t.Run(tt.name, func(t *testing.T) {

pkg/cloud/services/ec2/instances_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ func TestCreateInstance(t *testing.T) {
557557
},
558558
},
559559
expect: func(m *mocks.MockEC2APIMockRecorder) {
560-
amiName, err := GenerateAmiName("capa-ami-{{.BaseOS}}-?{{.K8sVersion}}-*", "ubuntu-18.04", "v1.16.1")
560+
amiName, err := GenerateAmiName("capa-ami-{{.BaseOS}}-?{{.K8sVersion}}-*", "ubuntu-18.04", "1.16.1")
561561
if err != nil {
562562
t.Fatalf("Failed to process ami format: %v", err)
563563
}
@@ -687,7 +687,7 @@ func TestCreateInstance(t *testing.T) {
687687
},
688688
},
689689
expect: func(m *mocks.MockEC2APIMockRecorder) {
690-
amiName, err := GenerateAmiName("capa-ami-{{.BaseOS}}-?{{.K8sVersion}}-*", "ubuntu-18.04", "v1.16.1")
690+
amiName, err := GenerateAmiName("capa-ami-{{.BaseOS}}-?{{.K8sVersion}}-*", "ubuntu-18.04", "1.16.1")
691691
if err != nil {
692692
t.Fatalf("Failed to process ami format: %v", err)
693693
}
@@ -818,7 +818,7 @@ func TestCreateInstance(t *testing.T) {
818818
},
819819
},
820820
expect: func(m *mocks.MockEC2APIMockRecorder) {
821-
amiName, err := GenerateAmiName("capa-ami-{{.BaseOS}}-?{{.K8sVersion}}-*", "ubuntu-18.04", "v1.16.1")
821+
amiName, err := GenerateAmiName("capa-ami-{{.BaseOS}}-?{{.K8sVersion}}-*", "ubuntu-18.04", "1.16.1")
822822
if err != nil {
823823
t.Fatalf("Failed to process ami format: %v", err)
824824
}

0 commit comments

Comments
 (0)