This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load_balancer_attachment.go
78 lines (63 loc) · 1.9 KB
/
load_balancer_attachment.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
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"
)
type LoadBalancerAttachment struct {
LoadBalancer *LoadBalancer
AutoscalingGroup *AutoscalingGroup
}
func (e *LoadBalancerAttachment) String() string {
return fi.TaskAsString(e)
}
func (e *LoadBalancerAttachment) Find(c *fi.Context) (*LoadBalancerAttachment, error) {
cloud := c.Cloud.(*awsup.AWSCloud)
if e.AutoscalingGroup != nil {
g, err := findAutoscalingGroup(cloud, *e.AutoscalingGroup.Name)
if err != nil {
return nil, err
}
if g == nil {
return nil, nil
}
for _, name := range g.LoadBalancerNames {
if aws.StringValue(name) != *e.LoadBalancer.ID {
continue
}
actual := &LoadBalancerAttachment{}
actual.LoadBalancer = e.LoadBalancer
actual.AutoscalingGroup = e.AutoscalingGroup
return actual, nil
}
}
return nil, nil
}
func (e *LoadBalancerAttachment) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (s *LoadBalancerAttachment) CheckChanges(a, e, changes *LoadBalancerAttachment) error {
if a == nil {
if e.LoadBalancer == nil {
return fi.RequiredField("LoadBalancer")
}
if e.AutoscalingGroup == nil {
return fi.RequiredField("AutoscalingGroup")
}
}
return nil
}
func (_ *LoadBalancerAttachment) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *LoadBalancerAttachment) error {
request := &autoscaling.AttachLoadBalancersInput{}
request.AutoScalingGroupName = e.AutoscalingGroup.Name
request.LoadBalancerNames = []*string{e.LoadBalancer.ID}
glog.V(2).Infof("Attaching autoscaling group %q to ELB %q", *e.AutoscalingGroup.Name, *e.LoadBalancer.Name)
_, err := t.Cloud.Autoscaling.AttachLoadBalancers(request)
if err != nil {
return fmt.Errorf("error attaching autoscaling group to ELB: %v", err)
}
return nil
}