Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated cherry pick of #8038: Fix Handling of LaunchTemplate Versions for #8261: Fix RollingUpdate behaviour when using LaunchTemplates for #8567: Treat nil of LaunchTemplateSpecification.Version as $Default #8807

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
20 changes: 19 additions & 1 deletion upup/pkg/fi/cloudup/awstasks/autoscalinggroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"

"k8s.io/kops/upup/pkg/fi"
Expand All @@ -30,6 +31,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
"k8s.io/klog"
)

Expand Down Expand Up @@ -271,6 +273,7 @@ func (v *AutoscalingGroup) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *Autos
LaunchTemplateName: e.LaunchTemplate.ID,
}
}

// @check if we are using mixed instance policies
if e.UseMixedInstancesPolicy() {
// we can zero this out for now and use the mixed instance policy for definition
Expand All @@ -284,7 +287,7 @@ func (v *AutoscalingGroup) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *Autos
SpotMaxPrice: e.MixedSpotMaxPrice,
},
LaunchTemplate: &autoscaling.LaunchTemplate{
LaunchTemplateSpecification: &autoscaling.LaunchTemplateSpecification{LaunchTemplateName: e.LaunchTemplate.ID},
LaunchTemplateSpecification: &autoscaling.LaunchTemplateSpecification{LaunchTemplateName: e.LaunchTemplate.ID, Version: aws.String("1")},
},
}
p := request.MixedInstancesPolicy.LaunchTemplate
Expand Down Expand Up @@ -345,12 +348,26 @@ func (v *AutoscalingGroup) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *Autos
return req.MixedInstancesPolicy
}

launchTemplateVersion := "1"
if e.LaunchTemplate != nil {
dltRequest := &ec2.DescribeLaunchTemplatesInput{
LaunchTemplateNames: []*string{e.LaunchTemplate.ID},
}
dltResponse, err := t.Cloud.EC2().DescribeLaunchTemplates(dltRequest)
if err != nil {
klog.Warningf("could not find existing LaunchTemplate: %v", err)
} else {
launchTemplateVersion = strconv.FormatInt(*dltResponse.LaunchTemplates[0].LatestVersionNumber, 10)
}
}

if changes.LaunchTemplate != nil {
// @note: at the moment we are only using launch templates when using mixed instance policies,
// but this might change
setup(request).LaunchTemplate = &autoscaling.LaunchTemplate{
LaunchTemplateSpecification: &autoscaling.LaunchTemplateSpecification{
LaunchTemplateName: changes.LaunchTemplate.ID,
Version: &launchTemplateVersion,
},
}
changes.LaunchTemplate = nil
Expand Down Expand Up @@ -381,6 +398,7 @@ func (v *AutoscalingGroup) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *Autos
setup(request).LaunchTemplate = &autoscaling.LaunchTemplate{
LaunchTemplateSpecification: &autoscaling.LaunchTemplateSpecification{
LaunchTemplateName: e.LaunchTemplate.ID,
Version: &launchTemplateVersion,
},
}
}
Expand Down
37 changes: 21 additions & 16 deletions upup/pkg/fi/cloudup/awsup/aws_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,25 +591,30 @@ func findAutoscalingGroupLaunchConfiguration(c AWSCloud, g *autoscaling.Group) (
if g.MixedInstancesPolicy != nil {
if g.MixedInstancesPolicy.LaunchTemplate != nil {
if g.MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification != nil {
// honestly!!
name = aws.StringValue(g.MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification.LaunchTemplateName)
request := &ec2.DescribeLaunchTemplateVersionsInput{
LaunchTemplateName: &name,
}

versions, err := c.EC2().DescribeLaunchTemplateVersions(request)
if err != nil {
return "", fmt.Errorf("error finding versions for launch template: %v", err)
}

var version string
for _, v := range versions.LaunchTemplateVersions {
if *v.DefaultVersion {
version = strconv.FormatInt(*v.VersionNumber, 10)
break
name = aws.StringValue(g.MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification.LaunchTemplateName)
//See what version the ASG is set to use
mixedVersion := aws.StringValue(g.MixedInstancesPolicy.LaunchTemplate.LaunchTemplateSpecification.Version)
//Correctly Handle Default and Latest Versions
if mixedVersion == "" || mixedVersion == "$Default" || mixedVersion == "$Latest" {
request := &ec2.DescribeLaunchTemplatesInput{
LaunchTemplateNames: []*string{&name},
}
dltResponse, err := c.EC2().DescribeLaunchTemplates(request)
if err != nil {
return "", fmt.Errorf("error describing launch templates: %v", err)
}
launchTemplate := dltResponse.LaunchTemplates[0]
if mixedVersion == "" || mixedVersion == "$Default" {
version = strconv.FormatInt(*launchTemplate.DefaultVersionNumber, 10)
} else {
version = strconv.FormatInt(*launchTemplate.LatestVersionNumber, 10)
}
} else {
version = mixedVersion
}

klog.V(4).Infof("Launch Template Version Specified By ASG: %v", mixedVersion)
klog.V(4).Infof("Launch Template Version we are using for compare: %v", version)
if name != "" {
launchTemplate := name + ":" + version
return launchTemplate, nil
Expand Down