ensure valid AWS LaunchTemplate version

The LaunchTemplateSpecification.Version is a pointer to
string. When the pointer is nil, EC2 AutoScaling API considers the value
to be "$Default", however aws.StringValue(ltSpec.Version) will return an
empty string (which is not considered the same as "$Default" or a nil
string pointer. So, in order to not pass an empty string as the version
for the launch template when we communicate with the EC2 AutoScaling API
using the information in the launchTemplate, we store the string
"$Default" when the ltSpec.Version is a nil pointer.

Issue #1728
This commit is contained in:
Jay Pipes 2019-09-25 09:47:53 -04:00
parent 6c5a504681
commit 54f3076b24
2 changed files with 88 additions and 1 deletions

View File

@ -461,9 +461,32 @@ func (m *asgCache) buildAsgFromAWS(g *autoscaling.Group) (*asg, error) {
}
func (m *asgCache) buildLaunchTemplateFromSpec(ltSpec *autoscaling.LaunchTemplateSpecification) *launchTemplate {
// NOTE(jaypipes): The LaunchTemplateSpecification.Version is a pointer to
// string. When the pointer is nil, EC2 AutoScaling API considers the value
// to be "$Default", however aws.StringValue(ltSpec.Version) will return an
// empty string (which is not considered the same as "$Default" or a nil
// string pointer. So, in order to not pass an empty string as the version
// for the launch template when we communicate with the EC2 AutoScaling API
// using the information in the launchTemplate, we store the string
// "$Default" here when the ltSpec.Version is a nil pointer.
//
// See:
//
// https://github.com/kubernetes/autoscaler/issues/1728
// https://github.com/aws/aws-sdk-go/blob/81fad3b797f4a9bd1b452a5733dd465eefef1060/service/autoscaling/api.go#L10666-L10671
//
// A cleaner alternative might be to make launchTemplate.version a string
// pointer instead of a string, or even store the aws-sdk-go's
// LaunchTemplateSpecification structs directly.
var version string
if ltSpec.Version == nil {
version = "$Default"
} else {
version = aws.StringValue(ltSpec.Version)
}
return &launchTemplate{
name: aws.StringValue(ltSpec.LaunchTemplateName),
version: aws.StringValue(ltSpec.Version),
version: version,
}
}

View File

@ -20,6 +20,9 @@ import (
"testing"
"github.com/stretchr/testify/assert"
sdkaws "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
)
func TestBuildAsg(t *testing.T) {
@ -46,3 +49,64 @@ func validateAsg(t *testing.T, asg *asg, name string, minSize int, maxSize int)
assert.Equal(t, minSize, asg.minSize)
assert.Equal(t, maxSize, asg.maxSize)
}
func TestBuildLaunchTemplateFromSpec(t *testing.T) {
assert := assert.New(t)
units := []struct {
name string
in *autoscaling.LaunchTemplateSpecification
exp *launchTemplate
}{
{
name: "non-default, specified version",
in: &autoscaling.LaunchTemplateSpecification{
LaunchTemplateName: sdkaws.String("foo"),
Version: sdkaws.String("1"),
},
exp: &launchTemplate{
name: "foo",
version: "1",
},
},
{
name: "non-default, specified $Latest",
in: &autoscaling.LaunchTemplateSpecification{
LaunchTemplateName: sdkaws.String("foo"),
Version: sdkaws.String("$Latest"),
},
exp: &launchTemplate{
name: "foo",
version: "$Latest",
},
},
{
name: "specified $Default",
in: &autoscaling.LaunchTemplateSpecification{
LaunchTemplateName: sdkaws.String("foo"),
Version: sdkaws.String("$Default"),
},
exp: &launchTemplate{
name: "foo",
version: "$Default",
},
},
{
name: "no version specified",
in: &autoscaling.LaunchTemplateSpecification{
LaunchTemplateName: sdkaws.String("foo"),
Version: nil,
},
exp: &launchTemplate{
name: "foo",
version: "$Default",
},
},
}
cache := &asgCache{}
for _, unit := range units {
got := cache.buildLaunchTemplateFromSpec(unit.in)
assert.Equal(unit.exp, got)
}
}