From 14ac9832418c64b65324d95f1e9108128a3bc765 Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Mon, 16 May 2016 11:19:12 -0400 Subject: [PATCH] upup: Map matching image ids to source name This avoids spurious changes, and also is more intuitive for the user - whatever name the user gave it, if it resolves to the same image, that is the name we will use. --- upup/pkg/fi/cloudup/awstasks/autoscaling_group.go | 13 +++++++++++++ upup/pkg/fi/cloudup/awstasks/instance.go | 13 +++++++++++++ upup/pkg/fi/cloudup/awsup/aws_cloud.go | 1 + 3 files changed, 27 insertions(+) diff --git a/upup/pkg/fi/cloudup/awstasks/autoscaling_group.go b/upup/pkg/fi/cloudup/awstasks/autoscaling_group.go index 72b35d0a38..9762885bc6 100644 --- a/upup/pkg/fi/cloudup/awstasks/autoscaling_group.go +++ b/upup/pkg/fi/cloudup/awstasks/autoscaling_group.go @@ -232,6 +232,19 @@ func (e *AutoscalingGroup) findLaunchConfiguration(c *fi.Context, name string, d dest.IAMInstanceProfile = &IAMInstanceProfile{Name: i.IamInstanceProfile} dest.AssociatePublicIP = i.AssociatePublicIpAddress + // Avoid spurious changes on ImageId + if e.ImageID != nil && dest.ImageID != nil && *dest.ImageID != *e.ImageID { + image, err := cloud.ResolveImage(*e.ImageID) + if err != nil { + glog.Warningf("unable to resolve image: %q: %v", *e.ImageID, err) + } else if image == nil { + glog.Warningf("unable to resolve image: %q: not found", *e.ImageID) + } else if aws.StringValue(image.ImageId) == *dest.ImageID { + glog.V(4).Infof("Returning matching ImageId as expected name: %q -> %q", *dest.ImageID, *e.ImageID) + dest.ImageID = e.ImageID + } + } + return true, nil } diff --git a/upup/pkg/fi/cloudup/awstasks/instance.go b/upup/pkg/fi/cloudup/awstasks/instance.go index 80ff209b60..7c98984ad1 100644 --- a/upup/pkg/fi/cloudup/awstasks/instance.go +++ b/upup/pkg/fi/cloudup/awstasks/instance.go @@ -106,6 +106,19 @@ func (e *Instance) Find(c *fi.Context) (*Instance, error) { e.ID = actual.ID + // Avoid spurious changes on ImageId + if e.ImageID != nil && actual.ImageID != nil && *actual.ImageID != *e.ImageID { + image, err := cloud.ResolveImage(*e.ImageID) + if err != nil { + glog.Warningf("unable to resolve image: %q: %v", *e.ImageID, err) + } else if image == nil { + glog.Warningf("unable to resolve image: %q: not found", *e.ImageID) + } else if aws.StringValue(image.ImageId) == *actual.ImageID { + glog.V(4).Infof("Returning matching ImageId as expected name: %q -> %q", *actual.ImageID, *e.ImageID) + actual.ImageID = e.ImageID + } + } + return actual, nil } diff --git a/upup/pkg/fi/cloudup/awsup/aws_cloud.go b/upup/pkg/fi/cloudup/awsup/aws_cloud.go index 6b579a69cb..84194b5e04 100644 --- a/upup/pkg/fi/cloudup/awsup/aws_cloud.go +++ b/upup/pkg/fi/cloudup/awsup/aws_cloud.go @@ -203,6 +203,7 @@ func (t *AWSCloud) DescribeVPC(vpcID string) (*ec2.Vpc, error) { // owner/name in which case we find the image with the specified name, owned by owner // name in which case we find the image with the specified name, with the current owner func (c *AWSCloud) ResolveImage(name string) (*ec2.Image, error) { + // TODO: Cache this result during a single execution (we get called multiple times) glog.V(2).Infof("Calilng DescribeImages to resolve name %q", name) request := &ec2.DescribeImagesInput{}