Skip to content

Commit 153e22f

Browse files
committed
Add tags to Auto Scaling Group and Fleet request
1 parent bd128aa commit 153e22f

File tree

4 files changed

+84
-4
lines changed

4 files changed

+84
-4
lines changed

docs/deployment/aws/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Escalator requires the following IAM policy to be able to properly integrate wit
1919
"Effect": "Allow",
2020
"Action": [
2121
"autoscaling:AttachInstances",
22+
"autoscaling:CreateOrUpdateTags",
2223
"autoscaling:DescribeAutoScalingGroups",
2324
"autoscaling:SetDesiredCapacity",
2425
"autoscaling:TerminateInstanceInAutoScalingGroup",

pkg/cloudprovider/aws/aws.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ const (
2626
LifecycleSpot = "spot"
2727
// The AttachInstances API only supports adding 20 instances at a time
2828
batchSize = 20
29+
// tagKey is the key for the tag applied to the ASG
30+
tagKey = "k8s.io/atlassian-escalator/enabled"
31+
// tagValue is the value for the tag applied to the ASG
32+
tagValue = "true"
2933
)
3034

3135
func instanceToProviderID(instance *autoscaling.Instance) string {
@@ -92,6 +96,34 @@ func (c *CloudProvider) RegisterNodeGroups(groups ...cloudprovider.NodeGroupConf
9296
continue
9397
}
9498

99+
// Search the asg for tagKey, then add it if it's not present
100+
hasTag := false
101+
tags := group.Tags
102+
for _, tag := range tags {
103+
if *tag.Key == tagKey {
104+
hasTag = true
105+
break
106+
}
107+
}
108+
if !hasTag {
109+
tagInput := &autoscaling.CreateOrUpdateTagsInput{
110+
Tags: []*autoscaling.Tag{
111+
{
112+
Key: awsapi.String(tagKey),
113+
PropagateAtLaunch: awsapi.Bool(true),
114+
ResourceId: awsapi.String(id),
115+
ResourceType: awsapi.String("auto-scaling-group"),
116+
Value: awsapi.String(tagValue),
117+
},
118+
},
119+
}
120+
log.WithField("asg", id).Infof("creating auto scaling tag")
121+
_, err := c.service.CreateOrUpdateTags(tagInput)
122+
if err != nil {
123+
log.Errorf("failed to create auto scaling tag for ASG %v", id)
124+
}
125+
}
126+
95127
c.nodeGroups[id] = NewNodeGroup(configs[id], group, c)
96128
}
97129

@@ -485,6 +517,17 @@ func createFleetInput(n NodeGroup, addCount int64) (*ec2.CreateFleetInput, error
485517
Overrides: launchTemplateOverrides,
486518
},
487519
},
520+
TagSpecifications: []*ec2.TagSpecification{
521+
{
522+
ResourceType: awsapi.String(ec2.ResourceTypeFleet),
523+
Tags: []*ec2.Tag{
524+
{
525+
Key: awsapi.String(tagKey),
526+
Value: awsapi.String(tagValue),
527+
},
528+
},
529+
},
530+
},
488531
}
489532

490533
if lifecycle == LifecycleOnDemand {

pkg/cloudprovider/aws/cloud_provider_test.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@ func TestCloudProvider_GetNodeGroup(t *testing.T) {
9191

9292
func TestCloudProvider_RegisterNodeGroups(t *testing.T) {
9393
tests := []struct {
94-
name string
95-
nodeGroups map[string]bool
96-
response *autoscaling.DescribeAutoScalingGroupsOutput
97-
err error
94+
name string
95+
nodeGroups map[string]bool
96+
response *autoscaling.DescribeAutoScalingGroupsOutput
97+
err error
98+
tagResponse *autoscaling.CreateOrUpdateTagsOutput
99+
tagErr error
98100
}{
99101
{
100102
"register node group that does not exist",
@@ -103,6 +105,8 @@ func TestCloudProvider_RegisterNodeGroups(t *testing.T) {
103105
},
104106
&autoscaling.DescribeAutoScalingGroupsOutput{},
105107
nil,
108+
&autoscaling.CreateOrUpdateTagsOutput{},
109+
nil,
106110
},
107111
{
108112
"register node groups that exist",
@@ -121,6 +125,8 @@ func TestCloudProvider_RegisterNodeGroups(t *testing.T) {
121125
},
122126
},
123127
nil,
128+
&autoscaling.CreateOrUpdateTagsOutput{},
129+
nil,
124130
},
125131
{
126132
"register node groups, some don't exist",
@@ -136,12 +142,32 @@ func TestCloudProvider_RegisterNodeGroups(t *testing.T) {
136142
},
137143
},
138144
nil,
145+
&autoscaling.CreateOrUpdateTagsOutput{},
146+
nil,
139147
},
140148
{
141149
"register no node groups",
142150
map[string]bool{},
143151
&autoscaling.DescribeAutoScalingGroupsOutput{},
144152
fmt.Errorf("no groups"),
153+
&autoscaling.CreateOrUpdateTagsOutput{},
154+
nil,
155+
},
156+
{
157+
"register existing node group with error from CreateOrUpdateTags",
158+
map[string]bool{
159+
"1": true,
160+
},
161+
&autoscaling.DescribeAutoScalingGroupsOutput{
162+
AutoScalingGroups: []*autoscaling.Group{
163+
{
164+
AutoScalingGroupName: aws.String("1"),
165+
},
166+
},
167+
},
168+
nil,
169+
&autoscaling.CreateOrUpdateTagsOutput{},
170+
fmt.Errorf("unauthorized operation"),
145171
},
146172
}
147173

@@ -151,6 +177,8 @@ func TestCloudProvider_RegisterNodeGroups(t *testing.T) {
151177
service := test.MockAutoscalingService{
152178
DescribeAutoScalingGroupsOutput: tt.response,
153179
DescribeAutoScalingGroupsErr: tt.err,
180+
CreateOrUpdateTagsOutput: tt.tagResponse,
181+
CreateOrUpdateTagsErr: tt.tagErr,
154182
}
155183

156184
var ids []string

pkg/test/aws.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ type MockAutoscalingService struct {
1616
AttachInstanceOutput *autoscaling.AttachInstancesOutput
1717
AttachInstanceErr error
1818

19+
CreateOrUpdateTagsOutput *autoscaling.CreateOrUpdateTagsOutput
20+
CreateOrUpdateTagsErr error
21+
1922
DescribeAutoScalingGroupsOutput *autoscaling.DescribeAutoScalingGroupsOutput
2023
DescribeAutoScalingGroupsErr error
2124

@@ -31,6 +34,11 @@ func (m MockAutoscalingService) AttachInstances(*autoscaling.AttachInstancesInpu
3134
return m.AttachInstanceOutput, m.AttachInstanceErr
3235
}
3336

37+
// CreateOrUpdateTags mock implementation for MockAutoscalingService
38+
func (m MockAutoscalingService) CreateOrUpdateTags(*autoscaling.CreateOrUpdateTagsInput) (*autoscaling.CreateOrUpdateTagsOutput, error) {
39+
return m.CreateOrUpdateTagsOutput, m.CreateOrUpdateTagsErr
40+
}
41+
3442
// DescribeAutoScalingGroups mock implementation for MockAutoscalingService
3543
func (m MockAutoscalingService) DescribeAutoScalingGroups(*autoscaling.DescribeAutoScalingGroupsInput) (*autoscaling.DescribeAutoScalingGroupsOutput, error) {
3644
return m.DescribeAutoScalingGroupsOutput, m.DescribeAutoScalingGroupsErr

0 commit comments

Comments
 (0)