forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoscalinggroup.go
261 lines (218 loc) · 6.92 KB
/
autoscalinggroup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package awstasks
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/golang/glog"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
"reflect"
"sort"
"strings"
)
//go:generate fitask -type=AutoscalingGroup
type AutoscalingGroup struct {
Name *string
MinSize *int64
MaxSize *int64
Subnets []*Subnet
Tags map[string]string
LaunchConfiguration *LaunchConfiguration
}
var _ fi.CompareWithID = &AutoscalingGroup{}
func (e *AutoscalingGroup) CompareWithID() *string {
return e.Name
}
func findAutoscalingGroup(cloud awsup.AWSCloud, name string) (*autoscaling.Group, error) {
request := &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{&name},
}
var found []*autoscaling.Group
err := cloud.Autoscaling().DescribeAutoScalingGroupsPages(request, func(p *autoscaling.DescribeAutoScalingGroupsOutput, lastPage bool) (shouldContinue bool) {
for _, g := range p.AutoScalingGroups {
if aws.StringValue(g.AutoScalingGroupName) == name {
found = append(found, g)
} else {
glog.Warningf("Got ASG with unexpected name")
}
}
return true
})
if err != nil {
return nil, fmt.Errorf("error listing AutoscalingGroups: %v", err)
}
if len(found) == 0 {
return nil, nil
}
if len(found) != 1 {
return nil, fmt.Errorf("Found multiple AutoscalingGroups with name %q", name)
}
return found[0], nil
}
func (e *AutoscalingGroup) Find(c *fi.Context) (*AutoscalingGroup, error) {
cloud := c.Cloud.(awsup.AWSCloud)
g, err := findAutoscalingGroup(cloud, *e.Name)
if err != nil {
return nil, err
}
if g == nil {
return nil, nil
}
actual := &AutoscalingGroup{}
actual.Name = g.AutoScalingGroupName
actual.MinSize = g.MinSize
actual.MaxSize = g.MaxSize
if g.VPCZoneIdentifier != nil {
subnets := strings.Split(*g.VPCZoneIdentifier, ",")
for _, subnet := range subnets {
actual.Subnets = append(actual.Subnets, &Subnet{ID: aws.String(subnet)})
}
}
if len(g.Tags) != 0 {
actual.Tags = make(map[string]string)
for _, tag := range g.Tags {
actual.Tags[*tag.Key] = *tag.Value
}
}
if g.LaunchConfigurationName == nil {
return nil, fmt.Errorf("autoscaling Group %q had no LaunchConfiguration", *actual.Name)
}
actual.LaunchConfiguration = &LaunchConfiguration{ID: g.LaunchConfigurationName}
if subnetSlicesEqualIgnoreOrder(actual.Subnets, e.Subnets) {
actual.Subnets = e.Subnets
}
return actual, nil
}
func (e *AutoscalingGroup) Run(c *fi.Context) error {
c.Cloud.(awsup.AWSCloud).AddTags(e.Name, e.Tags)
return fi.DefaultDeltaRunMethod(e, c)
}
func (s *AutoscalingGroup) CheckChanges(a, e, changes *AutoscalingGroup) error {
if a != nil {
if e.Name == nil {
return fi.RequiredField("Name")
}
}
return nil
}
func (e *AutoscalingGroup) buildTags(cloud fi.Cloud) map[string]string {
tags := make(map[string]string)
for k, v := range e.Tags {
tags[k] = v
}
return tags
}
func (_ *AutoscalingGroup) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *AutoscalingGroup) error {
if a == nil {
glog.V(2).Infof("Creating autoscaling Group with Name:%q", *e.Name)
request := &autoscaling.CreateAutoScalingGroupInput{}
request.AutoScalingGroupName = e.Name
request.LaunchConfigurationName = e.LaunchConfiguration.ID
request.MinSize = e.MinSize
request.MaxSize = e.MaxSize
var subnetIDs []string
for _, s := range e.Subnets {
subnetIDs = append(subnetIDs, *s.ID)
}
request.VPCZoneIdentifier = aws.String(strings.Join(subnetIDs, ","))
tags := []*autoscaling.Tag{}
for k, v := range e.buildTags(t.Cloud) {
tags = append(tags, &autoscaling.Tag{
Key: aws.String(k),
Value: aws.String(v),
ResourceId: e.Name,
ResourceType: aws.String("auto-scaling-group"),
})
}
request.Tags = tags
_, err := t.Cloud.Autoscaling().CreateAutoScalingGroup(request)
if err != nil {
return fmt.Errorf("error creating AutoscalingGroup: %v", err)
}
} else {
request := &autoscaling.UpdateAutoScalingGroupInput{
AutoScalingGroupName: e.Name,
}
if changes.LaunchConfiguration != nil {
request.LaunchConfigurationName = e.LaunchConfiguration.ID
changes.LaunchConfiguration = nil
}
if changes.MinSize != nil {
request.MinSize = e.MinSize
changes.MinSize = nil
}
if changes.MaxSize != nil {
request.MaxSize = e.MaxSize
changes.MaxSize = nil
}
if changes.Subnets != nil {
var subnetIDs []string
for _, s := range e.Subnets {
subnetIDs = append(subnetIDs, *s.ID)
}
request.VPCZoneIdentifier = aws.String(strings.Join(subnetIDs, ","))
changes.Subnets = nil
}
// Temporary workaround for user-added tags, until we have #241
// TODO: Remove once we have #241
if changes.Tags != nil {
glog.Warning("Ignoring tag changes until we have #241: %v", changes.Tags)
changes.Tags = nil
}
empty := &AutoscalingGroup{}
if !reflect.DeepEqual(empty, changes) {
glog.Warningf("cannot apply changes to AutoScalingGroup: %v", changes)
}
glog.V(2).Infof("Updating autoscaling group %s", *e.Name)
_, err := t.Cloud.Autoscaling().UpdateAutoScalingGroup(request)
if err != nil {
return fmt.Errorf("error updating AutoscalingGroup: %v", err)
}
}
// TODO: Use PropagateAtLaunch = false for tagging?
return nil // We have
}
type terraformASGTag struct {
Key *string `json:"key"`
Value *string `json:"value"`
PropagateAtLaunch *bool `json:"propagate_at_launch"`
}
type terraformAutoscalingGroup struct {
Name *string `json:"name,omitempty"`
LaunchConfigurationName *terraform.Literal `json:"launch_configuration,omitempty"`
MaxSize *int64 `json:"max_size,omitempty"`
MinSize *int64 `json:"min_size,omitempty"`
VPCZoneIdentifier []*terraform.Literal `json:"vpc_zone_identifier,omitempty"`
Tags []*terraformASGTag `json:"tag,omitempty"`
}
func (_ *AutoscalingGroup) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *AutoscalingGroup) error {
tf := &terraformAutoscalingGroup{
Name: e.Name,
MinSize: e.MinSize,
MaxSize: e.MaxSize,
LaunchConfigurationName: e.LaunchConfiguration.TerraformLink(),
}
for _, s := range e.Subnets {
tf.VPCZoneIdentifier = append(tf.VPCZoneIdentifier, s.TerraformLink())
}
tags := e.buildTags(t.Cloud)
// Make sure we output in a stable order
var tagKeys []string
for k := range tags {
tagKeys = append(tagKeys, k)
}
sort.Strings(tagKeys)
for _, k := range tagKeys {
v := tags[k]
tf.Tags = append(tf.Tags, &terraformASGTag{
Key: fi.String(k),
Value: fi.String(v),
PropagateAtLaunch: fi.Bool(true),
})
}
return t.RenderResource("aws_autoscaling_group", *e.Name, tf)
}
func (e *AutoscalingGroup) TerraformLink() *terraform.Literal {
return terraform.LiteralProperty("aws_autoscaling_group", *e.Name, "id")
}