-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathelb.go
151 lines (136 loc) · 4.71 KB
/
elb.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
package targetsync
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/sirupsen/logrus"
)
// NewAWSTargetGroup returns a new AWS target group destination
func NewAWSTargetGroup(cfg *AWSConfig) (*AWSTargetGroup, error) {
// TODO: verify that this client is good at creation time (ping or something)
sess := session.Must(session.NewSession())
return &AWSTargetGroup{
svc: elbv2.New(sess),
cfg: cfg,
}, nil
}
// AWSTargetGroup is a TargetDestination implementation for AWS target groups
type AWSTargetGroup struct {
svc *elbv2.ELBV2
cfg *AWSConfig
}
// GetTargets returns the current set of targets at the destination
func (tg *AWSTargetGroup) GetTargets(ctx context.Context) ([]*Target, error) {
input := &elbv2.DescribeTargetHealthInput{
TargetGroupArn: aws.String(tg.cfg.TargetGroupARN),
}
result, err := tg.svc.DescribeTargetHealthWithContext(ctx, input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case elbv2.ErrCodeInvalidTargetException:
fmt.Println(elbv2.ErrCodeInvalidTargetException, aerr.Error())
case elbv2.ErrCodeTargetGroupNotFoundException:
fmt.Println(elbv2.ErrCodeTargetGroupNotFoundException, aerr.Error())
case elbv2.ErrCodeHealthUnavailableException:
fmt.Println(elbv2.ErrCodeHealthUnavailableException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return nil, err
}
targets := make([]*Target, 0)
for _, healthDesc := range result.TargetHealthDescriptions {
if tg.cfg.AvailabilityZone == "" || *healthDesc.Target.AvailabilityZone == tg.cfg.AvailabilityZone {
if state := *healthDesc.TargetHealth.State; state != elbv2.TargetHealthStateEnumDraining {
targets = append(targets, &Target{
IP: *healthDesc.Target.Id,
Port: int(*healthDesc.Target.Port),
})
} else {
logrus.Debugf("Target %v excluded as it is in state %v", *healthDesc.Target, state)
}
}
}
return targets, nil
}
// AddTargets simply adds the targets described
func (tg *AWSTargetGroup) AddTargets(ctx context.Context, targets []*Target) error {
input := &elbv2.RegisterTargetsInput{
TargetGroupArn: aws.String(tg.cfg.TargetGroupARN),
Targets: tg.TargetToTargetDescription(targets),
}
// TODO: check output
_, err := tg.svc.RegisterTargetsWithContext(ctx, input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case elbv2.ErrCodeTargetGroupNotFoundException:
fmt.Println(elbv2.ErrCodeTargetGroupNotFoundException, aerr.Error())
case elbv2.ErrCodeTooManyTargetsException:
fmt.Println(elbv2.ErrCodeTooManyTargetsException, aerr.Error())
case elbv2.ErrCodeInvalidTargetException:
fmt.Println(elbv2.ErrCodeInvalidTargetException, aerr.Error())
case elbv2.ErrCodeTooManyRegistrationsForTargetIdException:
fmt.Println(elbv2.ErrCodeTooManyRegistrationsForTargetIdException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return err
}
return nil
}
// RemoveTargets simply removes the targets described
func (tg *AWSTargetGroup) RemoveTargets(ctx context.Context, targets []*Target) error {
input := &elbv2.DeregisterTargetsInput{
TargetGroupArn: aws.String(tg.cfg.TargetGroupARN),
Targets: tg.TargetToTargetDescription(targets),
}
// TODO: check output
_, err := tg.svc.DeregisterTargetsWithContext(ctx, input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case elbv2.ErrCodeTargetGroupNotFoundException:
fmt.Println(elbv2.ErrCodeTargetGroupNotFoundException, aerr.Error())
case elbv2.ErrCodeInvalidTargetException:
fmt.Println(elbv2.ErrCodeInvalidTargetException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
return err
}
return nil
}
// TargetToTargetDescription translates the `Target` struct into an ec2 `TargetDescription`
func (tg *AWSTargetGroup) TargetToTargetDescription(targets []*Target) []*elbv2.TargetDescription {
descs := make([]*elbv2.TargetDescription, len(targets))
for i, target := range targets {
descs[i] = &elbv2.TargetDescription{
Id: aws.String(target.IP),
Port: aws.Int64(int64(target.Port)),
}
if tg.cfg.AvailabilityZone != "" {
descs[i].AvailabilityZone = aws.String(tg.cfg.AvailabilityZone)
}
}
return descs
}