Merge pull request #4338 from denniswebb/latest-image

When kops searches for AMI by name, if > 1 are returned, uses the latest.
This commit is contained in:
k8s-ci-robot 2018-01-25 17:25:28 -08:00 committed by GitHub
commit 5e68a5a33e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 3 deletions

View File

@ -129,6 +129,7 @@ func (h *IntegrationTestHarness) SetupMockAWS() {
}})
mockEC2.Images = append(mockEC2.Images, &ec2.Image{
CreationDate: aws.String("2016-10-21T20:07:19.000Z"),
ImageId: aws.String("ami-12345678"),
Name: aws.String("k8s-1.4-debian-jessie-amd64-hvm-ebs-2016-10-21"),
OwnerId: aws.String(awsup.WellKnownAccountKopeio),
@ -136,6 +137,7 @@ func (h *IntegrationTestHarness) SetupMockAWS() {
})
mockEC2.Images = append(mockEC2.Images, &ec2.Image{
CreationDate: aws.String("2017-01-09T17:08:27.000Z"),
ImageId: aws.String("ami-15000000"),
Name: aws.String("k8s-1.5-debian-jessie-amd64-hvm-ebs-2017-01-09"),
OwnerId: aws.String(awsup.WellKnownAccountKopeio),

View File

@ -948,11 +948,16 @@ func resolveImage(ec2Client ec2iface.EC2API, name string) (*ec2.Image, error) {
if response == nil || len(response.Images) == 0 {
return nil, fmt.Errorf("could not find Image for %q", name)
}
if len(response.Images) != 1 {
return nil, fmt.Errorf("found multiple Images for %q", name)
}
image := response.Images[0]
for _, v := range response.Images {
itime, _ := time.Parse(time.RFC3339, *image.CreationDate)
vtime, _ := time.Parse(time.RFC3339, *v.CreationDate)
if vtime.After(itime) {
image = v
}
}
glog.V(4).Infof("Resolved image %q", aws.StringValue(image.ImageId))
return image, nil
}