Skip to content

Commit

Permalink
Merge pull request #8806 from hakman/automated-cherry-pick-of-#8462-#…
Browse files Browse the repository at this point in the history
…8466-upstream-release-1.16

Automated cherry pick of #8462: Tag EBS volumes when using launch templates with AWS API #8466: Update tags support for LaunchTemplates
  • Loading branch information
k8s-ci-robot committed Mar 27, 2020
2 parents 678a2a4 + 1b93c88 commit 51e1e6b
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 4 deletions.
6 changes: 6 additions & 0 deletions pkg/model/awsmodel/autoscalinggroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func (b *AutoscalingGroupModelBuilder) buildLaunchTemplateTask(c *fi.ModelBuilde
return nil, err
}

tags, err := b.CloudTagsForInstanceGroup(ig)
if err != nil {
return nil, fmt.Errorf("error building cloud tags: %v", err)
}

// @TODO check if there any a better way of doing this .. initially I had a type LaunchTemplate which included
// LaunchConfiguration as an anonymous field, bit given up the task dependency walker works this caused issues, due
// to the creation of a implicit dependency
Expand All @@ -115,6 +120,7 @@ func (b *AutoscalingGroupModelBuilder) buildLaunchTemplateTask(c *fi.ModelBuilde
RootVolumeType: lc.RootVolumeType,
SSHKey: lc.SSHKey,
SecurityGroups: lc.SecurityGroups,
Tags: tags,
Tenancy: lc.Tenancy,
UserData: lc.UserData,
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/model/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ func (m *KopsModelContext) CloudTagsForInstanceGroup(ig *kops.InstanceGroup) (ma
}
}

// Add cluster and ig names
labels[awsup.TagClusterName] = m.ClusterName()
labels["Name"] = m.AutoscalingGroupName(ig)

// The system tags take priority because the cluster likely breaks without them...

if ig.Spec.Role == kops.InstanceGroupRoleMaster {
Expand Down
1 change: 0 additions & 1 deletion pkg/model/spotinstmodel/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ go_library(
"//pkg/model/defaults:go_default_library",
"//upup/pkg/fi:go_default_library",
"//upup/pkg/fi/cloudup/awstasks:go_default_library",
"//upup/pkg/fi/cloudup/awsup:go_default_library",
"//upup/pkg/fi/cloudup/spotinsttasks:go_default_library",
"//vendor/k8s.io/klog:go_default_library",
],
Expand Down
3 changes: 0 additions & 3 deletions pkg/model/spotinstmodel/instance_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"k8s.io/kops/pkg/model/defaults"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awstasks"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/spotinsttasks"
)

Expand Down Expand Up @@ -605,8 +604,6 @@ func (b *InstanceGroupModelBuilder) buildTags(ig *kops.InstanceGroup) (map[strin
if err != nil {
return nil, err
}
tags[awsup.TagClusterName] = b.ClusterName()
tags["Name"] = b.AutoscalingGroupName(ig)
return tags, nil
}

Expand Down
2 changes: 2 additions & 0 deletions upup/pkg/fi/cloudup/awstasks/launchtemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ type LaunchTemplate struct {
SecurityGroups []*SecurityGroup
// SpotPrice is set to the spot-price bid if this is a spot pricing request
SpotPrice string
// Tags are the keypairs to apply to the instance and volume on launch.
Tags map[string]string
// Tenancy. Can be either default or dedicated.
Tenancy *string
// UserData is the user data configuration
Expand Down
27 changes: 27 additions & 0 deletions upup/pkg/fi/cloudup/awstasks/launchtemplate_target_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,24 @@ func (t *LaunchTemplate) RenderAWS(c *awsup.AWSAPITarget, a, ep, changes *Launch
} else {
lc.SecurityGroupIds = securityGroups
}
// @step: add the tags
if len(t.Tags) > 0 {
var tags []*ec2.Tag
for k, v := range t.Tags {
tags = append(tags, &ec2.Tag{
Key: aws.String(k),
Value: aws.String(v),
})
}
lc.TagSpecifications = append(lc.TagSpecifications, &ec2.LaunchTemplateTagSpecificationRequest{
ResourceType: aws.String(ec2.ResourceTypeInstance),
Tags: tags,
})
lc.TagSpecifications = append(lc.TagSpecifications, &ec2.LaunchTemplateTagSpecificationRequest{
ResourceType: aws.String(ec2.ResourceTypeVolume),
Tags: tags,
})
}
// @step: add the userdata
if t.UserData != nil {
d, err := t.UserData.AsBytes()
Expand Down Expand Up @@ -242,6 +260,15 @@ func (t *LaunchTemplate) Find(c *fi.Context) (*LaunchTemplate, error) {
actual.UserData = fi.WrapResource(fi.NewStringResource(string(ud)))
}

// @step: add tags
if len(lt.LaunchTemplateData.TagSpecifications) > 0 {
ts := lt.LaunchTemplateData.TagSpecifications[0]
if ts.Tags != nil {
tags := mapEC2TagsToMap(ts.Tags)
actual.Tags = tags
}
}

// @step: to avoid spurious changes on ImageId
if t.ImageID != nil && actual.ImageID != nil && *actual.ImageID != *t.ImageID {
image, err := cloud.ResolveImage(*t.ImageID)
Expand Down

0 comments on commit 51e1e6b

Please sign in to comment.